Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8907533

Browse files
committed
Highlight defs in highlight related
1 parent a64626d commit 8907533

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,16 @@ fn highlight_exit_points(
200200
) -> Option<Vec<HighlightedRange>> {
201201
fn hl(
202202
sema: &Semantics<'_, RootDatabase>,
203+
def_ranges: [Option<TextRange>; 2],
203204
body: Option<ast::Expr>,
204205
) -> Option<Vec<HighlightedRange>> {
205206
let mut highlights = Vec::new();
207+
highlights.extend(
208+
def_ranges
209+
.into_iter()
210+
.flatten()
211+
.map(|range| HighlightedRange { category: None, range }),
212+
);
206213
let body = body?;
207214
walk_expr(&body, &mut |expr| match expr {
208215
ast::Expr::ReturnExpr(expr) => {
@@ -246,10 +253,21 @@ fn highlight_exit_points(
246253
for anc in token.parent_ancestors() {
247254
return match_ast! {
248255
match anc {
249-
ast::Fn(fn_) => hl(sema, fn_.body().map(ast::Expr::BlockExpr)),
250-
ast::ClosureExpr(closure) => hl(sema, closure.body()),
256+
ast::Fn(fn_) => hl(sema, [fn_.fn_token().map(|it| it.text_range()), None], fn_.body().map(ast::Expr::BlockExpr)),
257+
ast::ClosureExpr(closure) => hl(
258+
sema,
259+
closure.param_list().map_or([None; 2], |p| [p.l_paren_token().map(|it| it.text_range()), p.r_paren_token().map(|it| it.text_range())]),
260+
closure.body()
261+
),
251262
ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) {
252-
hl(sema, Some(block_expr.into()))
263+
hl(
264+
sema,
265+
[block_expr.modifier().and_then(|modifier| match modifier {
266+
ast::BlockModifier::Async(t) | ast::BlockModifier::Try(t) | ast::BlockModifier::Const(t) => Some(t.text_range()),
267+
_ => None,
268+
}), None],
269+
Some(block_expr.into())
270+
)
253271
} else {
254272
continue;
255273
},
@@ -663,7 +681,8 @@ async fn foo() {
663681
fn test_hl_exit_points() {
664682
check(
665683
r#"
666-
fn foo() -> u32 {
684+
fn foo() -> u32 {
685+
//^^
667686
if true {
668687
return$0 0;
669688
// ^^^^^^
@@ -682,7 +701,8 @@ fn foo() -> u32 {
682701
fn test_hl_exit_points2() {
683702
check(
684703
r#"
685-
fn foo() ->$0 u32 {
704+
fn foo() ->$0 u32 {
705+
//^^
686706
if true {
687707
return 0;
688708
// ^^^^^^
@@ -701,7 +721,8 @@ fn foo() ->$0 u32 {
701721
fn test_hl_exit_points3() {
702722
check(
703723
r#"
704-
fn$0 foo() -> u32 {
724+
fn$0 foo() -> u32 {
725+
//^^
705726
if true {
706727
return 0;
707728
// ^^^^^^
@@ -747,7 +768,8 @@ macro_rules! never {
747768
() => { never() }
748769
}
749770
fn never() -> ! { loop {} }
750-
fn foo() ->$0 u32 {
771+
fn foo() ->$0 u32 {
772+
//^^
751773
never();
752774
// ^^^^^^^
753775
never!();
@@ -767,7 +789,8 @@ fn foo() ->$0 u32 {
767789
fn test_hl_inner_tail_exit_points() {
768790
check(
769791
r#"
770-
fn foo() ->$0 u32 {
792+
fn foo() ->$0 u32 {
793+
//^^
771794
if true {
772795
unsafe {
773796
return 5;
@@ -808,7 +831,8 @@ fn foo() ->$0 u32 {
808831
fn test_hl_inner_tail_exit_points_labeled_block() {
809832
check(
810833
r#"
811-
fn foo() ->$0 u32 {
834+
fn foo() ->$0 u32 {
835+
//^^
812836
'foo: {
813837
break 'foo 0;
814838
// ^^^^^
@@ -829,7 +853,8 @@ fn foo() ->$0 u32 {
829853
fn test_hl_inner_tail_exit_points_loops() {
830854
check(
831855
r#"
832-
fn foo() ->$0 u32 {
856+
fn foo() ->$0 u32 {
857+
//^^
833858
'foo: while { return 0; true } {
834859
// ^^^^^^
835860
break 'foo 0;
@@ -1240,7 +1265,8 @@ fn foo() -> i32 {
12401265

12411266
check_with_config(
12421267
r#"
1243-
fn foo() ->$0 i32 {
1268+
fn foo() ->$0 i32 {
1269+
//^^
12441270
let x = 5;
12451271
let y = x * 2;
12461272

0 commit comments

Comments
 (0)