Skip to content

Commit b70fab2

Browse files
committed
fix: sort references in tests
1 parent 5aa60cf commit b70fab2

File tree

3 files changed

+97
-22
lines changed

3 files changed

+97
-22
lines changed

src/tools/rust-analyzer/crates/ide/src/goto_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ fn nav_for_break_points(
386386
ast::Expr::LoopExpr(loop_) => loop_.loop_token()?.text_range(),
387387
ast::Expr::WhileExpr(while_) => while_.while_token()?.text_range(),
388388
ast::Expr::ForExpr(for_) => for_.for_token()?.text_range(),
389-
// We garentee that the label exists
389+
// We guarantee that the label exists
390390
ast::Expr::BlockExpr(blk) => blk.label().unwrap().syntax().text_range(),
391391
_ => return None,
392392
};

src/tools/rust-analyzer/crates/ide/src/highlight_related.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ pub(crate) fn highlight_exit_points(
286286
sema: &Semantics<'_, RootDatabase>,
287287
def_token: Option<SyntaxToken>,
288288
body: ast::Expr,
289-
) -> Option<FxHashMap<EditionedFileId, Vec<HighlightedRange>>> {
290-
let mut highlights: FxHashMap<EditionedFileId, Vec<_>> = FxHashMap::default();
289+
) -> Option<FxHashMap<EditionedFileId, FxHashSet<HighlightedRange>>> {
290+
let mut highlights: FxHashMap<EditionedFileId, FxHashSet<_>> = FxHashMap::default();
291291

292292
let mut push_to_highlights = |file_id, range| {
293293
if let Some(FileRange { file_id, range }) = original_frange(sema.db, file_id, range) {
294294
let hrange = HighlightedRange { category: ReferenceCategory::empty(), range };
295-
highlights.entry(file_id).or_default().push(hrange);
295+
highlights.entry(file_id).or_default().insert(hrange);
296296
}
297297
};
298298

@@ -379,7 +379,7 @@ pub(crate) fn highlight_exit_points(
379379
merge_map(&mut res, new_map);
380380
}
381381

382-
res
382+
res.into_iter().map(|(file_id, ranges)| (file_id, ranges.into_iter().collect())).collect()
383383
}
384384

