From 7a5bc94774a50837f8c9bf8b96c8272882aca640 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Aug 2018 16:30:42 +0300 Subject: complete self --- crates/libeditor/src/completion.rs | 13 +++++++++++++ crates/libeditor/src/scope/fn_scope.rs | 4 ++++ crates/libsyntax2/src/ast/generated.rs | 21 +++++++++++++++++++++ crates/libsyntax2/src/grammar.ron | 2 ++ 4 files changed, 40 insertions(+) diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs index 06a607265..f3058c023 100644 --- a/crates/libeditor/src/completion.rs +++ b/crates/libeditor/src/completion.rs @@ -142,6 +142,12 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec } } + ", r#"[CompletionItem { name: "self", snippet: None }]"#) + } + #[test] fn test_completion_kewords() { check_snippet_completion(r" diff --git a/crates/libeditor/src/scope/fn_scope.rs b/crates/libeditor/src/scope/fn_scope.rs index 5c04e2f9b..78e9c061c 100644 --- a/crates/libeditor/src/scope/fn_scope.rs +++ b/crates/libeditor/src/scope/fn_scope.rs @@ -13,6 +13,7 @@ type ScopeId = usize; #[derive(Debug)] pub struct FnScopes { + pub self_param: Option, scopes: Vec, scope_for: HashMap, } @@ -20,6 +21,9 @@ pub struct FnScopes { impl FnScopes { pub fn new(fn_def: ast::FnDef) -> FnScopes { let mut scopes = FnScopes { + self_param: fn_def.param_list() + .and_then(|it| it.self_param()) + .map(|it| it.syntax().owned()), scopes: Vec::new(), scope_for: HashMap::new() }; diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 58dcf574e..50dc41b27 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -1168,6 +1168,9 @@ impl<'a> ParamList<'a> { pub fn params(self) -> impl Iterator> + 'a { super::children(self) } +pub fn self_param(self) -> Option> { + super::child_opt(self) + } } // ParenExpr @@ -1579,6 +1582,24 @@ impl<'a> Root<'a> { } } +// SelfParam +#[derive(Debug, Clone, Copy)] +pub struct SelfParam<'a> { + syntax: SyntaxNodeRef<'a>, +} + +impl<'a> AstNode<'a> for SelfParam<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + SELF_PARAM => Some(SelfParam { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl<'a> SelfParam<'a> {} + // SlicePat #[derive(Debug, Clone, Copy)] pub struct SlicePat<'a> { diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 77730e306..522521229 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -488,10 +488,12 @@ Grammar( ] ), "ParamList": ( + options: [ "SelfParam" ], collections: [ ["params", "Param"] ] ), + "SelfParam": (), "Param": ( options: [ "Pat" ], ), -- cgit v1.2.3