aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-09-28 17:50:16 +0100
committerAleksey Kladov <[email protected]>2019-09-28 17:50:16 +0100
commit5dbbfda34ae423229487595fd0ae9e727ae42906 (patch)
tree647be92788a5d338bcfbb088ffb4e56582ad3367 /crates
parentdbdf0e24d51ce425c0066a76a0efc723e41e5071 (diff)
simplify strip attrs
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs12
-rw-r--r--crates/ra_assists/src/ast_editor.rs17
-rw-r--r--crates/ra_syntax/src/ast.rs2
-rw-r--r--crates/ra_syntax/src/ast/edit.rs21
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs12
5 files changed, 35 insertions, 29 deletions
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 682455bce..3fce4a5b7 100644
--- a/crates/ra_assists/src/assists/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs
@@ -1,6 +1,6 @@
1use hir::{db::HirDatabase, HasSource}; 1use hir::{db::HirDatabase, HasSource};
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, make, AstNode, NameOwner}, 3 ast::{self, edit, make, AstNode, NameOwner},
4 SmolStr, 4 SmolStr,
5}; 5};
6 6
@@ -76,8 +76,8 @@ fn add_missing_impl_members_inner(
76 ctx.add_action(AssistId(assist_id), label, |edit| { 76 ctx.add_action(AssistId(assist_id), label, |edit| {
77 let n_existing_items = impl_item_list.impl_items().count(); 77 let n_existing_items = impl_item_list.impl_items().count();
78 let items = missing_items.into_iter().map(|it| match it { 78 let items = missing_items.into_iter().map(|it| match it {
79 ast::ImplItem::FnDef(def) => strip_docstring(add_body(def).into()), 79 ast::ImplItem::FnDef(def) => edit::strip_attrs_and_docs(add_body(def).into()),
80 _ => strip_docstring(it), 80 _ => edit::strip_attrs_and_docs(it),
81 }); 81 });
82 let mut ast_editor = AstEditor::new(impl_item_list); 82 let mut ast_editor = AstEditor::new(impl_item_list);
83 83
@@ -93,12 +93,6 @@ fn add_missing_impl_members_inner(
93 ctx.build() 93 ctx.build()
94} 94}
95 95
96fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem {
97 let mut ast_editor = AstEditor::new(item);
98 ast_editor.strip_attrs_and_docs();
99 ast_editor.ast().to_owned()
100}
101
102fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 96fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
103 if fn_def.body().is_none() { 97 if fn_def.body().is_none() {
104 fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) 98 fn_def.with_body(make::block_from_expr(make::expr_unimplemented()))
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs
index 72c8c478a..60b8923e1 100644
--- a/crates/ra_assists/src/ast_editor.rs
+++ b/crates/ra_assists/src/ast_editor.rs
@@ -212,23 +212,6 @@ impl AstEditor<ast::ItemList> {
212 } 212 }
213} 213}
214 214
215impl AstEditor<ast::ImplItem> {
216 pub fn strip_attrs_and_docs(&mut self) {
217 while let Some(start) = self
218 .ast()
219 .syntax()
220 .children_with_tokens()
221 .find(|it| it.kind() == ATTR || it.kind() == COMMENT)
222 {
223 let end = match &start.next_sibling_or_token() {
224 Some(el) if el.kind() == WHITESPACE => el.clone(),
225 Some(_) | None => start.clone(),
226 };
227 self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty());
228 }
229 }
230}
231
232impl AstEditor<ast::TypeParam> { 215impl AstEditor<ast::TypeParam> {
233 pub fn remove_bounds(&mut self) -> &mut Self { 216 pub fn remove_bounds(&mut self) -> &mut Self {
234 let colon = match self.ast.colon_token() { 217 let colon = match self.ast.colon_token() {
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index fdffd8cb1..1b2ce921a 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -5,7 +5,7 @@ mod traits;
5mod tokens; 5mod tokens;
6mod extensions; 6mod extensions;
7mod expr_extensions; 7mod expr_extensions;
8mod edit; 8pub mod edit;
9pub mod make; 9pub mod make;
10 10
11use std::marker::PhantomData; 11use std::marker::PhantomData;
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index c65899812..7013cc9b5 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -1,14 +1,16 @@
1//! This module contains functions for editing syntax trees. As the trees are 1//! This module contains functions for editing syntax trees. As the trees are
2//! immutable, all function here return a fresh copy of the tree, instead of 2//! immutable, all function here return a fresh copy of the tree, instead of
3//! doing an in-place modification. 3//! doing an in-place modification.
4use std::{iter, ops::RangeInclusive};
4 5
5use arrayvec::ArrayVec; 6use arrayvec::ArrayVec;
6use std::ops::RangeInclusive;
7 7
8use crate::{ 8use crate::{
9 algo, 9 algo,
10 ast::{self, make, AstNode}, 10 ast::{self, make, AstNode},
11 InsertPosition, SyntaxElement, 11 InsertPosition, SyntaxElement,
12 SyntaxKind::{ATTR, COMMENT, WHITESPACE},
13 SyntaxNode,
12}; 14};
13 15
14impl ast::FnDef { 16impl ast::FnDef {
@@ -31,6 +33,23 @@ impl ast::FnDef {
31 } 33 }
32} 34}
33 35
36pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: N) -> N {
37 N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap()
38}
39
40fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
41 while let Some(start) =
42 node.children_with_tokens().find(|it| it.kind() == ATTR || it.kind() == COMMENT)
43 {
44 let end = match &start.next_sibling_or_token() {
45 Some(el) if el.kind() == WHITESPACE => el.clone(),
46 Some(_) | None => start.clone(),
47 };
48 node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty());
49 }
50 node
51}
52
34#[must_use] 53#[must_use]
35fn insert_children<N: AstNode>( 54fn insert_children<N: AstNode>(
36 parent: &N, 55 parent: &N,
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 0433edb84..8c5ece65d 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -4,7 +4,7 @@
4use itertools::Itertools; 4use itertools::Itertools;
5 5
6use crate::{ 6use crate::{
7 ast::{self, child_opt, children, AstNode, SyntaxNode}, 7 ast::{self, child_opt, children, AstChildren, AstNode, SyntaxNode},
8 SmolStr, SyntaxElement, 8 SmolStr, SyntaxElement,
9 SyntaxKind::*, 9 SyntaxKind::*,
10 SyntaxToken, T, 10 SyntaxToken, T,
@@ -203,6 +203,16 @@ impl ast::ImplBlock {
203 } 203 }
204} 204}
205 205
206impl ast::AttrsOwner for ast::ImplItem {
207 fn attrs(&self) -> AstChildren<ast::Attr> {
208 match self {
209 ast::ImplItem::FnDef(it) => it.attrs(),
210 ast::ImplItem::TypeAliasDef(it) => it.attrs(),
211 ast::ImplItem::ConstDef(it) => it.attrs(),
212 }
213 }
214}
215
206#[derive(Debug, Clone, PartialEq, Eq)] 216#[derive(Debug, Clone, PartialEq, Eq)]
207pub enum StructKind { 217pub enum StructKind {
208 Tuple(ast::TupleFieldDefList), 218 Tuple(ast::TupleFieldDefList),