Connection

Struct Connection 

Source
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::split and taking the first element of the tuple
  • By referencing the connection field of the Server struct.

§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>

Source

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();
Source

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>

Source

pub fn publish_diagnostics( &mut self, uri: DocumentUri, diagnostics: Vec<Diagnostic>, )

Publishes diagnostics for a specific document.

§Arguments
  • uri - The DocumentUri of the document to publish diagnostics for.
  • diagnostics - A list of diagnostics to publish.
Source§

impl<T: TypeProvider> Connection<T>

Source

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>

Source

pub fn log_message(&mut self, type: MessageType, message: String)

This notification may be used to log to a console on the client side.

§Arguments
  • r#type - The type of log message.
  • message - The message to log.
Source§

impl<T: TypeProvider> Connection<T>

Source

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>

Source

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>

Source

pub fn apply_edit(&mut self, tag: T::ApplyEditData, edit: WorkspaceEdit) -> bool

Request the application of a workspace edit

§Arguments
  • tag - A tag of type TypeProvider::ApplyEditData preserved throughout the request.
  • edit - The workspace edit to apply.
  • result - A boolean indicating whether the request was sent.

Auto Trait Implementations§

§

impl<T> !Freeze for Connection<T>

§

impl<T> !RefUnwindSafe for Connection<T>

§

impl<T> !Send for Connection<T>

§

impl<T> !Sync for Connection<T>

§

impl<T> Unpin for Connection<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Connection<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.