From 9cbf09ec4f24aa30af1d9855a909a6cfc67188f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 18 Mar 2021 12:57:55 +0300 Subject: rewrite merge use trees assist to use muatable syntax trees changelog internal --- crates/syntax/src/ted.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'crates/syntax/src/ted.rs') diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs index be2b846b1..177d4ff67 100644 --- a/crates/syntax/src/ted.rs +++ b/crates/syntax/src/ted.rs @@ -2,11 +2,14 @@ //! //! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix //! up elements around the edges. -use std::ops::RangeInclusive; +use std::{mem, ops::RangeInclusive}; use parser::T; -use crate::{ast::make, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken}; +use crate::{ + ast::{edit::IndentLevel, make}, + SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, +}; /// Utility trait to allow calling `ted` functions with references or owned /// nodes. Do not use outside of this module. @@ -101,12 +104,25 @@ pub fn insert_all_raw(position: Position, elements: Vec) { } pub fn remove(elem: impl Element) { - let elem = elem.syntax_element(); - remove_all(elem.clone()..=elem) + elem.syntax_element().detach() } pub fn remove_all(range: RangeInclusive) { replace_all(range, Vec::new()) } +pub fn remove_all_iter(range: impl IntoIterator) { + let mut it = range.into_iter(); + if let Some(mut first) = it.next() { + match it.last() { + Some(mut last) => { + if first.index() > last.index() { + mem::swap(&mut first, &mut last) + } + remove_all(first..=last) + } + None => remove(first), + } + } +} pub fn replace(old: impl Element, new: impl Element) { let old = old.syntax_element(); @@ -149,5 +165,9 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option