mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
define syntax_editor_add_generic_param
Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
parent
4af3d6f598
commit
642d4f3385
3 changed files with 34 additions and 7 deletions
|
@ -56,13 +56,11 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
|
||||||
)
|
)
|
||||||
.for_impl_trait_as_generic(&impl_trait_type);
|
.for_impl_trait_as_generic(&impl_trait_type);
|
||||||
|
|
||||||
let type_param = make
|
let type_param = make.type_param(make.name(&type_param_name), Some(type_bound_list));
|
||||||
.type_param(make.name(&type_param_name), Some(type_bound_list))
|
let new_ty = make.ty(&type_param_name);
|
||||||
.clone_for_update();
|
|
||||||
let new_ty = make.ty(&type_param_name).clone_for_update();
|
|
||||||
|
|
||||||
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
|
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
|
||||||
fn_generic_param_list.add_generic_param(type_param.into());
|
fn_generic_param_list.syntax_editor_add_generic_param(&mut editor, type_param.into());
|
||||||
|
|
||||||
if let Some(cap) = ctx.config.snippet_cap {
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
if let Some(generic_param) =
|
if let Some(generic_param) =
|
||||||
|
|
|
@ -7,6 +7,7 @@ use parser::{SyntaxKind, T};
|
||||||
use crate::{
|
use crate::{
|
||||||
algo::{self, neighbor},
|
algo::{self, neighbor},
|
||||||
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
|
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
|
||||||
|
syntax_editor::SyntaxEditor,
|
||||||
ted::{self, Position},
|
ted::{self, Position},
|
||||||
AstNode, AstToken, Direction, SyntaxElement,
|
AstNode, AstToken, Direction, SyntaxElement,
|
||||||
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
|
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
|
||||||
|
@ -257,6 +258,29 @@ impl ast::GenericParamList {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn syntax_editor_add_generic_param(
|
||||||
|
&self,
|
||||||
|
editor: &mut SyntaxEditor,
|
||||||
|
generic_param: ast::GenericParam,
|
||||||
|
) {
|
||||||
|
match self.generic_params().last() {
|
||||||
|
Some(last_param) => {
|
||||||
|
let position = crate::syntax_editor::Position::after(last_param.syntax());
|
||||||
|
let elements = vec![
|
||||||
|
make::token(T![,]).into(),
|
||||||
|
make::tokens::single_space().into(),
|
||||||
|
generic_param.syntax().clone().into(),
|
||||||
|
];
|
||||||
|
editor.insert_all(position, elements);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let after_l_angle =
|
||||||
|
crate::syntax_editor::Position::after(self.l_angle_token().unwrap());
|
||||||
|
editor.insert(after_l_angle, generic_param.syntax());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Removes the existing generic param
|
/// Removes the existing generic param
|
||||||
pub fn remove_generic_param(&self, generic_param: ast::GenericParam) {
|
pub fn remove_generic_param(&self, generic_param: ast::GenericParam) {
|
||||||
if let Some(previous) = generic_param.syntax().prev_sibling() {
|
if let Some(previous) = generic_param.syntax().prev_sibling() {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, make, HasName},
|
ast::{self, make, HasName, HasTypeBounds},
|
||||||
syntax_editor::SyntaxMappingBuilder,
|
syntax_editor::SyntaxMappingBuilder,
|
||||||
AstNode,
|
AstNode,
|
||||||
};
|
};
|
||||||
|
@ -15,7 +15,6 @@ impl SyntaxFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ty(&self, text: &str) -> ast::Type {
|
pub fn ty(&self, text: &str) -> ast::Type {
|
||||||
// FIXME: Is there anything to map here?
|
|
||||||
make::ty(text).clone_for_update()
|
make::ty(text).clone_for_update()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +28,12 @@ impl SyntaxFactory {
|
||||||
if let Some(mut mapping) = self.mappings() {
|
if let Some(mut mapping) = self.mappings() {
|
||||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||||
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
|
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
|
||||||
|
if let Some(input) = bounds {
|
||||||
|
builder.map_node(
|
||||||
|
input.syntax().clone(),
|
||||||
|
ast.type_bound_list().unwrap().syntax().clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
builder.finish(&mut mapping);
|
builder.finish(&mut mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue