mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-29 06:23:25 +00:00
Create trait Removable
, replace ted
APIs with builder APIs
This commit is contained in:
parent
37e20decad
commit
a695e900f6
8 changed files with 45 additions and 27 deletions
|
@ -5,6 +5,7 @@ use hir::{Adt, Crate, HasAttrs, HasSource, ModuleDef, Semantics};
|
|||
use ide_db::RootDatabase;
|
||||
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
|
||||
use itertools::Itertools;
|
||||
use syntax::ast::edit_in_place::Removable;
|
||||
use syntax::ast::{self, make, AstNode, HasName, MatchArmList, MatchExpr, Pat};
|
||||
|
||||
use crate::{
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
|
||||
use hir::{HasSource, PathResolution};
|
||||
use ide_db::{
|
||||
defs::Definition, imports::insert_use::remove_path_if_in_use_stmt, search::FileReference,
|
||||
defs::Definition, imports::insert_use::ast_to_remove_for_path_in_use_stmt,
|
||||
search::FileReference,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use std::collections::HashMap;
|
||||
|
@ -72,7 +73,10 @@ pub(crate) fn inline_type_alias_uses(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
path_type.syntax().ancestors().nth(3).and_then(ast::PathType::cast)
|
||||
});
|
||||
|
||||
path_type_uses.iter().for_each(remove_path_if_in_use_stmt);
|
||||
path_type_uses
|
||||
.iter()
|
||||
.flat_map(ast_to_remove_for_path_in_use_stmt)
|
||||
.for_each(|x| builder.delete(x.syntax().text_range()));
|
||||
for (target, replacement) in path_types.into_iter().filter_map(|path_type| {
|
||||
let replacement = inline(&ast_alias, &path_type)?.to_text(&concrete_type);
|
||||
let target = path_type.syntax().text_range();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use either::Either;
|
||||
use ide_db::imports::merge_imports::{try_merge_imports, try_merge_trees, MergeBehavior};
|
||||
use syntax::{algo::neighbor, ast, match_ast, ted, AstNode, SyntaxElement, SyntaxNode};
|
||||
use syntax::{algo::neighbor, ast::{self, edit_in_place::Removable}, match_ast, ted, AstNode, SyntaxElement, SyntaxNode};
|
||||
|
||||
use crate::{
|
||||
assist_context::{AssistContext, Assists},
|
||||
|
@ -76,7 +76,7 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
|
|||
.collect();
|
||||
for edit in edits_mut {
|
||||
match edit {
|
||||
Remove(it) => it.as_ref().either(ast::Use::remove, ast::UseTree::remove),
|
||||
Remove(it) => it.as_ref().either(Removable::remove, Removable::remove),
|
||||
Replace(old, new) => ted::replace(old, new),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use syntax::{
|
||||
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasName, HasTypeBounds},
|
||||
ast::{self, edit_in_place::{GenericParamsOwnerEdit, Removable}, make, AstNode, HasName, HasTypeBounds},
|
||||
match_ast,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use syntax::{
|
||||
ast::{self, make, HasVisibility},
|
||||
ast::{self, make, HasVisibility, edit_in_place::Removable},
|
||||
ted::{self, Position},
|
||||
AstNode, SyntaxKind,
|
||||
};
|
||||
|
|
|
@ -12,7 +12,7 @@ use syntax::{
|
|||
ast::{
|
||||
self,
|
||||
edit::{self, AstNodeEdit},
|
||||
edit_in_place::AttrsOwnerEdit,
|
||||
edit_in_place::{AttrsOwnerEdit, Removable},
|
||||
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
|
||||
},
|
||||
ted, AstNode, AstToken, Direction, SmolStr, SourceFile,
|
||||
|
|
|
@ -7,7 +7,10 @@ use std::cmp::Ordering;
|
|||
use hir::Semantics;
|
||||
use syntax::{
|
||||
algo,
|
||||
ast::{self, make, AstNode, HasAttrs, HasModuleItem, HasVisibility, PathSegmentKind},
|
||||
ast::{
|
||||
self, edit_in_place::Removable, make, AstNode, HasAttrs, HasModuleItem, HasVisibility,
|
||||
PathSegmentKind,
|
||||
},
|
||||
ted, Direction, NodeOrToken, SyntaxKind, SyntaxNode,
|
||||
};
|
||||
|
||||
|
@ -192,20 +195,24 @@ pub fn insert_use(scope: &ImportScope, path: ast::Path, cfg: &InsertUseConfig) {
|
|||
insert_use_(scope, &path, cfg.group, use_item);
|
||||
}
|
||||
|
||||
pub fn remove_path_if_in_use_stmt(path: &ast::Path) {
|
||||
pub fn ast_to_remove_for_path_in_use_stmt(path: &ast::Path) -> Option<Box<dyn Removable>> {
|
||||
// FIXME: improve this
|
||||
if path.parent_path().is_some() {
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
if let Some(use_tree) = path.syntax().parent().and_then(ast::UseTree::cast) {
|
||||
if use_tree.use_tree_list().is_some() || use_tree.star_token().is_some() {
|
||||
return;
|
||||
}
|
||||
if let Some(use_) = use_tree.syntax().parent().and_then(ast::Use::cast) {
|
||||
use_.remove();
|
||||
return;
|
||||
}
|
||||
use_tree.remove();
|
||||
let use_tree = path.syntax().parent().and_then(ast::UseTree::cast)?;
|
||||
if use_tree.use_tree_list().is_some() || use_tree.star_token().is_some() {
|
||||
return None;
|
||||
}
|
||||
if let Some(use_) = use_tree.syntax().parent().and_then(ast::Use::cast) {
|
||||
return Some(Box::new(use_));
|
||||
}
|
||||
Some(Box::new(use_tree))
|
||||
}
|
||||
|
||||
pub fn remove_path_if_in_use_stmt(path: &ast::Path) {
|
||||
if let Some(node) = ast_to_remove_for_path_in_use_stmt(path) {
|
||||
node.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -248,8 +248,12 @@ impl ast::WhereClause {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::TypeBoundList {
|
||||
pub fn remove(&self) {
|
||||
pub trait Removable : AstNode {
|
||||
fn remove(&self);
|
||||
}
|
||||
|
||||
impl Removable for ast::TypeBoundList {
|
||||
fn remove(&self) {
|
||||
match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
|
||||
Some(colon) => ted::remove_all(colon..=self.syntax().clone().into()),
|
||||
None => ted::remove(self.syntax()),
|
||||
|
@ -267,8 +271,8 @@ impl ast::PathSegment {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::UseTree {
|
||||
pub fn remove(&self) {
|
||||
impl Removable for ast::UseTree {
|
||||
fn remove(&self) {
|
||||
for dir in [Direction::Next, Direction::Prev] {
|
||||
if let Some(next_use_tree) = neighbor(self, dir) {
|
||||
let separators = self
|
||||
|
@ -282,7 +286,9 @@ impl ast::UseTree {
|
|||
}
|
||||
ted::remove(self.syntax());
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::UseTree {
|
||||
pub fn get_or_create_use_tree_list(&self) -> ast::UseTreeList {
|
||||
match self.use_tree_list() {
|
||||
Some(it) => it,
|
||||
|
@ -373,8 +379,8 @@ impl ast::UseTreeList {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::Use {
|
||||
pub fn remove(&self) {
|
||||
impl Removable for ast::Use {
|
||||
fn remove(&self) {
|
||||
let next_ws = self
|
||||
.syntax()
|
||||
.next_sibling_or_token()
|
||||
|
@ -444,8 +450,8 @@ impl ast::Fn {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::MatchArm {
|
||||
pub fn remove(&self) {
|
||||
impl Removable for ast::MatchArm {
|
||||
fn remove(&self) {
|
||||
if let Some(sibling) = self.syntax().prev_sibling_or_token() {
|
||||
if sibling.kind() == SyntaxKind::WHITESPACE {
|
||||
ted::remove(sibling);
|
||||
|
|
Loading…
Reference in a new issue