mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
refactor: move editing for ast using SyntaxEditor
to a separate file
Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
parent
2fb563f192
commit
9157761f5d
4 changed files with 74 additions and 74 deletions
|
@ -51,7 +51,7 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
|
||||||
|
|
||||||
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
|
editor.replace(impl_trait_type.syntax(), new_ty.syntax());
|
||||||
let generic_param = syntax::ast::GenericParam::from(type_param);
|
let generic_param = syntax::ast::GenericParam::from(type_param);
|
||||||
fn_.syntax_editor_add_generic_param(&mut editor, generic_param.clone());
|
editor.syntax_editor_add_generic_param(fn_, 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(), builder.make_tabstop_before(cap));
|
editor.add_annotation(generic_param.syntax(), builder.make_tabstop_before(cap));
|
||||||
|
|
|
@ -7,7 +7,6 @@ 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},
|
||||||
|
@ -55,78 +54,6 @@ impl GenericParamsOwnerEdit for ast::Fn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::Fn {
|
|
||||||
/// Adds a new generic param to the function using `SyntaxEditor`
|
|
||||||
pub fn syntax_editor_add_generic_param(
|
|
||||||
&self,
|
|
||||||
editor: &mut SyntaxEditor,
|
|
||||||
new_param: GenericParam,
|
|
||||||
) {
|
|
||||||
match self.generic_param_list() {
|
|
||||||
Some(generic_param_list) => match generic_param_list.generic_params().last() {
|
|
||||||
Some(last_param) => {
|
|
||||||
// There exists a generic param list and it's not empty
|
|
||||||
let position = generic_param_list.r_angle_token().map_or_else(
|
|
||||||
|| crate::syntax_editor::Position::last_child_of(self.syntax()),
|
|
||||||
crate::syntax_editor::Position::before,
|
|
||||||
);
|
|
||||||
|
|
||||||
if last_param
|
|
||||||
.syntax()
|
|
||||||
.next_sibling_or_token()
|
|
||||||
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
|
|
||||||
{
|
|
||||||
editor.insert(
|
|
||||||
crate::syntax_editor::Position::after(last_param.syntax()),
|
|
||||||
new_param.syntax().clone(),
|
|
||||||
);
|
|
||||||
editor.insert(
|
|
||||||
crate::syntax_editor::Position::after(last_param.syntax()),
|
|
||||||
make::token(SyntaxKind::WHITESPACE),
|
|
||||||
);
|
|
||||||
editor.insert(
|
|
||||||
crate::syntax_editor::Position::after(last_param.syntax()),
|
|
||||||
make::token(SyntaxKind::COMMA),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
let elements = vec![
|
|
||||||
make::token(SyntaxKind::COMMA).into(),
|
|
||||||
make::token(SyntaxKind::WHITESPACE).into(),
|
|
||||||
new_param.syntax().clone().into(),
|
|
||||||
];
|
|
||||||
editor.insert_all(position, elements);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
// There exists a generic param list but it's empty
|
|
||||||
let position = crate::syntax_editor::Position::after(
|
|
||||||
generic_param_list.l_angle_token().unwrap(),
|
|
||||||
);
|
|
||||||
editor.insert(position, new_param.syntax());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
// There was no generic param list
|
|
||||||
let position = if let Some(name) = self.name() {
|
|
||||||
crate::syntax_editor::Position::after(name.syntax)
|
|
||||||
} else if let Some(fn_token) = self.fn_token() {
|
|
||||||
crate::syntax_editor::Position::after(fn_token)
|
|
||||||
} else if let Some(param_list) = self.param_list() {
|
|
||||||
crate::syntax_editor::Position::before(param_list.syntax)
|
|
||||||
} else {
|
|
||||||
crate::syntax_editor::Position::last_child_of(self.syntax())
|
|
||||||
};
|
|
||||||
let elements = vec![
|
|
||||||
make::token(SyntaxKind::L_ANGLE).into(),
|
|
||||||
new_param.syntax().clone().into(),
|
|
||||||
make::token(SyntaxKind::R_ANGLE).into(),
|
|
||||||
];
|
|
||||||
editor.insert_all(position, elements);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GenericParamsOwnerEdit for ast::Impl {
|
impl GenericParamsOwnerEdit for ast::Impl {
|
||||||
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
fn get_or_create_generic_param_list(&self) -> ast::GenericParamList {
|
||||||
match self.generic_param_list() {
|
match self.generic_param_list() {
|
||||||
|
|
|
@ -16,6 +16,7 @@ use rustc_hash::FxHashMap;
|
||||||
use crate::{SyntaxElement, SyntaxNode, SyntaxToken};
|
use crate::{SyntaxElement, SyntaxNode, SyntaxToken};
|
||||||
|
|
||||||
mod edit_algo;
|
mod edit_algo;
|
||||||
|
mod edits;
|
||||||
mod mapping;
|
mod mapping;
|
||||||
|
|
||||||
pub use mapping::{SyntaxMapping, SyntaxMappingBuilder};
|
pub use mapping::{SyntaxMapping, SyntaxMappingBuilder};
|
||||||
|
|
72
crates/syntax/src/syntax_editor/edits.rs
Normal file
72
crates/syntax/src/syntax_editor/edits.rs
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
//! Structural editing for ast using `SyntaxEditor`
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
ast::make, ast::AstNode, ast::Fn, ast::GenericParam, ast::HasGenericParams, ast::HasName,
|
||||||
|
syntax_editor::Position, syntax_editor::SyntaxEditor, SyntaxKind,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl SyntaxEditor {
|
||||||
|
/// Adds a new generic param to the function using `SyntaxEditor`
|
||||||
|
pub fn syntax_editor_add_generic_param(&mut self, function: Fn, new_param: GenericParam) {
|
||||||
|
match function.generic_param_list() {
|
||||||
|
Some(generic_param_list) => match generic_param_list.generic_params().last() {
|
||||||
|
Some(last_param) => {
|
||||||
|
// There exists a generic param list and it's not empty
|
||||||
|
let position = generic_param_list.r_angle_token().map_or_else(
|
||||||
|
|| Position::last_child_of(function.syntax()),
|
||||||
|
Position::before,
|
||||||
|
);
|
||||||
|
|
||||||
|
if last_param
|
||||||
|
.syntax()
|
||||||
|
.next_sibling_or_token()
|
||||||
|
.map_or(false, |it| it.kind() == SyntaxKind::COMMA)
|
||||||
|
{
|
||||||
|
self.insert(
|
||||||
|
Position::after(last_param.syntax()),
|
||||||
|
new_param.syntax().clone(),
|
||||||
|
);
|
||||||
|
self.insert(
|
||||||
|
Position::after(last_param.syntax()),
|
||||||
|
make::token(SyntaxKind::WHITESPACE),
|
||||||
|
);
|
||||||
|
self.insert(
|
||||||
|
Position::after(last_param.syntax()),
|
||||||
|
make::token(SyntaxKind::COMMA),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
let elements = vec![
|
||||||
|
make::token(SyntaxKind::COMMA).into(),
|
||||||
|
make::token(SyntaxKind::WHITESPACE).into(),
|
||||||
|
new_param.syntax().clone().into(),
|
||||||
|
];
|
||||||
|
self.insert_all(position, elements);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// There exists a generic param list but it's empty
|
||||||
|
let position = Position::after(generic_param_list.l_angle_token().unwrap());
|
||||||
|
self.insert(position, new_param.syntax());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
// There was no generic param list
|
||||||
|
let position = if let Some(name) = function.name() {
|
||||||
|
Position::after(name.syntax)
|
||||||
|
} else if let Some(fn_token) = function.fn_token() {
|
||||||
|
Position::after(fn_token)
|
||||||
|
} else if let Some(param_list) = function.param_list() {
|
||||||
|
Position::before(param_list.syntax)
|
||||||
|
} else {
|
||||||
|
Position::last_child_of(function.syntax())
|
||||||
|
};
|
||||||
|
let elements = vec![
|
||||||
|
make::token(SyntaxKind::L_ANGLE).into(),
|
||||||
|
new_param.syntax().clone().into(),
|
||||||
|
make::token(SyntaxKind::R_ANGLE).into(),
|
||||||
|
];
|
||||||
|
self.insert_all(position, elements);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue