scope based comletion

This commit is contained in:
Aleksey Kladov 2018-08-26 12:09:28 +03:00
parent 4c121bfa2f
commit ac226021cf
8 changed files with 475 additions and 41 deletions

View file

@ -9,7 +9,7 @@ use libsyntax2::{
SyntaxNodeRef,
algo::{
Direction, siblings,
find_leaf_at_offset, ancestors,
find_leaf_at_offset,
},
};

View file

@ -1,7 +1,11 @@
use libsyntax2::{
File, TextUnit,
ast,
algo::find_leaf_at_offset,
File, TextUnit, AstNode, SyntaxNodeRef,
ast::{self, NameOwner},
algo::{
ancestors,
visit::{visitor_ctx, VisitorCtx},
walk::preorder,
},
};
use {
@ -25,7 +29,33 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI
}
fn complete(name_ref: ast::NameRef) -> Vec<CompletionItem> {
vec![CompletionItem {
name: "foo".to_string()
}]
let mut res = Vec::new();
for node in ancestors(name_ref.syntax()) {
process_scope(node, &mut res);
}
res
}
fn process_scope(node: SyntaxNodeRef, sink: &mut Vec<CompletionItem>) {
let _ = visitor_ctx(sink)
.visit::<ast::Block, _>(|block, sink| {
block.let_stmts()
.filter_map(|it| it.pat())
.for_each(move |it| process_pat(it, sink))
})
.visit::<ast::FnDef, _>(|fn_def, sink| {
fn_def.param_list().into_iter()
.flat_map(|it| it.params())
.filter_map(|it| it.pat())
.for_each(move |it| process_pat(it, sink))
})
.accept(node);
fn process_pat(pat: ast::Pat, sink: &mut Vec<CompletionItem>) {
let items = preorder(pat.syntax())
.filter_map(ast::BindPat::cast)
.filter_map(ast::BindPat::name)
.map(|name| CompletionItem { name: name.text().to_string() });
sink.extend(items);
}
}

View file

@ -256,25 +256,23 @@ struct Foo { f: u32 }
");
}
// #[test]
// fn test_completion() {
// fn do_check(code: &str, expected_completions: &str) {
// let (off, code) = extract_offset(&code);
// let file = file(&code);
// let completions = scope_completion(&file, off).unwrap();
// assert_eq_dbg(expected_completions, &completions);
// }
#[test]
fn test_completion() {
fn do_check(code: &str, expected_completions: &str) {
let (off, code) = extract_offset(&code);
let file = file(&code);
let completions = scope_completion(&file, off).unwrap();
assert_eq_dbg(expected_completions, &completions);
}
// do_check(r"
// fn foo(foo: i32) {
// let bar = 92;
// 1 + <|>
// }
// ", r#"
// CompletionItem { name: "bar" },
// CompletionItem { name: "foo" },
// "#);
// }
do_check(r"
fn quux(x: i32) {
let y = 92;
1 + <|>
}
", r#"[CompletionItem { name: "y" },
CompletionItem { name: "x" }]"#);
}
fn file(text: &str) -> File {
File::parse(text)

View file

@ -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))
}
)
}
}

View file

@ -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()),
}
}
};

View file

