mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge pull request #18982 from Veykril/push-lstmvzsowxyt
Extract variable assist triggers less eagerly
This commit is contained in:
commit
8a5aa80488
@ -109,6 +109,10 @@ impl<'a> AssistContext<'a> {
|
|||||||
self.trimmed_range
|
self.trimmed_range
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn source_file(&self) -> &SourceFile {
|
||||||
|
&self.source_file
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
|
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
|
||||||
self.source_file.syntax().token_at_offset(self.offset())
|
self.source_file.syntax().token_at_offset(self.offset())
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use ide_db::{
|
|||||||
syntax_helpers::{suggest_name, LexedStr},
|
syntax_helpers::{suggest_name, LexedStr},
|
||||||
};
|
};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
algo::ancestors_at_offset,
|
||||||
ast::{
|
ast::{
|
||||||
self, edit::IndentLevel, edit_in_place::Indent, make, syntax_factory::SyntaxFactory,
|
self, edit::IndentLevel, edit_in_place::Indent, make, syntax_factory::SyntaxFactory,
|
||||||
AstNode,
|
AstNode,
|
||||||
@ -68,7 +69,10 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
|
|||||||
let node = if ctx.has_empty_selection() {
|
let node = if ctx.has_empty_selection() {
|
||||||
if let Some(t) = ctx.token_at_offset().find(|it| it.kind() == T![;]) {
|
if let Some(t) = ctx.token_at_offset().find(|it| it.kind() == T![;]) {
|
||||||
t.parent().and_then(ast::ExprStmt::cast)?.syntax().clone()
|
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()
|
expr.syntax().ancestors().find_map(valid_target_expr)?.syntax().clone()
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
@ -469,11 +473,11 @@ mod tests {
|
|||||||
extract_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
if true {
|
if$0 true {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
2
|
2
|
||||||
}$0
|
}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
@ -581,11 +585,11 @@ fn main() {
|
|||||||
extract_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
if true {
|
if$0 true {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
2
|
2
|
||||||
}$0
|
}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
@ -676,11 +680,11 @@ fn main() {
|
|||||||
extract_variable,
|
extract_variable,
|
||||||
r#"
|
r#"
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
if true {
|
if$0 true {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
2
|
2
|
||||||
}$0
|
}
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user