diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs index 06a6072659..f3058c0231 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 5c04e2f9bc..78e9c061cc 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 58dcf574ea..50dc41b273 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 77730e3062..5225212298 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" ], ),