gleam icon indicating copy to clipboard operation
gleam copied to clipboard

LSP import autocompletion fails to trigger

Open rawhat opened this issue 1 year ago • 1 comments

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!

rawhat avatar Jul 26 '22 17:07 rawhat

Thank you

lpil avatar Jul 27 '22 17:07 lpil

I'm able to reproduce. I'll take a look at it.

lucasavila00 avatar Aug 22 '22 19:08 lucasavila00

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")
}

lucasavila00 avatar Aug 22 '22 21:08 lucasavila00

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.

lucasavila00 avatar Aug 22 '22 21:08 lucasavila00

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.

lpil avatar Aug 26 '22 10:08 lpil

I have improved the autocompletion to a degree where I think we can close this. Further improvements will come with partial compilation.

lpil avatar Aug 27 '22 17:08 lpil