mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 21:13:37 +00:00
fix: refactor introduce_named_generic
assist
Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
parent
5aaffe6dc9
commit
2fb563f192
2 changed files with 31 additions and 42 deletions
|
@ -1,9 +1,6 @@
|
||||||
use ide_db::syntax_helpers::suggest_name;
|
use ide_db::syntax_helpers::suggest_name;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::ast::{self, syntax_factory::SyntaxFactory, AstNode, HasGenericParams, HasName};
|
||||||
ast::{self, syntax_factory::SyntaxFactory, AstNode, HasGenericParams, HasName},
|
|
||||||
SyntaxElement,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
@ -25,20 +22,14 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
|
||||||
|
|
||||||
let type_bound_list = impl_trait_type.type_bound_list()?;
|
let type_bound_list = impl_trait_type.type_bound_list()?;
|
||||||
|
|
||||||
// FIXME: Is this node appropriate to use for SyntaxEditor in this case?
|
|
||||||
let parent_node = match ctx.covering_element() {
|
|
||||||
SyntaxElement::Node(n) => n,
|
|
||||||
SyntaxElement::Token(t) => t.parent()?,
|
|
||||||
};
|
|
||||||
let make = SyntaxFactory::new();
|
let make = SyntaxFactory::new();
|
||||||
|
|
||||||
let target = fn_.syntax().text_range();
|
let target = fn_.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("introduce_named_generic", AssistKind::RefactorRewrite),
|
AssistId("introduce_named_generic", AssistKind::RefactorRewrite),
|
||||||
"Replace impl trait with generic",
|
"Replace impl trait with generic",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|builder| {
|
||||||
let mut editor = edit.make_editor(&parent_node);
|
let mut editor = builder.make_editor(fn_.syntax());
|
||||||
|
|
||||||
let existing_names = match fn_.generic_param_list() {
|
let existing_names = match fn_.generic_param_list() {
|
||||||
Some(generic_param_list) => generic_param_list
|
Some(generic_param_list) => generic_param_list
|
||||||
|
@ -63,11 +54,11 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
|
||||||
fn_.syntax_editor_add_generic_param(&mut editor, generic_param.clone());
|
fn_.syntax_editor_add_generic_param(&mut editor, generic_param.clone());
|
||||||
|
|
||||||
if let Some(cap) = ctx.config.snippet_cap {
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
editor.add_annotation(generic_param.syntax(), edit.make_tabstop_before(cap));
|
editor.add_annotation(generic_param.syntax(), builder.make_tabstop_before(cap));
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.add_mappings(make.finish_with_mappings());
|
editor.add_mappings(make.finish_with_mappings());
|
||||||
edit.add_file_edits(ctx.file_id(), editor);
|
builder.add_file_edits(ctx.file_id(), editor);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,40 +64,38 @@ impl ast::Fn {
|
||||||
) {
|
) {
|
||||||
match self.generic_param_list() {
|
match self.generic_param_list() {
|
||||||
Some(generic_param_list) => match generic_param_list.generic_params().last() {
|
Some(generic_param_list) => match generic_param_list.generic_params().last() {
|
||||||
Some(_last_param) => {
|
Some(last_param) => {
|
||||||
// There exists a generic param list and it's not empty
|
// There exists a generic param list and it's not empty
|
||||||
let position = generic_param_list.r_angle_token().map_or_else(
|
let position = generic_param_list.r_angle_token().map_or_else(
|
||||||
|| crate::syntax_editor::Position::last_child_of(self.syntax()),
|
|| crate::syntax_editor::Position::last_child_of(self.syntax()),
|
||||||
crate::syntax_editor::Position::before,
|
crate::syntax_editor::Position::before,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(last_param) = generic_param_list.generic_params().last() {
|
if last_param
|
||||||
if last_param
|
.syntax()
|
||||||
.syntax()
|
.next_sibling_or_token()
|
||||||
.next_sibling_or_token()
|
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
|
||||||
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
|
{
|
||||||
{
|
editor.insert(
|
||||||
editor.insert(
|
crate::syntax_editor::Position::after(last_param.syntax()),
|
||||||
crate::syntax_editor::Position::after(last_param.syntax()),
|
new_param.syntax().clone(),
|
||||||
new_param.syntax().clone(),
|
);
|
||||||
);
|
editor.insert(
|
||||||
editor.insert(
|
crate::syntax_editor::Position::after(last_param.syntax()),
|
||||||
crate::syntax_editor::Position::after(last_param.syntax()),
|
make::token(SyntaxKind::WHITESPACE),
|
||||||
make::token(SyntaxKind::WHITESPACE),
|
);
|
||||||
);
|
editor.insert(
|
||||||
editor.insert(
|
crate::syntax_editor::Position::after(last_param.syntax()),
|
||||||
crate::syntax_editor::Position::after(last_param.syntax()),
|
make::token(SyntaxKind::COMMA),
|
||||||
make::token(SyntaxKind::COMMA),
|
);
|
||||||
);
|
} else {
|
||||||
} else {
|
let elements = vec![
|
||||||
let elements = vec![
|
make::token(SyntaxKind::COMMA).into(),
|
||||||
make::token(SyntaxKind::COMMA).into(),
|
make::token(SyntaxKind::WHITESPACE).into(),
|
||||||
make::token(SyntaxKind::WHITESPACE).into(),
|
new_param.syntax().clone().into(),
|
||||||
new_param.syntax().clone().into(),
|
];
|
||||||
];
|
editor.insert_all(position, elements);
|
||||||
editor.insert_all(position, elements);
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
// There exists a generic param list but it's empty
|
// There exists a generic param list but it's empty
|
||||||
|
|
Loading…
Reference in a new issue