arduino / arduino-language-server

An Arduino Language Server based on Clangd to Arduino code autocompletion
GNU Affero General Public License v3.0
117 stars 11 forks source link

neovim LSP <-> arduino-language-server crashes on textDocument/definition method. #159

Open amrlsayed opened 1 year ago

amrlsayed commented 1 year ago

Describe the problem

"nvim" LSP client detach from arduino-language-server with error Unresolved .ino path on textDocument/definition method, when the definition is located on a "not tracked" file. I learned from the source code that the "tracked files" are those which are saved from textDocument/didOpen methods. and to send that from "nvim", I just open the file containing the definition once.

To reproduce

  1. Create a sketch using arduino-cli
    arduino-cli sketch new test_server
  2. Create another file(callme.ino) along with the main sketch file(test_server.ino)
  3. Add the following to callme.ino
    void callme(void)
    {
    Serial.println("Hello World!");
    }
  4. Add the following to test_server.ino

void setup() { callme(); } void loop() { }

5. Using [neovim/nvim-lspconfig](https://github.com/neovim/nvim-lspconfig) configure LSP client by adding the following to your `init.lua`
```lua
require'lspconfig'.arduino_language_server.setup{
  filetypes = {"arduino", "cpp"},
  cmd = {"arduino-language-server",
         "-cli-config",
  "<PATH-TO-CONFIG/arduino-cli.yaml>",
         "-log",
         "-logpath",
 "<PATH-TO-LOG-FOLDER>"}
}
  1. Open nvim
    nvim test_server.ino
  2. Move the cursor to callme function call and run
    :lua vim.lsp.buf.definition()

Expected behavior

From the text editor point of view, the callme.ino file should get opened and the cursor should be on the function callme.

Arduino Language Server version

0.7.4 and latest main branch commit 6c64232f29f8e61

Arduino CLI version

0.27.1

Operating system

Linux

Operating system version

Ubuntu 22

Additional context

Logged Error.

    !!! Unresolved .ino path: <PATH-TO-/callme.ino>
    !!! Known doc paths are:
    !!! <PATH-To-/test_server/test_server.ino>

I fixed the problem suppressing the error by changing the idePathToIdeURI() function to

func (ls *INOLanguageServer) idePathToIdeURI(logger jsonrpc.FunctionLogger, inoPath string) (lsp.DocumentURI, error) {
    if inoPath == sourcemapper.NotIno.File {
        return sourcemapper.NotInoURI, nil
    }
    doc, ok := ls.trackedIdeDocs[inoPath]
    if !ok {
        logger.Logf("    !!! Unresolved .ino path: %s", inoPath)
        logger.Logf("    !!! Known doc paths are:")
        for p := range ls.trackedIdeDocs {
            logger.Logf("    !!! > %s", p)
        }
        uri := lsp.NewDocumentURI(inoPath)
        return uri, nil //&UnknownURIError{uri} <-- EDIT HERE -->
    }
    return doc.URI, nil
}

But I do not think this is a good solution and would introduce other problems.

Other tries I made

I tried to open the project with several commands but all gave the same behavior.

nvim test_server.ino
nvim .
nvim test_server.ino callme.ino

The only workaround that leads to opening the definition is by opening the callme.ino file :e callme.ino explicitly once before trying to call textDocument/definition.

Issue checklist