aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src')
-rw-r--r--crates/syntax/src/algo.rs122
1 files changed, 1 insertions, 121 deletions
diff --git a/crates/syntax/src/algo.rs b/crates/syntax/src/algo.rs
index ba263be0d..3f9b84ab9 100644
--- a/crates/syntax/src/algo.rs
+++ b/crates/syntax/src/algo.rs
@@ -1,10 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::{ 3use std::{fmt, hash::BuildHasherDefault, ops::RangeInclusive};
4 fmt,
5 hash::BuildHasherDefault,
6 ops::{self, RangeInclusive},
7};
8 4
9use indexmap::IndexMap; 5use indexmap::IndexMap;
10use itertools::Itertools; 6use itertools::Itertools;
@@ -358,107 +354,11 @@ impl fmt::Debug for SyntaxRewriter<'_> {
358} 354}
359 355
360impl SyntaxRewriter<'_> { 356impl SyntaxRewriter<'_> {
361 pub fn delete<T: Clone + Into<SyntaxElement>>(&mut self, what: &T) {
362 let what = what.clone().into();
363 let replacement = Replacement::Delete;
364 self.replacements.insert(what, replacement);
365 }
366 pub fn insert_before<T: Clone + Into<SyntaxElement>, U: Clone + Into<SyntaxElement>>(
367 &mut self,
368 before: &T,
369 what: &U,
370 ) {
371 let before = before.clone().into();
372 let pos = match before.prev_sibling_or_token() {
373 Some(sibling) => InsertPos::After(sibling),
374 None => match before.parent() {
375 Some(parent) => InsertPos::FirstChildOf(parent),
376 None => return,
377 },
378 };
379 self.insertions.entry(pos).or_insert_with(Vec::new).push(what.clone().into());
380 }
381 pub fn insert_after<T: Clone + Into<SyntaxElement>, U: Clone + Into<SyntaxElement>>(
382 &mut self,
383 after: &T,
384 what: &U,
385 ) {
386 self.insertions
387 .entry(InsertPos::After(after.clone().into()))
388 .or_insert_with(Vec::new)
389 .push(what.clone().into());
390 }
391 pub fn insert_as_first_child<T: Clone + Into<SyntaxNode>, U: Clone + Into<SyntaxElement>>(
392 &mut self,
393 parent: &T,
394 what: &U,
395 ) {
396 self.insertions
397 .entry(InsertPos::FirstChildOf(parent.clone().into()))
398 .or_insert_with(Vec::new)
399 .push(what.clone().into());
400 }
401 pub fn insert_many_before<
402 T: Clone + Into<SyntaxElement>,
403 U: IntoIterator<Item = SyntaxElement>,
404 >(
405 &mut self,
406 before: &T,
407 what: U,
408 ) {
409 let before = before.clone().into();
410 let pos = match before.prev_sibling_or_token() {
411 Some(sibling) => InsertPos::After(sibling),
412 None => match before.parent() {
413 Some(parent) => InsertPos::FirstChildOf(parent),
414 None => return,
415 },
416 };
417 self.insertions.entry(pos).or_insert_with(Vec::new).extend(what);
418 }
419 pub fn insert_many_after<
420 T: Clone + Into<SyntaxElement>,
421 U: IntoIterator<Item = SyntaxElement>,
422 >(
423 &mut self,
424 after: &T,
425 what: U,
426 ) {
427 self.insertions
428 .entry(InsertPos::After(after.clone().into()))
429 .or_insert_with(Vec::new)
430 .extend(what);
431 }
432 pub fn insert_many_as_first_children<
433 T: Clone + Into<SyntaxNode>,
434 U: IntoIterator<Item = SyntaxElement>,
435 >(
436 &mut self,
437 parent: &T,
438 what: U,
439 ) {
440 self.insertions
441 .entry(InsertPos::FirstChildOf(parent.clone().into()))
442 .or_insert_with(Vec::new)
443 .extend(what)
444 }
445 pub fn replace<T: Clone + Into<SyntaxElement>>(&mut self, what: &T, with: &T) { 357 pub fn replace<T: Clone + Into<SyntaxElement>>(&mut self, what: &T, with: &T) {
446 let what = what.clone().into(); 358 let what = what.clone().into();
447 let replacement = Replacement::Single(with.clone().into()); 359 let replacement = Replacement::Single(with.clone().into());
448 self.replacements.insert(what, replacement); 360 self.replacements.insert(what, replacement);
449 } 361 }
450 pub fn replace_with_many<T: Clone + Into<SyntaxElement>>(
451 &mut self,
452 what: &T,
453 with: Vec<SyntaxElement>,
454 ) {
455 let what = what.clone().into();
456 let replacement = Replacement::Many(with);
457 self.replacements.insert(what, replacement);
458 }
459 pub fn replace_ast<T: AstNode>(&mut self, what: &T, with: &T) {
460 self.replace(what.syntax(), with.syntax())
461 }
462 362
463 pub fn rewrite(&self, node: &SyntaxNode) -> SyntaxNode { 363 pub fn rewrite(&self, node: &SyntaxNode) -> SyntaxNode {
464 let _p = profile::span("rewrite"); 364 let _p = profile::span("rewrite");
@@ -534,10 +434,6 @@ impl SyntaxRewriter<'_> {
534 if let Some(replacement) = self.replacement(&element) { 434 if let Some(replacement) = self.replacement(&element) {
535 match replacement { 435 match replacement {
536 Replacement::Single(element) => acc.push(element_to_green(element)), 436 Replacement::Single(element) => acc.push(element_to_green(element)),
537 Replacement::Many(replacements) => {
538 acc.extend(replacements.into_iter().map(element_to_green))
539 }
540 Replacement::Delete => (),
541 }; 437 };
542 } else { 438 } else {
543 match element { 439 match element {
@@ -560,25 +456,9 @@ fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, row
560 } 456 }
561} 457}
562 458
563impl ops::AddAssign for SyntaxRewriter<'_> {
564 fn add_assign(&mut self, rhs: SyntaxRewriter) {
565 self.replacements.extend(rhs.replacements);
566 for (pos, insertions) in rhs.insertions.into_iter() {
567 match self.insertions.entry(pos) {
568 indexmap::map::Entry::Occupied(mut occupied) => {
569 occupied.get_mut().extend(insertions)
570 }
571 indexmap::map::Entry::Vacant(vacant) => drop(vacant.insert(insertions)),
572 }
573 }
574 }
575}
576
577#[derive(Clone, Debug)] 459#[derive(Clone, Debug)]
578enum Replacement { 460enum Replacement {
579 Delete,
580 Single(SyntaxElement), 461 Single(SyntaxElement),
581 Many(Vec<SyntaxElement>),
582} 462}
583 463
584fn with_children( 464fn with_children(