Skip to content

Commit 5c39c85

Browse files
committed
fix: keyword highlighting in macro expansion
1 parent 63bacc1 commit 5c39c85

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ pub(crate) fn highlight_exit_points(
306306
_ => None,
307307
};
308308

309+
if let Some(range) = original_range(sema.db, file_id, text_range) {
310+
highlights.push(HighlightedRange { category: ReferenceCategory::empty(), range })
311+
}
309312
});
310313

311314
// We should handle `return` separately because when it is used in `try` block
@@ -322,6 +325,10 @@ pub(crate) fn highlight_exit_points(
322325
_ => None,
323326
};
324327

328+
if let Some(range) = original_range(sema.db, file_id, text_range) {
329+
highlights
330+
.push(HighlightedRange { category: ReferenceCategory::empty(), range })
331+
}
325332
});
326333

327334
let tail = match body {
@@ -411,6 +418,10 @@ pub(crate) fn highlight_break_points(
411418
token_lt.map(|it| it.syntax().text_range()),
412419
);
413420

421+
if let Some(range) = original_range(sema.db, file_id, text_range) {
422+
highlights
423+
.push(HighlightedRange { category: ReferenceCategory::empty(), range })
424+
}
414425
});
415426

416427
Some(highlights)
@@ -483,6 +494,9 @@ pub(crate) fn highlight_yield_points(
483494
}
484495
.map(|it| it.text_range());
485496

497+
if let Some(range) = original_range(sema.db, file_id, token_range) {
498+
highlights.push(HighlightedRange { category: ReferenceCategory::empty(), range });
499+
}
486500
});
487501

488502
Some(highlights)
@@ -522,6 +536,20 @@ fn find_defs(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> FxHashSe
522536
.collect()
523537
}
524538

539+
fn original_range(
540+
db: &dyn db::ExpandDatabase,
541+
file_id: HirFileId,
542+
text_range: Option<TextRange>,
543+
) -> Option<TextRange> {
544+
if text_range.is_none() || !file_id.is_macro() {
545+
return text_range;
546+
}
547+
548+
InFile::new(file_id, text_range.unwrap())
549+
.original_node_file_range_opt(db)
550+
.map(|(frange, _)| frange.range)
551+
}
552+
525553
/// Preorder walk all the expression's child expressions.
526554
/// For macro calls, the callback will be called on the expanded expressions after
527555
/// visiting the macro call itself.
@@ -989,6 +1017,7 @@ impl Never {
9891017
}
9901018
macro_rules! never {
9911019
() => { never() }
1020+
// ^^^^^^^
9921021
}
9931022
fn never() -> ! { loop {} }
9941023
fn foo() ->$0 u32 {
@@ -1815,4 +1844,108 @@ fn test() {
18151844
"#,
18161845
);
18171846
}
1847+
1848+
#[test]
1849+
fn return_in_macros() {
1850+
check(
1851+
r#"
1852+
macro_rules! N {
1853+
($i:ident, $x:expr, $blk:expr) => {
1854+
for $i in 0..$x {
1855+
$blk
1856+
}
1857+
};
1858+
}
1859+
1860+
fn main() {
1861+
fn f() {
1862+
// ^^
1863+
N!(i, 5, {
1864+
println!("{}", i);
1865+
return$0;
1866+
// ^^^^^^
1867+
});
1868+
1869+
for i in 1..5 {
1870+
return;
1871+
// ^^^^^^
1872+
}
1873+
(|| {
1874+
return;
1875+
})();
1876+
}
1877+
}
1878+
"#,
1879+
)
1880+
}
1881+
1882+
#[test]
1883+
fn return_in_closure() {
1884+
check(
1885+
r#"
1886+
macro_rules! N {
1887+
($i:ident, $x:expr, $blk:expr) => {
1888+
for $i in 0..$x {
1889+
$blk
1890+
}
1891+
};
1892+
}
1893+
1894+
fn main() {
1895+
fn f() {
1896+
N!(i, 5, {
1897+
println!("{}", i);
1898+
return;
1899+
});
1900+
1901+
for i in 1..5 {
1902+
return;
1903+
}
1904+
(|| {
1905+
// ^
1906+
return$0;
1907+
// ^^^^^^
1908+
})();
1909+
}
1910+
}
1911+
"#,
1912+
)
1913+
}
1914+
1915+
#[test]
1916+
fn return_in_try() {
1917+
check(
1918+
r#"
1919+
fn main() {
1920+
fn f() {
1921+
// ^^
1922+
try {
1923+
return$0;
1924+
// ^^^^^^
1925+
}
1926+
1927+
return;
1928+
// ^^^^^^
1929+
}
1930+
}
1931+
"#,
1932+
)
1933+
}
1934+
1935+
#[test]
1936+
fn break_in_try() {
1937+
check(
1938+
r#"
1939+
fn main() {
1940+
for i in 1..100 {
1941+
// ^^^
1942+
let x: Result<(), ()> = try {
1943+
break$0;
1944+
// ^^^^^
1945+
};
1946+
}
1947+
}
1948+
"#,
1949+
)
1950+
}
18181951
}

0 commit comments

Comments
 (0)