Skip to main content

Document

Struct Document 

Source
pub struct Document<C> { /* private fields */ }
Expand description

A single open document, bridging LSP change events and an increparse Session.

apply_changes is the whole workflow: hand it the didChange payload and an engine; it splices the text, translates every change into a byte-range Edit (so the parse tree reuses everything the edit did not touch), and runs the engine synchronously. How you thread that call — inline in your server loop, or on a background thread with a CancelToken — is your framework’s business.

§Examples

use increparse::{CancelToken, Engine, Outcome, Pass, Schedule, SerialExecutor, Span};
use increparse_lsp::{Document, PositionEncoding};
use lsp_types::{TextDocumentContentChangeEvent, Uri};

#[derive(Clone, Debug, PartialEq, Eq)]
enum Ctx { File }

struct Accept;
impl Pass for Accept {
    type Ctx = Ctx;
    fn parse(&self, _source: &str, _span: Span, _ctx: &Ctx) -> Outcome<Ctx> {
        Outcome::Done
    }
}

let mut schedule = Schedule::new();
schedule.push(Accept);
let engine = Engine::new(schedule);

let uri: Uri = "file:///w.txt".parse()?;
let mut doc = Document::open(uri, 1, "".into(), "abc".into(), PositionEncoding::Utf16, Ctx::File);

// A client insert at (0, 1): "abc" -> "aXbc".
let change = TextDocumentContentChangeEvent {
    range: Some(lsp_types::Range {
        start: lsp_types::Position { line: 0, character: 1 },
        end: lsp_types::Position { line: 0, character: 1 },
    }),
    range_length: None,
    text: "X".into(),
};
let report = doc.apply_changes(
    &engine, 2, &[change], &SerialExecutor, &CancelToken::new(),
);

assert!(report.reached_fixpoint);
assert_eq!(doc.version(), 2);
assert_eq!(doc.text(), "aXbc");
assert_eq!(doc.revision(), 1);

Implementations§

Source§

impl<C: Clone + PartialEq + Send + 'static> Document<C>

Source

pub fn open( uri: Uri, version: i32, language_id: String, text: String, encoding: PositionEncoding, root_ctx: C, ) -> Self

Opens a document at version with the full initial text.

The tree root covers the whole text and carries root_ctx. language_id is the client’s language id for the document (the languageId field of didOpen); it is informational — servers that dispatch per language read it back with language_id. Pass "" when there is none.

Source

pub fn uri(&self) -> &Uri

The document’s URI.

Source

pub fn language_id(&self) -> &str

The client’s language id for this document ("" when opened without one).

Source

pub fn text(&self) -> &str

The current text.

Source

pub fn version(&self) -> i32

The client’s document version.

Source

pub fn encoding(&self) -> PositionEncoding

The negotiated position encoding.

Source

pub fn line_index(&self) -> &LineIndex

The line index of the current text.

Source

pub fn session(&self) -> &Session<C>

The parse session (tree, revisions, edits).

Source

pub fn session_mut(&mut self) -> &mut Session<C>

Mutable access to the session, for custom workflows.

Source

pub fn revision(&self) -> u64

Current source revision of the parse tree (bumped once per applied change event).

Source

pub fn location(&self, span: Span) -> Location

Converts a tree span to an LSP location in this document — the convenient shape for go-to-definition answers.

Source

pub fn range(&self, span: Span) -> Range

Converts a tree span to an LSP range in the document’s encoding.

Source

pub fn offset(&self, position: Position) -> usize

Converts an LSP position to a byte offset (clamped like LineIndex::offset).

Source

pub fn apply_changes<E>( &mut self, engine: &Engine<C>, version: i32, changes: &[TextDocumentContentChangeEvent], exec: &E, cancel: &CancelToken, ) -> RunReport
where E: Executor,

Applies a didChange batch and runs the engine once.

Events are applied in order, each interpreted against the text left by the previous one, per the LSP spec. A full-text event (range == None) replaces the whole document; ranged events splice their text into the given range. Positions past the end of a line or document are clamped, matching common client behavior while typing.

version becomes the document’s new version and is returned by version; the parse tree sees one revision bump per applied event and a single run at the end.

Auto Trait Implementations§

§

impl<C> !Freeze for Document<C>

§

impl<C> !RefUnwindSafe for Document<C>

§

impl<C> Send for Document<C>
where Session<C>: Send,

§

impl<C> Sync for Document<C>
where Session<C>: Sync,

§

impl<C> Unpin for Document<C>
where Session<C>: Unpin,

§

impl<C> UnsafeUnpin for Document<C>
where Session<C>: UnsafeUnpin,

§

impl<C> UnwindSafe for Document<C>
where Session<C>: UnwindSafe,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.