Skip to main content

LspClient

Trait LspClient 

Source
pub trait LspClient {
    // Provided methods
    fn did_open(&mut self, _document: &LspDocument, _text: &str) { ... }
    fn did_change(
        &mut self,
        _document: &LspDocument,
        _changes: &[LspTextChange],
    ) { ... }
    fn did_save(&mut self, _document: &LspDocument, _text: &str) { ... }
    fn did_close(&mut self, _document: &LspDocument) { ... }
    fn request_hover(&mut self, _document: &LspDocument, _position: LspPosition) { ... }
    fn request_completion(
        &mut self,
        _document: &LspDocument,
        _position: LspPosition,
    ) { ... }
    fn request_definition(
        &mut self,
        _document: &LspDocument,
        _position: LspPosition,
    ) { ... }
    fn request_formatting(
        &mut self,
        _document: &LspDocument,
        _options: LspFormattingOptions,
    ) { ... }
}
Expand description

LSP integration types and traits for editor clients. LSP client hooks invoked by the editor.

Every method has a no-op default, so an implementation only overrides the notifications and requests it actually cares about. Attach one with CodeEditor::attach_lsp; the editor never blocks on a response, so request results come back through whatever channel the implementation chooses.

§Example

use std::cell::RefCell;
use std::rc::Rc;

use iced_code_editor::{CodeEditor, LspClient, LspDocument, LspTextChange};

/// A client that only records how many change batches it received.
struct CountingClient(Rc<RefCell<usize>>);

impl LspClient for CountingClient {
    fn did_change(&mut self, _document: &LspDocument, changes: &[LspTextChange]) {
        *self.0.borrow_mut() += changes.len();
    }
}

let changes = Rc::new(RefCell::new(0));
let mut editor = CodeEditor::new("fn main() {}", "rs");
editor.attach_lsp(
    Box::new(CountingClient(Rc::clone(&changes))),
    LspDocument::new("file:///tmp/main.rs", "rust"),
);

// `did_open` fired, but no change has been flushed yet.
assert_eq!(*changes.borrow(), 0);

Provided Methods§

Source

fn did_open(&mut self, _document: &LspDocument, _text: &str)

Notifies the client that a document was opened.

Source

fn did_change(&mut self, _document: &LspDocument, _changes: &[LspTextChange])

Notifies the client that the document changed.

Source

fn did_save(&mut self, _document: &LspDocument, _text: &str)

Notifies the client that the document was saved.

Source

fn did_close(&mut self, _document: &LspDocument)

Notifies the client that the document was closed.

Source

fn request_hover(&mut self, _document: &LspDocument, _position: LspPosition)

Requests hover information at the given position.

Source

fn request_completion( &mut self, _document: &LspDocument, _position: LspPosition, )

Requests completion items at the given position.

Source

fn request_definition( &mut self, _document: &LspDocument, _position: LspPosition, )

Requests the definition location(s) for the symbol at the given position.

This method is called when the user triggers a “Go to Definition” action (e.g., via Ctrl+Click or a context menu). The client implementation should send a textDocument/definition request to the LSP server.

Source

fn request_formatting( &mut self, _document: &LspDocument, _options: LspFormattingOptions, )

Requests formatting edits for the whole document.

This method is called when the user triggers a “Format Document” action, or when the host formats before saving. The client implementation should send a textDocument/formatting request to the LSP server; the resulting edits come back through whatever channel the implementation uses, and the host applies them with CodeEditor::apply_lsp_text_edits.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§