@@ -16,7 +16,7 @@ use url::Url;
16
16
17
17
use self :: hygiene:: MarkData ;
18
18
pub use self :: hygiene:: { Mark , SyntaxContext } ;
19
- use crate :: { cache :: CacheCell , rustc_data_structures:: stable_hasher:: StableHasher , sync:: Lrc } ;
19
+ use crate :: { rustc_data_structures:: stable_hasher:: StableHasher , sync:: Lrc } ;
20
20
21
21
mod analyze_source_file;
22
22
pub mod hygiene;
@@ -827,26 +827,14 @@ pub struct SourceFile {
827
827
pub start_pos : BytePos ,
828
828
/// The end position of this source in the `SourceMap`
829
829
pub end_pos : BytePos ,
830
- /// A hash of the filename, used for speeding up the incr. comp. hashing.
831
- pub name_hash : u128 ,
832
-
833
- lazy : CacheCell < SourceFileAnalysis > ,
834
- }
835
-
836
- #[ cfg_attr(
837
- any( feature = "rkyv-impl" ) ,
838
- derive( rkyv:: Archive , rkyv:: Serialize , rkyv:: Deserialize )
839
- ) ]
840
- #[ cfg_attr( feature = "rkyv-impl" , archive( check_bytes) ) ]
841
- #[ cfg_attr( feature = "rkyv-impl" , archive_attr( repr( C ) ) ) ]
842
- #[ derive( Clone ) ]
843
- pub struct SourceFileAnalysis {
844
830
/// Locations of lines beginnings in the source code
845
831
pub lines : Vec < BytePos > ,
846
832
/// Locations of multi-byte characters in the source code
847
833
pub multibyte_chars : Vec < MultiByteChar > ,
848
834
/// Width of characters that are not narrow in the source code
849
835
pub non_narrow_chars : Vec < NonNarrowChar > ,
836
+ /// A hash of the filename, used for speeding up the incr. comp. hashing.
837
+ pub name_hash : u128 ,
850
838
}
851
839
852
840
impl fmt:: Debug for SourceFile {
@@ -900,6 +888,9 @@ impl SourceFile {
900
888
} ;
901
889
let end_pos = start_pos. to_usize ( ) + src. len ( ) ;
902
890
891
+ let ( lines, multibyte_chars, non_narrow_chars) =
892
+ analyze_source_file:: analyze_source_file ( & src[ ..] , start_pos) ;
893
+
903
894
SourceFile {
904
895
name,
905
896
name_was_remapped,
@@ -909,16 +900,17 @@ impl SourceFile {
909
900
src_hash,
910
901
start_pos,
911
902
end_pos : SmallPos :: from_usize ( end_pos) ,
903
+ lines,
904
+ multibyte_chars,
905
+ non_narrow_chars,
912
906
name_hash,
913
- lazy : CacheCell :: new ( ) ,
914
907
}
915
908
}
916
909
917
910
/// Return the BytePos of the beginning of the current line.
918
911
pub fn line_begin_pos ( & self , pos : BytePos ) -> BytePos {
919
912
let line_index = self . lookup_line ( pos) . unwrap ( ) ;
920
- let analysis = self . analyze ( ) ;
921
- analysis. lines [ line_index]
913
+ self . lines [ line_index]
922
914
}
923
915
924
916
/// Get a line from the list of pre-computed line-beginnings.
@@ -936,8 +928,7 @@ impl SourceFile {
936
928
}
937
929
938
930
let begin = {
939
- let analysis = self . analyze ( ) ;
940
- let line = analysis. lines . get ( line_number) ?;
931
+ let line = self . lines . get ( line_number) ?;
941
932
let begin: BytePos = * line - self . start_pos ;
942
933
begin. to_usize ( )
943
934
} ;
@@ -954,22 +945,20 @@ impl SourceFile {
954
945
}
955
946
956
947
pub fn count_lines ( & self ) -> usize {
957
- let analysis = self . analyze ( ) ;
958
- analysis. lines . len ( )
948
+ self . lines . len ( )
959
949
}
960
950
961
951
/// Find the line containing the given position. The return value is the
962
952
/// index into the `lines` array of this SourceFile, not the 1-based line
963
953
/// number. If the `source_file` is empty or the position is located before
964
954
/// the first line, `None` is returned.
965
955
pub fn lookup_line ( & self , pos : BytePos ) -> Option < usize > {
966
- let analysis = self . analyze ( ) ;
967
- if analysis. lines . is_empty ( ) {
956
+ if self . lines . is_empty ( ) {
968
957
return None ;
969
958
}
970
959
971
- let line_index = lookup_line ( & analysis . lines , pos) ;
972
- assert ! ( line_index < analysis . lines. len( ) as isize ) ;
960
+ let line_index = lookup_line ( & self . lines [ .. ] , pos) ;
961
+ assert ! ( line_index < self . lines. len( ) as isize ) ;
973
962
if line_index >= 0 {
974
963
Some ( line_index as usize )
975
964
} else {
@@ -982,32 +971,18 @@ impl SourceFile {
982
971
return ( self . start_pos , self . end_pos ) ;
983
972
}
984
973
985
- let analysis = self . analyze ( ) ;
986
-
987
- assert ! ( line_index < analysis. lines. len( ) ) ;
988
- if line_index == ( analysis. lines . len ( ) - 1 ) {
989
- ( analysis. lines [ line_index] , self . end_pos )
974
+ assert ! ( line_index < self . lines. len( ) ) ;
975
+ if line_index == ( self . lines . len ( ) - 1 ) {
976
+ ( self . lines [ line_index] , self . end_pos )
990
977
} else {
991
- ( analysis . lines [ line_index] , analysis . lines [ line_index + 1 ] )
978
+ ( self . lines [ line_index] , self . lines [ line_index + 1 ] )
992
979
}
993
980
}
994
981
995
982
#[ inline]
996
983
pub fn contains ( & self , byte_pos : BytePos ) -> bool {
997
984
byte_pos >= self . start_pos && byte_pos <= self . end_pos
998
985
}
999
-
1000
- pub fn analyze ( & self ) -> & SourceFileAnalysis {
1001
- self . lazy . get_or_init ( || {
1002
- let ( lines, multibyte_chars, non_narrow_chars) =
1003
- analyze_source_file:: analyze_source_file ( & self . src [ ..] , self . start_pos ) ;
1004
- SourceFileAnalysis {
1005
- lines,
1006
- multibyte_chars,
1007
- non_narrow_chars,
1008
- }
1009
- } )
1010
- }
1011
986
}
1012
987
1013
988
/// Remove utf-8 BOM if any.
0 commit comments