diff options
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r-- | crates/syntax/src/ast/edit.rs | 46 | ||||
-rw-r--r-- | crates/syntax/src/ast/edit_in_place.rs | 44 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 2 |
3 files changed, 50 insertions, 42 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 347862b8a..18820786a 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs | |||
@@ -9,7 +9,7 @@ use std::{ | |||
9 | use arrayvec::ArrayVec; | 9 | use arrayvec::ArrayVec; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | algo::{self, neighbor, SyntaxRewriter}, | 12 | algo::{self, SyntaxRewriter}, |
13 | ast::{ | 13 | ast::{ |
14 | self, | 14 | self, |
15 | make::{self, tokens}, | 15 | make::{self, tokens}, |
@@ -322,27 +322,6 @@ impl ast::Use { | |||
322 | } | 322 | } |
323 | self.clone() | 323 | self.clone() |
324 | } | 324 | } |
325 | |||
326 | pub fn remove(&self) -> SyntaxRewriter<'static> { | ||
327 | let mut res = SyntaxRewriter::default(); | ||
328 | res.delete(self.syntax()); | ||
329 | let next_ws = self | ||
330 | .syntax() | ||
331 | .next_sibling_or_token() | ||
332 | .and_then(|it| it.into_token()) | ||
333 | .and_then(ast::Whitespace::cast); | ||
334 | if let Some(next_ws) = next_ws { | ||
335 | let ws_text = next_ws.syntax().text(); | ||
336 | if let Some(rest) = ws_text.strip_prefix('\n') { | ||
337 | if rest.is_empty() { | ||
338 | res.delete(next_ws.syntax()) | ||
339 | } else { | ||
340 | res.replace(next_ws.syntax(), &make::tokens::whitespace(rest)); | ||
341 | } | ||
342 | } | ||
343 | } | ||
344 | res | ||
345 | } | ||
346 | } | 325 | } |
347 | 326 | ||
348 | impl ast::UseTree { | 327 | impl ast::UseTree { |
@@ -396,22 +375,6 @@ impl ast::UseTree { | |||
396 | Some(res) | 375 | Some(res) |
397 | } | 376 | } |
398 | } | 377 | } |
399 | |||
400 | pub fn remove(&self) -> SyntaxRewriter<'static> { | ||
401 | let mut res = SyntaxRewriter::default(); | ||
402 | res.delete(self.syntax()); | ||
403 | for &dir in [Direction::Next, Direction::Prev].iter() { | ||
404 | if let Some(nb) = neighbor(self, dir) { | ||
405 | self.syntax() | ||
406 | .siblings_with_tokens(dir) | ||
407 | .skip(1) | ||
408 | .take_while(|it| it.as_node() != Some(nb.syntax())) | ||
409 | .for_each(|el| res.delete(&el)); | ||
410 | return res; | ||
411 | } | ||
412 | } | ||
413 | res | ||
414 | } | ||
415 | } | 378 | } |
416 | 379 | ||
417 | impl ast::MatchArmList { | 380 | impl ast::MatchArmList { |
@@ -592,6 +555,13 @@ impl ops::Add<u8> for IndentLevel { | |||
592 | } | 555 | } |
593 | 556 | ||
594 | impl IndentLevel { | 557 | impl IndentLevel { |
558 | pub fn from_element(element: &SyntaxElement) -> IndentLevel { | ||
559 | match element { | ||
560 | rowan::NodeOrToken::Node(it) => IndentLevel::from_node(it), | ||
561 | rowan::NodeOrToken::Token(it) => IndentLevel::from_token(it), | ||
562 | } | ||
563 | } | ||
564 | |||
595 | pub fn from_node(node: &SyntaxNode) -> IndentLevel { | 565 | pub fn from_node(node: &SyntaxNode) -> IndentLevel { |
596 | match node.first_token() { | 566 | match node.first_token() { |
597 | Some(it) => Self::from_token(&it), | 567 | Some(it) => Self::from_token(&it), |
diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index b1eed0a2c..529bd0eb1 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs | |||
@@ -2,13 +2,13 @@ | |||
2 | 2 | ||
3 | use std::iter::empty; | 3 | use std::iter::empty; |
4 | 4 | ||
5 | use ast::{edit::AstNodeEdit, make, GenericParamsOwner, WhereClause}; | ||
6 | use parser::T; | 5 | use parser::T; |
7 | 6 | ||
8 | use crate::{ | 7 | use crate::{ |
9 | ast, | 8 | algo::neighbor, |
9 | ast::{self, edit::AstNodeEdit, make, GenericParamsOwner, WhereClause}, | ||
10 | ted::{self, Position}, | 10 | ted::{self, Position}, |
11 | AstNode, Direction, | 11 | AstNode, AstToken, Direction, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | use super::NameOwner; | 14 | use super::NameOwner; |
@@ -126,3 +126,41 @@ impl ast::TypeBoundList { | |||
126 | } | 126 | } |
127 | } | 127 | } |
128 | } | 128 | } |
129 | |||
130 | impl ast::UseTree { | ||
131 | pub fn remove(&self) { | ||
132 | for &dir in [Direction::Next, Direction::Prev].iter() { | ||
133 | if let Some(next_use_tree) = neighbor(self, dir) { | ||
134 | let separators = self | ||
135 | .syntax() | ||
136 | .siblings_with_tokens(dir) | ||
137 | .skip(1) | ||
138 | .take_while(|it| it.as_node() != Some(next_use_tree.syntax())); | ||
139 | ted::remove_all_iter(separators); | ||
140 | break; | ||
141 | } | ||
142 | } | ||
143 | ted::remove(self.syntax()) | ||
144 | } | ||
145 | } | ||
146 | |||
147 | impl ast::Use { | ||
148 | pub fn remove(&self) { | ||
149 | let next_ws = self | ||
150 | .syntax() | ||
151 | .next_sibling_or_token() | ||
152 | .and_then(|it| it.into_token()) | ||
153 | .and_then(ast::Whitespace::cast); | ||
154 | if let Some(next_ws) = next_ws { | ||
155 | let ws_text = next_ws.syntax().text(); | ||
156 | if let Some(rest) = ws_text.strip_prefix('\n') { | ||
157 | if rest.is_empty() { | ||
158 | ted::remove(next_ws.syntax()) | ||
159 | } else { | ||
160 | ted::replace(next_ws.syntax(), make::tokens::whitespace(rest)) | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | ted::remove(self.syntax()) | ||
165 | } | ||
166 | } | ||
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 7049affd9..c08f2c14f 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -560,7 +560,7 @@ pub mod tokens { | |||
560 | pub fn whitespace(text: &str) -> SyntaxToken { | 560 | pub fn whitespace(text: &str) -> SyntaxToken { |
561 | assert!(text.trim().is_empty()); | 561 | assert!(text.trim().is_empty()); |
562 | let sf = SourceFile::parse(text).ok().unwrap(); | 562 | let sf = SourceFile::parse(text).ok().unwrap(); |
563 | sf.syntax().first_child_or_token().unwrap().into_token().unwrap() | 563 | sf.syntax().clone_for_update().first_child_or_token().unwrap().into_token().unwrap() |
564 | } | 564 | } |
565 | 565 | ||
566 | pub fn doc_comment(text: &str) -> SyntaxToken { | 566 | pub fn doc_comment(text: &str) -> SyntaxToken { |