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 - crates/ra_ide_api/src/line_index.rs | 1 - crates/ra_ide_api/src/runnables.rs | 1 - crates/ra_lsp_server/src/markdown.rs | 1 - crates/ra_parser/src/grammar.rs | 1 - crates/ra_syntax/Cargo.toml | 2 + crates/ra_syntax/src/ast.rs | 1 + crates/ra_syntax/src/ast/edit.rs | 52 ++++++++++++++++ crates/ra_syntax/src/ast/make.rs | 48 +++++++++++++++ 12 files changed, 107 insertions(+), 82 deletions(-) create mode 100644 crates/ra_syntax/src/ast/edit.rs (limited to 'crates') 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"); } - } diff --git a/crates/ra_ide_api/src/line_index.rs b/crates/ra_ide_api/src/line_index.rs index 71de8a928..5fedad696 100644 --- a/crates/ra_ide_api/src/line_index.rs +++ b/crates/ra_ide_api/src/line_index.rs @@ -278,5 +278,4 @@ const C: char = \"メ メ\"; assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); } - } diff --git a/crates/ra_ide_api/src/runnables.rs b/crates/ra_ide_api/src/runnables.rs index 095ca56c4..8cf58fe79 100644 --- a/crates/ra_ide_api/src/runnables.rs +++ b/crates/ra_ide_api/src/runnables.rs @@ -229,5 +229,4 @@ mod tests { let runnables = analysis.runnables(pos.file_id).unwrap(); assert!(runnables.is_empty()) } - } diff --git a/crates/ra_lsp_server/src/markdown.rs b/crates/ra_lsp_server/src/markdown.rs index c1eb0236a..3659edf8e 100644 --- a/crates/ra_lsp_server/src/markdown.rs +++ b/crates/ra_lsp_server/src/markdown.rs @@ -70,5 +70,4 @@ let a = 1; "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```" ); } - } diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index e2355aff9..6e9e212b7 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -135,7 +135,6 @@ pub(crate) mod fragments { m.complete(p, MACRO_STMTS); } - } pub(crate) fn reparser( diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index 724c38e17..9bc85404a 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -12,6 +12,8 @@ itertools = "0.8.0" rowan = "0.6.1" rustc_lexer = "0.1.0" rustc-hash = "1.0.1" +arrayvec = "0.4.10" +once_cell = "1.2.0" # ideally, `serde` should be enabled by `ra_lsp_server`, but we enable it here # to reduce number of compilations diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index f464d6534..fdffd8cb1 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -5,6 +5,7 @@ mod traits; mod tokens; mod extensions; mod expr_extensions; +mod edit; pub mod make; use std::marker::PhantomData; diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs new file mode 100644 index 000000000..c65899812 --- /dev/null +++ b/crates/ra_syntax/src/ast/edit.rs @@ -0,0 +1,52 @@ +//! This module contains functions for editing syntax trees. As the trees are +//! immutable, all function here return a fresh copy of the tree, instead of +//! doing an in-place modification. + +use arrayvec::ArrayVec; +use std::ops::RangeInclusive; + +use crate::{ + algo, + ast::{self, make, AstNode}, + InsertPosition, SyntaxElement, +}; + +impl ast::FnDef { + #[must_use] + pub fn with_body(&self, body: ast::Block) -> ast::FnDef { + let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); + let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { + old_body.syntax().clone().into() + } else if let Some(semi) = self.semicolon_token() { + to_insert.push(make::tokens::single_space().into()); + semi.into() + } else { + to_insert.push(make::tokens::single_space().into()); + to_insert.push(body.syntax().clone().into()); + return insert_children(self, InsertPosition::Last, to_insert.into_iter()); + }; + to_insert.push(body.syntax().clone().into()); + let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi); + replace_children(self, replace_range, to_insert.into_iter()) + } +} + +#[must_use] +fn insert_children( + parent: &N, + position: InsertPosition, + mut to_insert: impl Iterator, +) -> N { + let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert); + N::cast(new_syntax).unwrap() +} + +#[must_use] +fn replace_children( + parent: &N, + to_replace: RangeInclusive, + mut to_insert: impl Iterator, +) -> N { + let new_syntax = algo::replace_children(parent.syntax(), to_replace, &mut to_insert); + N::cast(new_syntax).unwrap() +} diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index c06c62b3b..287a40bee 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -133,3 +133,51 @@ fn ast_from_text(text: &str) -> N { let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); res } + +pub mod tokens { + use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; + use once_cell::sync::Lazy; + + static SOURCE_FILE: Lazy> = Lazy::new(|| SourceFile::parse(",\n; ;")); + + pub fn comma() -> SyntaxToken { + SOURCE_FILE + .tree() + .syntax() + .descendants_with_tokens() + .filter_map(|it| it.into_token()) + .find(|it| it.kind() == T![,]) + .unwrap() + } + + pub 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() + } + + pub 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 struct WsBuilder(SourceFile); + + impl WsBuilder { + pub fn new(text: &str) -> WsBuilder { + WsBuilder(SourceFile::parse(text).ok().unwrap()) + } + pub fn ws(&self) -> SyntaxToken { + self.0.syntax().first_child_or_token().unwrap().into_token().unwrap() + } + } +} -- cgit v1.2.3