Skip to content

Commit dcbaa75

Browse files
bors[bot]Veykril
andauthored
Merge #9928
9928: internal: Highlight function exit points on `fn` keyword r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 067bae3 + 40455db commit dcbaa75

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

crates/ide/src/highlight_related.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ pub struct HighlightRelatedConfig {
3131
// Highlights constructs related to the thing under the cursor:
3232
// - if on an identifier, highlights all references to that identifier in the current file
3333
// - if on an `async` or `await token, highlights all yield points for that async context
34-
// - if on a `return` token, `?` character or `->` return type arrow, highlights all exit points for that context
34+
// - if on a `return` or `fn` keyword, `?` character or `->` return type arrow, highlights all exit points for that context
3535
// - if on a `break`, `loop`, `while` or `for` token, highlights all break points for that loop or block context
36+
//
37+
// Note: `?` and `->` do not currently trigger this behavior in the VSCode editor.
3638
pub(crate) fn highlight_related(
3739
sema: &Semantics<RootDatabase>,
3840
config: HighlightRelatedConfig,
@@ -42,20 +44,16 @@ pub(crate) fn highlight_related(
4244
let syntax = sema.parse(position.file_id).syntax().clone();
4345

4446
let token = pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
45-
T![?] => 2, // prefer `?` when the cursor is sandwiched like `await$0?`
46-
T![await]
47-
| T![async]
48-
| T![return]
49-
| T![break]
50-
| T![loop]
51-
| T![for]
52-
| T![while]
53-
| T![->] => 1,
47+
T![?] => 3, // prefer `?` when the cursor is sandwiched like in `await$0?`
48+
T![->] => 2,
49+
kind if kind.is_keyword() => 1,
5450
_ => 0,
5551
})?;
5652

5753
match token.kind() {
58-
T![return] | T![?] | T![->] if config.exit_points => highlight_exit_points(sema, token),
54+
T![fn] | T![return] | T![?] | T![->] if config.exit_points => {
55+
highlight_exit_points(sema, token)
56+
}
5957
T![await] | T![async] if config.yield_points => highlight_yield_points(token),
6058
T![break] | T![loop] | T![for] | T![while] if config.break_points => {
6159
highlight_break_points(token)
@@ -474,6 +472,25 @@ fn foo() ->$0 u32 {
474472
);
475473
}
476474

475+
#[test]
476+
fn test_hl_exit_points3() {
477+
check(
478+
r#"
479+
fn$0 foo() -> u32 {
480+
if true {
481+
return 0;
482+
// ^^^^^^
483+
}
484+
485+
0?;
486+
// ^
487+
0xDEAD_BEEF
488+
// ^^^^^^^^^^^
489+
}
490+
"#,
491+
);
492+
}
493+
477494
#[test]
478495
fn test_hl_prefer_ref_over_tail_exit() {
479496
check(

0 commit comments

Comments
 (0)