mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
8524: Fix extract function with partial block selection r=matklad a=brandondong **Reproduction:** ```rust fn foo() { let n = 1; let mut v = $0n * n;$0 v += 1; } ``` 1. Select the snippet ($0) and use the "Extract into function" assist. 2. Extracted function is incorrect and does not compile: ```rust fn foo() { let n = 1; let mut v = fun_name(n); v += 1; } fn fun_name(n: i32) {} ``` 3. Omitting the ending semicolon from the selection fixes the extracted function: ```rust fn fun_name(n: i32) -> i32 { n * n } ``` **Cause:** - When `extraction_target` uses a block extraction (semicolon case) instead of an expression extraction (no semicolon case), the user selection is directly used as the TextRange. - However, the existing function extraction logic for blocks requires that the TextRange spans from start to end of complete statements to work correctly. - For example: ```rust fn foo() { let m = 2; let n = 1; let mut v = m $0* n; let mut w = 3;$0 v += 1; w += 1; } ``` produces ```rust fn foo() { let m = 2; let n = 1; let mut v = m let mut w = fun_name(n); v += 1; w += 1; } fn fun_name(n: i32) -> i32 { let mut w = 3; w } ``` - The user selected TextRange is directly replaced by the function call which is now in the middle of another statement. The extracted function body only contains statements that were fully covered by the TextRange and so the `* n` code is deleted. The logic for calculating variable usage and outlived variables for the function parameters and return type respectively search within the TextRange and so do not include `m` or `v`. **Fix:** - Only extract full statements when using block extraction. If a user selected part of a statement, extract that full statement. 8527: Switch introduce_named_lifetime assist to use mutable syntax tree r=matklad a=iDawer This extends `GenericParamsOwnerEdit` trait with `get_or_create_generic_param_list` method Co-authored-by: Brandon <brandondong604@hotmail.com> Co-authored-by: Dawer <7803845+iDawer@users.noreply.github.com>
This commit is contained in:
commit
e7a8977358
4 changed files with 298 additions and 38 deletions
|
@ -14,10 +14,29 @@ use crate::{
|
|||
use super::NameOwner;
|
||||
|
||||
pub trait GenericParamsOwnerEdit: ast::GenericParamsOwner + AstNodeEdit {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList;
|
||||
fn get_or_create_where_clause(&self) -> ast::WhereClause;
|
||||
}
|
||||
|
||||
impl GenericParamsOwnerEdit for ast::Fn {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||
match self.generic_param_list() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let position = if let Some(name) = self.name() {
|
||||
Position::after(name.syntax)
|
||||
} else if let Some(fn_token) = self.fn_token() {
|
||||
Position::after(fn_token)
|
||||
} else if let Some(param_list) = self.param_list() {
|
||||
Position::before(param_list.syntax)
|
||||
} else {
|
||||
Position::last_child_of(self.syntax())
|
||||
};
|
||||
create_generic_param_list(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_where_clause(&self) -> WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let position = if let Some(ty) = self.ret_type() {
|
||||
|
@ -34,6 +53,20 @@ impl GenericParamsOwnerEdit for ast::Fn {
|
|||
}
|
||||
|
||||
impl GenericParamsOwnerEdit for ast::Impl {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||
match self.generic_param_list() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let position = if let Some(imp_token) = self.impl_token() {
|
||||
Position::after(imp_token)
|
||||
} else {
|
||||
Position::last_child_of(self.syntax())
|
||||
};
|
||||
create_generic_param_list(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_where_clause(&self) -> WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let position = if let Some(items) = self.assoc_item_list() {
|
||||
|
@ -48,6 +81,22 @@ impl GenericParamsOwnerEdit for ast::Impl {
|
|||
}
|
||||
|
||||
impl GenericParamsOwnerEdit for ast::Trait {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||
match self.generic_param_list() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let position = if let Some(name) = self.name() {
|
||||
Position::after(name.syntax)
|
||||
} else if let Some(trait_token) = self.trait_token() {
|
||||
Position::after(trait_token)
|
||||
} else {
|
||||
Position::last_child_of(self.syntax())
|
||||
};
|
||||
create_generic_param_list(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_where_clause(&self) -> WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let position = if let Some(items) = self.assoc_item_list() {
|
||||
|
@ -62,6 +111,22 @@ impl GenericParamsOwnerEdit for ast::Trait {
|
|||
}
|
||||
|
||||
impl GenericParamsOwnerEdit for ast::Struct {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||
match self.generic_param_list() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let position = if let Some(name) = self.name() {
|
||||
Position::after(name.syntax)
|
||||
} else if let Some(struct_token) = self.struct_token() {
|
||||
Position::after(struct_token)
|
||||
} else {
|
||||
Position::last_child_of(self.syntax())
|
||||
};
|
||||
create_generic_param_list(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_where_clause(&self) -> WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let tfl = self.field_list().and_then(|fl| match fl {
|
||||
|
@ -84,6 +149,22 @@ impl GenericParamsOwnerEdit for ast::Struct {
|
|||
}
|
||||
|
||||
impl GenericParamsOwnerEdit for ast::Enum {
|
||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||
match self.generic_param_list() {
|
||||
Some(it) => it,
|
||||
None => {
|
||||
let position = if let Some(name) = self.name() {
|
||||
Position::after(name.syntax)
|
||||
} else if let Some(enum_token) = self.enum_token() {
|
||||
Position::after(enum_token)
|
||||
} else {
|
||||
Position::last_child_of(self.syntax())
|
||||
};
|
||||
create_generic_param_list(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_or_create_where_clause(&self) -> WhereClause {
|
||||
if self.where_clause().is_none() {
|
||||
let position = if let Some(gpl) = self.generic_param_list() {
|
||||
|
@ -104,6 +185,37 @@ fn create_where_clause(position: Position) {
|
|||
ted::insert(position, where_clause.syntax());
|
||||
}
|
||||
|
||||
fn create_generic_param_list(position: Position) -> ast::GenericParamList {
|
||||
let gpl = make::generic_param_list(empty()).clone_for_update();
|
||||
ted::insert_raw(position, gpl.syntax());
|
||||
gpl
|
||||
}
|
||||
|
||||
impl ast::GenericParamList {
|
||||
pub fn add_generic_param(&self, generic_param: ast::GenericParam) {
|
||||
match self.generic_params().last() {
|
||||
Some(last_param) => {
|
||||
let mut elems = Vec::new();
|
||||
if !last_param
|
||||
.syntax()
|
||||
.siblings_with_tokens(Direction::Next)
|
||||
.any(|it| it.kind() == T![,])
|
||||
{
|
||||
elems.push(make::token(T![,]).into());
|
||||
elems.push(make::tokens::single_space().into());
|
||||
};
|
||||
elems.push(generic_param.syntax().clone().into());
|
||||
let after_last_param = Position::after(last_param.syntax());
|
||||
ted::insert_all(after_last_param, elems);
|
||||
}
|
||||
None => {
|
||||
let after_l_angle = Position::after(self.l_angle_token().unwrap());
|
||||
ted::insert(after_l_angle, generic_param.syntax())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::WhereClause {
|
||||
pub fn add_predicate(&self, predicate: ast::WherePred) {
|
||||
if let Some(pred) = self.predicates().last() {
|
||||
|
@ -164,3 +276,44 @@ impl ast::Use {
|
|||
ted::remove(self.syntax())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fmt;
|
||||
|
||||
use crate::SourceFile;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn ast_mut_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
parse.tree().syntax().descendants().find_map(N::cast).unwrap().clone_for_update()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_generic_param_list() {
|
||||
fn check_create_gpl<N: GenericParamsOwnerEdit + fmt::Display>(before: &str, after: &str) {
|
||||
let gpl_owner = ast_mut_from_text::<N>(before);
|
||||
gpl_owner.get_or_create_generic_param_list();
|
||||
assert_eq!(gpl_owner.to_string(), after);
|
||||
}
|
||||
|
||||
check_create_gpl::<ast::Fn>("fn foo", "fn foo<>");
|
||||
check_create_gpl::<ast::Fn>("fn foo() {}", "fn foo<>() {}");
|
||||
|
||||
check_create_gpl::<ast::Impl>("impl", "impl<>");
|
||||
check_create_gpl::<ast::Impl>("impl Struct {}", "impl<> Struct {}");
|
||||
check_create_gpl::<ast::Impl>("impl Trait for Struct {}", "impl<> Trait for Struct {}");
|
||||
|
||||
check_create_gpl::<ast::Trait>("trait Trait<>", "trait Trait<>");
|
||||
check_create_gpl::<ast::Trait>("trait Trait<> {}", "trait Trait<> {}");
|
||||
|
||||
check_create_gpl::<ast::Struct>("struct A", "struct A<>");
|
||||
check_create_gpl::<ast::Struct>("struct A;", "struct A<>;");
|
||||
check_create_gpl::<ast::Struct>("struct A();", "struct A<>();");
|
||||
check_create_gpl::<ast::Struct>("struct A {}", "struct A<> {}");
|
||||
|
||||
check_create_gpl::<ast::Enum>("enum E", "enum E<>");
|
||||
check_create_gpl::<ast::Enum>("enum E {", "enum E<> {");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue