Skip to content
CppCXYPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

C/C++ for Zed, powered by cpp_ls

Build

This extension brings cpp_ls to Zed: a C, C++ and CUDA language server written from scratch in Rust — no clangd, no libclang, and no compilation database needed to start. A compile_commands.json (any build system) or CMake's build_database.json is used when the project has one, and a .cppls.toml in the workspace root describes the project when it does not.

The extension itself is small on purpose: Zed's built-in C and C++ languages keep their tree-sitter grammars, queries, folding, indentation and text objects, and this extension attaches the language server to both of them. It ships no grammar, so it adds nothing to your highlighting that Zed did not already have.

What the server answers

Reading completion (members after ./->/::, scopes, includes), hover with the deduced type and the doc comment, signature help, go to definition / type definition / implementation, find references, document and workspace symbols, call and type hierarchy, document highlight, folding, selection ranges, inlay hints, code lens
Editing diagnostics (pull and push, 43 codes, each switchable in .cppls.toml), code actions from those diagnostics, rename, formatting and range formatting
Extras Doxygen commands in comments, [`symbol`] references in comments, format-string awareness for printf / std::format / fmt, semantic tokens, CUDA and C23 dialects

Since the server reads the program rather than a file, half-typed lines are answered like complete ones, and a modern C++ program (import std;, contracts, static reflection) is parsed as the standard spells it.

Installation

  1. Open Zed, run zed: extensions, search for C++ Language Server, and install it.
  2. Open a C or C++ file. On first use the extension downloads the server for your platform from CppCXY/cpp_ls releases (about 21 MB) and shows the progress in Zed's status bar.

Nothing needs to be installed beforehand. If you would rather use a build of your own, see Which server binary is started.

Which server binary is started

The first of these that applies wins:

  1. lsp.cppls.binary.path in your Zed settings — used exactly as written.
  2. A cpp_ls executable on $PATH (cargo install --locked cpp_ls, a system package, a manual build).
  3. The binary an earlier session downloaded and this one can still verify.
  4. A fresh download of the latest release, unpacked into Zed's extension work directory as cpp_ls-<version>/cpp_ls.

Only one downloaded version is kept: after an update, the older cpp_ls-* directory is removed. The download is staged in download/ and renamed into place when complete, so an interrupted download never leaves a half-written install behind. A failed update does not take away a working install — if GitHub cannot be reached, the newest installation already on disk is started and the failure is written to Zed's log (zed: open log).

Configuration

Zed settings

{
  "lsp": {
    "cppls": {
      "binary": {
        "path": "/usr/local/bin/cpp_ls",
        "arguments": ["--log-level", "debug", "--log-path", "/tmp/cpp_ls.log"],
        "env": {}
      },
      "initialization_options": {},
      "settings": {
        "exclude": ["**/build/**", "third_party/**"]
      }
    }
  },
  "languages": {
    "C++": {
      "format_on_save": "on",
      "formatter": "language_server"
    }
  }
}
  • binary — path bypasses the extension entirely. arguments replaces the arguments Zed passes (cpp_ls needs none: it speaks LSP over stdio), and is where --log-level, --log-path and --config go. env is added to the shell environment.
  • settings — answered to the server's workspace/configuration request for its own cppls section. The one setting the server reads from the client today is exclude: glob patterns it must not analyse, which is the same list your editor uses to hide files. (The extension nests this under the section name for you; writing the {"cppls": {...}} section yourself is also accepted.)
  • initialization_options — forwarded to the server as initializationOptions.

.cppls.toml

The project's own configuration lives in the workspace root and is what most settings should be written in: it travels with the repository, it is read by every client, and it is where the analysis is described. Every key is optional; see docs/configuration.md for the whole surface.

[workspace]
exclude = ["**/build/**", "third_party/**"]   # what the analysis must not read
source_extensions = ["cuh"]                   # extensions added to the engine's own list

[compile]
standard = "c++20"
# database = "build/compile_commands.json"    # when discovery would not find it
# args = ["-std=c++20", "-Iinclude"]          # the project's whole command line
extra_args = ["-DFEATURE=1"]

[compile.cuda]
enable = true                                 # .cu/.cuh read as CUDA, toolkit headers included

For a project with several compilations, [[compile.target]] describes each one's sources, include directories and defines. A dependency outside the workspace is a [[library]]. Diagnostics are switched per rule with [diagnostics] disable = ["missing-include"], and the formatter is chosen with [format] formatter.

The server caches what it has read in .cppls/ under the workspace root (change it with [index] cache_dir); you will want that directory in .gitignore.

Semantic tokens

Zed's semantic tokens are off by default. Turn them on globally (or per language) to get the server's names, types, macros, documentation commands and format strings:

{
  "semantic_tokens": "combined"
}

cpp_ls also sends two token types of its own. Zed has no style for them until you name one:

{
  "global_lsp_settings": {
    "semantic_token_rules": [
      { "token_type": "declarationKeyword", "style": ["keyword"] },
      { "token_type": "variableTemplate", "style": ["variable"] }
    ]
  }
}

Formatting

The server answers textDocument/formatting and textDocument/rangeFormatting. It runs clang-format from $PATH when it is there, and falls back to its own formatter when it is not, so format-on-save works on a machine with nothing installed. To hear nothing about a missing clang-format, set [format] formatter = "cpp_formatter" in .cppls.toml.

Notes

  • The first workspace folder is what gets analysed. cpp_ls opens one workspace root; if you add several folders to a Zed project, the server indexes the first one.
  • .h headers. A header is C or C++ depending on what includes it, so the server never guesses from the extension alone: it is told the language of the file the client opened. The extension maps Zed's language names to the ids the server expects (C → c, C++ → cpp), which is what keeps C++-only rules off a C project's headers.
  • CUDA. Zed's C++ language already owns .cu and .cuh, so CUDA files are parsed from the first keystroke; the CUDA semantics come from [compile.cuda] in .cppls.toml.
  • Logs. The server writes a log file by default (its path is printed to stderr at startup) and honours --log-path none to stop. Zed's own log (zed: open log) has what the extension did.

Development

cargo build --release --target wasm32-wasip2     # what Zed builds and runs
cargo clippy --release --target wasm32-wasip2 -- -D warnings
cargo fmt --all -- --check

Then run zed: install dev extension and pick this directory. Zed rebuilds the extension when the source changes.

The extension talks to Zed through zed_extension_api 0.7.0 and holds no state that matters: the only decisions are which binary to start (see above), which arguments to pass, and how to answer the server's configuration request.

License

MIT — see LICENSE.

cpp_ls itself is MIT-licensed and lives at CppCXY/cpp_ls.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages