9902: minor: move functionality to a better place r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-08-14 17:43:52 +00:00 committed by GitHub
commit 933a4dc3a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 30 deletions

View File

@ -13,6 +13,7 @@
ast::{ ast::{
self, self,
edit::{self, AstNodeEdit}, edit::{self, AstNodeEdit},
edit_in_place::AttrsOwnerEdit,
make, ArgListOwner, AttrsOwner, GenericParamsOwner, NameOwner, TypeBoundsOwner, make, ArgListOwner, AttrsOwner, GenericParamsOwner, NameOwner, TypeBoundsOwner,
}, },
ted, AstNode, Direction, SmolStr, ted, AstNode, Direction, SmolStr,
@ -130,7 +131,7 @@ pub fn add_trait_assoc_items_to_impl(
let items = items.into_iter().map(|assoc_item| { let items = items.into_iter().map(|assoc_item| {
let assoc_item = assoc_item.clone_for_update(); let assoc_item = assoc_item.clone_for_update();
transform.apply(assoc_item.syntax()); transform.apply(assoc_item.syntax());
edit::remove_attrs_and_docs(&assoc_item); assoc_item.remove_attrs_and_docs();
assoc_item assoc_item
}); });

View File

@ -34,7 +34,7 @@
use hir::{self, HasAttrs, HasSource}; use hir::{self, HasAttrs, HasSource};
use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind}; use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
use syntax::{ use syntax::{
ast::{self, edit}, ast::{self, edit_in_place::AttrsOwnerEdit},
display::function_declaration, display::function_declaration,
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T, AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T,
}; };
@ -195,7 +195,7 @@ fn get_transformed_assoc_item(
transform.apply(assoc_item.syntax()); transform.apply(assoc_item.syntax());
if let ast::AssocItem::Fn(func) = &assoc_item { if let ast::AssocItem::Fn(func) = &assoc_item {
edit::remove_attrs_and_docs(func) func.remove_attrs_and_docs()
} }
Some(assoc_item) Some(assoc_item)
} }
@ -253,7 +253,7 @@ fn add_const_impl(
} }
fn make_const_compl_syntax(const_: &ast::Const) -> String { fn make_const_compl_syntax(const_: &ast::Const) -> String {
edit::remove_attrs_and_docs(const_); const_.remove_attrs_and_docs();
let const_start = const_.syntax().text_range().start(); let const_start = const_.syntax().text_range().start();
let const_end = const_.syntax().text_range().end(); let const_end = const_.syntax().text_range().end();

View File

@ -6,9 +6,7 @@
use crate::{ use crate::{
algo, algo,
ast::{self, make, AstNode}, ast::{self, make, AstNode},
ted, AstToken, NodeOrToken, SyntaxElement, ted, AstToken, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
SyntaxNode, SyntaxToken,
}; };
impl ast::UseTree { impl ast::UseTree {
@ -48,28 +46,6 @@ fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> {
} }
} }
pub fn remove_attrs_and_docs<N: ast::AttrsOwner>(node: &N) {
remove_attrs_and_docs_inner(node.syntax())
}
fn remove_attrs_and_docs_inner(node: &SyntaxNode) {
let mut remove_next_ws = false;
for child in node.children_with_tokens() {
match child.kind() {
ATTR | COMMENT => {
remove_next_ws = true;
child.detach();
continue;
}
WHITESPACE if remove_next_ws => {
child.detach();
}
_ => (),
}
remove_next_ws = false;
}
}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct IndentLevel(pub u8); pub struct IndentLevel(pub u8);

View File

@ -13,7 +13,9 @@
make, GenericParamsOwner, make, GenericParamsOwner,
}, },
ted::{self, Position}, ted::{self, Position},
AstNode, AstToken, Direction, SyntaxNode, AstNode, AstToken, Direction,
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
SyntaxNode,
}; };
use super::NameOwner; use super::NameOwner;
@ -196,6 +198,32 @@ fn create_generic_param_list(position: Position) -> ast::GenericParamList {
gpl gpl
} }
pub trait AttrsOwnerEdit: ast::AttrsOwner + AstNodeEdit {
fn remove_attrs_and_docs(&self) {
remove_attrs_and_docs(self.syntax());
fn remove_attrs_and_docs(node: &SyntaxNode) {
let mut remove_next_ws = false;
for child in node.children_with_tokens() {
match child.kind() {
ATTR | COMMENT => {
remove_next_ws = true;
child.detach();
continue;
}
WHITESPACE if remove_next_ws => {
child.detach();
}
_ => (),
}
remove_next_ws = false;
}
}
}
}
impl<T: ast::AttrsOwner + AstNodeEdit> AttrsOwnerEdit for T {}
impl ast::GenericParamList { impl ast::GenericParamList {
pub fn add_generic_param(&self, generic_param: ast::GenericParam) { pub fn add_generic_param(&self, generic_param: ast::GenericParam) {
match self.generic_params().last() { match self.generic_params().last() {