FullTextDocument

Struct FullTextDocument 

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

Implementations§

Source§

impl FullTextDocument

Source

pub fn new(language_id: String, version: i32, content: String) -> Self

Source

pub fn new_with_encoding( language_id: String, version: i32, content: String, encoding: PositionEncodingKind, ) -> Self

Create a new text document with a specific position encoding

This method allows you to create a text document with a specific position encoding for character positions. The encoding determines how character offsets are calculated and is important for proper LSP communication between client and server.

Use this method instead of new() when you need to use a position encoding other than UTF-16, or when you want to explicitly specify the encoding to match what was negotiated with the LSP client during initialization.

§Arguments
  • language_id - The language identifier for the document (e.g., “rust”, “javascript”)
  • version - The version number of the document
  • content - The full text content of the document
  • 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. Use new() for this.
  • 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.
§Examples

Basic usage with UTF-16 (default):

use lsp_textdocument::FullTextDocument;
use lsp_types::PositionEncodingKind;

let doc = FullTextDocument::new_with_encoding(
    "rust".to_string(),
    1,
    "fn main() {}".to_string(),
    PositionEncodingKind::UTF16
);

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

use lsp_textdocument::FullTextDocument;
use lsp_types::PositionEncodingKind;

let doc = FullTextDocument::new_with_encoding(
    "javascript".to_string(),
    1,
    "console.log('Hello');".to_string(),
    PositionEncodingKind::UTF8
);

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

use lsp_textdocument::FullTextDocument;
use lsp_types::PositionEncodingKind;

let doc = FullTextDocument::new_with_encoding(
    "plain".to_string(),
    1,
    "Hello 🦀 World".to_string(),
    PositionEncodingKind::UTF32
);
Source

pub fn update( &mut self, changes: &[TextDocumentContentChangeEvent], version: i32, )

Source

pub fn language_id(&self) -> &str

Document’s language id

Source

pub fn encoding(&self) -> PositionEncodingKind

Returns the position encoding kind used by this document when converting between LSP positions and byte offsets.

Source

pub fn version(&self) -> i32

Document’s version

Source

pub fn get_content(&self, range: Option<Range>) -> &str

Get document content

§Examples

Basic usage:

use lsp_textdocument::FullTextDocument;
use lsp_types::{Range, Position};

let text_documents = FullTextDocument::new("plain_text".to_string(), 1, "hello rust!".to_string());

// get document all content
let content = text_documents.get_content(None);
assert_eq!(content, "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_content(Some(range));
assert_eq!(sub_content, "ello rus");
Source

pub fn line_count(&self) -> u32

A amount of document content line

Source

pub fn content_len(&self) -> u32

The length of the document content in UTF-8 bytes

Source

pub fn position_at(&self, offset: u32) -> Position

Converts a zero-based byte offset in the UTF8-encoded content to a position

the offset is in bytes, the position is in UTF16 code units. rounds down if the offset is not on a code unit boundary, or is beyond the end of the content.

Source

pub fn offset_at(&self, position: Position) -> u32

Converts a position to a zero-based byte offset, suitable for slicing the UTF-8 encoded content.

Trait Implementations§

Source§

impl Debug for FullTextDocument

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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.