TextDocuments

Struct TextDocuments 

Source
pub struct TextDocuments { /* private fields */ }

Implementations§

Source§

impl TextDocuments

Source

pub fn new() -> Self

Create a text documents

§Examples

Basic usage:

use lsp_textdocument::TextDocuments;

let text_documents = TextDocuments::new();
Source

pub fn with_encoding(default_encoding: PositionEncodingKind) -> Self

Create a TextDocuments instance with a specific position encoding

This method allows you to specify the position encoding used for character positions in text documents. The encoding determines how character offsets are calculated and is important for proper LSP communication between client and server.

§Arguments
  • default_encoding - The position encoding to use. Can be UTF-8, UTF-16, or UTF-32.
§Position Encodings
  • UTF-16: The default encoding for backward compatibility with LSP 3.16 and earlier. Each UTF-16 code unit counts as one position unit.
  • UTF-8: Each byte counts as one position unit. More efficient for ASCII-heavy text.
  • UTF-32: Each Unicode code point counts as one position unit.

The encoding should match what was negotiated with the LSP client during initialization.

§Examples

Basic usage with UTF-16 (default):

use lsp_textdocument::TextDocuments;
use lsp_types::PositionEncodingKind;

let text_documents = TextDocuments::with_encoding(PositionEncodingKind::UTF16);

Using UTF-8 encoding for better performance with ASCII text:

use lsp_textdocument::TextDocuments;
use lsp_types::PositionEncodingKind;

let text_documents = TextDocuments::with_encoding(PositionEncodingKind::UTF8);

Using UTF-32 encoding where each Unicode code point is one unit:

use lsp_textdocument::TextDocuments;
use lsp_types::PositionEncodingKind;

let text_documents = TextDocuments::with_encoding(PositionEncodingKind::UTF32);
Source

pub fn documents(&self) -> &BTreeMap<Uri, FullTextDocument>

Source

pub fn default_encoding(&self) -> PositionEncodingKind

Returns the default position encoding used for newly created documents.

This is useful for checking which encoding was configured or negotiated (for example, during server initialization) when this TextDocuments instance was created.

Source

pub fn get_document(&self, uri: &Uri) -> Option<&FullTextDocument>

Get specify document by giving Uri

§Examples:

Basic usage:

use lsp_textdocument::TextDocuments;
use lsp_types::Uri;

let text_documents = TextDocuments::new();
let uri:Uri = "file://example.txt".parse().unwrap();
text_documents.get_document(&uri);
Source

pub fn get_document_content( &self, uri: &Uri, range: Option<Range>, ) -> Option<&str>

Get specify document content by giving Range

§Examples

Basic usage:

use lsp_textdocument::TextDocuments;
use lsp_types::{Uri, Range, Position};

let uri: Uri = "file://example.txt".parse().unwrap();
let text_documents = TextDocuments::new();

// get document all content
let content = text_documents.get_document_content(&uri, None);
assert_eq!(content, Some("hello rust!"));

// get document specify content by range
let (start, end) = (Position::new(0, 1), Position::new(0, 9));
let range = Range::new(start, end);
let sub_content = text_documents.get_document_content(&uri, Some(range));
assert_eq!(sub_content, Some("ello rus"));
Source

pub fn get_document_language(&self, uri: &Uri) -> Option<&str>

Get specify document’s language by giving Uri

§Examples

Basic usage:

use lsp_textdocument::TextDocuments;
use lsp_types::Uri;

let text_documents = TextDocuments::new();
let uri:Uri = "file://example.js".parse().unwrap();
let language =  text_documents.get_document_language(&uri);
assert_eq!(language, Some("javascript"));
Source

pub fn listen(&mut self, method: &str, params: &Value) -> bool

Listening the notification from client, you just need to pass method and params

§Examples:

Basic usage:

use lsp_textdocument::TextDocuments;

let method = "textDocument/didOpen";
let params = serde_json::to_value("message produced by client").unwrap();

let mut text_documents = TextDocuments::new();
let accept: bool = text_documents.listen(method, &params);

Trait Implementations§

Source§

impl Default for TextDocuments

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.