diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2025-01-20 13:45:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-20 13:45:26 +0000 |
| commit | 5770977d7276dc7e516a2a917d238f1dd3ba4656 (patch) | |
| tree | a1ad4deceb0028a3208bcf2bffe8fefea929c0d2 | |
| parent | 2233c315313964f2e5b3a572c8e0b23e8ab1cb15 (diff) | |
| parent | b53b2990cdd436ed4aa419ff4df3c66128cd3a13 (diff) | |
| download | rust-5770977d7276dc7e516a2a917d238f1dd3ba4656.tar.gz rust-5770977d7276dc7e516a2a917d238f1dd3ba4656.zip | |
Merge pull request #18982 from Veykril/push-lstmvzsowxyt
Extract variable assist triggers less eagerly
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/assist_context.rs | 4 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs | 18 |
2 files changed, 15 insertions, 7 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/assist_context.rs b/src/tools/rust-analyzer/crates/ide-assists/src/assist_context.rs index 074d943719f..64e77b2d698 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/assist_context.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/assist_context.rs @@ -109,6 +109,10 @@ impl<'a> AssistContext<'a> { self.trimmed_range } + pub(crate) fn source_file(&self) -> &SourceFile { + &self.source_file + } + pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> { self.source_file.syntax().token_at_offset(self.offset()) } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs index 0cc807aff64..97321f4ec1e 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_variable.rs @@ -4,6 +4,7 @@ use ide_db::{ syntax_helpers::{suggest_name, LexedStr}, }; use syntax::{ + algo::ancestors_at_offset, ast::{ self, edit::IndentLevel, edit_in_place::Indent, make, syntax_factory::SyntaxFactory, AstNode, @@ -68,7 +69,10 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op let node = if ctx.has_empty_selection() { if let Some(t) = ctx.token_at_offset().find(|it| it.kind() == T![;]) { t.parent().and_then(ast::ExprStmt::cast)?.syntax().clone() - } else if let Some(expr) = ctx.find_node_at_offset::<ast::Expr>() { + } else if let Some(expr) = ancestors_at_offset(ctx.source_file().syntax(), ctx.offset()) + .next() + .and_then(ast::Expr::cast) + { expr.syntax().ancestors().find_map(valid_target_expr)?.syntax().clone() } else { return None; @@ -469,11 +473,11 @@ mod tests { extract_variable, r#" fn main() -> i32 { - if true { + if$0 true { 1 } else { 2 - }$0 + } } "#, r#" @@ -581,11 +585,11 @@ fn main() { extract_variable, r#" fn main() -> i32 { - if true { + if$0 true { 1 } else { 2 - }$0 + } } "#, r#" @@ -676,11 +680,11 @@ fn main() { extract_variable, r#" fn main() -> i32 { - if true { + if$0 true { 1 } else { 2 - }$0 + } } "#, r#" |
