diff options
Diffstat (limited to 'crates/syntax/src/ted.rs')
-rw-r--r-- | crates/syntax/src/ted.rs | 28 |
1 files changed, 24 insertions, 4 deletions
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 @@ | |||
2 | //! | 2 | //! |
3 | //! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix | 3 | //! The `_raw`-suffixed functions insert elements as is, unsuffixed versions fix |
4 | //! up elements around the edges. | 4 | //! up elements around the edges. |
5 | use std::ops::RangeInclusive; | 5 | use std::{mem, ops::RangeInclusive}; |
6 | 6 | ||
7 | use parser::T; | 7 | use parser::T; |
8 | 8 | ||
9 | use crate::{ast::make, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken}; | 9 | use crate::{ |
10 | ast::{edit::IndentLevel, make}, | ||
11 | SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, | ||
12 | }; | ||
10 | 13 | ||
11 | /// Utility trait to allow calling `ted` functions with references or owned | 14 | /// Utility trait to allow calling `ted` functions with references or owned |
12 | /// nodes. Do not use outside of this module. | 15 | /// nodes. Do not use outside of this module. |
@@ -101,12 +104,25 @@ pub fn insert_all_raw(position: Position, elements: Vec<SyntaxElement>) { | |||
101 | } | 104 | } |
102 | 105 | ||
103 | pub fn remove(elem: impl Element) { | 106 | pub fn remove(elem: impl Element) { |
104 | let elem = elem.syntax_element(); | 107 | elem.syntax_element().detach() |
105 | remove_all(elem.clone()..=elem) | ||
106 | } | 108 | } |
107 | pub fn remove_all(range: RangeInclusive<SyntaxElement>) { | 109 | pub fn remove_all(range: RangeInclusive<SyntaxElement>) { |
108 | replace_all(range, Vec::new()) | 110 | replace_all(range, Vec::new()) |
109 | } | 111 | } |
112 | pub fn remove_all_iter(range: impl IntoIterator<Item = SyntaxElement>) { | ||
113 | let mut it = range.into_iter(); | ||
114 | if let Some(mut first) = it.next() { | ||
115 | match it.last() { | ||
116 | Some(mut last) => { | ||
117 | if first.index() > last.index() { | ||
118 | mem::swap(&mut first, &mut last) | ||
119 | } | ||
120 | remove_all(first..=last) | ||
121 | } | ||
122 | None => remove(first), | ||
123 | } | ||
124 | } | ||
125 | } | ||
110 | 126 | ||
111 | pub fn replace(old: impl Element, new: impl Element) { | 127 | pub fn replace(old: impl Element, new: impl Element) { |
112 | let old = old.syntax_element(); | 128 | let old = old.syntax_element(); |
@@ -149,5 +165,9 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option<SyntaxToken | |||
149 | if right.kind() == T![;] || right.kind() == T![,] { | 165 | if right.kind() == T![;] || right.kind() == T![,] { |
150 | return None; | 166 | return None; |
151 | } | 167 | } |
168 | if right.kind() == SyntaxKind::USE { | ||
169 | let indent = IndentLevel::from_element(left); | ||
170 | return Some(make::tokens::whitespace(&format!("\n{}", indent))); | ||
171 | } | ||
152 | Some(make::tokens::single_space()) | 172 | Some(make::tokens::single_space()) |
153 | } | 173 | } |