Skip to content

Commit 8a5aa80

Browse files
authored
Merge pull request #18982 from Veykril/push-lstmvzsowxyt
Extract variable assist triggers less eagerly
2 parents b795b7b + b1def0c commit 8a5aa80

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

crates/ide-assists/src/assist_context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ impl<'a> AssistContext<'a> {
109109
self.trimmed_range
110110
}
111111

112+
pub(crate) fn source_file(&self) -> &SourceFile {
113+
&self.source_file
114+
}
115+
112116
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
113117
self.source_file.syntax().token_at_offset(self.offset())
114118
}

crates/ide-assists/src/handlers/extract_variable.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use ide_db::{
44
syntax_helpers::{suggest_name, LexedStr},
55
};
66
use syntax::{
7+
algo::ancestors_at_offset,
78
ast::{
89
self, edit::IndentLevel, edit_in_place::Indent, make, syntax_factory::SyntaxFactory,
910
AstNode,
@@ -68,7 +69,10 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
6869
let node = if ctx.has_empty_selection() {
6970
if let Some(t) = ctx.token_at_offset().find(|it| it.kind() == T![;]) {
7071
t.parent().and_then(ast::ExprStmt::cast)?.syntax().clone()
71-
} else if let Some(expr) = ctx.find_node_at_offset::<ast::Expr>() {
72+
} else if let Some(expr) = ancestors_at_offset(ctx.source_file().syntax(), ctx.offset())
73+
.next()
74+
.and_then(ast::Expr::cast)
75+
{
7276
expr.syntax().ancestors().find_map(valid_target_expr)?.syntax().clone()
7377
} else {
7478
return None;
@@ -469,11 +473,11 @@ mod tests {
469473
extract_variable,
470474
r#"
471475
fn main() -> i32 {
472-
if true {
476+
if$0 true {
473477
1
474478
} else {
475479
2
476-
}$0
480+
}
477481
}
478482
"#,
479483
r#"
@@ -581,11 +585,11 @@ fn main() {
581585
extract_variable,
582586
r#"
583587
fn main() -> i32 {
584-
if true {
588+
if$0 true {
585589
1
586590
} else {
587591
2
588-
}$0
592+
}
589593
}
590594
"#,
591595
r#"
@@ -676,11 +680,11 @@ fn main() {
676680
extract_variable,
677681
r#"
678682
fn main() -> i32 {
679-
if true {
683+
if$0 true {
680684
1
681685
} else {
682686
2
683-
}$0
687+
}
684688
}
685689
"#,
686690
r#"

0 commit comments

Comments
 (0)