Improving import handling in VS Code
Based on the reasoning from @anthony and @cocreature have a custom prelude is not such a good idea. It could easily be misused, could be hard to follow.
I guess the real issue here is to manually import things.
DAML Studio already has a feature that offers import of types, functions. This is fantastic! However, it seems it works only for “built-in” types, modules, e.g. Optional, functions in DA.Action etc.
Can we improve this in the way that this works for types, modules in the current project as well?
Trying to ascertain I understand this correctly (I don’t use VSCode much myself). Is the issue that:
- When trying to autocomplete an import statement, the VSCode extension only suggests packages from the stdlib, and should in addition suggest packages from all (data-)dependencies, or that
- When you use a function from a stdlib module you have not imported, the extension automatically suggests adding the import and can do it as a quickfix, and you would like that to also work for all modules in (data-)dependencies?
Or both?
I thought of the second one. The first one would be just nice to have for me, not that important.
Typical use case for example is that I type somewhere in the code fromOptional. Now VSCode highlights it with error if it’s not important, I can hit Cmd/Ctrl + . and the IDE offers to import the missing function.
This is a production booster for me.
Hi Tamas,
I was trying to reproduce in the process of writing the issue, and the behaviour I am observing does not seem to match your description. Would you mind helping me understand where I’m going wrong?
If my DAML project consists entirely of this Main.daml file:
module Main where
setup : Scenario ()
setup = scenario do
let x : Int = fromOptional 0 $ Some 4
debug $ show x
I do not get any useful auto-import suggestion. If, however, my file looks like (note the existing import):
module Main where
import DA.Optional ()
setup : Scenario ()
setup = scenario do
let x : Int = fromOptional 0 $ Some 4
debug $ show x
then I do get a suggestion to add fromOptional to the list of imports for the DA.Optional module. Is this the feature you were referring to?
If so, at least on my machine (using 1.4.0-snapshot.20200722.4796.0.28ab504b), if I make a 2-file project with MyLib.daml as
module MyLib where
fun : Int -> Int
fun x = x + 1
and Main.daml as
module Main where
import MyLib ()
setup : Scenario ()
setup = scenario do
let x = fun 3
debug $ show x
I do get the suggestion to automatically add fun to the import list of MyLib, just like with the built-in library DA.Optional.
Is this different from what you observe? Am I misunderstanding what feature you were referring to?
Hm, this is quite interesting. Trying out your example VSCode only offered import fromRational which is strange.
Because I was certain it offered me fromOptional the way I described I started to play with it. If you add an import for instance to DA.Assert then all of a sudden fromOptional appears in the suggested imports. E.g.:
import DA.Assert ((===))
setup : Scenario ()
setup = scenario do
let x : Int = fromOptional 0 $ Some 4
debug $ show x
Apparently it seems any module from DA would make this work, i.e. having an import for DA.List or DA.Functor. See the attached screenshot.
I tried having an empty import list as you suggested, like import DA.Optional (). That works, however it’s a bit inconvenient in my mind, as I have to add it manually. I do admit that it does help if I do not want a whole module, but specific functions, and after one manual step I get auto import.
Also, I think this thread about the imports is worth having while the original discussion about prelude is not. Can we split the topic somehow? 
Moved, let me know if this works, feel free to edit the title if you think it can be more specific.
Hi Tamas,
I can reproduce, but it seems so weird I can’t imagine this is the intended behaviour. I’ll have to let someone who knows a bit more about how our autocomplete is implemented, and what the intended behaviour was, chime in.
