Skip to content

Commit

Permalink
Deduplicate lsp locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Feb 19, 2024
1 parent 181c029 commit daaa791
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
33 changes: 8 additions & 25 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! Protocol. This module specifically handles requests.
use std::{
collections::HashSet,
fs,
io::Write as _,
path::PathBuf,
Expand All @@ -14,10 +13,10 @@ use anyhow::Context;
use ide::{
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange,
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, RangeLimit,
ReferenceCategory, ReferenceSearchResult, Runnable, RunnableKind, SingleResolve, SourceChange,
TextEdit,
ReferenceCategory, Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
};
use ide_db::SymbolKind;
use itertools::Itertools;
use lsp_server::ErrorCode;
use lsp_types::{
CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem,
Expand All @@ -30,8 +29,6 @@ use lsp_types::{
};
use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
use serde_json::json;
#[allow(unused_imports)]
use stdx::IsNoneOr;
use stdx::{format_to, never};
use syntax::{algo, ast, AstNode, TextRange, TextSize};
use triomphe::Arc;
Expand Down Expand Up @@ -1059,10 +1056,9 @@ pub(crate) fn handle_references(
let exclude_imports = snap.config.find_all_refs_exclude_imports();
let exclude_tests = snap.config.find_all_refs_exclude_tests();

let Some(mut refs) = snap.analysis.find_all_refs(position, None)? else {
let Some(refs) = snap.analysis.find_all_refs(position, None)? else {
return Ok(None);
};
deduplicate_declarations(&mut refs);

let include_declaration = params.context.include_declaration;
let locations = refs
Expand All @@ -1088,23 +1084,13 @@ pub(crate) fn handle_references(
})
.chain(decl)
})
.unique()
.filter_map(|frange| to_proto::location(&snap, frange).ok())
.collect();

Ok(Some(locations))
}

fn deduplicate_declarations(refs: &mut Vec<ReferenceSearchResult>) {
if refs.iter().filter(|decl| decl.declaration.is_some()).take(2).count() > 1 {
let mut seen_navigation_targets = HashSet::new();
refs.retain(|res| {
res.declaration
.as_ref()
.is_none_or(|decl| seen_navigation_targets.insert(decl.nav.clone()))
});
}
}

pub(crate) fn handle_formatting(
snap: GlobalStateSnapshot,
params: lsp_types::DocumentFormattingParams,
Expand Down Expand Up @@ -1809,21 +1795,18 @@ fn show_ref_command_link(
position: &FilePosition,
) -> Option<lsp_ext::CommandLinkGroup> {
if snap.config.hover_actions().references && snap.config.client_commands().show_reference {
if let Some(mut ref_search_res) =
snap.analysis.find_all_refs(*position, None).unwrap_or(None)
{
deduplicate_declarations(&mut ref_search_res);
if let Some(ref_search_res) = snap.analysis.find_all_refs(*position, None).unwrap_or(None) {
let uri = to_proto::url(snap, position.file_id);
let line_index = snap.file_line_index(position.file_id).ok()?;
let position = to_proto::position(&line_index, position.offset);
let locations: Vec<_> = ref_search_res
.into_iter()
.flat_map(|res| res.references)
.flat_map(|(file_id, ranges)| {
ranges.into_iter().filter_map(move |(range, _)| {
to_proto::location(snap, FileRange { file_id, range }).ok()
})
ranges.into_iter().map(move |(range, _)| FileRange { file_id, range })
})
.unique()
.filter_map(|range| to_proto::location(snap, range).ok())
.collect();
let title = to_proto::reference_title(locations.len());
let command = to_proto::command::show_references(title, &uri, position, locations);
Expand Down
7 changes: 4 additions & 3 deletions crates/rust-analyzer/src/lsp/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,15 +904,16 @@ pub(crate) fn goto_definition_response(
if snap.config.location_link() {
let links = targets
.into_iter()
.unique_by(|nav| (nav.file_id, nav.full_range, nav.focus_range))
.map(|nav| location_link(snap, src, nav))
.collect::<Cancellable<Vec<_>>>()?;
Ok(links.into())
} else {
let locations = targets
.into_iter()
.map(|nav| {
location(snap, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
})
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
.unique()
.map(|range| location(snap, range))
.collect::<Cancellable<Vec<_>>>()?;
Ok(locations.into())
}
Expand Down

0 comments on commit daaa791

Please sign in to comment.