diff options
Diffstat (limited to 'crates/syntax/src/ast/edit.rs')
-rw-r--r-- | crates/syntax/src/ast/edit.rs | 52 |
1 files changed, 10 insertions, 42 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 64fac13a7..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,28 +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 ws_text.starts_with('\n') { | ||
337 | let rest = &ws_text[1..]; | ||
338 | if rest.is_empty() { | ||
339 | res.delete(next_ws.syntax()) | ||
340 | } else { | ||
341 | res.replace(next_ws.syntax(), &make::tokens::whitespace(rest)); | ||
342 | } | ||
343 | } | ||
344 | } | ||
345 | res | ||
346 | } | ||
347 | } | 325 | } |
348 | 326 | ||
349 | impl ast::UseTree { | 327 | impl ast::UseTree { |
@@ -397,22 +375,6 @@ impl ast::UseTree { | |||
397 | Some(res) | 375 | Some(res) |
398 | } | 376 | } |
399 | } | 377 | } |
400 | |||
401 | pub fn remove(&self) -> SyntaxRewriter<'static> { | ||
402 | let mut res = SyntaxRewriter::default(); | ||
403 | res.delete(self.syntax()); | ||
404 | for &dir in [Direction::Next, Direction::Prev].iter() { | ||
405 | if let Some(nb) = neighbor(self, dir) { | ||
406 | self.syntax() | ||
407 | .siblings_with_tokens(dir) | ||
408 | .skip(1) | ||
409 | .take_while(|it| it.as_node() != Some(nb.syntax())) | ||
410 | .for_each(|el| res.delete(&el)); | ||
411 | return res; | ||
412 | } | ||
413 | } | ||
414 | res | ||
415 | } | ||
416 | } | 378 | } |
417 | 379 | ||
418 | impl ast::MatchArmList { | 380 | impl ast::MatchArmList { |
@@ -462,8 +424,7 @@ impl ast::MatchArmList { | |||
462 | let end = if let Some(comma) = start | 424 | let end = if let Some(comma) = start |
463 | .siblings_with_tokens(Direction::Next) | 425 | .siblings_with_tokens(Direction::Next) |
464 | .skip(1) | 426 | .skip(1) |
465 | .skip_while(|it| it.kind().is_trivia()) | 427 | .find(|it| !it.kind().is_trivia()) |
466 | .next() | ||
467 | .filter(|it| it.kind() == T![,]) | 428 | .filter(|it| it.kind() == T![,]) |
468 | { | 429 | { |
469 | comma | 430 | comma |
@@ -594,10 +555,17 @@ impl ops::Add<u8> for IndentLevel { | |||
594 | } | 555 | } |
595 | 556 | ||
596 | 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 | |||
597 | pub fn from_node(node: &SyntaxNode) -> IndentLevel { | 565 | pub fn from_node(node: &SyntaxNode) -> IndentLevel { |
598 | match node.first_token() { | 566 | match node.first_token() { |
599 | Some(it) => Self::from_token(&it), | 567 | Some(it) => Self::from_token(&it), |
600 | None => return IndentLevel(0), | 568 | None => IndentLevel(0), |
601 | } | 569 | } |
602 | } | 570 | } |
603 | 571 | ||