remove ast_editor.rs
This commit is contained in:
parent
054c53aeb9
commit
05ca252fb5
@ -1,11 +1,11 @@
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast::{self, make, AstNode, NameOwner, TypeBoundsOwner},
|
||||
ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner},
|
||||
SyntaxElement,
|
||||
SyntaxKind::*,
|
||||
};
|
||||
|
||||
use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
|
||||
@ -43,9 +43,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
|
||||
(type_param, without_bounds)
|
||||
});
|
||||
|
||||
let mut ast_editor = AstEditor::new(type_param_list.clone());
|
||||
ast_editor.replace_descendants(new_params);
|
||||
ast_editor.into_text_edit(edit.text_edit_builder());
|
||||
let new_type_param_list = edit::replace_descendants(&type_param_list, new_params);
|
||||
edit.replace_ast(type_param_list.clone(), new_type_param_list);
|
||||
|
||||
let where_clause = {
|
||||
let predicates = type_param_list.type_params().filter_map(build_predicate);
|
||||
|
@ -1,53 +0,0 @@
|
||||
use std::{iter, ops::RangeInclusive};
|
||||
|
||||
use ra_syntax::{
|
||||
algo,
|
||||
ast::{self, TypeBoundsOwner},
|
||||
AstNode, SyntaxElement,
|
||||
};
|
||||
use ra_text_edit::TextEditBuilder;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
pub struct AstEditor<N: AstNode> {
|
||||
original_ast: N,
|
||||
ast: N,
|
||||
}
|
||||
|
||||
impl<N: AstNode> AstEditor<N> {
|
||||
pub fn new(node: N) -> AstEditor<N>
|
||||
where
|
||||
N: Clone,
|
||||
{
|
||||
AstEditor { original_ast: node.clone(), ast: node }
|
||||
}
|
||||
|
||||
pub fn into_text_edit(self, builder: &mut TextEditBuilder) {
|
||||
algo::diff(&self.original_ast.syntax(), self.ast().syntax()).into_text_edit(builder)
|
||||
}
|
||||
|
||||
pub fn ast(&self) -> &N {
|
||||
&self.ast
|
||||
}
|
||||
|
||||
pub fn replace_descendants<T: AstNode>(
|
||||
&mut self,
|
||||
replacement_map: impl Iterator<Item = (T, T)>,
|
||||
) -> &mut Self {
|
||||
let map = replacement_map
|
||||
.map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
|
||||
.collect::<FxHashMap<_, _>>();
|
||||
let new_syntax = algo::replace_descendants(self.ast.syntax(), &map);
|
||||
self.ast = N::cast(new_syntax).unwrap();
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn replace_children(
|
||||
&self,
|
||||
to_delete: RangeInclusive<SyntaxElement>,
|
||||
mut to_insert: impl Iterator<Item = SyntaxElement>,
|
||||
) -> N {
|
||||
let new_syntax = algo::replace_children(self.ast().syntax(), to_delete, &mut to_insert);
|
||||
N::cast(new_syntax).unwrap()
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@
|
||||
|
||||
mod assist_ctx;
|
||||
mod marks;
|
||||
pub mod ast_editor;
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use itertools::Itertools;
|
||||
|
@ -4,6 +4,7 @@
|
||||
use std::{iter, ops::RangeInclusive};
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{
|
||||
algo,
|
||||
@ -216,6 +217,17 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
|
||||
node
|
||||
}
|
||||
|
||||
pub fn replace_descendants<N: AstNode, D: AstNode>(
|
||||
parent: &N,
|
||||
replacement_map: impl Iterator<Item = (D, D)>,
|
||||
) -> N {
|
||||
let map = replacement_map
|
||||
.map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
|
||||
.collect::<FxHashMap<_, _>>();
|
||||
let new_syntax = algo::replace_descendants(parent.syntax(), &map);
|
||||
N::cast(new_syntax).unwrap()
|
||||
}
|
||||
|
||||
// Note this is copy-pasted from fmt. It seems like fmt should be a separate
|
||||
// crate, but basic tree building should be this crate. However, tree building
|
||||
// might want to call into fmt...
|
||||
|
Loading…
Reference in New Issue
Block a user