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>
impl<C> Session<C>
Sourcepub fn new(source_rev: u64, root_span: Span, root_ctx: C) -> Session<C>
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.
Sourcepub fn from_source(source: &str, source_rev: u64, root_ctx: C) -> Session<C>
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.
Sourcepub fn tree_mut(&mut self) -> &mut ParseTree<C>
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.