1 unstable release

0.1.0 Dec 4, 2023

#822 in Development tools

MIT license

150KB
2.5K SLoC

Sync Lsp

MIT licensed Static Badge

Sync Lsp is a synchronous lsp implementation for language servers. These are the main features of this library:

  • Automation: Sync Lsp handles registration and unregistration aswell as capabilitie negotiations by itself. Ontop of that none of the lifecycle messages are exposed to users of this library.

  • Compatabilitie: Capabilitie handling is also done internally. This means that the user of this library does not have to worry about checking if a client supports a certain feature or not.

  • Error Handling: Almost all protocol related errors are proccessed internally. Therefore the api is very easy to use and does not require the user to implement their own error handling.

Example

use sync_lsp::{
    Transport,
    TypeProvider,
    Server,
    text_document::did_open::TextDocumentItem
};

use sync_lsp::window::{
    MessageType,
    show_message_request::MessageActionItem
};

// The state of the server, in this case it's empty,
// but it could be used to store information like
// syntax trees, diagnostics, etc.
struct MyServerState;

// Configuring the server to use strings as the 
// data attached to show message requests and
// using the default implementation for the rest
// by using a macro
#[sync_lsp::type_provider]
impl TypeProvider for MyServerState {
    type ShowMessageRequestData = String;
}

fn main() {
    // Creating a transport that uses stdin and stdout
    let transport = Transport::stdio();
    let mut server = Server::new(MyServerState, transport);

    // Listeners for events can be set via server.on_* methods
    server.on_open(MyServerState::on_open);
    server.on_show_message_response(MyServerState::on_show_message_response);
    // Block the current thread and listen for messages
    server.serve().unwrap();
}

impl MyServerState {
    fn on_open(server: &mut Server<Self>, document: TextDocumentItem) {
        server.connection.show_message_request(
            MessageType::Warning,
            format!("Example query: {}", document.uri),
            vec![
                MessageActionItem {
                    title: "Action 1".to_string(),
                    data: document.uri.clone()
                },
                MessageActionItem {
                    title: "Action 2".to_string(),
                    data: document.uri.clone()
                }
            ]
        );
    }

    fn on_show_message_response(server: &mut Server<Self>, item: MessageActionItem<String>) {
        server.connection.show_message(
            MessageType::Info,
            format!("Performing {} on {}", item.title, item.data)
        );
    }
}

Feature Flags

Flag Description
mio The mio crate will be used to poll for messages and therefore enable request cancellation support. Without this flag the Connection::cancelled method is still available, but will always return false.
dynamic-callbacks If this feature is disabled, there should be no calls to Server::on_* after Server::server is called, and the server's performance may improve. Note that this is mainly a performance feature and does not equate to the client's ability to register capabilities dynamically via the lsp.

Dependencies

~1–11MB
~93K SLoC