Highlight defs in highlight related

This commit is contained in:
Lukas Wirth 2023-05-02 08:37:40 +02:00
parent a64626d99e
commit 8907533536

View file

@ -200,9 +200,16 @@ fn highlight_exit_points(
) -> Option<Vec<HighlightedRange>> { ) -> Option<Vec<HighlightedRange>> {
fn hl( fn hl(
sema: &Semantics<'_, RootDatabase>, sema: &Semantics<'_, RootDatabase>,
def_ranges: [Option<TextRange>; 2],
body: Option<ast::Expr>, body: Option<ast::Expr>,
) -> Option<Vec<HighlightedRange>> { ) -> Option<Vec<HighlightedRange>> {
let mut highlights = Vec::new(); let mut highlights = Vec::new();
highlights.extend(
def_ranges
.into_iter()
.flatten()
.map(|range| HighlightedRange { category: None, range }),
);
let body = body?; let body = body?;
walk_expr(&body, &mut |expr| match expr { walk_expr(&body, &mut |expr| match expr {
ast::Expr::ReturnExpr(expr) => { ast::Expr::ReturnExpr(expr) => {
@ -246,10 +253,21 @@ fn highlight_exit_points(
for anc in token.parent_ancestors() { for anc in token.parent_ancestors() {
return match_ast! { return match_ast! {
match anc { match anc {
ast::Fn(fn_) => hl(sema, fn_.body().map(ast::Expr::BlockExpr)), ast::Fn(fn_) => hl(sema, [fn_.fn_token().map(|it| it.text_range()), None], fn_.body().map(ast::Expr::BlockExpr)),
ast::ClosureExpr(closure) => hl(sema, closure.body()), ast::ClosureExpr(closure) => hl(
sema,
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())]),
closure.body()
),
ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) { ast::BlockExpr(block_expr) => if matches!(block_expr.modifier(), Some(ast::BlockModifier::Async(_) | ast::BlockModifier::Try(_)| ast::BlockModifier::Const(_))) {
hl(sema, Some(block_expr.into())) hl(
sema,
[block_expr.modifier().and_then(|modifier| match modifier {
ast::BlockModifier::Async(t) | ast::BlockModifier::Try(t) | ast::BlockModifier::Const(t) => Some(t.text_range()),
_ => None,
}), None],
Some(block_expr.into())
)
} else { } else {
continue; continue;
}, },
@ -663,7 +681,8 @@ async fn foo() {
fn test_hl_exit_points() { fn test_hl_exit_points() {
check( check(
r#" r#"
fn foo() -> u32 { fn foo() -> u32 {
//^^
if true { if true {
return$0 0; return$0 0;
// ^^^^^^ // ^^^^^^
@ -682,7 +701,8 @@ fn foo() -> u32 {
fn test_hl_exit_points2() { fn test_hl_exit_points2() {
check( check(
r#" r#"
fn foo() ->$0 u32 { fn foo() ->$0 u32 {
//^^
if true { if true {
return 0; return 0;
// ^^^^^^ // ^^^^^^
@ -701,7 +721,8 @@ fn foo() ->$0 u32 {
fn test_hl_exit_points3() { fn test_hl_exit_points3() {
check( check(
r#" r#"
fn$0 foo() -> u32 { fn$0 foo() -> u32 {
//^^
if true { if true {
return 0; return 0;
// ^^^^^^ // ^^^^^^
@ -747,7 +768,8 @@ macro_rules! never {
() => { never() } () => { never() }
} }
fn never() -> ! { loop {} } fn never() -> ! { loop {} }
fn foo() ->$0 u32 { fn foo() ->$0 u32 {
//^^
never(); never();
// ^^^^^^^ // ^^^^^^^
never!(); never!();
@ -767,7 +789,8 @@ fn foo() ->$0 u32 {
fn test_hl_inner_tail_exit_points() { fn test_hl_inner_tail_exit_points() {
check( check(
r#" r#"
fn foo() ->$0 u32 { fn foo() ->$0 u32 {
//^^
if true { if true {
unsafe { unsafe {
return 5; return 5;
@ -808,7 +831,8 @@ fn foo() ->$0 u32 {
fn test_hl_inner_tail_exit_points_labeled_block() { fn test_hl_inner_tail_exit_points_labeled_block() {
check( check(
r#" r#"
fn foo() ->$0 u32 { fn foo() ->$0 u32 {
//^^
'foo: { 'foo: {
break 'foo 0; break 'foo 0;
// ^^^^^ // ^^^^^
@ -829,7 +853,8 @@ fn foo() ->$0 u32 {
fn test_hl_inner_tail_exit_points_loops() { fn test_hl_inner_tail_exit_points_loops() {
check( check(
r#" r#"
fn foo() ->$0 u32 { fn foo() ->$0 u32 {
//^^
'foo: while { return 0; true } { 'foo: while { return 0; true } {
// ^^^^^^ // ^^^^^^
break 'foo 0; break 'foo 0;
@ -1240,7 +1265,8 @@ fn foo() -> i32 {
check_with_config( check_with_config(
r#" r#"
fn foo() ->$0 i32 { fn foo() ->$0 i32 {
//^^
let x = 5; let x = 5;
let y = x * 2; let y = x * 2;