https://github.com/stort0/cpp-lsp-api
https://github.com/stort0/cpp-lsp-api
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/stort0/cpp-lsp-api
- Owner: stort0
- Created: 2026-03-15T09:00:44.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2026-04-17T21:03:09.000Z (3 months ago)
- Last Synced: 2026-04-17T21:31:15.019Z (3 months ago)
- Language: C++
- Size: 203 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# C++ LSP API
Wrapper of the `glaze` json library for use with `lsp` server and client.
The library uses `C++23`.
## Language Server
The `LSP::LanguageServer` class takes a template type which uses to parse and
handle the client requests. The server features are checked through **concepts**.
> The `FileT` template is used as a const variable and is **never** mutated. The
> server will **replace** the old one when the file is updated.
> All functions used by the `LanguageServer` **must** be `const`.
> If a `TokenT` type token spans across multiple lines, the method `split()` is
> required for semantic tokens (otherwise token will be skipped).
### Disable catch of exception
To forward all exception that happen during the language server execution,
define `CPP_LSP_API_DISABLE_CATCH` or `CPP_LSP_API_DISABLE_CATCH_DEBUG` to only
disable it during debug mode.
### Complete implementation of a file parser
```c++
class MyTokenType {
public:
/*
* Required elements
*/
auto range() const -> Range;
/*
* Required for semantic tokens
*/
static const uinteger invalid_type;
auto operator<=> (Position pos) const -> std::weak_ordering; // == means the position is inside range()
auto type() const -> uinteger;
auto modifier() const -> uinteger;
auto split() const -> std::vector; // optional
};
class MyRefactorContext {
};
class MyLanguageFile {
public:
/*
* Required elements
*/
using TokenT = MyTokenType; // MUST BE A BASIC TYPE (NO CVREF)
using TokenPtrT = const MyTokenType *;
static auto parse(const std::string &text) -> std::unique_ptr;
/*
* Required for notebook parsing
*/
static const NotebookDocumentSyncOptions::_NotebookSelector notebook_selector;
static auto parse(const std::string &text, NotebookCellKind kind, const std::vector &previousCells) -> std::unique_ptr;
/*
* Required for edits on file save
*/
auto save() const -> std::vector;
/*
* Required for call/type/implementation resolution
*/
auto resolve(Position pos) const -> TokenPtrT;
/*
* Required for element references
*/
auto references(Position pos) const -> std::vector;
/*
* Required for semantic tokens
*/
static const std::vector token_types;
static const std::vector token_modifiers;
auto visibleTokens() const -> std::ranges::input_range; // Any range with value_type == Token
// optional, should be set to true if visibleTokens() returns a sorted array
// based on the token Range.
static const bool ordered_visible_tokens;
/*
* Required for hover
*/
auto hover(Position pos) const -> std::optional;
/*
* Required for code lenses
*/
auto codeLenses() const -> std::vector;
/*
* Required for code diagnostics
*/
static const bool diagnostic_internal_dependencies;
auto diagnostics() const -> std::vector;
/*
* Required for call/type hierarchy
*/
auto roots(Position pos) const -> std::vector;
auto children(const std::optional &data) const -> std::vector;
auto parents(const std::optional &data) const -> std::vector;
/*
* Required for highlights
*/
auto highlights(Position pos) const -> std::vector;
/*
* Required for links
*/
auto links() const -> std::vector;
/*
* Required for folding ranges
*/
auto foldingRanges() const -> std::vector;
/*
* Required for selection ranges
*/
auto selectionRanges(Position pos) const -> std::vector; // RANGES MUST BE INCREASINGLY BIGGER,
// ELEMENTS MUST CONTAIN THE PREVIOUS ONES
/*
* Required for symbols
*/
auto symbols() const -> std::vector;
/*
* Required for inlay hints
*/
auto hints(const Range &range) const -> std::vector;
/*
* Required for inlay hints
*/
auto inlineValues(const Range &range, const InlineValueContext &ctx) const -> std::vector;
/*
* Required for completions
*/
auto completions(Position pos) const -> std::vector;
/*
* Required for signature help
*/
auto signatureHelp(Position pos) const -> std::optional;
/*
* Required for code actions
*/
auto actions(const Range &range, std::optional kind) const -> std::vector>;
/*
* Required for colors
*/
auto colors() const -> std::vector;
auto placeColor(const Color &color, const Range &range) const -> std::vector;
/*
* Required for format
*/
auto format(const FormattingOptions &opts, const std::optional &range) const -> std::vector;
/*
* Required for on type formatting
*/
static const std::vector on_type_trigger_chars;
auto format(const FormattingOptions &opts, Position pos) const -> std::vector;
/*
* Required for element rename
*/
using RefactorContextT = MyRefactorContext; // MUST BE A BASIC TYPE (NO CVREF)
auto createRenameCtx(Position pos, const string &newName) const -> RefactorContextT;
auto rename(const RefactorContextT &ctx) const -> std::vector;
/*
* Required rename check
*/
auto canRename(Position pos) const -> std::expected;
/*
* Required linked editing ranges
*/
auto editingRanges(Position pos) const -> LinkedEditingRanges;
/*
* Required inline completions
*/
auto inlineCompletions(Position pos) const -> std::vector;
};
```
## Language client
> TODO