From fa20a5064be85349d2d05abcd66f5662d3aecb0c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 20 Apr 2021 02:05:22 +0200 Subject: Remove SyntaxRewriter usage in insert_use in favor of ted --- crates/syntax/src/ast/make.rs | 1 + crates/syntax/src/ted.rs | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 94d4f2cf0..882e9fa09 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -598,6 +598,7 @@ pub mod tokens { SOURCE_FILE .tree() .syntax() + .clone_for_update() .descendants_with_tokens() .filter_map(|it| it.into_token()) .find(|it| it.kind() == WHITESPACE && it.text() == "\n\n") diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs index 450f2e447..91a06101f 100644 --- a/crates/syntax/src/ted.rs +++ b/crates/syntax/src/ted.rs @@ -7,7 +7,7 @@ use std::{mem, ops::RangeInclusive}; use parser::T; use crate::{ - ast::{edit::IndentLevel, make}, + ast::{self, edit::IndentLevel, make, AstNode}, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, }; @@ -147,6 +147,16 @@ pub fn append_child_raw(node: &(impl Into + Clone), child: impl Elem fn ws_before(position: &Position, new: &SyntaxElement) -> Option { let prev = match &position.repr { PositionRepr::FirstChild(_) => return None, + PositionRepr::After(it) if it.kind() == SyntaxKind::L_CURLY => { + if new.kind() == SyntaxKind::USE { + if let Some(item_list) = it.parent().and_then(ast::ItemList::cast) { + let mut indent = IndentLevel::from_element(&item_list.syntax().clone().into()); + indent.0 += 1; + return Some(make::tokens::whitespace(&format!("\n{}", indent))); + } + } + it + } PositionRepr::After(it) => it, }; ws_between(prev, new) @@ -173,7 +183,10 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option Date: Fri, 23 Apr 2021 18:36:43 +0200 Subject: Remove SyntaxRewriter::from_fn --- crates/syntax/src/algo.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/algo.rs b/crates/syntax/src/algo.rs index a153a9e1c..c9229c4e0 100644 --- a/crates/syntax/src/algo.rs +++ b/crates/syntax/src/algo.rs @@ -342,10 +342,10 @@ enum InsertPos { #[derive(Default)] pub struct SyntaxRewriter<'a> { - f: Option Option + 'a>>, //FIXME: add debug_assertions that all elements are in fact from the same file. replacements: FxHashMap, insertions: IndexMap>, + _pd: std::marker::PhantomData<&'a ()>, } impl fmt::Debug for SyntaxRewriter<'_> { @@ -357,14 +357,7 @@ impl fmt::Debug for SyntaxRewriter<'_> { } } -impl<'a> SyntaxRewriter<'a> { - pub fn from_fn(f: impl Fn(&SyntaxElement) -> Option + 'a) -> SyntaxRewriter<'a> { - SyntaxRewriter { - f: Some(Box::new(f)), - replacements: FxHashMap::default(), - insertions: IndexMap::default(), - } - } +impl SyntaxRewriter<'_> { pub fn delete>(&mut self, what: &T) { let what = what.clone().into(); let replacement = Replacement::Delete; @@ -470,7 +463,7 @@ impl<'a> SyntaxRewriter<'a> { pub fn rewrite(&self, node: &SyntaxNode) -> SyntaxNode { let _p = profile::span("rewrite"); - if self.f.is_none() && self.replacements.is_empty() && self.insertions.is_empty() { + if self.replacements.is_empty() && self.insertions.is_empty() { return node.clone(); } let green = self.rewrite_children(node); @@ -495,7 +488,6 @@ impl<'a> SyntaxRewriter<'a> { } } - assert!(self.f.is_none()); self.replacements .keys() .filter_map(element_to_node_or_parent) @@ -510,10 +502,6 @@ impl<'a> SyntaxRewriter<'a> { } fn replacement(&self, element: &SyntaxElement) -> Option { - if let Some(f) = &self.f { - assert!(self.replacements.is_empty()); - return f(element).map(Replacement::Single); - } self.replacements.get(element).cloned() } @@ -574,7 +562,6 @@ fn element_to_green(element: SyntaxElement) -> NodeOrToken { fn add_assign(&mut self, rhs: SyntaxRewriter) { - assert!(rhs.f.is_none()); self.replacements.extend(rhs.replacements); for (pos, insertions) in rhs.insertions.into_iter() { match self.insertions.entry(pos) { -- cgit v1.2.3