mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
Auto merge of #16163 - Veykril:goto-impl-fix, r=Veykril
fix: Deduplicate annotations Fixes https://github.com/rust-lang/rust-analyzer/issues/16157
This commit is contained in:
commit
7204ee1262
7 changed files with 179 additions and 182 deletions
|
@ -3,8 +3,9 @@ use ide_db::{
|
|||
base_db::{FileId, FilePosition, FileRange},
|
||||
defs::Definition,
|
||||
helpers::visit_file_defs,
|
||||
RootDatabase,
|
||||
FxHashSet, RootDatabase,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use syntax::{ast::HasName, AstNode, TextRange};
|
||||
|
||||
use crate::{
|
||||
|
@ -23,13 +24,13 @@ mod fn_references;
|
|||
// and running/debugging binaries.
|
||||
//
|
||||
// image::https://user-images.githubusercontent.com/48062697/113020672-b7c34f00-917a-11eb-8f6e-858735660a0e.png[]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||
pub struct Annotation {
|
||||
pub range: TextRange,
|
||||
pub kind: AnnotationKind,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Hash, PartialEq, Eq)]
|
||||
pub enum AnnotationKind {
|
||||
Runnable(Runnable),
|
||||
HasImpls { pos: FilePosition, data: Option<Vec<NavigationTarget>> },
|
||||
|
@ -56,7 +57,7 @@ pub(crate) fn annotations(
|
|||
config: &AnnotationConfig,
|
||||
file_id: FileId,
|
||||
) -> Vec<Annotation> {
|
||||
let mut annotations = Vec::default();
|
||||
let mut annotations = FxHashSet::default();
|
||||
|
||||
if config.annotate_runnables {
|
||||
for runnable in runnables(db, file_id) {
|
||||
|
@ -66,7 +67,7 @@ pub(crate) fn annotations(
|
|||
|
||||
let range = runnable.nav.focus_or_full_range();
|
||||
|
||||
annotations.push(Annotation { range, kind: AnnotationKind::Runnable(runnable) });
|
||||
annotations.insert(Annotation { range, kind: AnnotationKind::Runnable(runnable) });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,13 +100,13 @@ pub(crate) fn annotations(
|
|||
})
|
||||
.for_each(|range| {
|
||||
let (annotation_range, target_position) = mk_ranges(range);
|
||||
annotations.push(Annotation {
|
||||
annotations.insert(Annotation {
|
||||
range: annotation_range,
|
||||
kind: AnnotationKind::HasReferences {
|
||||
pos: target_position,
|
||||
data: None,
|
||||
},
|
||||
})
|
||||
});
|
||||
})
|
||||
}
|
||||
if config.annotate_references || config.annotate_impls {
|
||||
|
@ -131,14 +132,14 @@ pub(crate) fn annotations(
|
|||
};
|
||||
let (annotation_range, target_pos) = mk_ranges(range);
|
||||
if config.annotate_impls && !matches!(def, Definition::Const(_)) {
|
||||
annotations.push(Annotation {
|
||||
annotations.insert(Annotation {
|
||||
range: annotation_range,
|
||||
kind: AnnotationKind::HasImpls { pos: target_pos, data: None },
|
||||
});
|
||||
}
|
||||
|
||||
if config.annotate_references {
|
||||
annotations.push(Annotation {
|
||||
annotations.insert(Annotation {
|
||||
range: annotation_range,
|
||||
kind: AnnotationKind::HasReferences { pos: target_pos, data: None },
|
||||
});
|
||||
|
@ -149,7 +150,7 @@ pub(crate) fn annotations(
|
|||
node: InFile<T>,
|
||||
source_file_id: FileId,
|
||||
) -> Option<(TextRange, Option<TextRange>)> {
|
||||
if let Some(InRealFile { file_id, value }) = node.original_ast_node(db) {
|
||||
if let Some(InRealFile { file_id, value }) = node.original_ast_node_rooted(db) {
|
||||
if file_id == source_file_id {
|
||||
return Some((
|
||||
value.syntax().text_range(),
|
||||
|
@ -171,7 +172,7 @@ pub(crate) fn annotations(
|
|||
}));
|
||||
}
|
||||
|
||||
annotations
|
||||
annotations.into_iter().sorted_by_key(|a| (a.range.start(), a.range.end())).collect()
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation) -> Annotation {
|
||||
|
@ -252,25 +253,6 @@ fn main() {
|
|||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
Annotation {
|
||||
range: 53..57,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 50..85,
|
||||
focus_range: 53..57,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 6..10,
|
||||
kind: HasReferences {
|
||||
|
@ -306,6 +288,25 @@ fn main() {
|
|||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 53..57,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 50..85,
|
||||
focus_range: 53..57,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 53..57,
|
||||
kind: HasReferences {
|
||||
|
@ -337,39 +338,6 @@ fn main() {
|
|||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
Annotation {
|
||||
range: 17..21,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 14..48,
|
||||
focus_range: 17..21,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 7,
|
||||
},
|
||||
data: Some(
|
||||
[],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasReferences {
|
||||
|
@ -391,6 +359,39 @@ fn main() {
|
|||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 7,
|
||||
},
|
||||
data: Some(
|
||||
[],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 17..21,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 14..48,
|
||||
focus_range: 17..21,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 17..21,
|
||||
kind: HasReferences {
|
||||
|
@ -426,49 +427,6 @@ fn main() {
|
|||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
Annotation {
|
||||
range: 69..73,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 66..100,
|
||||
focus_range: 69..73,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 7,
|
||||
},
|
||||
data: Some(
|
||||
[
|
||||
NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 36..64,
|
||||
focus_range: 57..61,
|
||||
name: "impl",
|
||||
kind: Impl,
|
||||
},
|
||||
],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasReferences {
|
||||
|
@ -496,6 +454,30 @@ fn main() {
|
|||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 7,
|
||||
},
|
||||
data: Some(
|
||||
[
|
||||
NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 36..64,
|
||||
focus_range: 57..61,
|
||||
name: "impl",
|
||||
kind: Impl,
|
||||
},
|
||||
],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 20..31,
|
||||
kind: HasImpls {
|
||||
|
@ -555,6 +537,25 @@ fn main() {
|
|||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 69..73,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 66..100,
|
||||
focus_range: 69..73,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
]
|
||||
"#]],
|
||||
);
|
||||
|
@ -622,49 +623,6 @@ fn main() {
|
|||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
Annotation {
|
||||
range: 61..65,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 58..95,
|
||||
focus_range: 61..65,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 7,
|
||||
},
|
||||
data: Some(
|
||||
[
|
||||
NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 14..56,
|
||||
focus_range: 19..23,
|
||||
name: "impl",
|
||||
kind: Impl,
|
||||
},
|
||||
],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasReferences {
|
||||
|
@ -692,6 +650,30 @@ fn main() {
|
|||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 7..11,
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 7,
|
||||
},
|
||||
data: Some(
|
||||
[
|
||||
NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 14..56,
|
||||
focus_range: 19..23,
|
||||
name: "impl",
|
||||
kind: Impl,
|
||||
},
|
||||
],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 33..44,
|
||||
kind: HasReferences {
|
||||
|
@ -727,6 +709,25 @@ fn main() {
|
|||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 61..65,
|
||||
kind: Runnable(
|
||||
Runnable {
|
||||
use_name_in_title: false,
|
||||
nav: NavigationTarget {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
full_range: 58..95,
|
||||
focus_range: 61..65,
|
||||
name: "main",
|
||||
kind: Function,
|
||||
},
|
||||
kind: Bin,
|
||||
cfg: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
]
|
||||
"#]],
|
||||
);
|
||||
|
@ -745,6 +746,20 @@ mod tests {
|
|||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
Annotation {
|
||||
range: 3..7,
|
||||
kind: HasReferences {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 3,
|
||||
},
|
||||
data: Some(
|
||||
[],
|
||||
),
|
||||
},
|
||||
},
|
||||
Annotation {
|
||||
range: 3..7,
|
||||
kind: Runnable(
|
||||
|
@ -812,20 +827,6 @@ mod tests {
|
|||
},
|
||||
),
|
||||
},
|
||||
Annotation {
|
||||
range: 3..7,
|
||||
kind: HasReferences {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
),
|
||||
offset: 3,
|
||||
},
|
||||
data: Some(
|
||||
[],
|
||||
),
|
||||
},
|
||||
},
|
||||
]
|
||||
"#]],
|
||||
);
|
||||
|
@ -877,7 +878,7 @@ struct Foo;
|
|||
[
|
||||
Annotation {
|
||||
range: 0..71,
|
||||
kind: HasImpls {
|
||||
kind: HasReferences {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
|
@ -891,7 +892,7 @@ struct Foo;
|
|||
},
|
||||
Annotation {
|
||||
range: 0..71,
|
||||
kind: HasReferences {
|
||||
kind: HasImpls {
|
||||
pos: FilePosition {
|
||||
file_id: FileId(
|
||||
0,
|
||||
|
|
|
@ -4,7 +4,6 @@ use ide_db::{
|
|||
helpers::pick_best_token,
|
||||
RootDatabase,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use syntax::{ast, AstNode, SyntaxKind::*, T};
|
||||
|
||||
use crate::{FilePosition, NavigationTarget, RangeInfo, TryToNav};
|
||||
|
@ -34,10 +33,10 @@ pub(crate) fn goto_implementation(
|
|||
})?;
|
||||
let range = original_token.text_range();
|
||||
let navs =
|
||||
sema.descend_into_macros(DescendPreference::None, original_token)
|
||||
.into_iter()
|
||||
.filter_map(|token| token.parent().and_then(ast::NameLike::cast))
|
||||
.filter_map(|node| match &node {
|
||||
sema.descend_into_macros_single(DescendPreference::SameText, original_token)
|
||||
.parent()
|
||||
.and_then(ast::NameLike::cast)
|
||||
.and_then(|node| match &node {
|
||||
ast::NameLike::Name(name) => {
|
||||
NameClass::classify(&sema, name).and_then(|class| match class {
|
||||
NameClass::Definition(it) | NameClass::ConstReference(it) => Some(it),
|
||||
|
@ -52,8 +51,7 @@ pub(crate) fn goto_implementation(
|
|||
}),
|
||||
ast::NameLike::Lifetime(_) => None,
|
||||
})
|
||||
.unique()
|
||||
.filter_map(|def| {
|
||||
.and_then(|def| {
|
||||
let navs = match def {
|
||||
Definition::Trait(trait_) => impls_for_trait(&sema, trait_),
|
||||
Definition::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
|
||||
|
@ -75,8 +73,7 @@ pub(crate) fn goto_implementation(
|
|||
};
|
||||
Some(navs)
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
.unwrap_or_default();
|
||||
|
||||
Some(RangeInfo { range, info: navs })
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue