add logic to highlight continue and break keywords according to expectations

This commit is contained in:
Moritz Vetter 2022-02-13 12:48:04 +01:00
parent cad0cf6950
commit 3da08071ce
2 changed files with 60 additions and 32 deletions

View File

@ -2,7 +2,9 @@ use hir::Semantics;
use ide_db::{ use ide_db::{
base_db::{FileId, FilePosition}, base_db::{FileId, FilePosition},
defs::{Definition, IdentClass}, defs::{Definition, IdentClass},
helpers::{for_each_break_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token}, helpers::{
for_each_break_and_continue_expr, for_each_tail_expr, node_ext::walk_expr, pick_best_token,
},
search::{FileReference, ReferenceCategory, SearchScope}, search::{FileReference, ReferenceCategory, SearchScope},
RootDatabase, RootDatabase,
}; };
@ -10,7 +12,7 @@ use rustc_hash::FxHashSet;
use syntax::{ use syntax::{
ast::{self, HasLoopBody}, ast::{self, HasLoopBody},
match_ast, AstNode, match_ast, AstNode,
SyntaxKind::{IDENT, INT_NUMBER}, SyntaxKind::{self, IDENT, INT_NUMBER},
SyntaxNode, SyntaxToken, TextRange, T, SyntaxNode, SyntaxToken, TextRange, T,
}; };
@ -66,7 +68,9 @@ pub(crate) fn highlight_related(
T![for] if config.break_points && token.parent().and_then(ast::ForExpr::cast).is_some() => { T![for] if config.break_points && token.parent().and_then(ast::ForExpr::cast).is_some() => {
highlight_break_points(token) highlight_break_points(token)
} }
T![break] | T![loop] | T![while] if config.break_points => highlight_break_points(token), T![break] | T![loop] | T![while] | T![continue] if config.break_points => {
highlight_break_points(token)
}
_ if config.references => highlight_references(sema, &syntax, token, file_id), _ if config.references => highlight_references(sema, &syntax, token, file_id),
_ => None, _ => None,
} }
@ -187,6 +191,7 @@ fn highlight_exit_points(
fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> { fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
fn hl( fn hl(
cursor_token_kind: SyntaxKind,
token: Option<SyntaxToken>, token: Option<SyntaxToken>,
label: Option<ast::Label>, label: Option<ast::Label>,
body: Option<ast::StmtList>, body: Option<ast::StmtList>,
@ -197,11 +202,20 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
label.as_ref().map(|it| it.syntax().text_range()), label.as_ref().map(|it| it.syntax().text_range()),
); );
highlights.extend(range.map(|range| HighlightedRange { category: None, range })); highlights.extend(range.map(|range| HighlightedRange { category: None, range }));
for_each_break_expr(label, body, &mut |expr| { for_each_break_and_continue_expr(label, body, &mut |expr| {
let range: Option<TextRange> = match expr { let range: Option<TextRange> = match (cursor_token_kind, expr) {
ast::Expr::BreakExpr(break_) => cover_range( (T![for] | T![while] | T![loop] | T![break], ast::Expr::BreakExpr(break_)) => {
cover_range(
break_.break_token().map(|it| it.text_range()), break_.break_token().map(|it| it.text_range()),
break_.lifetime().map(|it| it.syntax().text_range()), break_.lifetime().map(|it| it.syntax().text_range()),
)
}
(
T![for] | T![while] | T![loop] | T![continue],
ast::Expr::ContinueExpr(continue_),
) => cover_range(
continue_.continue_token().map(|it| it.text_range()),
continue_.lifetime().map(|it| it.syntax().text_range()),
), ),
_ => None, _ => None,
}; };
@ -213,6 +227,7 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
let lbl = match_ast! { let lbl = match_ast! {
match parent { match parent {
ast::BreakExpr(b) => b.lifetime(), ast::BreakExpr(b) => b.lifetime(),
ast::ContinueExpr(c) => c.lifetime(),
ast::LoopExpr(l) => l.label().and_then(|it| it.lifetime()), ast::LoopExpr(l) => l.label().and_then(|it| it.lifetime()),
ast::ForExpr(f) => f.label().and_then(|it| it.lifetime()), ast::ForExpr(f) => f.label().and_then(|it| it.lifetime()),
ast::WhileExpr(w) => w.label().and_then(|it| it.lifetime()), ast::WhileExpr(w) => w.label().and_then(|it| it.lifetime()),
@ -227,19 +242,29 @@ fn highlight_break_points(token: SyntaxToken) -> Option<Vec<HighlightedRange>> {
} }
None => true, None => true,
}; };
let token_kind = token.kind();
for anc in token.ancestors().flat_map(ast::Expr::cast) { for anc in token.ancestors().flat_map(ast::Expr::cast) {
return match anc { return match anc {
ast::Expr::LoopExpr(l) if label_matches(l.label()) => { ast::Expr::LoopExpr(l) if label_matches(l.label()) => hl(
hl(l.loop_token(), l.label(), l.loop_body().and_then(|it| it.stmt_list())) token_kind,
} l.loop_token(),
ast::Expr::ForExpr(f) if label_matches(f.label()) => { l.label(),
hl(f.for_token(), f.label(), f.loop_body().and_then(|it| it.stmt_list())) l.loop_body().and_then(|it| it.stmt_list()),
} ),
ast::Expr::WhileExpr(w) if label_matches(w.label()) => { ast::Expr::ForExpr(f) if label_matches(f.label()) => hl(
hl(w.while_token(), w.label(), w.loop_body().and_then(|it| it.stmt_list())) token_kind,
} f.for_token(),
f.label(),
f.loop_body().and_then(|it| it.stmt_list()),
),
ast::Expr::WhileExpr(w) if label_matches(w.label()) => hl(
token_kind,
w.while_token(),
w.label(),
w.loop_body().and_then(|it| it.stmt_list()),
),
ast::Expr::BlockExpr(e) if e.label().is_some() && label_matches(e.label()) => { ast::Expr::BlockExpr(e) if e.label().is_some() && label_matches(e.label()) => {
hl(None, e.label(), e.stmt_list()) hl(token_kind, None, e.label(), e.stmt_list())
} }
_ => continue, _ => continue,
}; };
@ -882,7 +907,7 @@ fn foo() {
check( check(
r#" r#"
fn foo() { fn foo() {
'outer$0: for _ in () { 'outer: fo$0r _ in () {
// ^^^^^^^^^^^ // ^^^^^^^^^^^
break; break;
// ^^^^^ // ^^^^^

View File

@ -120,9 +120,7 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
) => return cb(expr), ) => return cb(expr),
Some(ast::BlockModifier::Label(label)) => { Some(ast::BlockModifier::Label(label)) => {
for_each_break_expr(Some(label), b.stmt_list(), &mut |b| { for_each_break_and_continue_expr(Some(label), b.stmt_list(), &mut |b| cb(&b));
cb(&b)
});
} }
Some(ast::BlockModifier::Unsafe(_)) => (), Some(ast::BlockModifier::Unsafe(_)) => (),
None => (), None => (),
@ -149,16 +147,16 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
} }
} }
} }
ast::Expr::LoopExpr(l) => { ast::Expr::LoopExpr(l) => for_each_break_and_continue_expr(
for_each_break_expr(l.label(), l.loop_body().and_then(|it| it.stmt_list()), &mut |b| { l.label(),
cb(&b) l.loop_body().and_then(|it| it.stmt_list()),
}) &mut |b| cb(&b),
} ),
ast::Expr::MatchExpr(m) => { ast::Expr::MatchExpr(m) => {
if let Some(arms) = m.match_arm_list() { if let Some(arms) = m.match_arm_list() {
arms.arms().filter_map(|arm| arm.expr()).for_each(|e| for_each_tail_expr(&e, &mut |b| { arms.arms()
cb(&b) .filter_map(|arm| arm.expr())
})); .for_each(|e| for_each_tail_expr(&e, &mut |b| cb(&b)));
} }
} }
ast::Expr::ArrayExpr(_) ast::Expr::ArrayExpr(_)
@ -192,8 +190,8 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
} }
} }
/// Calls `cb` on each break expr inside of `body` that is applicable for the given label. /// Calls `cb` on each break expr and continue expr inside of `body` that is applicable for the given label.
pub fn for_each_break_expr( pub fn for_each_break_and_continue_expr(
label: Option<ast::Label>, label: Option<ast::Label>,
body: Option<ast::StmtList>, body: Option<ast::StmtList>,
cb: &mut dyn FnMut(ast::Expr), cb: &mut dyn FnMut(ast::Expr),
@ -221,6 +219,11 @@ pub fn for_each_break_expr(
{ {
cb(ast::Expr::BreakExpr(b)); cb(ast::Expr::BreakExpr(b));
} }
ast::Expr::ContinueExpr(c)
if (depth == 0 && c.lifetime().is_none()) || eq_label(c.lifetime()) =>
{
cb(ast::Expr::ContinueExpr(c))
}
_ => (), _ => (),
}, },
WalkEvent::Leave(expr) => match expr { WalkEvent::Leave(expr) => match expr {