385385
pub(crate) fn highlight_break_points(
@@ -392,13 +392,13 @@ pub(crate) fn highlight_break_points(
392392
loop_token: Option<SyntaxToken>,
393393
label: Option<ast::Label>,
394394
expr: ast::Expr,
395-
) -> Option<FxHashMap<EditionedFileId, Vec<HighlightedRange>>> {
396-
let mut highlights: FxHashMap<EditionedFileId, Vec<_>> = FxHashMap::default();
395+
) -> Option<FxHashMap<EditionedFileId, FxHashSet<HighlightedRange>>> {
396+
let mut highlights: FxHashMap<EditionedFileId, FxHashSet<_>> = FxHashMap::default();
397397

398398
let mut push_to_highlights = |file_id, range| {
399399
if let Some(FileRange { file_id, range }) = original_frange(sema.db, file_id, range) {
400400
let hrange = HighlightedRange { category: ReferenceCategory::empty(), range };
401-
highlights.entry(file_id).or_default().push(hrange);
401+
highlights.entry(file_id).or_default().insert(hrange);
402402
}
403403
};
404404

@@ -445,11 +445,12 @@ pub(crate) fn highlight_break_points(
445445
Some(highlights)
446446
}
447447

448-
let mut res = FxHashMap::default();
449-
let token_kind = token.kind();
450448
let Some(loops) = goto_definition::find_loops(sema, &token) else {
451-
return res;
449+
return FxHashMap::default();
452450
};
451+
452+
let mut res = FxHashMap::default();
453+
let token_kind = token.kind();
453454
for expr in loops {
454455
let new_map = match &expr {
455456
ast::Expr::LoopExpr(l) => hl(sema, token_kind, l.loop_token(), l.label(), expr),
@@ -461,7 +462,7 @@ pub(crate) fn highlight_break_points(
461462
merge_map(&mut res, new_map);
462463
}
463464

464-
res
465+
res.into_iter().map(|(file_id, ranges)| (file_id, ranges.into_iter().collect())).collect()
465466
}
466467

467468
pub(crate) fn highlight_yield_points(
@@ -472,13 +473,13 @@ pub(crate) fn highlight_yield_points(
472473
sema: &Semantics<'_, RootDatabase>,
473474
async_token: Option<SyntaxToken>,
474475
body: Option<ast::Expr>,
475-
) -> Option<FxHashMap<EditionedFileId, Vec<HighlightedRange>>> {
476-
let mut highlights: FxHashMap<EditionedFileId, Vec<_>> = FxHashMap::default();
476+
) -> Option<FxHashMap<EditionedFileId, FxHashSet<HighlightedRange>>> {
477+
let mut highlights: FxHashMap<EditionedFileId, FxHashSet<_>> = FxHashMap::default();
477478

478479
let mut push_to_highlights = |file_id, range| {
479480
if let Some(FileRange { file_id, range }) = original_frange(sema.db, file_id, range) {
480481
let hrange = HighlightedRange { category: ReferenceCategory::empty(), range };
481-
highlights.entry(file_id).or_default().push(hrange);
482+
highlights.entry(file_id).or_default().insert(hrange);
482483
}
483484
};
484485

@@ -524,7 +525,7 @@ pub(crate) fn highlight_yield_points(
524525
merge_map(&mut res, new_map);
525526
}
526527

527-
res
528+
res.into_iter().map(|(file_id, ranges)| (file_id, ranges.into_iter().collect())).collect()
528529
}
529530

530531
fn cover_range(r0: Option<TextRange>, r1: Option<TextRange>) -> Option<TextRange> {
@@ -553,8 +554,8 @@ fn original_frange(
553554
}
554555

555556
fn merge_map(
556-
res: &mut FxHashMap<EditionedFileId, Vec<HighlightedRange>>,
557-
new: Option<FxHashMap<EditionedFileId, Vec<HighlightedRange>>>,
557+
res: &mut FxHashMap<EditionedFileId, FxHashSet<HighlightedRange>>,
558+
new: Option<FxHashMap<EditionedFileId, FxHashSet<HighlightedRange>>>,
558559
) {
559560
let Some(new) = new else {
560561
return;
@@ -1967,6 +1968,38 @@ fn main() {
19671968
};
19681969
}
19691970
}
1971+
"#,
1972+
)
1973+
}
1974+
1975+
#[test]
1976+
fn no_highlight_on_return_in_macro_call() {
1977+
check(
1978+
r#"
1979+
//- minicore:include
1980+
//- /lib.rs
1981+
macro_rules! M {
1982+
($blk:expr) => {
1983+
$blk
1984+
};
1985+
}
1986+
1987+
fn main() {
1988+
fn f() {
1989+
// ^^
1990+
M!({ return$0; });
1991+
// ^^^^^^
1992+
// ^^^^^^^^^^^^^^^
1993+
1994+
include!("a.rs")
1995+
// ^^^^^^^^^^^^^^^^
1996+
}
1997+
}
1998+
1999+
//- /a.rs
2000+
{
2001+
return;
2002+
}
19702003
"#,
19712004
)
19722005
}

src/tools/rust-analyzer/crates/ide/src/references.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ impl Foo {
12361236
let refs = analysis.find_all_refs(pos, search_scope).unwrap().unwrap();
12371237

12381238
let mut actual = String::new();
1239-
for refs in refs {
1239+
for mut refs in refs {
12401240
actual += "\n\n";
12411241

12421242
if let Some(decl) = refs.declaration {
@@ -1247,7 +1247,8 @@ impl Foo {
12471247
actual += "\n\n";
12481248
}
12491249

1250-
for (file_id, references) in &refs.references {
1250+
for (file_id, references) in &mut refs.references {
1251+
references.sort_by_key(|(range, _)| range.start());
12511252
for (range, category) in references {
12521253
format_to!(actual, "{:?} {:?}", file_id, range);
12531254
for (name, _flag) in category.iter_names() {
@@ -2276,8 +2277,8 @@ fn$0 foo() -> u32 {
22762277
"#,
22772278
expect![[r#"
22782279
FileId(0) 0..2
2279-
FileId(0) 62..63
22802280
FileId(0) 40..46
2281+
FileId(0) 62..63
22812282
FileId(0) 69..80
22822283
"#]],
22832284
);
@@ -2297,8 +2298,8 @@ pub async$0 fn foo() {
22972298
"#,
22982299
expect![[r#"
22992300
FileId(0) 4..9
2300-
FileId(0) 63..68
23012301
FileId(0) 48..53
2302+
FileId(0) 63..68
23022303
FileId(0) 114..119
23032304
"#]],
23042305
);
@@ -2442,4 +2443,45 @@ fn main() {
24422443
"#]],
24432444
)
24442445
}
2446+
2447+
#[test]
2448+
fn goto_ref_on_return_in_macro_call() {
2449+
check(
2450+
r#"
2451+
//- minicore:include
2452+
//- /lib.rs
2453+
macro_rules! M {
2454+
($blk:expr) => {
2455+
fn f() {
2456+
$blk
2457+
}
2458+
2459+
$blk
2460+
};
2461+
}
2462+
2463+
fn main() {
2464+
M!({
2465+
return$0;
2466+
});
2467+
2468+
f();
2469+
include!("a.rs")
2470+
}
2471+
2472+
//- /a.rs
2473+
{
2474+
return;
2475+
}
2476+
"#,
2477+
expect![[r#"
2478+
FileId(0) 46..48
2479+
FileId(0) 106..108
2480+
FileId(0) 122..149
2481+
FileId(0) 135..141
2482+
FileId(0) 165..181
2483+
FileId(1) 6..12
2484+
"#]],
2485+
)
2486+
}
24452487
}

0 commit comments

Comments
 (0)