mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-10-30 19:49:36 +00:00 
			
		
		
		
	fix: Stabilize sort order of related_tests
				
					
				
			This commit is contained in:
		
							parent
							
								
									b332a053e0
								
							
						
					
					
						commit
						1afbc220bc
					
				
					 2 changed files with 35 additions and 27 deletions
				
			
		|  | @ -4,8 +4,8 @@ use arrayvec::ArrayVec; | ||||||
| use ast::HasName; | use ast::HasName; | ||||||
| use cfg::{CfgAtom, CfgExpr}; | use cfg::{CfgAtom, CfgExpr}; | ||||||
| use hir::{ | use hir::{ | ||||||
|     db::HirDatabase, sym, AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, HasSource, HirFileIdExt, |     db::HirDatabase, sym, symbols::FxIndexSet, AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, | ||||||
|     ModPath, Name, PathKind, Semantics, Symbol, |     HasSource, HirFileIdExt, ModPath, Name, PathKind, Semantics, Symbol, | ||||||
| }; | }; | ||||||
| use ide_assists::utils::{has_test_related_attribute, test_related_attribute_syn}; | use ide_assists::utils::{has_test_related_attribute, test_related_attribute_syn}; | ||||||
| use ide_db::{ | use ide_db::{ | ||||||
|  | @ -13,7 +13,7 @@ use ide_db::{ | ||||||
|     documentation::docs_from_attrs, |     documentation::docs_from_attrs, | ||||||
|     helpers::visit_file_defs, |     helpers::visit_file_defs, | ||||||
|     search::{FileReferenceNode, SearchScope}, |     search::{FileReferenceNode, SearchScope}, | ||||||
|     FilePosition, FxHashMap, FxHashSet, FxIndexMap, RootDatabase, SymbolKind, |     FilePosition, FxHashMap, FxIndexMap, RootDatabase, SymbolKind, | ||||||
| }; | }; | ||||||
| use itertools::Itertools; | use itertools::Itertools; | ||||||
| use smallvec::SmallVec; | use smallvec::SmallVec; | ||||||
|  | @ -182,20 +182,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { | ||||||
|             r |             r | ||||||
|         }) |         }) | ||||||
|     })); |     })); | ||||||
|     res.sort_by(|Runnable { nav, kind, .. }, Runnable { nav: nav_b, kind: kind_b, .. }| { |     res.sort_by(cmp_runnables); | ||||||
|         // 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 |     res | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -215,12 +202,30 @@ pub(crate) fn related_tests( | ||||||
|     search_scope: Option<SearchScope>, |     search_scope: Option<SearchScope>, | ||||||
| ) -> Vec<Runnable> { | ) -> Vec<Runnable> { | ||||||
|     let sema = Semantics::new(db); |     let sema = Semantics::new(db); | ||||||
|     let mut res: FxHashSet<Runnable> = FxHashSet::default(); |     let mut res: FxIndexSet<Runnable> = FxIndexSet::default(); | ||||||
|     let syntax = sema.parse_guess_edition(position.file_id).syntax().clone(); |     let syntax = sema.parse_guess_edition(position.file_id).syntax().clone(); | ||||||
| 
 | 
 | ||||||
|     find_related_tests(&sema, &syntax, position, search_scope, &mut res); |     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( | fn find_related_tests( | ||||||
|  | @ -228,7 +233,7 @@ fn find_related_tests( | ||||||
|     syntax: &SyntaxNode, |     syntax: &SyntaxNode, | ||||||
|     position: FilePosition, |     position: FilePosition, | ||||||
|     search_scope: Option<SearchScope>, |     search_scope: Option<SearchScope>, | ||||||
|     tests: &mut FxHashSet<Runnable>, |     tests: &mut FxIndexSet<Runnable>, | ||||||
| ) { | ) { | ||||||
|     // FIXME: why is this using references::find_defs, this should use ide_db::search
 |     // FIXME: why is this using references::find_defs, this should use ide_db::search
 | ||||||
|     let defs = match references::find_defs(sema, syntax, position.offset) { |     let defs = match references::find_defs(sema, syntax, position.offset) { | ||||||
|  | @ -268,7 +273,7 @@ fn find_related_tests_in_module( | ||||||
|     syntax: &SyntaxNode, |     syntax: &SyntaxNode, | ||||||
|     fn_def: &ast::Fn, |     fn_def: &ast::Fn, | ||||||
|     parent_module: &hir::Module, |     parent_module: &hir::Module, | ||||||
|     tests: &mut FxHashSet<Runnable>, |     tests: &mut FxIndexSet<Runnable>, | ||||||
| ) { | ) { | ||||||
|     let fn_name = match fn_def.name() { |     let fn_name = match fn_def.name() { | ||||||
|         Some(it) => it, |         Some(it) => it, | ||||||
|  | @ -1501,18 +1506,18 @@ mod tests { | ||||||
|                         file_id: FileId( |                         file_id: FileId( | ||||||
|                             0, |                             0, | ||||||
|                         ), |                         ), | ||||||
|                         full_range: 121..185, |                         full_range: 52..115, | ||||||
|                         focus_range: 136..145, |                         focus_range: 67..75, | ||||||
|                         name: "foo2_test", |                         name: "foo_test", | ||||||
|                         kind: Function, |                         kind: Function, | ||||||
|                     }, |                     }, | ||||||
|                     NavigationTarget { |                     NavigationTarget { | ||||||
|                         file_id: FileId( |                         file_id: FileId( | ||||||
|                             0, |                             0, | ||||||
|                         ), |                         ), | ||||||
|                         full_range: 52..115, |                         full_range: 121..185, | ||||||
|                         focus_range: 67..75, |                         focus_range: 136..145, | ||||||
|                         name: "foo_test", |                         name: "foo2_test", | ||||||
|                         kind: Function, |                         kind: Function, | ||||||
|                     }, |                     }, | ||||||
|                 ] |                 ] | ||||||
|  |  | ||||||
|  | @ -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] | #[test] | ||||||
| fn test_rainbow_highlighting() { | fn test_rainbow_highlighting() { | ||||||
|     check_highlighting( |     check_highlighting( | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lukas Wirth
						Lukas Wirth