Skip to content

Commit 88df24f

Browse files
committed
fix: keyword highlighting in macro expansion
1 parent 59d697e commit 88df24f

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ pub(crate) fn highlight_exit_points(
301301
_ => None,
302302
};
303303

304+
if let Some(range) = original_range(sema.db, file_id, text_range) {
305+
highlights.push(HighlightedRange { category: ReferenceCategory::empty(), range })
306+
}
304307
});
305308

306309
// We should handle `return` separately because when it is used in `try` block
@@ -317,6 +320,10 @@ pub(crate) fn highlight_exit_points(
317320
_ => None,
318321
};
319322

323+
if let Some(range) = original_range(sema.db, file_id, text_range) {
324+
highlights
325+
.push(HighlightedRange { category: ReferenceCategory::empty(), range })
326+
}
320327
});
321328

322329
let tail = match body {
@@ -406,6 +413,10 @@ pub(crate) fn highlight_break_points(
406413
token_lt.map(|it| it.syntax().text_range()),
407414
);
408415

416+
if let Some(range) = original_range(sema.db, file_id, text_range) {
417+
highlights
418+
.push(HighlightedRange { category: ReferenceCategory::empty(), range })
419+
}
409420
});
410421

411422
Some(highlights)
@@ -478,6 +489,9 @@ pub(crate) fn highlight_yield_points(
478489
}
479490
.map(|it| it.text_range());
480491

492+
if let Some(range) = original_range(sema.db, file_id, token_range) {
493+
highlights.push(HighlightedRange { category: ReferenceCategory::empty(), range });
494+
}
481495
});
482496

483497
Some(highlights)
@@ -517,6 +531,20 @@ fn find_defs(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> FxHashSe
517531
.collect()
518532
}
519533

534+
fn original_range(
535+
db: &dyn db::ExpandDatabase,
536+
file_id: HirFileId,
537+
text_range: Option<TextRange>,
538+
) -> Option<TextRange> {
539+
if text_range.is_none() || !file_id.is_macro() {
540+
return text_range;
541+
}
542+
543+
InFile::new(file_id, text_range.unwrap())
544+
.original_node_file_range_opt(db)
545+
.map(|(frange, _)| frange.range)
546+
}
547+
520548
/// Preorder walk all the expression's child expressions.
521549
/// For macro calls, the callback will be called on the expanded expressions after
522550
/// visiting the macro call itself.
@@ -984,6 +1012,7 @@ impl Never {
9841012
}
9851013
macro_rules! never {
9861014
() => { never() }
1015+
// ^^^^^^^
9871016
}
9881017
fn never() -> ! { loop {} }
9891018
fn foo() ->$0 u32 {
@@ -1810,4 +1839,108 @@ fn test() {
18101839
"#,
18111840
);
18121841
}
1842+
1843+
#[test]
1844+
fn return_in_macros() {
1845+
check(
1846+
r#"
1847+
macro_rules! N {
1848+
($i:ident, $x:expr, $blk:expr) => {
1849+
for $i in 0..$x {
1850+
$blk
1851+
}
1852+
};
1853+
}
1854+
1855+
fn main() {
1856+
fn f() {
1857+
// ^^
1858+
N!(i, 5, {
1859+
println!("{}", i);
1860+
return$0;
1861+
// ^^^^^^
1862+
});
1863+
1864+
for i in 1..5 {
1865+
return;
1866+
// ^^^^^^
1867+
}
1868+
(|| {
1869+
return;
1870+
})();
1871+
}
1872+
}
1873+
"#,
1874+
)
1875+
}
1876+
1877+
#[test]
1878+
fn return_in_closure() {
1879+
check(
1880+
r#"
1881+
macro_rules! N {
1882+
($i:ident, $x:expr, $blk:expr) => {
1883+
for $i in 0..$x {
1884+
$blk
1885+
}
1886+
};
1887+
}
1888+
1889+
fn main() {
1890+
fn f() {
1891+
N!(i, 5, {
1892+
println!("{}", i);
1893+
return;
1894+
});
1895+
1896+
for i in 1..5 {
1897+
return;
1898+
}
1899+
(|| {
1900+
// ^
1901+
return$0;
1902+
// ^^^^^^
1903+
})();
1904+
}
1905+
}
1906+
"#,
1907+
)
1908+
}
1909+
1910+
#[test]
1911+
fn return_in_try() {
1912+
check(
1913+
r#"
1914+
fn main() {
1915+
fn f() {
1916+
// ^^
1917+
try {
1918+
return$0;
1919+
// ^^^^^^
1920+
}
1921+
1922+
return;
1923+
// ^^^^^^
1924+
}
1925+
}
1926+
"#,
1927+
)
1928+
}
1929+
1930+
#[test]
1931+
fn break_in_try() {
1932+
check(
1933+
r#"
1934+
fn main() {
1935+
for i in 1..100 {
1936+
// ^^^
1937+
let x: Result<(), ()> = try {
1938+
break$0;
1939+
// ^^^^^
1940+
};
1941+
}
1942+
}
1943+
"#,
1944+
)
1945+
}
18131946
}

0 commit comments

Comments
 (0)