mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
complete self
This commit is contained in:
parent
cdb9b4cbf4
commit
7a5bc94774
4 changed files with 40 additions and 0 deletions
|
@ -142,6 +142,12 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<Completi
|
|||
snippet: None,
|
||||
})
|
||||
);
|
||||
if scopes.self_param.is_some() {
|
||||
acc.push(CompletionItem {
|
||||
name: "self".to_string(),
|
||||
snippet: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -249,6 +255,13 @@ mod tests {
|
|||
CompletionItem { name: "foo", snippet: None }]"#)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complete_self() {
|
||||
check_scope_completion(r"
|
||||
impl S { fn foo(&self) { <|> } }
|
||||
", r#"[CompletionItem { name: "self", snippet: None }]"#)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_completion_kewords() {
|
||||
check_snippet_completion(r"
|
||||
|
|
|
@ -13,6 +13,7 @@ type ScopeId = usize;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct FnScopes {
|
||||
pub self_param: Option<SyntaxNode>,
|
||||
scopes: Vec<ScopeData>,
|
||||
scope_for: HashMap<SyntaxNode, ScopeId>,
|
||||
}
|
||||
|
@ -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()
|
||||
};
|
||||
|
|
|
@ -1168,6 +1168,9 @@ impl<'a> ParamList<'a> {
|
|||
pub fn params(self) -> impl Iterator<Item = Param<'a>> + 'a {
|
||||
super::children(self)
|
||||
}
|
||||
pub fn self_param(self) -> Option<SelfParam<'a>> {
|
||||
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<Self> {
|
||||
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> {
|
||||
|
|
|
@ -488,10 +488,12 @@ Grammar(
|
|||
]
|
||||
),
|
||||
"ParamList": (
|
||||
options: [ "SelfParam" ],
|
||||
collections: [
|
||||
["params", "Param"]
|
||||
]
|
||||
),
|
||||
"SelfParam": (),
|
||||
"Param": (
|
||||
options: [ "Pat" ],
|
||||
),
|
||||
|
|
Loading…
Reference in a new issue