@@ -777,16 +777,35 @@ impl SourceMap {
777
777
self . span_until_char ( sp, '{' )
778
778
}
779
779
780
- /// Returns a new span representing just the start point of this span.
780
+ /// Returns a new span representing just the first character of the given span.
781
781
pub fn start_point ( & self , sp : Span ) -> Span {
782
- let pos = sp. lo ( ) . 0 ;
783
- let width = self . find_width_of_character_at_span ( sp, false ) ;
784
- let corrected_start_position = pos. checked_add ( width) . unwrap_or ( pos) ;
785
- let end_point = BytePos ( cmp:: max ( corrected_start_position, sp. lo ( ) . 0 ) ) ;
786
- sp. with_hi ( end_point)
782
+ let width = {
783
+ let sp = sp. data ( ) ;
784
+ let local_begin = self . lookup_byte_offset ( sp. lo ) ;
785
+ let start_index = local_begin. pos . to_usize ( ) ;
786
+ let src = local_begin. sf . external_src . borrow ( ) ;
787
+
788
+ let snippet = if let Some ( ref src) = local_begin. sf . src {
789
+ Some ( & src[ start_index..] )
790
+ } else if let Some ( src) = src. get_source ( ) {
791
+ Some ( & src[ start_index..] )
792
+ } else {
793
+ None
794
+ } ;
795
+
796
+ match snippet {
797
+ None => 1 ,
798
+ Some ( snippet) => match snippet. chars ( ) . next ( ) {
799
+ None => 1 ,
800
+ Some ( c) => c. len_utf8 ( ) ,
801
+ } ,
802
+ }
803
+ } ;
804
+
805
+ sp. with_hi ( BytePos ( sp. lo ( ) . 0 + width as u32 ) )
787
806
}
788
807
789
- /// Returns a new span representing just the end point of this span.
808
+ /// Returns a new span representing just the last character of this span.
790
809
pub fn end_point ( & self , sp : Span ) -> Span {
791
810
let pos = sp. hi ( ) . 0 ;
792
811
@@ -815,7 +834,8 @@ impl SourceMap {
815
834
Span :: new ( BytePos ( start_of_next_point) , end_of_next_point, sp. ctxt ( ) )
816
835
}
817
836
818
- /// Finds the width of a character, either before or after the provided span.
837
+ /// Finds the width of the character, either before or after the end of provided span,
838
+ /// depending on the `forwards` parameter.
819
839
fn find_width_of_character_at_span ( & self , sp : Span , forwards : bool ) -> u32 {
820
840
let sp = sp. data ( ) ;
821
841
if sp. lo == sp. hi {
@@ -862,11 +882,9 @@ impl SourceMap {
862
882
// We need to extend the snippet to the end of the src rather than to end_index so when
863
883
// searching forwards for boundaries we've got somewhere to search.
864
884
let snippet = if let Some ( ref src) = local_begin. sf . src {
865
- let len = src. len ( ) ;
866
- & src[ start_index..len]
885
+ & src[ start_index..]
867
886
} else if let Some ( src) = src. get_source ( ) {
868
- let len = src. len ( ) ;
869
- & src[ start_index..len]
887
+ & src[ start_index..]
870
888
} else {
871
889
return 1 ;
872
890
} ;
0 commit comments