Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use std::iter;

use hir::{ExpandResult, InFile, Semantics, Type, TypeInfo, Variant};
use ide_db::{RootDatabase, active_parameter::ActiveParameter};
use ide_db::{
RootDatabase, active_parameter::ActiveParameter, syntax_helpers::node_ext::find_loops,
};
use itertools::Either;
use stdx::always;
use syntax::{
Expand Down Expand Up @@ -752,6 +754,12 @@ fn expected_type_and_name<'db>(
});
(ty, None)
},
ast::BreakExpr(it) => {
let ty = it.break_token()
.and_then(|it| find_loops(sema, &it)?.next())
.and_then(|expr| sema.type_of_expr(&expr));
(ty.map(TypeInfo::original), None)
},
ast::ClosureExpr(it) => {
let ty = sema.type_of_expr(&it.into());
ty.and_then(|ty| ty.original.as_callable(sema.db))
Expand Down Expand Up @@ -2006,7 +2014,8 @@ fn prev_special_biased_token_at_trivia(mut token: SyntaxToken) -> SyntaxToken {
| T![|]
| T![return]
| T![break]
| T![continue] = prev.kind()
| T![continue]
| T![lifetime_ident] = prev.kind()
{
token = prev
}
Expand Down
119 changes: 119 additions & 0 deletions crates/ide-completion/src/context/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,125 @@ fn foo() {
);
}

#[test]
fn expected_type_break_expr_in_loop() {
check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
let _x: State = loop {
{
break State::Stop;
break $0;
}
};
}
"#,
expect![[r#"ty: State, name: ?"#]],
);

check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
let _x: State = 'a: loop {
{
break State::Stop;
break $0;
}
};
}
"#,
expect![[r#"ty: State, name: ?"#]],
);

check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
let _x: State = 'a: loop {
while true {
break $0;
}
};
}
"#,
expect![[r#"ty: (), name: ?"#]],
);
}

#[test]
fn expected_type_break_expr_in_labeled_loop() {
check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
let _x: State = 'a: loop {
let _y: i32 = loop {
{
break 'a State::Stop;
break 'a $0;
}
};
};
}
"#,
expect![[r#"ty: State, name: ?"#]],
);

check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
let _x: State = 'a: loop {
let _y: i32 = loop {
while true {
break 'a State::Stop;
break 'a $0;
}
};
};
}
"#,
expect![[r#"ty: State, name: ?"#]],
);

check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
'a: while true {
let _x: State = loop {
break State::Stop;
break 'a $0;
};
}
}
"#,
expect![[r#"ty: (), name: ?"#]],
);
}

#[test]
fn expected_type_break_expr_in_labeled_block() {
check_expected_type_and_name(
r#"
enum State { Stop }
fn foo() {
let _x: State = 'a: {
let _y: i32 = 'b: {
{
break 'a State::Stop;
break 'a $0;
};
};
};
}
"#,
expect![[r#"ty: State, name: ?"#]],
);
}

