pub struct TextDocuments { /* private fields */ }Implementations§
Source§impl TextDocuments
impl TextDocuments
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a text documents
§Examples
Basic usage:
use lsp_textdocument::TextDocuments;
let text_documents = TextDocuments::new();Sourcepub fn with_encoding(default_encoding: PositionEncodingKind) -> Self
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);pub fn documents(&self) -> &BTreeMap<Uri, FullTextDocument>
Sourcepub fn default_encoding(&self) -> PositionEncodingKind
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.
Sourcepub fn get_document(&self, uri: &Uri) -> Option<&FullTextDocument>
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);Sourcepub fn get_document_content(
&self,
uri: &Uri,
range: Option<Range>,
) -> Option<&str>
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"));Sourcepub fn get_document_language(&self, uri: &Uri) -> Option<&str>
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"));Sourcepub fn listen(&mut self, method: &str, params: &Value) -> bool
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, ¶ms);