gleam
gleam copied to clipboard
LSP import autocompletion fails to trigger
The example posted in the Discord showed gleam_erlang
displaying autocompletion in the file
module. I can confirm that this works in that sub-module.
Using mist, I get suggestions in the main src/mist.gleam
module. However, in any submodule, i.e. src/mist/file.gleam
, I don't get any. It behaves similarly for glisten
.
If I create a new project with gleam new lsp_test
, I do not get any autocompletion suggestions, even in the src/lsp_test.gleam
module for gleam_stdlib
.
This is being run with cargo build --release
on the latest main
branch. This behavior is identical in both neovim and VSCode.
I added some logging for my neovim LSP, and I can see that for the "working" modules, it does receive an rpc response:
[DEBUG][2022-07-25 17:39:48] .../vim/lsp/rpc.lua:364 "rpc.send" { id = 3, jsonrpc = "2.0", method = "textDocument/completion", params = { context = { triggerCharacter = " ", triggerKind = 2 }, position = { character = 7, line = 3 }, textDocument = { uri = "file:///home/alex/gleams/mist/src/mist.gleam" } }}
// truncated cause it's the full response
However, the ones that do not work seem to get some kind of incomplete response:
[DEBUG][2022-07-25 17:40:45] .../vim/lsp/rpc.lua:364 "rpc.send" { id = 3, jsonrpc = "2.0", method = "textDocument/completion", params = { context = { triggerCharacter = " ", triggerKind = 2 }, position = { character = 7, line = 14 }, textDocument = { uri = "file:///home/alex/elixirs/spades/src/spades.gleam" } }}
[DEBUG][2022-07-25 17:40:45] .../vim/lsp/rpc.lua:473 "rpc.receive" { id = 3, jsonrpc = "2.0"}
Happy to provide any more info, if it's helpful!
Thank you
I'm able to reproduce. I'll take a look at it.
mist.gleam works because of the docs, which are at the cursor position at the time of writing a suggestion.
import gleam/io
/// now it will work
/// now it will work
/// now it will work
/// now it will work
/// now it will work
/// now it will work
/// now it will work
pub fn a() {
io.debug("a")
}
The underlying issue is that textDocument/didChange
events are not stored and used for the auto-completion part.
Or, in other words, that the auto-completion part does not use the in-memory text.
If the compiler is updated with the source code, it will fail to parse.
If the compiler does not get the updates from source code, it can't know where in the source code one is typing.
I see no fix to this until we have fault tolerant parsing.
We could move the import autocompletion over to use the latest but as-yet untyped source, like the formatter does. This would work as we don't need type information to know what modules exist, yet it would give us accurate information about whether the cursor is within a statement or not.
I have improved the autocompletion to a degree where I think we can close this. Further improvements will come with partial compilation.