#[test]
fn expected_type_logic_op() {
check_expected_type_and_name(
Expand Down
42 changes: 42 additions & 0 deletions crates/ide-db/src/syntax_helpers/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,48 @@ pub fn eq_label_lt(lt1: &Option<ast::Lifetime>, lt2: &Option<ast::Lifetime>) ->
lt1.as_ref().zip(lt2.as_ref()).is_some_and(|(lt, lbl)| lt.text() == lbl.text())
}

/// Find the loop or block to break or continue, multiple results may be caused by macros.
pub fn find_loops(
sema: &hir::Semantics<'_, crate::RootDatabase>,
token: &syntax::SyntaxToken,
) -> Option<impl Iterator<Item = ast::Expr>> {
let parent = token.parent()?;
let lbl = syntax::match_ast! {
match parent {
ast::BreakExpr(break_) => break_.lifetime(),
ast::ContinueExpr(continue_) => continue_.lifetime(),
_ => None,
}
};
let label_matches =
move |it: Option<ast::Label>| match (lbl.as_ref(), it.and_then(|it| it.lifetime())) {
(Some(lbl), Some(it)) => lbl.text() == it.text(),
(None, _) => true,
(Some(_), None) => false,
};

let find_ancestors = move |token| {
for anc in sema.token_ancestors_with_macros(token).filter_map(ast::Expr::cast) {
let node = match &anc {
ast::Expr::LoopExpr(loop_) if label_matches(loop_.label()) => anc,
ast::Expr::WhileExpr(while_) if label_matches(while_.label()) => anc,
ast::Expr::ForExpr(for_) if label_matches(for_.label()) => anc,
ast::Expr::BlockExpr(blk)
if blk.label().is_some() && label_matches(blk.label()) =>
{
anc
}
_ => continue,
};

return Some(node);
}
None
};

sema.descend_into_macros(token.clone()).into_iter().filter_map(find_ancestors).into()
}

struct TreeWithDepthIterator {
preorder: Preorder<RustLanguage>,
depth: u32,
Expand Down
53 changes: 2 additions & 51 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ use ide_db::{
defs::{Definition, IdentClass},
famous_defs::FamousDefs,
helpers::pick_best_token,
syntax_helpers::node_ext::find_loops,
};
use itertools::Itertools;
use span::{Edition, FileId};
use syntax::{
AstNode, AstToken,
SyntaxKind::*,
SyntaxNode, SyntaxToken, T, TextRange,
ast::{self, HasLoopBody},
match_ast,
AstNode, AstToken, SyntaxKind::*, SyntaxNode, SyntaxToken, T, TextRange, ast, match_ast,
};

#[derive(Debug)]
Expand Down Expand Up @@ -511,59 +508,13 @@ fn nav_for_branch_exit_points(
Some(navs)
}

pub(crate) fn find_loops(
sema: &Semantics<'_, RootDatabase>,
token: &SyntaxToken,
) -> Option<Vec<ast::Expr>> {
let parent = token.parent()?;
let lbl = match_ast! {
match parent {
ast::BreakExpr(break_) => break_.lifetime(),
ast::ContinueExpr(continue_) => continue_.lifetime(),
_ => None,
}
};
let label_matches =
|it: Option<ast::Label>| match (lbl.as_ref(), it.and_then(|it| it.lifetime())) {
(Some(lbl), Some(it)) => lbl.text() == it.text(),
(None, _) => true,
(Some(_), None) => false,
};

let find_ancestors = |token: SyntaxToken| {
for anc in sema.token_ancestors_with_macros(token).filter_map(ast::Expr::cast) {
let node = match &anc {
ast::Expr::LoopExpr(loop_) if label_matches(loop_.label()) => anc,
ast::Expr::WhileExpr(while_) if label_matches(while_.label()) => anc,
ast::Expr::ForExpr(for_) if label_matches(for_.label()) => anc,
ast::Expr::BlockExpr(blk)
if blk.label().is_some() && label_matches(blk.label()) =>
{
anc
}
_ => continue,
};

return Some(node);
}
None
};

sema.descend_into_macros(token.clone())
.into_iter()
.filter_map(find_ancestors)
.collect_vec()
.into()
}

fn nav_for_break_points(
sema: &Semantics<'_, RootDatabase>,
token: &SyntaxToken,
) -> Option<Vec<NavigationTarget>> {
let db = sema.db;

let navs = find_loops(sema, token)?
.into_iter()
.filter_map(|expr| {
let file_id = sema.hir_file_for(expr.syntax());
let expr_in_file = InFile::new(file_id, expr.clone());
Expand Down
6 changes: 3 additions & 3 deletions crates/ide/src/highlight_related.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use ide_db::{
helpers::pick_best_token,
search::{FileReference, ReferenceCategory, SearchScope},
syntax_helpers::node_ext::{
eq_label_lt, for_each_tail_expr, full_path_of_name_ref, is_closure_or_blk_with_modif,
preorder_expr_with_ctx_checker,
eq_label_lt, find_loops, for_each_tail_expr, full_path_of_name_ref,
is_closure_or_blk_with_modif, preorder_expr_with_ctx_checker,
},
};
use syntax::{
Expand Down Expand Up @@ -564,7 +564,7 @@ pub(crate) fn highlight_break_points(
Some(highlights)
}

let Some(loops) = goto_definition::find_loops(sema, &token) else {
let Some(loops) = find_loops(sema, &token) else {
return FxHashMap::default();
};

Expand Down