Skip to content

fix: Improve sort order of runnables #19161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions crates/ide/src/annotations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hir::{HasSource, InFile, InRealFile, Semantics};
use ide_db::{
defs::Definition, helpers::visit_file_defs, FileId, FilePosition, FileRange, FxHashSet,
defs::Definition, helpers::visit_file_defs, FileId, FilePosition, FileRange, FxIndexSet,
RootDatabase,
};
use itertools::Itertools;
Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) fn annotations(
config: &AnnotationConfig,
file_id: FileId,
) -> Vec<Annotation> {
let mut annotations = FxHashSet::default();
let mut annotations = FxIndexSet::default();

if config.annotate_runnables {
for runnable in runnables(db, file_id) {
Expand Down Expand Up @@ -170,7 +170,19 @@ pub(crate) fn annotations(
}));
}

annotations.into_iter().sorted_by_key(|a| (a.range.start(), a.range.end())).collect()
annotations
.into_iter()
.sorted_by_key(|a| {
(
a.range.start(),
a.range.end(),
match &a.kind {
AnnotationKind::Runnable(runnable) => Some(runnable.nav.name.clone()),
_ => None,
},
)
})
.collect()
}

pub(crate) fn resolve_annotation(db: &RootDatabase, mut annotation: Annotation) -> Annotation {
Expand Down Expand Up @@ -535,6 +547,20 @@ fn main() {
),
},
},
Annotation {
range: 69..73,
kind: HasReferences {
pos: FilePositionWrapper {
file_id: FileId(
0,
),
offset: 69,
},
data: Some(
[],
),
},
},
Annotation {
range: 69..73,
kind: Runnable(
Expand All @@ -559,20 +585,6 @@ fn main() {
},
),
},
Annotation {
range: 69..73,
kind: HasReferences {
pos: FilePositionWrapper {
file_id: FileId(
0,
),
offset: 69,
},
data: Some(
[],
),
},
},
]
"#]],
);
Expand Down Expand Up @@ -717,6 +729,20 @@ fn main() {
),
},
},
Annotation {
range: 61..65,
kind: HasReferences {
pos: FilePositionWrapper {
file_id: FileId(
0,
),
offset: 61,
},
data: Some(
[],
),
},
},
Annotation {
range: 61..65,
kind: Runnable(
Expand All @@ -741,20 +767,6 @@ fn main() {
},
),
},
Annotation {
range: 61..65,
kind: HasReferences {
pos: FilePositionWrapper {
file_id: FileId(
0,
),
offset: 61,
},
data: Some(
[],
),
},
},
]
"#]],
);
Expand Down
4 changes: 2 additions & 2 deletions crates/ide/src/runnables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ide_db::{
documentation::docs_from_attrs,
helpers::visit_file_defs,
search::{FileReferenceNode, SearchScope},
FilePosition, FxHashMap, FxHashSet, RootDatabase, SymbolKind,
FilePosition, FxHashMap, FxHashSet, FxIndexMap, RootDatabase, SymbolKind,
};
use itertools::Itertools;
use smallvec::SmallVec;
Expand Down Expand Up @@ -130,7 +130,7 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
let mut res = Vec::new();
// Record all runnables that come from macro expansions here instead.
// In case an expansion creates multiple runnables we want to name them to avoid emitting a bunch of equally named runnables.
let mut in_macro_expansion = FxHashMap::<hir::HirFileId, Vec<Runnable>>::default();
let mut in_macro_expansion = FxIndexMap::<hir::HirFileId, Vec<Runnable>>::default();
let mut add_opt = |runnable: Option<Runnable>, def| {
if let Some(runnable) = runnable.filter(|runnable| runnable.nav.file_id == file_id) {
if let Some(def) = def {
Expand Down