Skip to content

Commit d580944

Browse files
committed
Auto merge of rust-lang#17228 - Veykril:stable-runnables-order, r=Veykril
internal: Sort computed runnables Fixes rust-lang/rust-analyzer#17223
2 parents 24cd443 + e32a0a6 commit d580944

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

Diff for: src/tools/rust-analyzer/crates/ide/src/runnables.rs

+34-20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ide_db::{
1515
FxHashMap, FxHashSet, RootDatabase, SymbolKind,
1616
};
1717
use itertools::Itertools;
18+
use span::TextSize;
1819
use stdx::{always, format_to};
1920
use syntax::{
2021
ast::{self, AstNode},
@@ -48,23 +49,34 @@ impl fmt::Display for TestId {
4849

4950
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
5051
pub enum RunnableKind {
51-
Test { test_id: TestId, attr: TestAttr },
5252
TestMod { path: String },
53+
Test { test_id: TestId, attr: TestAttr },
5354
Bench { test_id: TestId },
5455
DocTest { test_id: TestId },
5556
Bin,
5657
}
5758

58-
#[cfg(test)]
59-
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
60-
enum RunnableTestKind {
59+
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
60+
enum RunnableDiscKind {
6161
Test,
6262
TestMod,
6363
DocTest,
6464
Bench,
6565
Bin,
6666
}
6767

68+
impl RunnableKind {
69+
fn disc(&self) -> RunnableDiscKind {
70+
match self {
71+
RunnableKind::TestMod { .. } => RunnableDiscKind::TestMod,
72+
RunnableKind::Test { .. } => RunnableDiscKind::Test,
73+
RunnableKind::DocTest { .. } => RunnableDiscKind::DocTest,
74+
RunnableKind::Bench { .. } => RunnableDiscKind::Bench,
75+
RunnableKind::Bin => RunnableDiscKind::Bin,
76+
}
77+
}
78+
}
79+
6880
impl Runnable {
6981
// test package::module::testname
7082
pub fn label(&self, target: Option<String>) -> String {
@@ -97,17 +109,6 @@ impl Runnable {
97109
s.push_str(suffix);
98110
s
99111
}
100-
101-
#[cfg(test)]
102-
fn test_kind(&self) -> RunnableTestKind {
103-
match &self.kind {
104-
RunnableKind::TestMod { .. } => RunnableTestKind::TestMod,
105-
RunnableKind::Test { .. } => RunnableTestKind::Test,
106-
RunnableKind::DocTest { .. } => RunnableTestKind::DocTest,
107-
RunnableKind::Bench { .. } => RunnableTestKind::Bench,
108-
RunnableKind::Bin => RunnableTestKind::Bin,
109-
}
110-
}
111112
}
112113

113114
// Feature: Run
@@ -193,6 +194,20 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
193194
r
194195
})
195196
}));
197+
res.sort_by(|Runnable { nav, kind, .. }, Runnable { nav: nav_b, kind: kind_b, .. }| {
198+
// full_range.start < focus_range.start < name, should give us a decent unique ordering
199+
nav.full_range
200+
.start()
201+
.cmp(&nav_b.full_range.start())
202+
.then_with(|| {
203+
let t_0 = || TextSize::from(0);
204+
nav.focus_range
205+
.map_or_else(t_0, |it| it.start())
206+
.cmp(&nav_b.focus_range.map_or_else(t_0, |it| it.start()))
207+
})
208+
.then_with(|| kind.disc().cmp(&kind_b.disc()))
209+
.then_with(|| nav.name.cmp(&nav_b.name))
210+
});
196211
res
197212
}
198213

@@ -571,13 +586,12 @@ mod tests {
571586

572587
fn check(ra_fixture: &str, expect: Expect) {
573588
let (analysis, position) = fixture::position(ra_fixture);
574-
let mut runnables = analysis.runnables(position.file_id).unwrap();
575-
runnables.sort_by_key(|it| (it.nav.full_range.start(), it.nav.name.clone()));
576-
577-
let result = runnables
589+
let result = analysis
590+
.runnables(position.file_id)
591+
.unwrap()
578592
.into_iter()
579593
.map(|runnable| {
580-
let mut a = format!("({:?}, {:?}", runnable.test_kind(), runnable.nav);
594+
let mut a = format!("({:?}, {:?}", runnable.kind.disc(), runnable.nav);
581595
if runnable.use_name_in_title {
582596
a.push_str(", true");
583597
}

0 commit comments

Comments
 (0)