Auto LSP
A Rust crate for creating Abstract Syntax Trees (AST) and Language Server Protocol (LSP) servers.
auto_lsp
is designed to be as language-agnostic as possible, allowing any Tree-sitter grammar to be used.
Defining a simple AST involves two steps: writing the queries and then defining the corresponding AST structures in Rust.
cargo add auto_lsp
Quick example
Let's say you have a toy language with a root node named document containing a list of function nodes, each containing a unique name.
A simple query file to capture the root document and function names:
(document) @document
(function
(name) @name) @function
The corresponding AST definition in Rust:
use auto_lsp::seq;
#[seq(query = "document")]
struct Document {
functions: Vec<Function>
}
#[seq(query = "function")]
struct Function {
name: Name
}
#[seq(query = "name")]
struct Name {}
Now that you have your AST defined, you can:
- Implement the AST traits and create a LSP server (with the
lsp_server
feature). - Add your own logic for testing purposes, code_generation, etc.
Simplicity
auto-lsp
only has 2 macros to define an AST:
All symbols are thread-safe and have their own parse function via blanket implementations. This means any symbol can be used as a root node, allowing you to:
- Create a full AST from any Tree-sitter grammar.
- Derive a subset of the grammar, depending on your needs.
However, this level of flexibility and permissiveness comes with some caveats. It can be more prone to errors and requires careful attention when writing your queries.
To address this, auto_lsp
provides testing and logging utilities to help you ensure that the AST behaves as intended.
Features
deadlock_detection
: Enableparking_lot
's deadlock detection (not compatible withwasm
).log
: Enable logging. (usesstderrlog
)lsp_server
: Enable the LSP server (useslsp_server
).rayon
: Enablerayon
support (not compatible withwasm
).wasm
: Enable wasm support.html
: Enable the html workspace mock for testing purposes.python
: Enable the python workspace mock for testing purposes.