@ -80,6 +80,25 @@ impl<'a> AstNode<'a> for BinExpr<'a> {
impl<'a> BinExpr<'a> {}
// BindPat
#[derive(Debug, Clone, Copy)]
pub struct BindPat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for BindPat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
BIND_PAT => Some(BindPat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> ast::NameOwner<'a> for BindPat<'a> {}
impl<'a> BindPat<'a> {}
// Block
#[derive(Debug, Clone, Copy)]
pub struct Block<'a> {
@ -96,7 +115,11 @@ impl<'a> AstNode<'a> for Block<'a> {
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Block<'a> {}
impl<'a> Block<'a> {
pub fn let_stmts(self) -> impl Iterator<Item = LetStmt<'a>> + 'a {
super::children(self)
}
}
// BlockExpr
#[derive(Debug, Clone, Copy)]
@ -378,6 +401,24 @@ impl<'a> AstNode<'a> for FieldExpr<'a> {
impl<'a> FieldExpr<'a> {}
// FieldPatList
#[derive(Debug, Clone, Copy)]
pub struct FieldPatList<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for FieldPatList<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
FIELD_PAT_LIST => Some(FieldPatList { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> FieldPatList<'a> {}
// FnDef
#[derive(Debug, Clone, Copy)]
pub struct FnDef<'a> {
@ -397,7 +438,11 @@ impl<'a> AstNode<'a> for FnDef<'a> {
impl<'a> ast::NameOwner<'a> for FnDef<'a> {}
impl<'a> ast::TypeParamsOwner<'a> for FnDef<'a> {}
impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {}
impl<'a> FnDef<'a> {}
impl<'a> FnDef<'a> {
pub fn param_list(self) -> Option<ParamList<'a>> {
super::child_opt(self)
}
}
// FnPointerType
#[derive(Debug, Clone, Copy)]
@ -561,6 +606,28 @@ impl<'a> AstNode<'a> for LambdaExpr<'a> {
impl<'a> LambdaExpr<'a> {}
// LetStmt
#[derive(Debug, Clone, Copy)]
pub struct LetStmt<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for LetStmt<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
LET_STMT => Some(LetStmt { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> LetStmt<'a> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self)
}
}
// LoopExpr
#[derive(Debug, Clone, Copy)]
pub struct LoopExpr<'a> {
@ -831,6 +898,50 @@ impl<'a> ast::TypeParamsOwner<'a> for NominalDef<'a> {}
impl<'a> ast::AttrsOwner<'a> for NominalDef<'a> {}
impl<'a> NominalDef<'a> {}
// Param
#[derive(Debug, Clone, Copy)]
pub struct Param<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for Param<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
PARAM => Some(Param { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Param<'a> {
pub fn pat(self) -> Option<Pat<'a>> {
super::child_opt(self)
}
}
// ParamList
#[derive(Debug, Clone, Copy)]
pub struct ParamList<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for ParamList<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
PARAM_LIST => Some(ParamList { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> ParamList<'a> {
pub fn params(self) -> impl Iterator<Item = Param<'a>> + 'a {
super::children(self)
}
}
// ParenExpr
#[derive(Debug, Clone, Copy)]
pub struct ParenExpr<'a> {
@ -867,6 +978,55 @@ impl<'a> AstNode<'a> for ParenType<'a> {
impl<'a> ParenType<'a> {}
// Pat
#[derive(Debug, Clone, Copy)]
pub enum Pat<'a> {
RefPat(RefPat<'a>),
BindPat(BindPat<'a>),
PlaceholderPat(PlaceholderPat<'a>),
PathPat(PathPat<'a>),
StructPat(StructPat<'a>),
FieldPatList(FieldPatList<'a>),
TupleStructPat(TupleStructPat<'a>),
TuplePat(TuplePat<'a>),
SlicePat(SlicePat<'a>),
RangePat(RangePat<'a>),
}
impl<'a> AstNode<'a> for Pat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
REF_PAT => Some(Pat::RefPat(RefPat { syntax })),
BIND_PAT => Some(Pat::BindPat(BindPat { syntax })),
PLACEHOLDER_PAT => Some(Pat::PlaceholderPat(PlaceholderPat { syntax })),
PATH_PAT => Some(Pat::PathPat(PathPat { syntax })),
STRUCT_PAT => Some(Pat::StructPat(StructPat { syntax })),
FIELD_PAT_LIST => Some(Pat::FieldPatList(FieldPatList { syntax })),
TUPLE_STRUCT_PAT => Some(Pat::TupleStructPat(TupleStructPat { syntax })),
TUPLE_PAT => Some(Pat::TuplePat(TuplePat { syntax })),
SLICE_PAT => Some(Pat::SlicePat(SlicePat { syntax })),
RANGE_PAT => Some(Pat::RangePat(RangePat { syntax })),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> {
match self {
Pat::RefPat(inner) => inner.syntax(),
Pat::BindPat(inner) => inner.syntax(),
Pat::PlaceholderPat(inner) => inner.syntax(),
Pat::PathPat(inner) => inner.syntax(),
Pat::StructPat(inner) => inner.syntax(),
Pat::FieldPatList(inner) => inner.syntax(),
Pat::TupleStructPat(inner) => inner.syntax(),
Pat::TuplePat(inner) => inner.syntax(),
Pat::SlicePat(inner) => inner.syntax(),
Pat::RangePat(inner) => inner.syntax(),
}
}
}
impl<'a> Pat<'a> {}
// PathExpr
#[derive(Debug, Clone, Copy)]
pub struct PathExpr<'a> {
@ -885,6 +1045,24 @@ impl<'a> AstNode<'a> for PathExpr<'a> {
impl<'a> PathExpr<'a> {}
// PathPat
#[derive(Debug, Clone, Copy)]
pub struct PathPat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for PathPat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
PATH_PAT => Some(PathPat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> PathPat<'a> {}
// PathType
#[derive(Debug, Clone, Copy)]
pub struct PathType<'a> {
@ -903,6 +1081,24 @@ impl<'a> AstNode<'a> for PathType<'a> {
impl<'a> PathType<'a> {}
// PlaceholderPat
#[derive(Debug, Clone, Copy)]
pub struct PlaceholderPat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for PlaceholderPat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
PLACEHOLDER_PAT => Some(PlaceholderPat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> PlaceholderPat<'a> {}
// PlaceholderType
#[derive(Debug, Clone, Copy)]
pub struct PlaceholderType<'a> {
@ -975,6 +1171,24 @@ impl<'a> AstNode<'a> for RangeExpr<'a> {
impl<'a> RangeExpr<'a> {}
// RangePat
#[derive(Debug, Clone, Copy)]
pub struct RangePat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for RangePat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
RANGE_PAT => Some(RangePat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> RangePat<'a> {}
// RefExpr
#[derive(Debug, Clone, Copy)]
pub struct RefExpr<'a> {
@ -993,6 +1207,24 @@ impl<'a> AstNode<'a> for RefExpr<'a> {
impl<'a> RefExpr<'a> {}
// RefPat
#[derive(Debug, Clone, Copy)]
pub struct RefPat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for RefPat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
REF_PAT => Some(RefPat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> RefPat<'a> {}
// ReferenceType
#[derive(Debug, Clone, Copy)]
pub struct ReferenceType<'a> {
@ -1055,6 +1287,24 @@ impl<'a> Root<'a> {
}
}
// SlicePat
#[derive(Debug, Clone, Copy)]
pub struct SlicePat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for SlicePat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
SLICE_PAT => Some(SlicePat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> SlicePat<'a> {}
// SliceType
#[derive(Debug, Clone, Copy)]
pub struct SliceType<'a> {
@ -1137,6 +1387,24 @@ impl<'a> AstNode<'a> for StructLit<'a> {
impl<'a> StructLit<'a> {}
// StructPat
#[derive(Debug, Clone, Copy)]
pub struct StructPat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for StructPat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
STRUCT_PAT => Some(StructPat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> StructPat<'a> {}
// TokenTree
#[derive(Debug, Clone, Copy)]
pub struct TokenTree<'a> {
@ -1211,6 +1479,42 @@ impl<'a> AstNode<'a> for TupleExpr<'a> {
impl<'a> TupleExpr<'a> {}
// TuplePat
#[derive(Debug, Clone, Copy)]
pub struct TuplePat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for TuplePat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
TUPLE_PAT => Some(TuplePat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> TuplePat<'a> {}
// TupleStructPat
#[derive(Debug, Clone, Copy)]
pub struct TupleStructPat<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for TupleStructPat<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
TUPLE_STRUCT_PAT => Some(TupleStructPat { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> TupleStructPat<'a> {}
// TupleType
#[derive(Debug, Clone, Copy)]
pub struct TupleType<'a> {

View file

@ -241,11 +241,16 @@ Grammar(
["modules", "Module"],
]
),
"FnDef": ( traits: [
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
] ),
"FnDef": (
traits: [
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
],
options: [
["param_list", "ParamList"]
],
),
"StructDef": (
traits: [
"NameOwner",
@ -393,13 +398,52 @@ Grammar(
],
),
"RefPat": (),
"BindPat": ( traits: ["NameOwner"] ),
"PlaceholderPat": (),
"PathPat": (),
"StructPat": (),
"FieldPatList": (),
"TupleStructPat": (),
"TuplePat": (),
"SlicePat": (),
"RangePat": (),
"Pat": (
enum: [
"RefPat",
"BindPat",
"PlaceholderPat",
"PathPat",
"StructPat",
"FieldPatList",
"TupleStructPat",
"TuplePat",
"SlicePat",
"RangePat",
],
),
"Name": (),
"NameRef": (),
"Attr": ( options: [ ["value", "TokenTree"] ] ),
"TokenTree": (),
"TypeParamList": ( collections: [ ["type_params", "TypeParam" ] ]),
"TypeParam": ( traits: ["NameOwner"]),
"TypeParam": ( traits: ["NameOwner"] ),
"WhereClause": (),
"Block": (),
"LetStmt": ( options: [ ["pat", "Pat"] ]),
"Block": (
collections: [
["let_stmts", "LetStmt"],
]
),
"ParamList": (
collections: [
["params", "Param"]
]
),
"Param": (
options: [["pat", "Pat"]],
)
},
)

View file

@ -115,7 +115,7 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
// test unsafe_fn
// unsafe fn foo() {}
FN_KW => {
function(p, flavor);
fn_def(p, flavor);
FN_DEF
}
@ -227,7 +227,7 @@ fn extern_item_list(p: &mut Parser) {
m.complete(p, EXTERN_ITEM_LIST);
}
fn function(p: &mut Parser, flavor: ItemFlavor) {
fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
assert!(p.at(FN_KW));
p.bump();