From c5f49cf07194185a02a5dd353d3125775fe5ed82 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 16 Feb 2025 13:11:50 +0100 Subject: [PATCH] fix: Stabilize sort order of `related_tests` --- .../rust-analyzer/crates/ide/src/runnables.rs | 59 ++++++++++--------- .../ide/src/syntax_highlighting/tests.rs | 3 + 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide/src/runnables.rs b/src/tools/rust-analyzer/crates/ide/src/runnables.rs index 16dd039eabb..9e3b70fa8ea 100644 --- a/src/tools/rust-analyzer/crates/ide/src/runnables.rs +++ b/src/tools/rust-analyzer/crates/ide/src/runnables.rs @@ -4,8 +4,8 @@ use arrayvec::ArrayVec; use ast::HasName; use cfg::{CfgAtom, CfgExpr}; use hir::{ - db::HirDatabase, sym, AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, HasSource, HirFileIdExt, - ModPath, Name, PathKind, Semantics, Symbol, + db::HirDatabase, sym, symbols::FxIndexSet, AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, + HasSource, HirFileIdExt, ModPath, Name, PathKind, Semantics, Symbol, }; use ide_assists::utils::{has_test_related_attribute, test_related_attribute_syn}; use ide_db::{ @@ -13,7 +13,7 @@ use ide_db::{ documentation::docs_from_attrs, helpers::visit_file_defs, search::{FileReferenceNode, SearchScope}, - FilePosition, FxHashMap, FxHashSet, FxIndexMap, RootDatabase, SymbolKind, + FilePosition, FxHashMap, FxIndexMap, RootDatabase, SymbolKind, }; use itertools::Itertools; use smallvec::SmallVec; @@ -182,20 +182,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec { r }) })); - res.sort_by(|Runnable { nav, kind, .. }, Runnable { nav: nav_b, kind: kind_b, .. }| { - // full_range.start < focus_range.start < name, should give us a decent unique ordering - nav.full_range - .start() - .cmp(&nav_b.full_range.start()) - .then_with(|| { - let t_0 = || TextSize::from(0); - nav.focus_range - .map_or_else(t_0, |it| it.start()) - .cmp(&nav_b.focus_range.map_or_else(t_0, |it| it.start())) - }) - .then_with(|| kind.disc().cmp(&kind_b.disc())) - .then_with(|| nav.name.cmp(&nav_b.name)) - }); + res.sort_by(cmp_runnables); res } @@ -215,12 +202,30 @@ pub(crate) fn related_tests( search_scope: Option, ) -> Vec { let sema = Semantics::new(db); - let mut res: FxHashSet = FxHashSet::default(); + let mut res: FxIndexSet = FxIndexSet::default(); let syntax = sema.parse_guess_edition(position.file_id).syntax().clone(); find_related_tests(&sema, &syntax, position, search_scope, &mut res); - res.into_iter().collect() + res.into_iter().sorted_by(cmp_runnables).collect() +} + +fn cmp_runnables( + Runnable { nav, kind, .. }: &Runnable, + Runnable { nav: nav_b, kind: kind_b, .. }: &Runnable, +) -> std::cmp::Ordering { + // full_range.start < focus_range.start < name, should give us a decent unique ordering + nav.full_range + .start() + .cmp(&nav_b.full_range.start()) + .then_with(|| { + let t_0 = || TextSize::from(0); + nav.focus_range + .map_or_else(t_0, |it| it.start()) + .cmp(&nav_b.focus_range.map_or_else(t_0, |it| it.start())) + }) + .then_with(|| kind.disc().cmp(&kind_b.disc())) + .then_with(|| nav.name.cmp(&nav_b.name)) } fn find_related_tests( @@ -228,7 +233,7 @@ fn find_related_tests( syntax: &SyntaxNode, position: FilePosition, search_scope: Option, - tests: &mut FxHashSet, + tests: &mut FxIndexSet, ) { // FIXME: why is this using references::find_defs, this should use ide_db::search let defs = match references::find_defs(sema, syntax, position.offset) { @@ -268,7 +273,7 @@ fn find_related_tests_in_module( syntax: &SyntaxNode, fn_def: &ast::Fn, parent_module: &hir::Module, - tests: &mut FxHashSet, + tests: &mut FxIndexSet, ) { let fn_name = match fn_def.name() { Some(it) => it, @@ -1501,18 +1506,18 @@ mod tests { file_id: FileId( 0, ), - full_range: 121..185, - focus_range: 136..145, - name: "foo2_test", + full_range: 52..115, + focus_range: 67..75, + name: "foo_test", kind: Function, }, NavigationTarget { file_id: FileId( 0, ), - full_range: 52..115, - focus_range: 67..75, - name: "foo_test", + full_range: 121..185, + focus_range: 136..145, + name: "foo2_test", kind: Function, }, ] diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs index b9520ae2bba..30b8fa74f81 100644 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs +++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs @@ -1084,6 +1084,9 @@ pub struct Struct; ); } +// Rainbow highlighting uses a deterministic hash (fxhash) but the hashing does differ +// depending on the pointer width so only runs this on 64-bit targets. +#[cfg(target_pointer_width = "64")] #[test] fn test_rainbow_highlighting() { check_highlighting(