From d847d53e36571c8f7925b72cedf66bb203976148 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 26 Sep 2019 22:08:44 +0300 Subject: Start simplifying editing API --- .../src/assists/add_missing_impl_members.rs | 8 +-- crates/ra_assists/src/assists/auto_import.rs | 1 - crates/ra_assists/src/ast_editor.rs | 72 +--------------------- crates/ra_assists/src/lib.rs | 1 - 4 files changed, 4 insertions(+), 78 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 23da1e65f..682455bce 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -100,12 +100,11 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { } fn add_body(fn_def: ast::FnDef) -> ast::FnDef { - let mut ast_editor = AstEditor::new(fn_def.clone()); if fn_def.body().is_none() { - let body = make::block_from_expr(make::expr_unimplemented()); - ast_editor.set_body(&body); + fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) + } else { + fn_def } - ast_editor.ast().to_owned() } /// Given an `ast::ImplBlock`, resolves the target trait (the one being @@ -332,5 +331,4 @@ impl Foo for S { }", ) } - } diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 5aae98546..a91c170b9 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -448,7 +448,6 @@ fn make_assist_add_in_tree_list( fmt_segments_raw(target, &mut buf); edit.insert(offset, buf); } else { - } } diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 2a685f26e..72c8c478a 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -6,7 +6,7 @@ use rustc_hash::FxHashMap; use ra_fmt::leading_indent; use ra_syntax::{ algo, - ast::{self, TypeBoundsOwner}, + ast::{self, make::tokens, TypeBoundsOwner}, AstNode, Direction, InsertPosition, SyntaxElement, SyntaxKind::*, T, @@ -229,26 +229,6 @@ impl AstEditor { } } -impl AstEditor { - pub fn set_body(&mut self, body: &ast::Block) { - let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); - let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.ast().body() { - old_body.syntax().clone().into() - } else if let Some(semi) = self.ast().semicolon_token() { - to_insert.push(tokens::single_space().into()); - semi.into() - } else { - to_insert.push(tokens::single_space().into()); - to_insert.push(body.syntax().clone().into()); - self.ast = self.insert_children(InsertPosition::Last, to_insert.into_iter()); - return; - }; - to_insert.push(body.syntax().clone().into()); - let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi); - self.ast = self.replace_children(replace_range, to_insert.into_iter()) - } -} - impl AstEditor { pub fn remove_bounds(&mut self) -> &mut Self { let colon = match self.ast.colon_token() { @@ -263,53 +243,3 @@ impl AstEditor { self } } - -mod tokens { - use once_cell::sync::Lazy; - use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; - - static SOURCE_FILE: Lazy> = Lazy::new(|| SourceFile::parse(",\n; ;")); - - pub(crate) fn comma() -> SyntaxToken { - SOURCE_FILE - .tree() - .syntax() - .descendants_with_tokens() - .filter_map(|it| it.into_token()) - .find(|it| it.kind() == T![,]) - .unwrap() - } - - pub(crate) fn single_space() -> SyntaxToken { - SOURCE_FILE - .tree() - .syntax() - .descendants_with_tokens() - .filter_map(|it| it.into_token()) - .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") - .unwrap() - } - - #[allow(unused)] - pub(crate) fn single_newline() -> SyntaxToken { - SOURCE_FILE - .tree() - .syntax() - .descendants_with_tokens() - .filter_map(|it| it.into_token()) - .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") - .unwrap() - } - - pub(crate) struct WsBuilder(SourceFile); - - impl WsBuilder { - pub(crate) fn new(text: &str) -> WsBuilder { - WsBuilder(SourceFile::parse(text).ok().unwrap()) - } - pub(crate) fn ws(&self) -> SyntaxToken { - self.0.syntax().first_child_or_token().unwrap().into_token().unwrap() - } - } - -} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 897af2b02..3ca3320f7 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -340,5 +340,4 @@ mod tests { assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable"); assert_eq!(assists.next().expect("expected assist").0.label, "replace with match"); } - } -- cgit v1.2.3