pub struct Connection<T: TypeProvider> { /* private fields */ }Expand description
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:
- By calling
Server::splitand taking the first element of the tuple - By referencing the
connectionfield of theServerstruct.
§Example
use sync_lsp::{Transport, TypeProvider, Server};
// For this example, we don't need any state.
struct MyServerState;
#[sync_lsp::type_provider]
impl TypeProvider for MyServerState {}
fn main() {
let transport = Transport::stdio();
let mut server = Server::new(MyServerState, transport);
server.on_open(|server, _| {
#[allow(unused)]
let (connection, state) = server.split();
});
server.serve().unwrap();
}Implementations§
Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn error<R: Default>(&mut self, code: ErrorCode, message: String) -> R
pub fn error<R: Default>(&mut self, code: ErrorCode, message: String) -> R
This will send a error if called from a request and log it,
if called from a notification. For usability reasons it also
returns a default value of the type R, which makes it possible
to write
return self.error(ErrorCode::InvalidParams, "Test Error".to_string());instead of
self.error(ErrorCode::InvalidParams, "Test Error".to_string());
return SomeType::default();Sourcepub fn cancelled(&mut self) -> bool
pub fn cancelled(&mut self) -> bool
Check whether the current request has been cancelled.
If this method has been called in a cancelled request,
a error with code ErrorCode::RequestCancelled will be returned to the client,
regardless of what the request handler returns.
§Example
use sync_lsp::{Transport, TypeProvider, Server, text_document::completion::CompletionList};
// For this example, we don't need any state.
struct MyServerState;
// This macro provides default implementations for all required types.
#[sync_lsp::type_provider]
impl TypeProvider for MyServerState {}
fn main() {
let transport = Transport::stdio();
let mut server = Server::new(MyServerState, transport);
server.on_completion(|server, _, _| {
let result = Vec::new();
while !server.connection.cancelled() {
// Do expensive work here
}
CompletionList {
is_incomplete: false,
items: result
}
});
server.serve().unwrap();
}Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn publish_diagnostics(
&mut self,
uri: DocumentUri,
diagnostics: Vec<Diagnostic>,
)
pub fn publish_diagnostics( &mut self, uri: DocumentUri, diagnostics: Vec<Diagnostic>, )
Publishes diagnostics for a specific document.
§Arguments
uri- TheDocumentUriof the document to publish diagnostics for.diagnostics- A list of diagnostics to publish.
Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn show_message(&mut self, type: MessageType, message: String)
pub fn show_message(&mut self, type: MessageType, message: String)
This notification may be used to show a message to the user.
§Arguments
r#type- The type of message to show.message- The message to show.
Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn log_message(&mut self, type: MessageType, message: String)
pub fn log_message(&mut self, type: MessageType, message: String)
Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn telemetry(&mut self, params: impl Serialize)
pub fn telemetry(&mut self, params: impl Serialize)
This notification sends arbitrary telemetry data to the client.
§Arguments
params- The data to send.
Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn show_message_request(
&mut self,
type: MessageType,
message: String,
actions: Vec<MessageActionItem<T::ShowMessageRequestData>>,
) -> bool
pub fn show_message_request( &mut self, type: MessageType, message: String, actions: Vec<MessageActionItem<T::ShowMessageRequestData>>, ) -> bool
This request will trigger a query to the user.
§Arguments
r#type- The type of message to show.message- The message to show.actions- The actions to show.result- A boolean indicating whether the request was sent.
Source§impl<T: TypeProvider> Connection<T>
impl<T: TypeProvider> Connection<T>
Sourcepub fn apply_edit(&mut self, tag: T::ApplyEditData, edit: WorkspaceEdit) -> bool
pub fn apply_edit(&mut self, tag: T::ApplyEditData, edit: WorkspaceEdit) -> bool
Request the application of a workspace edit
§Arguments
tag- A tag of typeTypeProvider::ApplyEditDatapreserved throughout the request.edit- The workspace edit to apply.result- A boolean indicating whether the request was sent.