Search code examples
c++clanghelix-editor

Autocompletion for cpp in helix editor not working, Ubuntu 24.10


I use Ubuntu 24.10 and I have just installed helix text editor (helix 24.7 (079f5442)). I am new to the editor and I am finding my way out. I have copied and installed in languages.toml the whole lsp for cpp from here. But I have not succeeded in installing the DAP and the Formatter.

hakiza@VBox:~/cpp/ppp/ch02$ helix --health cpp
Configured language servers:
  ✓ clangd: /usr/bin/clangd
Configured debug adapter: lldb-dap
Binary for debug adapter: 'lldb-dap' not found in $PATH
Configured formatter: None
Highlight queries: ✓
Textobject queries: ✓
Indent queries: ✓

When I try to write some ccp code, I can see that the autocompletion is not working. When I open an existing cpp file, I see "Language server exited" message at the bottom left of the window.

How to proceed further to make helix work for cpp environment, when the cpp version is c++20?

Here is the content of my languages.toml file:

[[language]]
name = "cpp"
scope = "source.cpp"
injection-regex = "cpp"
file-types = ["cc", "hh", "c++", "cpp", "hpp", "h", "ipp", "tpp", "cxx", "hxx", "ixx", "txx", "ino", "C", "H", "cu", "cuh", "cppm", "h++", "ii", "inl", { glob = ".hpp.in" }, { glob = ".h.in" }]
comment-token = "//"
block-comment-tokens = { start = "/*", end = "*/" }
language-servers = [ "clangd" ]
indent = { tab-width = 2, unit = "  " }

[language.debugger]
name = "lldb-dap"
transport = "stdio"
command = "lldb-dap"

[[language.debugger.templates]]
name = "binary"
request = "launch"
completion = [ { name = "binary", completion = "filename" } ]
args = { console = "internalConsole", program = "{0}" }

[[language.debugger.templates]]
name = "attach"
request = "attach"
completion = [ "pid" ]
args = { console = "internalConsole", pid = "{0}" }

[[language.debugger.templates]]
name = "gdbserver attach"
request = "attach"
completion = [ { name = "lldb connect url", default = "connect://localhost:3333" }, { name = "file", completion = "filename" }, "pid" ]
args = { console = "internalConsole", attachCommands = [ "platform select remote-gdb-server", "platform connect {0}", "file {1}", "attach {2}" ] }

[[grammar]]
name = "cpp"
source = { git = "https://github.com/tree-sitter/tree-sitter-cpp", rev = "670404d7c689be1c868a46f919ba2a3912f2b7ef" }

Edited: Here is the content of hx --health

Language   LSP        DAP        Formatter… Highlight… Textobjec… Indent     
ada        ✘ ada_l…   None       None       ✓          ✓          ✘          
           ✘ ada_l…   
adl        None       None       None       ✓          ✓          ✓          
agda       None       None       None       ✓          ✘          ✘          
astro      None       None       None       ✓          ✘          ✘          
awk        ✘ awk-l…   None       None       ✓          ✓          ✘          
bash       ✘ bash-…   None       None       ✓          ✓          ✓          
bass       ✘ bass     None       None       ✓          ✘          ✘          
beancount… None       None       None       ✓          ✘          ✘          
bibtex     ✘ texla…   None       ✘ bibte…   ✓          ✘          ✘          
bicep      ✘ bicep…   None       None       ✓          ✘          ✘          
bitbake    ✘ bitba…   None       None       ✓          ✘          ✘          
blade      None       None       None       ✓          ✘          ✘          
blueprint… ✘ bluep…   None       None       ✓          ✘          ✘          
c          ✓ clang…   ✓ lldb-…   None       ✓          ✓          ✓          
c-sharp    ✘ OmniS…   ✘ netco…   None       ✓          ✓          ✘          
cabal      ✓ haske…   None       None       ✘          ✘          ✘          
cairo      ✘ cairo…   None       None       ✓          ✓          ✓          
capnp      None       None       None       ✓          ✘          ✓          
cel        None       None       None       ✓          ✘          ✘          
clojure    ✘ cloju…   None       None       ✓          ✘          ✘          
cmake      ✘ cmake…   None       None       ✓          ✓          ✓          
comment    None       None       None       ✓          ✘          ✘          
common-li… ✘ cl-ls…   None       None       ✓          ✘          ✓          
cpon       None       None       None       ✓          ✘          ✓          
cpp        ✓ clang…   ✓ lldb-…   None       ✓          ✓          ✓ 

     

And here is the content of hx --health cpp

hakiza@VBox:~$ hx --health cpp
Configured language servers:
  ✓ clangd: /usr/bin/clangd
Configured debug adapter: lldb-dap
Binary for debug adapter: /usr/bin/lldb-dap
Configured formatter: None
Highlight queries: ✓
Textobject queries: ✓
Indent queries: ✓

Solution

  • I don't think there are something like "--std=c++20" flag for clangd command.

    If there are flag like this, you can easily use helix config "[language-server.clangd] args=[]" to launch clangd with the flag.

    Therefore, a separate file is needed to provide clangd the compiling information.

    Option 1: Generate "compile_commands.json" for clangd

    If clangd is being used, generate an compile_commands.json and clangd will see that if that's in "<project_root>" or "<project_root>/build".

    I think this is more ideal because you may have other projects need different c++ version or even different toolchain support.

    cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=true -Bbuild
    
    bear -- make
    

    Option 2: clangd config

    Config path for the project: "<project_root>/.clangd"

    Per user global config: "~/.config/clangd/config.yaml"

    CompileFlags:
      Add: [-std=c++20]