mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
scope based comletion
This commit is contained in:
parent
4c121bfa2f
commit
ac226021cf
8 changed files with 475 additions and 41 deletions
|
@ -6,6 +6,10 @@ pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> {
|
|||
EmptyVisitor { ph: PhantomData }
|
||||
}
|
||||
|
||||
pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output=T, Ctx=C> {
|
||||
EmptyVisitorCtx { ph: PhantomData, ctx }
|
||||
}
|
||||
|
||||
pub trait Visitor<'a>: Sized {
|
||||
type Output;
|
||||
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>;
|
||||
|
@ -17,6 +21,18 @@ pub trait Visitor<'a>: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait VisitorCtx<'a>: Sized {
|
||||
type Output;
|
||||
type Ctx;
|
||||
fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx>;
|
||||
fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F>
|
||||
where N: AstNode<'a>,
|
||||
F: FnOnce(N, Self::Ctx) -> Self::Output,
|
||||
{
|
||||
VisCtx { inner: self, f, ph: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct EmptyVisitor<T> {
|
||||
ph: PhantomData<fn() -> T>
|
||||
|
@ -30,6 +46,21 @@ impl<'a, T> Visitor<'a> for EmptyVisitor<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct EmptyVisitorCtx<T, C> {
|
||||
ctx: C,
|
||||
ph: PhantomData<fn() -> T>,
|
||||
}
|
||||
|
||||
impl<'a, T, C> VisitorCtx<'a> for EmptyVisitorCtx<T, C> {
|
||||
type Output = T;
|
||||
type Ctx = C;
|
||||
|
||||
fn accept(self, _node: SyntaxNodeRef<'a>) -> Result<T, C> {
|
||||
Err(self.ctx)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Vis<V, N, F> {
|
||||
inner: V,
|
||||
|
@ -50,3 +81,30 @@ impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
|
|||
inner.accept(node).or_else(|| N::cast(node).map(f))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VisCtx<V, N, F> {
|
||||
inner: V,
|
||||
f: F,
|
||||
ph: PhantomData<fn(N)>,
|
||||
}
|
||||
|
||||
impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F>
|
||||
where
|
||||
V: VisitorCtx<'a>,
|
||||
N: AstNode<'a>,
|
||||
F: FnOnce(N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output,
|
||||
{
|
||||
type Output = <V as VisitorCtx<'a>>::Output;
|
||||
type Ctx = <V as VisitorCtx<'a>>::Ctx;
|
||||
|
||||
fn accept(self, node: SyntaxNodeRef<'a>) -> Result<Self::Output, Self::Ctx> {
|
||||
let VisCtx { inner, f, .. } = self;
|
||||
inner.accept(node).or_else(|ctx|
|
||||
match N::cast(node) {
|
||||
None => Err(ctx),
|
||||
Some(node) => Ok(f(node, ctx))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,19 +17,19 @@ pub enum WalkEvent<'a> {
|
|||
}
|
||||
|
||||
pub fn walk<'a>(root: SyntaxNodeRef<'a>) -> impl Iterator<Item = WalkEvent<'a>> {
|
||||
generate(Some(WalkEvent::Enter(root)), |pos| {
|
||||
generate(Some(WalkEvent::Enter(root)), move |pos| {
|
||||
let next = match *pos {
|
||||
WalkEvent::Enter(node) => match node.first_child() {
|
||||
Some(child) => WalkEvent::Enter(child),
|
||||
None => WalkEvent::Exit(node),
|
||||
},
|
||||
WalkEvent::Exit(node) => {
|
||||
if node == root {
|
||||
return None;
|
||||
}
|
||||
match node.next_sibling() {
|
||||
Some(sibling) => WalkEvent::Enter(sibling),
|
||||
None => match node.parent() {
|
||||
Some(node) => WalkEvent::Exit(node),
|
||||
None => return None,
|
||||
},
|
||||
None => WalkEvent::Exit(node.parent().unwrap()),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue