Skip to content

Commit 91a8f34

Browse files
committed
Deduplicate lsp locations
1 parent 30b992e commit 91a8f34

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

crates/rust-analyzer/src/handlers/request.rs

+8-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! Protocol. This module specifically handles requests.
33
44
use std::{
5-
collections::HashSet,
65
fs,
76
io::Write as _,
87
path::PathBuf,
@@ -14,10 +13,10 @@ use anyhow::Context;
1413
use ide::{
1514
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange,
1615
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, RangeLimit,
17-
ReferenceCategory, ReferenceSearchResult, Runnable, RunnableKind, SingleResolve, SourceChange,
18-
TextEdit,
16+
ReferenceCategory, Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
1917
};
2018
use ide_db::SymbolKind;
19+
use itertools::Itertools;
2120
use lsp_server::ErrorCode;
2221
use lsp_types::{
2322
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
@@ -30,8 +29,6 @@ use lsp_types::{
3029
};
3130
use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
3231
use serde_json::json;
33-
#[allow(unused_imports)]
34-
use stdx::IsNoneOr;
3532
use stdx::{format_to, never};
3633
use syntax::{algo, ast, AstNode, TextRange, TextSize};
3734
use triomphe::Arc;
@@ -1059,10 +1056,9 @@ pub(crate) fn handle_references(
10591056
let exclude_imports = snap.config.find_all_refs_exclude_imports();
10601057
let exclude_tests = snap.config.find_all_refs_exclude_tests();
10611058

1062-
let Some(mut refs) = snap.analysis.find_all_refs(position, None)? else {
1059+
let Some(refs) = snap.analysis.find_all_refs(position, None)? else {
10631060
return Ok(None);
10641061
};
1065-
deduplicate_declarations(&mut refs);
10661062

10671063
let include_declaration = params.context.include_declaration;
10681064
let locations = refs
@@ -1088,23 +1084,13 @@ pub(crate) fn handle_references(
10881084
})
10891085
.chain(decl)
10901086
})
1087+
.unique()
10911088
.filter_map(|frange| to_proto::location(&snap, frange).ok())
10921089
.collect();
10931090

10941091
Ok(Some(locations))
10951092
}
10961093

1097-
fn deduplicate_declarations(refs: &mut Vec<ReferenceSearchResult>) {
1098-
if refs.iter().filter(|decl| decl.declaration.is_some()).take(2).count() > 1 {
1099-
let mut seen_navigation_targets = HashSet::new();
1100-
refs.retain(|res| {
1101-
res.declaration
1102-
.as_ref()
1103-
.is_none_or(|decl| seen_navigation_targets.insert(decl.nav.clone()))
1104-
});
1105-
}
1106-
}
1107-
11081094
pub(crate) fn handle_formatting(
11091095
snap: GlobalStateSnapshot,
11101096
params: lsp_types::DocumentFormattingParams,
@@ -1809,21 +1795,18 @@ fn show_ref_command_link(
18091795
position: &FilePosition,
18101796
) -> Option<lsp_ext::CommandLinkGroup> {
18111797
if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
1812-
if let Some(mut ref_search_res) =
1813-
snap.analysis.find_all_refs(*position, None).unwrap_or(None)
1814-
{
1815-
deduplicate_declarations(&mut ref_search_res);
1798+
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
18161799
let uri = to_proto::url(snap, position.file_id);
18171800
let line_index = snap.file_line_index(position.file_id).ok()?;
18181801
let position = to_proto::position(&line_index, position.offset);
18191802
let locations: Vec<_> = ref_search_res
18201803
.into_iter()
18211804
.flat_map(|res| res.references)
18221805
.flat_map(|(file_id, ranges)| {
1823-
ranges.into_iter().filter_map(move |(range, _)| {
1824-
to_proto::location(snap, FileRange { file_id, range }).ok()
1825-
})
1806+
ranges.into_iter().map(move |(range, _)| FileRange { file_id, range })
18261807
})
1808+
.unique()
1809+
.filter_map(|range| to_proto::location(snap, range).ok())
18271810
.collect();
18281811
let title = to_proto::reference_title(locations.len());
18291812
let command = to_proto::command::show_references(title, &uri, position, locations);

crates/rust-analyzer/src/lsp/to_proto.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -904,15 +904,16 @@ pub(crate) fn goto_definition_response(
904904
if snap.config.location_link() {
905905
let links = targets
906906
.into_iter()
907+
.unique_by(|nav| (nav.file_id, nav.full_range, nav.focus_range))
907908
.map(|nav| location_link(snap, src, nav))
908909
.collect::<Cancellable<Vec<_>>>()?;
909910
Ok(links.into())
910911
} else {
911912
let locations = targets
912913
.into_iter()
913-
.map(|nav| {
914-
location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
915-
})
914+
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
915+
.unique()
916+
.map(|range| location(snap, range))
916917
.collect::<Cancellable<Vec<_>>>()?;
917918
Ok(locations.into())
918919
}

0 commit comments

Comments
 (0)