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>
impl<C: Clone + PartialEq + Send + 'static> Document<C>
Sourcepub fn open(
uri: Uri,
version: i32,
language_id: String,
text: String,
encoding: PositionEncoding,
root_ctx: C,
) -> Self
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.
Sourcepub fn language_id(&self) -> &str
pub fn language_id(&self) -> &str
The client’s language id for this document ("" when opened
without one).
Sourcepub fn encoding(&self) -> PositionEncoding
pub fn encoding(&self) -> PositionEncoding
The negotiated position encoding.
Sourcepub fn line_index(&self) -> &LineIndex
pub fn line_index(&self) -> &LineIndex
The line index of the current text.
Sourcepub fn session_mut(&mut self) -> &mut Session<C>
pub fn session_mut(&mut self) -> &mut Session<C>
Mutable access to the session, for custom workflows.
Sourcepub fn revision(&self) -> u64
pub fn revision(&self) -> u64
Current source revision of the parse tree (bumped once per applied change event).
Sourcepub fn location(&self, span: Span) -> Location
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.
Sourcepub fn range(&self, span: Span) -> Range
pub fn range(&self, span: Span) -> Range
Converts a tree span to an LSP range in the document’s encoding.
Sourcepub fn offset(&self, position: Position) -> usize
pub fn offset(&self, position: Position) -> usize
Converts an LSP position to a byte offset (clamped like
LineIndex::offset).
Sourcepub fn apply_changes<E>(
&mut self,
engine: &Engine<C>,
version: i32,
changes: &[TextDocumentContentChangeEvent],
exec: &E,
cancel: &CancelToken,
) -> RunReportwhere
E: Executor,
pub fn apply_changes<E>(
&mut self,
engine: &Engine<C>,
version: i32,
changes: &[TextDocumentContentChangeEvent],
exec: &E,
cancel: &CancelToken,
) -> RunReportwhere
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.