@@ -597,21 +597,6 @@ impl CodeMap {
597
597
self . span_to_source ( sp, |src, start_index, _| src[ ..start_index] . to_string ( ) )
598
598
}
599
599
600
- /// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char`
601
- pub fn span_until_char ( & self , sp : Span , c : char ) -> Span {
602
- match self . span_to_snippet ( sp) {
603
- Ok ( snippet) => {
604
- let snippet = snippet. split ( c) . nth ( 0 ) . unwrap_or ( "" ) . trim_right ( ) ;
605
- if !snippet. is_empty ( ) && !snippet. contains ( '\n' ) {
606
- sp. with_hi ( BytePos ( sp. lo ( ) . 0 + snippet. len ( ) as u32 ) )
607
- } else {
608
- sp
609
- }
610
- }
611
- _ => sp,
612
- }
613
- }
614
-
615
600
/// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
616
601
/// if no character could be found or if an error occurred while retrieving the code snippet.
617
602
pub fn span_extend_to_prev_char ( & self , sp : Span , c : char ) -> Span {
@@ -646,55 +631,58 @@ impl CodeMap {
646
631
sp
647
632
}
648
633
634
+ /// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char`
635
+ pub fn span_until_char ( & self , sp : Span , c : char ) -> Span {
636
+ match self . span_to_snippet ( sp) {
637
+ Ok ( snippet) => {
638
+ let snippet = snippet. split ( c) . nth ( 0 ) . unwrap_or ( "" ) . trim_right ( ) ;
639
+ if !snippet. is_empty ( ) && !snippet. contains ( '\n' ) {
640
+ sp. with_hi ( BytePos ( sp. lo ( ) . 0 + snippet. len ( ) as u32 ) )
641
+ } else {
642
+ sp
643
+ }
644
+ }
645
+ _ => sp,
646
+ }
647
+ }
648
+
649
+ /// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
650
+ /// `c`.
651
+ pub fn span_through_char ( & self , sp : Span , c : char ) -> Span {
652
+ if let Ok ( snippet) = self . span_to_snippet ( sp) {
653
+ if let Some ( offset) = snippet. find ( c) {
654
+ return sp. with_hi ( BytePos ( sp. lo ( ) . 0 + ( offset + c. len_utf8 ( ) ) as u32 ) ) ;
655
+ }
656
+ }
657
+ sp
658
+ }
659
+
649
660
/// Given a `Span`, get a new `Span` covering the first token and all its trailing whitespace or
650
661
/// the original `Span`.
651
662
///
652
663
/// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
653
664
pub fn span_until_non_whitespace ( & self , sp : Span ) -> Span {
654
- if let Ok ( snippet) = self . span_to_snippet ( sp) {
655
- let mut offset = 0 ;
656
- // get the bytes width of all the non-whitespace characters
657
- for c in snippet. chars ( ) . take_while ( |c| !c. is_whitespace ( ) ) {
658
- offset += c. len_utf8 ( ) ;
659
- }
660
- // get the bytes width of all the whitespace characters after that
661
- for c in snippet[ offset..] . chars ( ) . take_while ( |c| c. is_whitespace ( ) ) {
662
- offset += c. len_utf8 ( ) ;
665
+ let mut whitespace_found = false ;
666
+
667
+ self . span_take_while ( sp, |c| {
668
+ if !whitespace_found && c. is_whitespace ( ) {
669
+ whitespace_found = true ;
663
670
}
664
- if offset > 1 {
665
- return sp. with_hi ( BytePos ( sp. lo ( ) . 0 + offset as u32 ) ) ;
671
+
672
+ if whitespace_found && !c. is_whitespace ( ) {
673
+ false
674
+ } else {
675
+ true
666
676
}
667
- }
668
- sp
677
+ } )
669
678
}
670
679
671
680
/// Given a `Span`, get a new `Span` covering the first token without its trailing whitespace or
672
681
/// the original `Span` in case of error.
673
682
///
674
683
/// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
675
684
pub fn span_until_whitespace ( & self , sp : Span ) -> Span {
676
- if let Ok ( snippet) = self . span_to_snippet ( sp) {
677
- let mut offset = 0 ;
678
- // Get the bytes width of all the non-whitespace characters
679
- for c in snippet. chars ( ) . take_while ( |c| !c. is_whitespace ( ) ) {
680
- offset += c. len_utf8 ( ) ;
681
- }
682
- if offset > 1 {
683
- return sp. with_hi ( BytePos ( sp. lo ( ) . 0 + offset as u32 ) ) ;
684
- }
685
- }
686
- sp
687
- }
688
-
689
- /// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
690
- /// `c`.
691
- pub fn span_through_char ( & self , sp : Span , c : char ) -> Span {
692
- if let Ok ( snippet) = self . span_to_snippet ( sp) {
693
- if let Some ( offset) = snippet. find ( c) {
694
- return sp. with_hi ( BytePos ( sp. lo ( ) . 0 + ( offset + c. len_utf8 ( ) ) as u32 ) ) ;
695
- }
696
- }
697
- sp
685
+ self . span_take_while ( sp, |c| !c. is_whitespace ( ) )
698
686
}
699
687
700
688
/// Given a `Span`, get a shorter one until `predicate` yields false.
0 commit comments