mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
Goto type definition works for self
This commit is contained in:
parent
5fa8f8e376
commit
b85042601d
5 changed files with 51 additions and 8 deletions
|
@ -192,6 +192,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
|||
self.imp.type_of_pat(pat)
|
||||
}
|
||||
|
||||
pub fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
|
||||
self.imp.type_of_self(param)
|
||||
}
|
||||
|
||||
pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
|
||||
self.imp.resolve_method_call(call)
|
||||
}
|
||||
|
@ -216,6 +220,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
|||
self.imp.resolve_path(path)
|
||||
}
|
||||
|
||||
// TODO: id
|
||||
pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option<VariantId> {
|
||||
self.imp.resolve_variant(record_lit)
|
||||
}
|
||||
|
@ -377,6 +382,10 @@ impl<'db> SemanticsImpl<'db> {
|
|||
self.analyze(pat.syntax()).type_of_pat(self.db, &pat)
|
||||
}
|
||||
|
||||
pub fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
|
||||
self.analyze(param.syntax()).type_of_self(self.db, ¶m)
|
||||
}
|
||||
|
||||
pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
|
||||
self.analyze(call.syntax()).resolve_method_call(self.db, call)
|
||||
}
|
||||
|
|
|
@ -115,6 +115,7 @@ impl SourceAnalyzer {
|
|||
Some(res)
|
||||
}
|
||||
|
||||
// TODO: rename
|
||||
pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<Type> {
|
||||
let expr_id = self.expr_id(db, expr)?;
|
||||
let ty = self.infer.as_ref()?[expr_id].clone();
|
||||
|
@ -127,6 +128,17 @@ impl SourceAnalyzer {
|
|||
Type::new_with_resolver(db, &self.resolver, ty)
|
||||
}
|
||||
|
||||
pub(crate) fn type_of_self(
|
||||
&self,
|
||||
db: &dyn HirDatabase,
|
||||
param: &ast::SelfParam,
|
||||
) -> Option<Type> {
|
||||
let src = InFile { file_id: self.file_id, value: param };
|
||||
let pat_id = self.body_source_map.as_ref()?.node_self_param(src)?;
|
||||
let ty = self.infer.as_ref()?[pat_id].clone();
|
||||
Type::new_with_resolver(db, &self.resolver, ty)
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_method_call(
|
||||
&self,
|
||||
db: &dyn HirDatabase,
|
||||
|
|
|
@ -302,6 +302,11 @@ impl BodySourceMap {
|
|||
self.pat_map.get(&src).cloned()
|
||||
}
|
||||
|
||||
pub fn node_self_param(&self, node: InFile<&ast::SelfParam>) -> Option<PatId> {
|
||||
let src = node.map(|it| Either::Right(AstPtr::new(it)));
|
||||
self.pat_map.get(&src).cloned()
|
||||
}
|
||||
|
||||
pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile<AstPtr<ast::RecordField>> {
|
||||
self.field_map[&(expr, field)].clone()
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use ra_syntax::{
|
|||
ast::{self},
|
||||
match_ast, AstNode,
|
||||
SyntaxKind::*,
|
||||
SyntaxToken, TokenAtOffset,
|
||||
SyntaxToken, TokenAtOffset, T,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -32,9 +32,10 @@ pub(crate) fn goto_definition(
|
|||
let file = sema.parse(position.file_id).syntax().clone();
|
||||
let original_token = pick_best(file.token_at_offset(position.offset))?;
|
||||
let token = sema.descend_into_macros(original_token.clone());
|
||||
let parent = token.parent();
|
||||
|
||||
let nav_targets = match_ast! {
|
||||
match (token.parent()) {
|
||||
match parent {
|
||||
ast::NameRef(name_ref) => {
|
||||
reference_definition(&sema, &name_ref).to_vec()
|
||||
},
|
||||
|
@ -57,7 +58,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
|||
return tokens.max_by_key(priority);
|
||||
fn priority(n: &SyntaxToken) -> usize {
|
||||
match n.kind() {
|
||||
IDENT | INT_NUMBER => 2,
|
||||
IDENT | INT_NUMBER | T![self] => 2,
|
||||
kind if kind.is_trivia() => 0,
|
||||
_ => 1,
|
||||
}
|
||||
|
@ -121,7 +122,8 @@ mod tests {
|
|||
data => panic!("bad data: {}", data),
|
||||
}
|
||||
|
||||
let mut navs = analysis.goto_definition(position).unwrap().unwrap().info;
|
||||
let mut navs =
|
||||
analysis.goto_definition(position).unwrap().expect("no definition found").info;
|
||||
if navs.len() == 0 {
|
||||
panic!("unresolved reference")
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use ra_ide_db::RootDatabase;
|
||||
use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
|
||||
use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
|
||||
|
||||
use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo};
|
||||
|
||||
|
@ -25,8 +25,9 @@ pub(crate) fn goto_type_definition(
|
|||
let (ty, node) = sema.ancestors_with_macros(token.parent()).find_map(|node| {
|
||||
let ty = match_ast! {
|
||||
match node {
|
||||
ast::Expr(expr) => sema.type_of_expr(&expr)?,
|
||||
ast::Pat(pat) => sema.type_of_pat(&pat)?,
|
||||
ast::Expr(it) => sema.type_of_expr(&it)?,
|
||||
ast::Pat(it) => sema.type_of_pat(&it)?,
|
||||
ast::SelfParam(it) => sema.type_of_self(&it)?,
|
||||
_ => return None,
|
||||
}
|
||||
};
|
||||
|
@ -44,7 +45,7 @@ fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
|||
return tokens.max_by_key(priority);
|
||||
fn priority(n: &SyntaxToken) -> usize {
|
||||
match n.kind() {
|
||||
IDENT | INT_NUMBER => 2,
|
||||
IDENT | INT_NUMBER | T![self] => 2,
|
||||
kind if kind.is_trivia() => 0,
|
||||
_ => 1,
|
||||
}
|
||||
|
@ -140,4 +141,18 @@ mod tests {
|
|||
"Foo STRUCT_DEF FileId(1) 0..11 7..10",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_def_for_self_param() {
|
||||
check_goto(
|
||||
r#"
|
||||
struct Foo;
|
||||
impl Foo {
|
||||
//^^^
|
||||
fn f(&self<|>) {}
|
||||
}
|
||||
"#,
|
||||
"Foo STRUCT_DEF FileId(1) 0..11 7..10",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue