mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 05:08:52 +00:00
refactor: move EditorRemovable trait to syntax_editor
Signed-off-by: Tarek <tareknaser360@gmail.com>
This commit is contained in:
parent
e511e182f0
commit
59cfb84379
3 changed files with 8 additions and 73 deletions
|
@ -6,8 +6,10 @@ use ide_db::imports::{
|
|||
use itertools::Itertools;
|
||||
use syntax::{
|
||||
algo::neighbor,
|
||||
ast::{self, edit_in_place::EditorRemovable, syntax_factory::SyntaxFactory},
|
||||
match_ast, AstNode, SyntaxElement, SyntaxNode,
|
||||
ast::{self, syntax_factory::SyntaxFactory},
|
||||
match_ast,
|
||||
syntax_editor::edits::Removable,
|
||||
AstNode, SyntaxElement, SyntaxNode,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -68,7 +70,6 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
|
|||
(selection_range, edits?)
|
||||
};
|
||||
|
||||
// FIXME: Is this the best to get a `SyntaxNode` object? We need one for `SourceChangeBuilder::make_editor`.
|
||||
let parent_node = match ctx.covering_element() {
|
||||
SyntaxElement::Node(n) => n,
|
||||
SyntaxElement::Token(t) => t.parent()?,
|
||||
|
|
|
@ -6,11 +6,7 @@ use parser::{SyntaxKind, T};
|
|||
|
||||
use crate::{
|
||||
algo::{self, neighbor},
|
||||
ast::{
|
||||
self, edit::IndentLevel, make, syntax_factory::SyntaxFactory, HasGenericArgs,
|
||||
HasGenericParams,
|
||||
},
|
||||
syntax_editor::SyntaxEditor,
|
||||
ast::{self, edit::IndentLevel, make, HasGenericArgs, HasGenericParams},
|
||||
ted::{self, Position},
|
||||
AstNode, AstToken, Direction, SyntaxElement,
|
||||
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
|
||||
|
@ -389,10 +385,6 @@ pub trait Removable: AstNode {
|
|||
fn remove(&self);
|
||||
}
|
||||
|
||||
pub trait EditorRemovable: AstNode {
|
||||
fn remove(&self, editor: &mut SyntaxEditor);
|
||||
}
|
||||
|
||||
impl Removable for ast::TypeBoundList {
|
||||
fn remove(&self) {
|
||||
match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
|
||||
|
@ -447,35 +439,16 @@ impl Removable for ast::UseTree {
|
|||
}
|
||||
}
|
||||
|
||||
impl EditorRemovable for ast::UseTree {
|
||||
fn remove(&self, editor: &mut SyntaxEditor) {
|
||||
for dir in [Direction::Next, Direction::Prev] {
|
||||
if let Some(next_use_tree) = neighbor(self, dir) {
|
||||
let separators = self
|
||||
.syntax()
|
||||
.siblings_with_tokens(dir)
|
||||
.skip(1)
|
||||
.take_while(|it| it.as_node() != Some(next_use_tree.syntax()));
|
||||
for sep in separators {
|
||||
editor.delete(sep);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
editor.delete(self.syntax());
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::UseTree {
|
||||
/// Deletes the usetree node represented by the input. Recursively removes parents, including use nodes that become empty.
|
||||
pub fn remove_recursive(self) {
|
||||
let parent = self.syntax().parent();
|
||||
|
||||
Removable::remove(&self);
|
||||
self.remove();
|
||||
|
||||
if let Some(u) = parent.clone().and_then(ast::Use::cast) {
|
||||
if u.use_tree().is_none() {
|
||||
Removable::remove(&u);
|
||||
u.remove()
|
||||
}
|
||||
} else if let Some(u) = parent.and_then(ast::UseTreeList::cast) {
|
||||
if u.use_trees().next().is_none() {
|
||||
|
@ -643,45 +616,6 @@ impl Removable for ast::Use {
|
|||
}
|
||||
}
|
||||
|
||||
impl EditorRemovable for ast::Use {
|
||||
fn remove(&self, editor: &mut SyntaxEditor) {
|
||||
let make = SyntaxFactory::new();
|
||||
|
||||
let next_ws = self
|
||||
.syntax()
|
||||
.next_sibling_or_token()
|
||||
.and_then(|it| it.into_token())
|
||||
.and_then(ast::Whitespace::cast);
|
||||
if let Some(next_ws) = next_ws {
|
||||
let ws_text = next_ws.syntax().text();
|
||||
if let Some(rest) = ws_text.strip_prefix('\n') {
|
||||
if rest.is_empty() {
|
||||
editor.delete(next_ws.syntax());
|
||||
} else {
|
||||
editor.replace(next_ws.syntax(), make.whitespace(rest));
|
||||
}
|
||||
}
|
||||
}
|
||||
let prev_ws = self
|
||||
.syntax()
|
||||
.prev_sibling_or_token()
|
||||
.and_then(|it| it.into_token())
|
||||
.and_then(ast::Whitespace::cast);
|
||||
if let Some(prev_ws) = prev_ws {
|
||||
let ws_text = prev_ws.syntax().text();
|
||||
let prev_newline = ws_text.rfind('\n').map(|x| x + 1).unwrap_or(0);
|
||||
let rest = &ws_text[0..prev_newline];
|
||||
if rest.is_empty() {
|
||||
editor.delete(prev_ws.syntax());
|
||||
} else {
|
||||
editor.replace(prev_ws.syntax(), make.whitespace(rest));
|
||||
}
|
||||
}
|
||||
|
||||
editor.delete(self.syntax());
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Impl {
|
||||
pub fn get_or_create_assoc_item_list(&self) -> ast::AssocItemList {
|
||||
if self.assoc_item_list().is_none() {
|
||||
|
|
|
@ -16,7 +16,7 @@ use rustc_hash::FxHashMap;
|
|||
use crate::{SyntaxElement, SyntaxNode, SyntaxToken};
|
||||
|
||||
mod edit_algo;
|
||||
mod edits;
|
||||
pub mod edits;
|
||||
mod mapping;
|
||||
|
||||
pub use mapping::{SyntaxMapping, SyntaxMappingBuilder};
|
||||
|
|
Loading…
Reference in a new issue