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

nvim-lspconfig + arduino-language-server doesn't attach to buffer #187

Closed realhackcraft closed 4 weeks ago

realhackcraft commented 4 weeks ago

Describe the problem

I'm using NvChad. My arduino ls doesn't crash, but doesn't attach to the buffer either.

To reproduce

exerpt of my config:

local lspconfig = require "lspconfig"

local MY_FQBN = "arduino:avr:leonardo"

local on_attach = require("nvchad.configs.lspconfig").on_attach
local on_init = require("nvchad.configs.lspconfig").on_init
local capabilities = require("nvchad.configs.lspconfig").capabilities

lspconfig.arduino_language_server.setup {
  on_attach = on_attach,
  on_init = on_init,
  capabilities = capabilities,
  cmd = {
    "arduino-language-server",
    "-cli-config",
    "$HOME/.arduino15/arduino-cli.yaml",
    "-fqbn",
    MY_FQBN,
  },
}

Expected behavior

Attach to the buffer with the .ino file

Arduino Language Server version

0.7.6

Arduino CLI version

arduino-cli Version: 0.35.3 Commit: 95cfd654 Date: 2024-02-19T13:15:51Z

Operating system

macOS

Operating system version

14.5

Additional context

Screenshot 2024-06-08 at 23 12 38

Issue checklist

realhackcraft commented 4 weeks ago

This is in the last lines of :LspLog:

[START][2024-06-09 12:16:08] LSP logging initiated
[ERROR][2024-06-09 12:16:08] .../vim/lsp/rpc.lua:770    "rpc"   "/Users/Borui/.local/share/nvim/mason/bin/arduino-language-server"  "stderr"    "12:16:08.152281 clangd found at /Users/Borui/.local/share/nvim/mason/bin/clangd\n12:16:08.154789 \27[97mLS: : Initial board configuration: arduino:avr:leonardo\27[0m\n12:16:08.154969 \27[97mLS: : arduino-language-server Version: 0.7.6 Commit: 9c2f44d Date: 2024-02-06T14:12:59Z\27[0m\n12:16:08.154991 \27[97mLS: : Language server temp directory: /private/var/folders/rj/bw9lqpk15g9ckdkpv0n7nhvr0000gp/T/arduino-language-server3164570696\27[0m\n12:16:08.154994 \27[97mLS: : Language server build path: /private/var/folders/rj/bw9lqpk15g9ckdkpv0n7nhvr0000gp/T/arduino-language-server3164570696/build\27[0m\n12:16:08.154995 \27[97mLS: : Language server build sketch root: /private/var/folders/rj/bw9lqpk15g9ckdkpv0n7nhvr0000gp/T/arduino-language-server3164570696/build/sketch\27[0m\n12:16:08.154997 \27[97mLS: : Language server FULL build path: /private/var/folders/rj/bw9lqpk15g9ckdkpv0n7nhvr0000gp/T/arduino-language-server3164570696/fullbuild\27[0m\n12:16:08.157664 IN Elapsed: 921.084µs\n12:16:08.160606 \27[92mIDE --> LS REQU initialize 1\27[0m\n"

It appears to be the same as the output of the arduino-language-server sh command with the arguments.

realhackcraft commented 4 weeks ago
-cli-daemon-addr string
        TCP address and port of the Arduino CLI daemon (for example: localhost:50051)

Does this mean that I need to spin up a arduino cli daemon myself, or is the ls going to do it?

HannHank commented 4 weeks ago

I encountered a similar issue, which seems to align with the one described in #186. I resolved it by downgrading to Neovim version v0.9.5. It appears that there are breaking changes in the newer versions of Neovim that prevent the LSP client from connecting properly.

realhackcraft commented 4 weeks ago

Right. Thank you!

fspv commented 2 weeks ago

I found the problem. It is in the https://github.com/bugst/go-lsp package, which arduino LSP depends on.

nvim 0.10 passes some new capabilities to the LSP, such as inlayHint. The problem is that the lib mentioned above seems to be not maintained and/or updated and hence it doesn't support these new capabilities (try to search for inlayHint here https://github.com/bugst/go-lsp/blob/master/lsp_capabilities_client.go).

So the solution here will be to add new capabilities to the library mentioned above. The problem is, unfortunately, not fixable from this repo, because the initial request handling and parsing is done in the LSP lib.

Vosjedev commented 1 week ago

what are the missing capabilities? I would like to know, because then I could try setting them to false in the lsp configuration:

-- [...]
local arduino_capabilities = vim.lsp.protocol.make_client_capabilities()
-- now here set some capabilities to false:
arduino_capabilities.unsupportedCapability = false
-- then use arduino_capabilities as the capabilities value for the lspconfig entry
-- [...]
fspv commented 1 week ago

I tried setting them to false, but it didn't work. I tried to look at the neovim code and I think there is no way to say not to pass inlay hint field to the language server. So even if you set it to false, it will be sent and the language server will crash, because it doesn't expect to see such a field in the json.

But anyway, I found out that you can just use clangd directly with the arduino code. The trick is to create a separate test.h and test.cpp files and import them from the .ino file. You can find the paths of the libraries to include in cat build/libraries.cache generated by the arduino-cli compile and place them into the .clangd file like this

CompileFlags:
  Add:
  - -I/home/fspv/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino
  - -I/home/fspv/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/standard
  - -I/home/fspv/Arduino/libraries/MD_Parola/src
  - -I/home/fspv/Arduino/libraries/MD_MAX72XX/src
  - -I/home/fspv/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SPI/src

So I personally don't need an arduino-language-server anymore :)

xdanep commented 1 week ago

I have the same issue, i tried everything but nothing works image