diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/libeditor/src/completion.rs | 13 | ||||
-rw-r--r-- | crates/libeditor/src/scope/fn_scope.rs | 4 | ||||
-rw-r--r-- | crates/libsyntax2/src/ast/generated.rs | 21 | ||||
-rw-r--r-- | crates/libsyntax2/src/grammar.ron | 2 |
4 files changed, 40 insertions, 0 deletions
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<Completi | |||
142 | snippet: None, | 142 | snippet: None, |
143 | }) | 143 | }) |
144 | ); | 144 | ); |
145 | if scopes.self_param.is_some() { | ||
146 | acc.push(CompletionItem { | ||
147 | name: "self".to_string(), | ||
148 | snippet: None, | ||
149 | }) | ||
150 | } | ||
145 | } | 151 | } |
146 | 152 | ||
147 | #[cfg(test)] | 153 | #[cfg(test)] |
@@ -250,6 +256,13 @@ mod tests { | |||
250 | } | 256 | } |
251 | 257 | ||
252 | #[test] | 258 | #[test] |
259 | fn test_complete_self() { | ||
260 | check_scope_completion(r" | ||
261 | impl S { fn foo(&self) { <|> } } | ||
262 | ", r#"[CompletionItem { name: "self", snippet: None }]"#) | ||
263 | } | ||
264 | |||
265 | #[test] | ||
253 | fn test_completion_kewords() { | 266 | fn test_completion_kewords() { |
254 | check_snippet_completion(r" | 267 | check_snippet_completion(r" |
255 | fn quux() { | 268 | fn quux() { |
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; | |||
13 | 13 | ||
14 | #[derive(Debug)] | 14 | #[derive(Debug)] |
15 | pub struct FnScopes { | 15 | pub struct FnScopes { |
16 | pub self_param: Option<SyntaxNode>, | ||
16 | scopes: Vec<ScopeData>, | 17 | scopes: Vec<ScopeData>, |
17 | scope_for: HashMap<SyntaxNode, ScopeId>, | 18 | scope_for: HashMap<SyntaxNode, ScopeId>, |
18 | } | 19 | } |
@@ -20,6 +21,9 @@ pub struct FnScopes { | |||
20 | impl FnScopes { | 21 | impl FnScopes { |
21 | pub fn new(fn_def: ast::FnDef) -> FnScopes { | 22 | pub fn new(fn_def: ast::FnDef) -> FnScopes { |
22 | let mut scopes = FnScopes { | 23 | let mut scopes = FnScopes { |
24 | self_param: fn_def.param_list() | ||
25 | .and_then(|it| it.self_param()) | ||
26 | .map(|it| it.syntax().owned()), | ||
23 | scopes: Vec::new(), | 27 | scopes: Vec::new(), |
24 | scope_for: HashMap::new() | 28 | scope_for: HashMap::new() |
25 | }; | 29 | }; |
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> { | |||
1168 | pub fn params(self) -> impl Iterator<Item = Param<'a>> + 'a { | 1168 | pub fn params(self) -> impl Iterator<Item = Param<'a>> + 'a { |
1169 | super::children(self) | 1169 | super::children(self) |
1170 | } | 1170 | } |
1171 | pub fn self_param(self) -> Option<SelfParam<'a>> { | ||
1172 | super::child_opt(self) | ||
1173 | } | ||
1171 | } | 1174 | } |
1172 | 1175 | ||
1173 | // ParenExpr | 1176 | // ParenExpr |
@@ -1579,6 +1582,24 @@ impl<'a> Root<'a> { | |||
1579 | } | 1582 | } |
1580 | } | 1583 | } |
1581 | 1584 | ||
1585 | // SelfParam | ||
1586 | #[derive(Debug, Clone, Copy)] | ||
1587 | pub struct SelfParam<'a> { | ||
1588 | syntax: SyntaxNodeRef<'a>, | ||
1589 | } | ||
1590 | |||
1591 | impl<'a> AstNode<'a> for SelfParam<'a> { | ||
1592 | fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { | ||
1593 | match syntax.kind() { | ||
1594 | SELF_PARAM => Some(SelfParam { syntax }), | ||
1595 | _ => None, | ||
1596 | } | ||
1597 | } | ||
1598 | fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } | ||
1599 | } | ||
1600 | |||
1601 | impl<'a> SelfParam<'a> {} | ||
1602 | |||
1582 | // SlicePat | 1603 | // SlicePat |
1583 | #[derive(Debug, Clone, Copy)] | 1604 | #[derive(Debug, Clone, Copy)] |
1584 | pub struct SlicePat<'a> { | 1605 | 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( | |||
488 | ] | 488 | ] |
489 | ), | 489 | ), |
490 | "ParamList": ( | 490 | "ParamList": ( |
491 | options: [ "SelfParam" ], | ||
491 | collections: [ | 492 | collections: [ |
492 | ["params", "Param"] | 493 | ["params", "Param"] |
493 | ] | 494 | ] |
494 | ), | 495 | ), |
496 | "SelfParam": (), | ||
495 | "Param": ( | 497 | "Param": ( |
496 | options: [ "Pat" ], | 498 | options: [ "Pat" ], |
497 | ), | 499 | ), |