Expand description
§Sync Lsp
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. |
Modules§
- text_
document - A module containing all text document related functionality.
- window
- This module contains functionality centered arround logging and ui features.
- workspace
- Contains various notifications and request related to a workspace.
Structs§
- Connection
- This struct may be used to send notifications and requests to the client. The connection to the client can be obtained in one of two way:
- Server
- This struct is a wrapper around the server state, which provides
type via the
TypeProvidertrait. It also contains the connection to the client and all callbacks for the different endpoints. - Transport
- The transport defines how data is sent and received from the client.
Enums§
- Error
Code - Error codes used to either return a response or log to the client.
Traits§
- Type
Provider - This trait is used to set type definitions for requests and notifications with dynamic parameters.
Attribute Macros§
- type_
provider - This macro provides default implementations for all required types in
TypeProvider.