mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +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,
|
snippet: None,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
if scopes.self_param.is_some() {
|
||||||
|
acc.push(CompletionItem {
|
||||||
|
name: "self".to_string(),
|
||||||
|
snippet: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -249,6 +255,13 @@ mod tests {
|
||||||
CompletionItem { name: "foo", snippet: None }]"#)
|
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]
|
#[test]
|
||||||
fn test_completion_kewords() {
|
fn test_completion_kewords() {
|
||||||
check_snippet_completion(r"
|
check_snippet_completion(r"
|
||||||
|
|
|
@ -13,6 +13,7 @@ type ScopeId = usize;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FnScopes {
|
pub struct FnScopes {
|
||||||
|
pub self_param: Option<SyntaxNode>,
|
||||||
scopes: Vec<ScopeData>,
|
scopes: Vec<ScopeData>,
|
||||||
scope_for: HashMap<SyntaxNode, ScopeId>,
|
scope_for: HashMap<SyntaxNode, ScopeId>,
|
||||||
}
|
}
|
||||||
|
@ -20,6 +21,9 @@ pub struct FnScopes {
|
||||||
impl FnScopes {
|
impl FnScopes {
|
||||||
pub fn new(fn_def: ast::FnDef) -> FnScopes {
|
pub fn new(fn_def: ast::FnDef) -> FnScopes {
|
||||||
let mut scopes = 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(),
|
scopes: Vec::new(),
|
||||||
scope_for: HashMap::new()
|
scope_for: HashMap::new()
|
||||||
};
|
};
|
||||||
|
|
|
@ -1168,6 +1168,9 @@ impl<'a> ParamList<'a> {
|
||||||
pub fn params(self) -> impl Iterator<Item = Param<'a>> + 'a {
|
pub fn params(self) -> impl Iterator<Item = Param<'a>> + 'a {
|
||||||
super::children(self)
|
super::children(self)
|
||||||
}
|
}
|
||||||
|
pub fn self_param(self) -> Option<SelfParam<'a>> {
|
||||||
|
super::child_opt(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParenExpr
|
// 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
|
// SlicePat
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct SlicePat<'a> {
|
pub struct SlicePat<'a> {
|
||||||
|
|
|
@ -488,10 +488,12 @@ Grammar(
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"ParamList": (
|
"ParamList": (
|
||||||
|
options: [ "SelfParam" ],
|
||||||
collections: [
|
collections: [
|
||||||
["params", "Param"]
|
["params", "Param"]
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
|
"SelfParam": (),
|
||||||
"Param": (
|
"Param": (
|
||||||
options: [ "Pat" ],
|
options: [ "Pat" ],
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in a new issue