lite-xl / lite-xl-lsp

LSP Plugin for Lite XL editor
MIT License
159 stars 21 forks source link

clangd cannot find my compile_commands.json #133

Closed paulross80 closed 3 months ago

paulross80 commented 3 months ago

Hello. I'm trying to use lite-xl editor (latest version 2.1.5, installed using fedora dnf) with the IDE plugin, that includes this lsp plugin. I'm using xmake as my build tool. I made a C++23 project with modules just to test some things. I ask xmake to generate the compile_commands.json file, like this:

xmake project -k compile_commands

It works. But when I open Lite XL, some clangd errors appear, like it doesn't understand my code. I have also created a .clangd file in the project root with this content:

CompileFlags:
  CompilationDatabase: /home/paul/Programming/xmake/test1/
  #CompilationDatabase: .

I restart lite xl but the same problem persists. I have noticed that QtCreator uses this parameter for clangd: --compile-commands-dir=, so I edited the config.lua from this plugin to add that parameter:

command = { "clangd", "--background-index --compile-commands-dir=build" }
verbose = true

config.zip

But since I have edited this config file, every time I open litexl, it says "[LSP]: clangd was shutdown, revise your configuration" How can I fix this?

Guldoman commented 3 months ago

In theory clangd automatically finds the compile_commands.json file in the directory/parent directory of the source files, but I don't know much about how the search works, so I can't help you much there.

command = { "clangd", "--background-index --compile-commands-dir=build" }

You need to split the parameters like this:

command = { "clangd", "--background-index", "--compile-commands-dir=build" }

Also, please don't edit config.lua directly, but use your user module:

local lspconfig = require "plugins.lsp.config"
lspconfig.clangd.setup({
  command = { "clangd", "--background-index", "--compile-commands-dir=build" },
  verbose = true
})
paulross80 commented 3 months ago

I'll try splitting the parameters User module. Can you please tell me what file is that?

Guldoman commented 3 months ago

User module. Can you please tell me what file is that?

Click on the gear icon in the bottom left. If it opens a file, that's the user module. If it opens the settings GUI, in "Core", expand "General", click on "User Module".

paulross80 commented 3 months ago

If it opens a file, that's the user module

Found it, thanks. I've pasted your code in that file, saved and restarted litexl. No errors It still doesn't understand what -fmodules-ts and importing modules are The directory specified here: "--compile-commands-dir=build" should be relative to the project root, right?

paulross80 commented 3 months ago

Today I've tried pasting the code in the project module, using core:open-project-module command

local lspconfig = require "plugins.lsp.config"

lspconfig.clangd.setup({
  command = { "clangd", "--background-index", "--compile-commands-dir=~/Programming/xmake/test1" },
  verbose = false
})

After saving the file, restarted with core:restart Even using the full path to the compile commands file, still doesn't work as expected. What else can I try to fix it?

Guldoman commented 3 months ago

What happens if you run the LSP server manually? Does it warn about a bad config? For example try with clangd --log=verbose --check=/path/to/a/source/file.

paulross80 commented 3 months ago

It works. The problem was that my project uses C++20 modules. Clang does support them, but clangd doesn't. Thanks for your help!

Guldoman commented 3 months ago

Glad that you found the problem!