mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Merge #645
645: WIP: support goto for fields. r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
daaba4be17
@ -11,7 +11,7 @@ use ra_syntax::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, AsName, Struct, Enum, EnumVariant, Crate,
|
Name, AsName, Struct, Enum, EnumVariant, Crate,
|
||||||
HirDatabase, HirFileId,
|
HirDatabase, HirFileId, StructField, FieldSource,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -150,7 +150,7 @@ impl VariantData {
|
|||||||
impl VariantData {
|
impl VariantData {
|
||||||
fn new(flavor: StructFlavor) -> Self {
|
fn new(flavor: StructFlavor) -> Self {
|
||||||
let inner = match flavor {
|
let inner = match flavor {
|
||||||
StructFlavor::Tuple(fl) => {
|
ast::StructFlavor::Tuple(fl) => {
|
||||||
let fields = fl
|
let fields = fl
|
||||||
.fields()
|
.fields()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@ -161,7 +161,7 @@ impl VariantData {
|
|||||||
.collect();
|
.collect();
|
||||||
VariantDataInner::Tuple(fields)
|
VariantDataInner::Tuple(fields)
|
||||||
}
|
}
|
||||||
StructFlavor::Named(fl) => {
|
ast::StructFlavor::Named(fl) => {
|
||||||
let fields = fl
|
let fields = fl
|
||||||
.fields()
|
.fields()
|
||||||
.map(|fd| StructFieldData {
|
.map(|fd| StructFieldData {
|
||||||
@ -171,8 +171,70 @@ impl VariantData {
|
|||||||
.collect();
|
.collect();
|
||||||
VariantDataInner::Struct(fields)
|
VariantDataInner::Struct(fields)
|
||||||
}
|
}
|
||||||
StructFlavor::Unit => VariantDataInner::Unit,
|
ast::StructFlavor::Unit => VariantDataInner::Unit,
|
||||||
};
|
};
|
||||||
VariantData(inner)
|
VariantData(inner)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub enum VariantDef {
|
||||||
|
Struct(Struct),
|
||||||
|
EnumVariant(EnumVariant),
|
||||||
|
}
|
||||||
|
impl_froms!(VariantDef: Struct, EnumVariant);
|
||||||
|
|
||||||
|
impl VariantDef {
|
||||||
|
pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
|
||||||
|
match self {
|
||||||
|
VariantDef::Struct(it) => it.field(db, name),
|
||||||
|
VariantDef::EnumVariant(it) => it.field(db, name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(crate) fn variant_data(self, db: &impl HirDatabase) -> Arc<VariantData> {
|
||||||
|
match self {
|
||||||
|
VariantDef::Struct(it) => it.variant_data(db),
|
||||||
|
VariantDef::EnumVariant(it) => it.variant_data(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StructField {
|
||||||
|
pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, FieldSource) {
|
||||||
|
let var_data = self.parent.variant_data(db);
|
||||||
|
let fields = var_data.fields().unwrap();
|
||||||
|
let ss;
|
||||||
|
let es;
|
||||||
|
let (file_id, struct_flavor) = match self.parent {
|
||||||
|
VariantDef::Struct(s) => {
|
||||||
|
let (file_id, source) = s.source(db);
|
||||||
|
ss = source;
|
||||||
|
(file_id, ss.flavor())
|
||||||
|
}
|
||||||
|
VariantDef::EnumVariant(e) => {
|
||||||
|
let (file_id, source) = e.source(db);
|
||||||
|
es = source;
|
||||||
|
(file_id, es.flavor())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let field_sources = match struct_flavor {
|
||||||
|
ast::StructFlavor::Tuple(fl) => fl
|
||||||
|
.fields()
|
||||||
|
.map(|it| FieldSource::Pos(it.to_owned()))
|
||||||
|
.collect(),
|
||||||
|
ast::StructFlavor::Named(fl) => fl
|
||||||
|
.fields()
|
||||||
|
.map(|it| FieldSource::Named(it.to_owned()))
|
||||||
|
.collect(),
|
||||||
|
ast::StructFlavor::Unit => Vec::new(),
|
||||||
|
};
|
||||||
|
let field = field_sources
|
||||||
|
.into_iter()
|
||||||
|
.zip(fields.iter())
|
||||||
|
.find(|(_syntax, (id, _))| *id == self.id)
|
||||||
|
.unwrap()
|
||||||
|
.0;
|
||||||
|
(file_id, field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,8 +10,8 @@ use crate::{
|
|||||||
nameres::{ModuleScope, lower::ImportId},
|
nameres::{ModuleScope, lower::ImportId},
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
expr::BodySyntaxMapping,
|
expr::BodySyntaxMapping,
|
||||||
ty::{InferenceResult, VariantDef},
|
ty::InferenceResult,
|
||||||
adt::{EnumVariantId, StructFieldId},
|
adt::{EnumVariantId, StructFieldId, VariantDef},
|
||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
docs::{Documentation, Docs, docs_from_ast},
|
docs::{Documentation, Docs, docs_from_ast},
|
||||||
module_tree::ModuleId,
|
module_tree::ModuleId,
|
||||||
@ -179,10 +179,16 @@ impl Module {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct StructField {
|
pub struct StructField {
|
||||||
parent: VariantDef,
|
pub(crate) parent: VariantDef,
|
||||||
pub(crate) id: StructFieldId,
|
pub(crate) id: StructFieldId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum FieldSource {
|
||||||
|
Named(TreeArc<ast::NamedFieldDef>),
|
||||||
|
Pos(TreeArc<ast::PosField>),
|
||||||
|
}
|
||||||
|
|
||||||
impl StructField {
|
impl StructField {
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Name {
|
pub fn name(&self, db: &impl HirDatabase) -> Name {
|
||||||
self.parent.variant_data(db).fields().unwrap()[self.id]
|
self.parent.variant_data(db).fields().unwrap()[self.id]
|
||||||
@ -190,6 +196,10 @@ impl StructField {
|
|||||||
.clone()
|
.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, FieldSource) {
|
||||||
|
self.source_impl(db)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ty(&self, db: &impl HirDatabase) -> Ty {
|
pub fn ty(&self, db: &impl HirDatabase) -> Ty {
|
||||||
db.type_for_field(*self)
|
db.type_for_field(*self)
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ pub use self::code_model_api::{
|
|||||||
Module, ModuleDef, ModuleSource, Problem,
|
Module, ModuleDef, ModuleSource, Problem,
|
||||||
Struct, Enum, EnumVariant,
|
Struct, Enum, EnumVariant,
|
||||||
Function, FnSignature, ScopeEntryWithSyntax,
|
Function, FnSignature, ScopeEntryWithSyntax,
|
||||||
StructField,
|
StructField, FieldSource,
|
||||||
Static, Const,
|
Static, Const,
|
||||||
Trait, Type,
|
Trait, Type,
|
||||||
};
|
};
|
||||||
|
@ -38,7 +38,7 @@ use crate::{
|
|||||||
expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat},
|
expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat},
|
||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
path::GenericArg,
|
path::GenericArg,
|
||||||
adt::VariantData,
|
adt::VariantDef,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The ID of a type variable.
|
/// The ID of a type variable.
|
||||||
@ -696,28 +696,6 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def: TypableDef) -> Ty {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
||||||
pub enum VariantDef {
|
|
||||||
Struct(Struct),
|
|
||||||
EnumVariant(EnumVariant),
|
|
||||||
}
|
|
||||||
impl_froms!(VariantDef: Struct, EnumVariant);
|
|
||||||
|
|
||||||
impl VariantDef {
|
|
||||||
pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
|
|
||||||
match self {
|
|
||||||
VariantDef::Struct(it) => it.field(db, name),
|
|
||||||
VariantDef::EnumVariant(it) => it.field(db, name),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub(crate) fn variant_data(self, db: &impl HirDatabase) -> Arc<VariantData> {
|
|
||||||
match self {
|
|
||||||
VariantDef::Struct(it) => it.variant_data(db),
|
|
||||||
VariantDef::EnumVariant(it) => it.variant_data(db),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
|
pub(super) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
|
||||||
let parent_def = field.parent_def(db);
|
let parent_def = field.parent_def(db);
|
||||||
let (generics, module) = match parent_def {
|
let (generics, module) = match parent_def {
|
||||||
@ -732,8 +710,10 @@ pub(super) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
|
|||||||
/// The result of type inference: A mapping from expressions and patterns to types.
|
/// The result of type inference: A mapping from expressions and patterns to types.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
pub struct InferenceResult {
|
pub struct InferenceResult {
|
||||||
/// For each method call expr, record the function it resolved to.
|
/// For each method call expr, records the function it resolves to.
|
||||||
method_resolutions: FxHashMap<ExprId, Function>,
|
method_resolutions: FxHashMap<ExprId, Function>,
|
||||||
|
/// For each field access expr, records the field it resolves to.
|
||||||
|
field_resolutions: FxHashMap<ExprId, StructField>,
|
||||||
type_of_expr: ArenaMap<ExprId, Ty>,
|
type_of_expr: ArenaMap<ExprId, Ty>,
|
||||||
type_of_pat: ArenaMap<PatId, Ty>,
|
type_of_pat: ArenaMap<PatId, Ty>,
|
||||||
}
|
}
|
||||||
@ -742,6 +722,9 @@ impl InferenceResult {
|
|||||||
pub fn method_resolution(&self, expr: ExprId) -> Option<Function> {
|
pub fn method_resolution(&self, expr: ExprId) -> Option<Function> {
|
||||||
self.method_resolutions.get(&expr).map(|it| *it)
|
self.method_resolutions.get(&expr).map(|it| *it)
|
||||||
}
|
}
|
||||||
|
pub fn field_resolution(&self, expr: ExprId) -> Option<StructField> {
|
||||||
|
self.field_resolutions.get(&expr).map(|it| *it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index<ExprId> for InferenceResult {
|
impl Index<ExprId> for InferenceResult {
|
||||||
@ -770,6 +753,7 @@ struct InferenceContext<'a, D: HirDatabase> {
|
|||||||
impl_block: Option<ImplBlock>,
|
impl_block: Option<ImplBlock>,
|
||||||
var_unification_table: InPlaceUnificationTable<TypeVarId>,
|
var_unification_table: InPlaceUnificationTable<TypeVarId>,
|
||||||
method_resolutions: FxHashMap<ExprId, Function>,
|
method_resolutions: FxHashMap<ExprId, Function>,
|
||||||
|
field_resolutions: FxHashMap<ExprId, StructField>,
|
||||||
type_of_expr: ArenaMap<ExprId, Ty>,
|
type_of_expr: ArenaMap<ExprId, Ty>,
|
||||||
type_of_pat: ArenaMap<PatId, Ty>,
|
type_of_pat: ArenaMap<PatId, Ty>,
|
||||||
/// The return type of the function being inferred.
|
/// The return type of the function being inferred.
|
||||||
@ -861,6 +845,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
InferenceContext {
|
InferenceContext {
|
||||||
method_resolutions: FxHashMap::default(),
|
method_resolutions: FxHashMap::default(),
|
||||||
|
field_resolutions: FxHashMap::default(),
|
||||||
type_of_expr: ArenaMap::default(),
|
type_of_expr: ArenaMap::default(),
|
||||||
type_of_pat: ArenaMap::default(),
|
type_of_pat: ArenaMap::default(),
|
||||||
var_unification_table: InPlaceUnificationTable::new(),
|
var_unification_table: InPlaceUnificationTable::new(),
|
||||||
@ -886,6 +871,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
}
|
}
|
||||||
InferenceResult {
|
InferenceResult {
|
||||||
method_resolutions: mem::replace(&mut self.method_resolutions, Default::default()),
|
method_resolutions: mem::replace(&mut self.method_resolutions, Default::default()),
|
||||||
|
field_resolutions: mem::replace(&mut self.field_resolutions, Default::default()),
|
||||||
type_of_expr: expr_types,
|
type_of_expr: expr_types,
|
||||||
type_of_pat: pat_types,
|
type_of_pat: pat_types,
|
||||||
}
|
}
|
||||||
@ -899,6 +885,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
self.method_resolutions.insert(expr, func);
|
self.method_resolutions.insert(expr, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_field_resolution(&mut self, expr: ExprId, field: StructField) {
|
||||||
|
self.field_resolutions.insert(expr, field);
|
||||||
|
}
|
||||||
|
|
||||||
fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {
|
fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {
|
||||||
self.type_of_pat.insert(pat, ty);
|
self.type_of_pat.insert(pat, ty);
|
||||||
}
|
}
|
||||||
@ -1251,9 +1241,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
ty
|
ty
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infer_expr(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
|
fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
|
||||||
let body = Arc::clone(&self.body); // avoid borrow checker problem
|
let body = Arc::clone(&self.body); // avoid borrow checker problem
|
||||||
let ty = match &body[expr] {
|
let ty = match &body[tgt_expr] {
|
||||||
Expr::Missing => Ty::Unknown,
|
Expr::Missing => Ty::Unknown,
|
||||||
Expr::If {
|
Expr::If {
|
||||||
condition,
|
condition,
|
||||||
@ -1344,7 +1334,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
let resolved = receiver_ty.clone().lookup_method(self.db, method_name);
|
let resolved = receiver_ty.clone().lookup_method(self.db, method_name);
|
||||||
let method_ty = match resolved {
|
let method_ty = match resolved {
|
||||||
Some(func) => {
|
Some(func) => {
|
||||||
self.write_method_resolution(expr, func);
|
self.write_method_resolution(tgt_expr, func);
|
||||||
self.db.type_for_def(func.into())
|
self.db.type_for_def(func.into())
|
||||||
}
|
}
|
||||||
None => Ty::Unknown,
|
None => Ty::Unknown,
|
||||||
@ -1389,7 +1379,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
|
|
||||||
expected.ty
|
expected.ty
|
||||||
}
|
}
|
||||||
Expr::Path(p) => self.infer_path_expr(expr, p).unwrap_or(Ty::Unknown),
|
Expr::Path(p) => self.infer_path_expr(tgt_expr, p).unwrap_or(Ty::Unknown),
|
||||||
Expr::Continue => Ty::Never,
|
Expr::Continue => Ty::Never,
|
||||||
Expr::Break { expr } => {
|
Expr::Break { expr } => {
|
||||||
if let Some(expr) = expr {
|
if let Some(expr) = expr {
|
||||||
@ -1436,9 +1426,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
def_id: AdtDef::Struct(s),
|
def_id: AdtDef::Struct(s),
|
||||||
ref substs,
|
ref substs,
|
||||||
..
|
..
|
||||||
} => s
|
} => s.field(self.db, name).map(|field| {
|
||||||
.field(self.db, name)
|
self.write_field_resolution(tgt_expr, field);
|
||||||
.map(|field| field.ty(self.db).subst(substs)),
|
field.ty(self.db).subst(substs)
|
||||||
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.unwrap_or(Ty::Unknown);
|
.unwrap_or(Ty::Unknown);
|
||||||
@ -1545,7 +1536,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
let ty = self.insert_type_vars_shallow(ty);
|
let ty = self.insert_type_vars_shallow(ty);
|
||||||
self.unify(&ty, &expected.ty);
|
self.unify(&ty, &expected.ty);
|
||||||
let ty = self.resolve_ty_as_possible(ty);
|
let ty = self.resolve_ty_as_possible(ty);
|
||||||
self.write_expr_ty(expr, ty.clone());
|
self.write_expr_ty(tgt_expr, ty.clone());
|
||||||
ty
|
ty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ use ra_syntax::{
|
|||||||
AstNode, ast,
|
AstNode, ast,
|
||||||
algo::find_node_at_offset,
|
algo::find_node_at_offset,
|
||||||
};
|
};
|
||||||
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
|
use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
|
||||||
|
|
||||||
@ -60,6 +61,7 @@ pub(crate) fn reference_definition(
|
|||||||
.parent()
|
.parent()
|
||||||
.and_then(ast::MethodCallExpr::cast)
|
.and_then(ast::MethodCallExpr::cast)
|
||||||
{
|
{
|
||||||
|
tested_by!(goto_definition_works_for_methods);
|
||||||
let infer_result = function.infer(db);
|
let infer_result = function.infer(db);
|
||||||
let syntax_mapping = function.body_syntax_mapping(db);
|
let syntax_mapping = function.body_syntax_mapping(db);
|
||||||
let expr = ast::Expr::cast(method_call.syntax()).unwrap();
|
let expr = ast::Expr::cast(method_call.syntax()).unwrap();
|
||||||
@ -70,6 +72,19 @@ pub(crate) fn reference_definition(
|
|||||||
return Exact(NavigationTarget::from_function(db, func));
|
return Exact(NavigationTarget::from_function(db, func));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
// It could also be a field access
|
||||||
|
if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::FieldExpr::cast) {
|
||||||
|
tested_by!(goto_definition_works_for_fields);
|
||||||
|
let infer_result = function.infer(db);
|
||||||
|
let syntax_mapping = function.body_syntax_mapping(db);
|
||||||
|
let expr = ast::Expr::cast(field_expr.syntax()).unwrap();
|
||||||
|
if let Some(field) = syntax_mapping
|
||||||
|
.node_expr(expr)
|
||||||
|
.and_then(|it| infer_result.field_resolution(it))
|
||||||
|
{
|
||||||
|
return Exact(NavigationTarget::from_field(db, field));
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Then try module name resolution
|
// Then try module name resolution
|
||||||
if let Some(module) = hir::source_binder::module_from_child_node(db, file_id, name_ref.syntax())
|
if let Some(module) = hir::source_binder::module_from_child_node(db, file_id, name_ref.syntax())
|
||||||
@ -117,6 +132,8 @@ fn name_definition(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use test_utils::covers;
|
||||||
|
|
||||||
use crate::mock_analysis::analysis_and_position;
|
use crate::mock_analysis::analysis_and_position;
|
||||||
|
|
||||||
fn check_goto(fixuture: &str, expected: &str) {
|
fn check_goto(fixuture: &str, expected: &str) {
|
||||||
@ -183,6 +200,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn goto_definition_works_for_methods() {
|
fn goto_definition_works_for_methods() {
|
||||||
|
covers!(goto_definition_works_for_methods);
|
||||||
check_goto(
|
check_goto(
|
||||||
"
|
"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
@ -197,15 +215,23 @@ mod tests {
|
|||||||
",
|
",
|
||||||
"frobnicate FN_DEF FileId(1) [27; 52) [30; 40)",
|
"frobnicate FN_DEF FileId(1) [27; 52) [30; 40)",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn goto_definition_works_for_fields() {
|
||||||
|
covers!(goto_definition_works_for_fields);
|
||||||
check_goto(
|
check_goto(
|
||||||
"
|
"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
mod <|>foo;
|
struct Foo {
|
||||||
//- /foo/mod.rs
|
spam: u32,
|
||||||
// empty
|
}
|
||||||
|
|
||||||
|
fn bar(foo: &Foo) {
|
||||||
|
foo.spam<|>;
|
||||||
|
}
|
||||||
",
|
",
|
||||||
"foo SOURCE_FILE FileId(2) [0; 10)",
|
"spam NAMED_FIELD_DEF FileId(1) [17; 26) [17; 21)",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1,5 @@
|
|||||||
test_utils::marks!(inserts_parens_for_function_calls);
|
test_utils::marks!(
|
||||||
|
inserts_parens_for_function_calls
|
||||||
|
goto_definition_works_for_methods
|
||||||
|
goto_definition_works_for_fields
|
||||||
|
);
|
||||||
|
@ -3,7 +3,7 @@ use ra_syntax::{
|
|||||||
SyntaxNode, AstNode, SmolStr, TextRange, ast,
|
SyntaxNode, AstNode, SmolStr, TextRange, ast,
|
||||||
SyntaxKind::{self, NAME},
|
SyntaxKind::{self, NAME},
|
||||||
};
|
};
|
||||||
use hir::{ModuleSource};
|
use hir::{ModuleSource, FieldSource};
|
||||||
|
|
||||||
use crate::{FileSymbol, db::RootDatabase};
|
use crate::{FileSymbol, db::RootDatabase};
|
||||||
|
|
||||||
@ -101,6 +101,17 @@ impl NavigationTarget {
|
|||||||
NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
|
NavigationTarget::from_named(file_id.original_file(db), &*fn_def)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
|
||||||
|
let (file_id, field) = field.source(db);
|
||||||
|
let file_id = file_id.original_file(db);
|
||||||
|
match field {
|
||||||
|
FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it),
|
||||||
|
FieldSource::Pos(it) => {
|
||||||
|
NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO once Def::Item is gone, this should be able to always return a NavigationTarget
|
// TODO once Def::Item is gone, this should be able to always return a NavigationTarget
|
||||||
pub(crate) fn from_def(
|
pub(crate) fn from_def(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
|
Loading…
Reference in New Issue
Block a user