Skip to main content

Session

Struct Session 

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

A long-lived document: a ParseTree that can absorb Edits.

Session::edit remaps every span into the new coordinates, bumps the source revision, and resets only the nodes the edit could have changed back to Unparsed — their children stay attached so the next run can reuse them.

Reuse works because the engine matches a re-expansion’s children against the existing children by span and context: any child whose span and context are unchanged keeps its identity, its status, and its whole subtree. Only the edited chain (and anything whose context references changed text) gets re-parsed — the rest of the tree is carried over as-is.

§Examples

use increparse::{CancelToken, Engine, Edit, Outcome, Pass, ParseTree, Schedule, Session, Span, Status};

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

struct Split;
impl Pass for Split {
    type Ctx = Ctx;
    fn parse(&self, _source: &str, span: Span, ctx: &Ctx) -> Outcome<Ctx> {
        match ctx {
            Ctx::File if span.len() >= 4 => Outcome::Expand(vec![
                (Span::new(span.start, span.start + 2, span.rev), Ctx::Region),
                (Span::new(span.start + 2, span.end, span.rev), Ctx::Region),
            ]),
            Ctx::File => Outcome::Failed,
            Ctx::Region => Outcome::Done,
        }
    }
}

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

let source = "abcd";
let mut session: Session<Ctx> =
    Session::new(0, Span::new(0, source.len(), 0), Ctx::File);
session.run(&engine, source, &increparse::SerialExecutor, &CancelToken::new());
let left = session.tree().children(session.tree().root())[0];
assert_eq!(session.tree().status(left), Status::Done);

// Same-length edit inside the right region: the left region is reused.
let source = "abCd";
session.edit(Edit::replace(2, 3, 3));
session.run(&engine, source, &increparse::SerialExecutor, &CancelToken::new());

assert_eq!(session.revision(), 1);
assert_eq!(session.tree().children(session.tree().root())[0], left);
assert_eq!(session.tree().status(left), Status::Done);

Implementations§

Source§

impl<C> Session<C>
where C: Clone + PartialEq + Send + 'static,

Source

pub fn new(source_rev: u64, root_span: Span, root_ctx: C) -> Session<C>

Creates a session whose tree root covers root_span of source revision source_rev, carrying root_ctx.

Source

pub fn from_source(source: &str, source_rev: u64, root_ctx: C) -> Session<C>

Creates a session whose root covers all of source at revision source_rev — the common case, with no hand-built root span.

Source

pub fn revision(&self) -> u64

The current source revision; bumped by every edit.

Source

pub fn tree(&self) -> &ParseTree<C>

The parse tree accumulated so far.

Source

pub fn tree_mut(&mut self) -> &mut ParseTree<C>

Mutable access to the tree, for queries that need it (e.g. custom traversal helpers); prefer tree otherwise.

Source

pub fn edit(&mut self, edit: Edit)

Applies a text edit: remaps spans, bumps the revision, and resets the touched nodes for re-parsing on the next run.

Source

pub fn run<E>( &mut self, engine: &Engine<C>, source: &str, exec: &E, cancel: &CancelToken, ) -> RunReport
where E: Executor,

Runs the engine over the current tree and source, returning the run report.

source must be the text at the session’s current revision.

Auto Trait Implementations§

§

impl<C> Freeze for Session<C>
where ParseTree<C>: Freeze,

§

impl<C> RefUnwindSafe for Session<C>

§

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

§

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

§

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

§

impl<C> UnsafeUnpin for Session<C>

§

impl<C> UnwindSafe for Session<C>

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.