Skip to content

Commit 5abc459

Browse files
bors[bot]matklad
andauthored
Merge #3428
3428: Move reference classification to ra_ide_db r=matklad a=matklad Lost some marks along the way :-( bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 074474f + 53cab1c commit 5abc459

File tree

8 files changed

+90
-122
lines changed

8 files changed

+90
-122
lines changed

crates/ra_ide/src/display/navigation_target.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
use either::Either;
44
use hir::{original_range, AssocItem, FieldSource, HasSource, InFile, ModuleSource};
55
use ra_db::{FileId, SourceDatabase};
6-
use ra_ide_db::RootDatabase;
6+
use ra_ide_db::{defs::Definition, RootDatabase};
77
use ra_syntax::{
88
ast::{self, DocCommentsOwner, NameOwner},
99
match_ast, AstNode, SmolStr,
1010
SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
1111
TextRange,
1212
};
1313

14-
use crate::{
15-
// expand::original_range,
16-
references::Definition,
17-
FileSymbol,
18-
};
14+
use crate::FileSymbol;
1915

2016
use super::short_label::ShortLabel;
2117

crates/ra_ide/src/goto_definition.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! FIXME: write short doc here
22
33
use hir::Semantics;
4-
use ra_ide_db::{defs::classify_name, symbol_index, RootDatabase};
4+
use ra_ide_db::{
5+
defs::{classify_name, classify_name_ref},
6+
symbol_index, RootDatabase,
7+
};
58
use ra_syntax::{
69
ast::{self},
710
match_ast, AstNode,
@@ -11,7 +14,6 @@ use ra_syntax::{
1114

1215
use crate::{
1316
display::{ToNav, TryToNav},
14-
references::classify_name_ref,
1517
FilePosition, NavigationTarget, RangeInfo,
1618
};
1719

@@ -94,7 +96,7 @@ pub(crate) fn reference_definition(
9496

9597
#[cfg(test)]
9698
mod tests {
97-
use test_utils::{assert_eq_text, covers};
99+
use test_utils::assert_eq_text;
98100

99101
use crate::mock_analysis::analysis_and_position;
100102

@@ -206,7 +208,6 @@ mod tests {
206208

207209
#[test]
208210
fn goto_def_for_macros() {
209-
covers!(goto_def_for_macros);
210211
check_goto(
211212
"
212213
//- /lib.rs
@@ -223,7 +224,6 @@ mod tests {
223224

224225
#[test]
225226
fn goto_def_for_macros_from_other_crates() {
226-
covers!(goto_def_for_macros);
227227
check_goto(
228228
"
229229
//- /lib.rs
@@ -335,7 +335,6 @@ mod tests {
335335

336336
#[test]
337337
fn goto_def_for_methods() {
338-
covers!(goto_def_for_methods);
339338
check_goto(
340339
"
341340
//- /lib.rs
@@ -355,7 +354,6 @@ mod tests {
355354

356355
#[test]
357356
fn goto_def_for_fields() {
358-
covers!(goto_def_for_fields);
359357
check_goto(
360358
"
361359
//- /lib.rs
@@ -374,7 +372,6 @@ mod tests {
374372

375373
#[test]
376374
fn goto_def_for_record_fields() {
377-
covers!(goto_def_for_record_fields);
378375
check_goto(
379376
"
380377
//- /lib.rs
@@ -787,7 +784,6 @@ mod tests {
787784

788785
#[test]
789786
fn goto_def_for_field_init_shorthand() {
790-
covers!(goto_def_for_field_init_shorthand);
791787
check_goto(
792788
"
793789
//- /lib.rs

crates/ra_ide/src/hover.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use hir::{Adt, HasSource, HirDisplay, Semantics};
44
use ra_ide_db::{
5-
defs::{classify_name, Definition},
5+
defs::{classify_name, classify_name_ref, Definition},
66
RootDatabase,
77
};
88
use ra_syntax::{
@@ -14,7 +14,6 @@ use ra_syntax::{
1414

1515
use crate::{
1616
display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
17-
references::classify_name_ref,
1817
FilePosition, RangeInfo,
1918
};
2019

crates/ra_ide/src/marks.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
test_utils::marks!(
44
inserts_angle_brackets_for_generics
55
inserts_parens_for_function_calls
6-
goto_def_for_macros
7-
goto_def_for_methods
8-
goto_def_for_fields
9-
goto_def_for_record_fields
10-
goto_def_for_field_init_shorthand
116
call_info_bad_offset
127
dont_complete_current_use
138
test_resolve_parent_module_on_module_decl

crates/ra_ide/src/references.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
//! at the index that the match starts at and its tree parent is
1010
//! resolved to the search element definition, we get a reference.
1111
12-
mod classify;
1312
mod rename;
1413
mod search_scope;
1514

1615
use hir::Semantics;
1716
use once_cell::unsync::Lazy;
1817
use ra_db::SourceDatabaseExt;
19-
use ra_ide_db::RootDatabase;
18+
use ra_ide_db::{
19+
defs::{classify_name, classify_name_ref, Definition},
20+
RootDatabase,
21+
};
2022
use ra_prof::profile;
2123
use ra_syntax::{
2224
algo::find_node_at_offset,
@@ -27,11 +29,7 @@ use test_utils::tested_by;
2729

2830
use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo};
2931

30-
pub(crate) use self::{
31-
classify::{classify_name_ref, NameRefClass},
32-
rename::rename,
33-
};
34-
pub(crate) use ra_ide_db::defs::{classify_name, Definition};
32+
pub(crate) use self::rename::rename;
3533

3634
pub use self::search_scope::SearchScope;
3735

crates/ra_ide/src/references/classify.rs

Lines changed: 0 additions & 84 deletions
This file was deleted.

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod tests;
77

88
use hir::{Name, Semantics};
99
use ra_ide_db::{
10-
defs::{classify_name, Definition, NameClass},
10+
defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass},
1111
RootDatabase,
1212
};
1313
use ra_prof::profile;
@@ -19,11 +19,7 @@ use ra_syntax::{
1919
};
2020
use rustc_hash::FxHashMap;
2121

22-
use crate::{
23-
call_info::call_info_for_token,
24-
references::{classify_name_ref, NameRefClass},
25-
Analysis, FileId,
26-
};
22+
use crate::{call_info::call_info_for_token, Analysis, FileId};
2723

2824
pub(crate) use html::highlight_as_html;
2925
pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};

crates/ra_ide_db/src/defs.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
77

88
use hir::{
9-
Adt, FieldSource, HasSource, ImplDef, Local, MacroDef, Module, ModuleDef, Name, Semantics,
10-
StructField, TypeParam,
9+
Adt, FieldSource, HasSource, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
10+
Semantics, StructField, TypeParam,
1111
};
1212
use ra_prof::profile;
1313
use ra_syntax::{
@@ -117,6 +117,8 @@ impl NameClass {
117117
}
118118

119119
pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> {
120+
let _p = profile("classify_name");
121+
120122
if let Some(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) {
121123
if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
122124
return Some(NameClass::ConstReference(Definition::ModuleDef(def)));
@@ -127,7 +129,6 @@ pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option
127129
}
128130

129131
fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<Definition> {
130-
let _p = profile("classify_name");
131132
let parent = name.syntax().parent()?;
132133

133134
match_ast! {
@@ -192,3 +193,74 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
192193
}
193194
}
194195
}
196+
197+
pub enum NameRefClass {
198+
Definition(Definition),
199+
FieldShorthand { local: Local, field: Definition },
200+
}
201+
202+
impl NameRefClass {
203+
pub fn definition(self) -> Definition {
204+
match self {
205+
NameRefClass::Definition(def) => def,
206+
NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local),
207+
}
208+
}
209+
}
210+
211+
pub fn classify_name_ref(
212+
sema: &Semantics<RootDatabase>,
213+
name_ref: &ast::NameRef,
214+
) -> Option<NameRefClass> {
215+
let _p = profile("classify_name_ref");
216+
217+
let parent = name_ref.syntax().parent()?;
218+
219+
if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
220+
if let Some(func) = sema.resolve_method_call(&method_call) {
221+
return Some(NameRefClass::Definition(Definition::ModuleDef(func.into())));
222+
}
223+
}
224+
225+
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
226+
if let Some(field) = sema.resolve_field(&field_expr) {
227+
return Some(NameRefClass::Definition(Definition::StructField(field)));
228+
}
229+
}
230+
231+
if let Some(record_field) = ast::RecordField::cast(parent.clone()) {
232+
if let Some((field, local)) = sema.resolve_record_field(&record_field) {
233+
let field = Definition::StructField(field);
234+
let res = match local {
235+
None => NameRefClass::Definition(field),
236+
Some(local) => NameRefClass::FieldShorthand { field, local },
237+
};
238+
return Some(res);
239+
}
240+
}
241+
242+
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
243+
if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
244+
return Some(NameRefClass::Definition(Definition::Macro(macro_def)));
245+
}
246+
}
247+
248+
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
249+
let resolved = sema.resolve_path(&path)?;
250+
let res = match resolved {
251+
PathResolution::Def(def) => Definition::ModuleDef(def),
252+
PathResolution::AssocItem(item) => {
253+
let def = match item {
254+
hir::AssocItem::Function(it) => it.into(),
255+
hir::AssocItem::Const(it) => it.into(),
256+
hir::AssocItem::TypeAlias(it) => it.into(),
257+
};
258+
Definition::ModuleDef(def)
259+
}
260+
PathResolution::Local(local) => Definition::Local(local),
261+
PathResolution::TypeParam(par) => Definition::TypeParam(par),
262+
PathResolution::Macro(def) => Definition::Macro(def),
263+
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
264+
};
265+
Some(NameRefClass::Definition(res))
266+
}

0 commit comments

Comments
 (0)