Skip to content

Commit cadb01c

Browse files
committed
Move reference imports filtering into to_proto layer
1 parent f64c956 commit cadb01c

File tree

7 files changed

+45
-50
lines changed

7 files changed

+45
-50
lines changed

crates/ide-db/src/search.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{mem, sync::Arc};
99
use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt};
1010
use hir::{DefWithBody, HasAttrs, HasSource, InFile, ModuleSource, Semantics, Visibility};
1111
use once_cell::unsync::Lazy;
12+
use parser::SyntaxKind;
1213
use stdx::hash::NoHashHashMap;
1314
use syntax::{ast, match_ast, AstNode, TextRange, TextSize};
1415

@@ -67,6 +68,7 @@ pub enum ReferenceCategory {
6768
// Create
6869
Write,
6970
Read,
71+
Import,
7072
// FIXME: Some day should be able to search in doc comments. Would probably
7173
// need to switch from enum to bitflags then?
7274
// DocComment
@@ -577,7 +579,7 @@ impl<'a> FindUsages<'a> {
577579
let reference = FileReference {
578580
range,
579581
name: ast::NameLike::NameRef(name_ref.clone()),
580-
category: None,
582+
category: is_name_ref_in_import(name_ref).then(|| ReferenceCategory::Import),
581583
};
582584
sink(file_id, reference)
583585
}
@@ -756,7 +758,7 @@ impl ReferenceCategory {
756758
fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> {
757759
// Only Locals and Fields have accesses for now.
758760
if !matches!(def, Definition::Local(_) | Definition::Field(_)) {
759-
return None;
761+
return is_name_ref_in_import(r).then(|| ReferenceCategory::Import);
760762
}
761763

762764
let mode = r.syntax().ancestors().find_map(|node| {
@@ -783,3 +785,12 @@ impl ReferenceCategory {
783785
mode.or(Some(ReferenceCategory::Read))
784786
}
785787
}
788+
789+
fn is_name_ref_in_import(name_ref: &ast::NameRef) -> bool {
790+
name_ref
791+
.syntax()
792+
.parent()
793+
.and_then(ast::PathSegment::cast)
794+
.and_then(|it| it.parent_path().top_path().syntax().parent())
795+
.map_or(false, |it| it.kind() == SyntaxKind::USE_TREE)
796+
}

crates/ide/src/annotations.rs

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation)
158158
&Semantics::new(db),
159159
FilePosition { file_id, offset: annotation.range.start() },
160160
None,
161-
false,
162161
)
163162
.map(|result| {
164163
result

crates/ide/src/highlight_related.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ mod tests {
377377
match it {
378378
ReferenceCategory::Read => "read",
379379
ReferenceCategory::Write => "write",
380+
ReferenceCategory::Import => "import",
380381
}
381382
.to_string()
382383
}),
@@ -423,20 +424,20 @@ struct Foo;
423424
check(
424425
r#"
425426
use crate$0;
426-
//^^^^^
427+
//^^^^^ import
427428
use self;
428-
//^^^^
429+
//^^^^ import
429430
mod __ {
430431
use super;
431-
//^^^^^
432+
//^^^^^ import
432433
}
433434
"#,
434435
);
435436
check(
436437
r#"
437438
//- /main.rs crate:main deps:lib
438439
use lib$0;
439-
//^^^
440+
//^^^ import
440441
//- /lib.rs crate:lib
441442
"#,
442443
);
@@ -450,7 +451,7 @@ use lib$0;
450451
mod foo;
451452
//- /foo.rs
452453
use self$0;
453-
// ^^^^
454+
// ^^^^ import
454455
"#,
455456
);
456457
}

crates/ide/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,8 @@ impl Analysis {
425425
&self,
426426
position: FilePosition,
427427
search_scope: Option<SearchScope>,
428-
exclude_imports: bool,
429428
) -> Cancellable<Option<Vec<ReferenceSearchResult>>> {
430-
self.with_db(|db| {
431-
references::find_all_refs(&Semantics::new(db), position, search_scope, exclude_imports)
432-
})
429+
self.with_db(|db| references::find_all_refs(&Semantics::new(db), position, search_scope))
433430
}
434431

435432
/// Finds all methods and free functions for the file. Does not return tests!

crates/ide/src/references.rs

+11-27
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub(crate) fn find_all_refs(
5454
sema: &Semantics<'_, RootDatabase>,
5555
position: FilePosition,
5656
search_scope: Option<SearchScope>,
57-
exclude_imports: bool,
5857
) -> Option<Vec<ReferenceSearchResult>> {
5958
let _p = profile::span("find_all_refs");
6059
let syntax = sema.parse(position.file_id).syntax().clone();
@@ -80,10 +79,6 @@ pub(crate) fn find_all_refs(
8079
retain_adt_literal_usages(&mut usages, def, sema);
8180
}
8281

83-
if exclude_imports {
84-
filter_import_references(&mut usages);
85-
}
86-
8782
let references = usages
8883
.into_iter()
8984
.map(|(file_id, refs)| {
@@ -117,17 +112,6 @@ pub(crate) fn find_all_refs(
117112
}
118113
}
119114

120-
fn filter_import_references(usages: &mut UsageSearchResult) {
121-
for (_file_id, refs) in &mut usages.references {
122-
refs.retain(|it| match it.name.as_name_ref() {
123-
Some(name_ref) => {
124-
!name_ref.syntax().ancestors().any(|it_ref| matches!(it_ref.kind(), USE))
125-
}
126-
None => true,
127-
});
128-
}
129-
}
130-
131115
pub(crate) fn find_defs<'a>(
132116
sema: &'a Semantics<'_, RootDatabase>,
133117
syntax: &SyntaxNode,
@@ -758,7 +742,7 @@ pub struct Foo {
758742
expect![[r#"
759743
foo Module FileId(0) 0..8 4..7
760744
761-
FileId(0) 14..17
745+
FileId(0) 14..17 Import
762746
"#]],
763747
);
764748
}
@@ -776,7 +760,7 @@ use self$0;
776760
expect![[r#"
777761
foo Module FileId(0) 0..8 4..7
778762
779-
FileId(1) 4..8
763+
FileId(1) 4..8 Import
780764
"#]],
781765
);
782766
}
@@ -791,7 +775,7 @@ use self$0;
791775
expect![[r#"
792776
Module FileId(0) 0..10
793777
794-
FileId(0) 4..8
778+
FileId(0) 4..8 Import
795779
"#]],
796780
);
797781
}
@@ -819,7 +803,7 @@ pub(super) struct Foo$0 {
819803
expect![[r#"
820804
Foo Struct FileId(2) 0..41 18..21
821805
822-
FileId(1) 20..23
806+
FileId(1) 20..23 Import
823807
FileId(1) 47..50
824808
"#]],
825809
);
@@ -982,7 +966,7 @@ fn g() { f(); }
982966
expect![[r#"
983967
f Function FileId(0) 22..31 25..26
984968
985-
FileId(1) 11..12
969+
FileId(1) 11..12 Import
986970
FileId(1) 24..25
987971
"#]],
988972
);
@@ -1110,7 +1094,7 @@ impl Foo {
11101094

11111095
fn check_with_scope(ra_fixture: &str, search_scope: Option<SearchScope>, expect: Expect) {
11121096
let (analysis, pos) = fixture::position(ra_fixture);
1113-
let refs = analysis.find_all_refs(pos, search_scope, false).unwrap().unwrap();
1097+
let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
11141098

11151099
let mut actual = String::new();
11161100
for refs in refs {
@@ -1440,9 +1424,9 @@ pub use level1::Foo;
14401424
expect![[r#"
14411425
Foo Struct FileId(0) 0..15 11..14
14421426
1443-
FileId(1) 16..19
1444-
FileId(2) 16..19
1445-
FileId(3) 16..19
1427+
FileId(1) 16..19 Import
1428+
FileId(2) 16..19 Import
1429+
FileId(3) 16..19 Import
14461430
"#]],
14471431
);
14481432
}
@@ -1470,7 +1454,7 @@ lib::foo!();
14701454
expect![[r#"
14711455
foo Macro FileId(1) 0..61 29..32
14721456
1473-
FileId(0) 46..49
1457+
FileId(0) 46..49 Import
14741458
FileId(2) 0..3
14751459
FileId(3) 5..8
14761460
"#]],
@@ -1633,7 +1617,7 @@ struct Foo;
16331617
expect![[r#"
16341618
derive_identity Derive FileId(2) 1..107 45..60
16351619
1636-
FileId(0) 17..31
1620+
FileId(0) 17..31 Import
16371621
FileId(0) 56..70
16381622
"#]],
16391623
);

crates/rust-analyzer/src/handlers.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{
1010
use anyhow::Context;
1111
use ide::{
1212
AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange,
13-
HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve,
14-
SourceChange, TextEdit,
13+
HoverAction, HoverGotoTypeData, Query, RangeInfo, ReferenceCategory, Runnable, RunnableKind,
14+
SingleResolve, SourceChange, TextEdit,
1515
};
1616
use ide_db::SymbolKind;
1717
use lsp_server::ErrorCode;
@@ -1014,7 +1014,7 @@ pub(crate) fn handle_references(
10141014

10151015
let exclude_imports = snap.config.find_all_refs_exclude_imports();
10161016

1017-
let refs = match snap.analysis.find_all_refs(position, None, exclude_imports)? {
1017+
let refs = match snap.analysis.find_all_refs(position, None)? {
10181018
None => return Ok(None),
10191019
Some(refs) => refs,
10201020
};
@@ -1034,7 +1034,11 @@ pub(crate) fn handle_references(
10341034
refs.references
10351035
.into_iter()
10361036
.flat_map(|(file_id, refs)| {
1037-
refs.into_iter().map(move |(range, _)| FileRange { file_id, range })
1037+
refs.into_iter()
1038+
.filter(|&(_, category)| {
1039+
!exclude_imports || category != Some(ReferenceCategory::Import)
1040+
})
1041+
.map(move |(range, _)| FileRange { file_id, range })
10381042
})
10391043
.chain(decl)
10401044
})
@@ -1285,7 +1289,7 @@ pub(crate) fn handle_document_highlight(
12851289
.into_iter()
12861290
.map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight {
12871291
range: to_proto::range(&line_index, range),
1288-
kind: category.map(to_proto::document_highlight_kind),
1292+
kind: category.and_then(to_proto::document_highlight_kind),
12891293
})
12901294
.collect();
12911295
Ok(Some(res))
@@ -1654,9 +1658,7 @@ fn show_ref_command_link(
16541658
position: &FilePosition,
16551659
) -> Option<lsp_ext::CommandLinkGroup> {
16561660
if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1657-
if let Some(ref_search_res) =
1658-
snap.analysis.find_all_refs(*position, None, false).unwrap_or(None)
1659-
{
1661+
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
16601662
let uri = to_proto::url(snap, position.file_id);
16611663
let line_index = snap.file_line_index(position.file_id).ok()?;
16621664
let position = to_proto::position(&line_index, position.offset);

crates/rust-analyzer/src/to_proto.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolK
8383

8484
pub(crate) fn document_highlight_kind(
8585
category: ReferenceCategory,
86-
) -> lsp_types::DocumentHighlightKind {
86+
) -> Option<lsp_types::DocumentHighlightKind> {
8787
match category {
88-
ReferenceCategory::Read => lsp_types::DocumentHighlightKind::READ,
89-
ReferenceCategory::Write => lsp_types::DocumentHighlightKind::WRITE,
88+
ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ),
89+
ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE),
90+
ReferenceCategory::Import => None,
9091
}
9192
}
9293

0 commit comments

Comments
 (0)