@@ -131,7 +131,45 @@ impl Variable {
131
131
}
132
132
}
133
133
134
- pub type SourceRange = core:: ops:: Range < usize > ;
134
+ #[ derive( Clone , Debug , PartialEq ) ]
135
+ pub struct SourceRange {
136
+ file_path : String ,
137
+ range : core:: ops:: Range < usize > ,
138
+ }
139
+
140
+ impl SourceRange {
141
+ pub fn new ( file_path : & str , range : core:: ops:: Range < usize > ) -> SourceRange {
142
+ SourceRange {
143
+ file_path : file_path. into ( ) ,
144
+ range,
145
+ }
146
+ }
147
+
148
+ pub fn undefined ( ) -> SourceRange {
149
+ SourceRange {
150
+ file_path : "" . into ( ) ,
151
+ range : 0 ..0 ,
152
+ }
153
+ }
154
+
155
+ pub fn get_file_path ( & self ) -> & str {
156
+ & self . file_path
157
+ }
158
+
159
+ pub fn get_start ( & self ) -> usize {
160
+ self . range . start
161
+ }
162
+
163
+ pub fn get_end ( & self ) -> usize {
164
+ self . range . end
165
+ }
166
+ }
167
+
168
+ impl From < std:: ops:: Range < usize > > for SourceRange {
169
+ fn from ( range : std:: ops:: Range < usize > ) -> SourceRange {
170
+ SourceRange :: new ( "" , range)
171
+ }
172
+ }
135
173
136
174
#[ derive( Clone , Debug , PartialEq ) ]
137
175
pub struct NewLines {
@@ -610,25 +648,55 @@ impl Statement {
610
648
Statement :: LiteralArray { location, .. } => location. clone ( ) ,
611
649
Statement :: Reference { location, .. } => location. clone ( ) ,
612
650
Statement :: QualifiedReference { elements, .. } => {
613
- elements. first ( ) . map_or ( 0 , |it| it. get_location ( ) . start )
614
- ..elements. last ( ) . map_or ( 0 , |it| it. get_location ( ) . end )
651
+ let first = elements
652
+ . first ( )
653
+ . map_or_else ( SourceRange :: undefined, |it| it. get_location ( ) ) ;
654
+ let last = elements
655
+ . last ( )
656
+ . map_or_else ( SourceRange :: undefined, |it| it. get_location ( ) ) ;
657
+ SourceRange :: new ( first. get_file_path ( ) , first. get_start ( ) ..last. get_end ( ) )
615
658
}
616
659
Statement :: BinaryExpression { left, right, .. } => {
617
- left. get_location ( ) . start ..right. get_location ( ) . end
660
+ let left_loc = left. get_location ( ) ;
661
+ let right_loc = right. get_location ( ) ;
662
+ SourceRange :: new (
663
+ & left_loc. file_path ,
664
+ left_loc. range . start ..right_loc. range . end ,
665
+ )
618
666
}
619
667
Statement :: UnaryExpression { location, .. } => location. clone ( ) ,
620
668
Statement :: ExpressionList { expressions } => {
621
- expressions. first ( ) . map_or ( 0 , |it| it. get_location ( ) . start )
622
- ..expressions. last ( ) . map_or ( 0 , |it| it. get_location ( ) . end )
669
+ let first = expressions
670
+ . first ( )
671
+ . map_or_else ( SourceRange :: undefined, |it| it. get_location ( ) ) ;
672
+ let last = expressions
673
+ . last ( )
674
+ . map_or_else ( SourceRange :: undefined, |it| it. get_location ( ) ) ;
675
+ SourceRange :: new ( first. get_file_path ( ) , first. get_start ( ) ..last. get_end ( ) )
623
676
}
624
677
Statement :: RangeStatement { start, end } => {
625
- start. get_location ( ) . start ..end. get_location ( ) . end
678
+ let start_loc = start. get_location ( ) ;
679
+ let end_loc = end. get_location ( ) ;
680
+ SourceRange :: new (
681
+ & start_loc. file_path ,
682
+ start_loc. range . start ..end_loc. range . end ,
683
+ )
626
684
}
627
685
Statement :: Assignment { left, right } => {
628
- left. get_location ( ) . start ..right. get_location ( ) . end
686
+ let left_loc = left. get_location ( ) ;
687
+ let right_loc = right. get_location ( ) ;
688
+ SourceRange :: new (
689
+ & left_loc. file_path ,
690
+ left_loc. range . start ..right_loc. range . end ,
691
+ )
629
692
}
630
693
Statement :: OutputAssignment { left, right } => {
631
- left. get_location ( ) . start ..right. get_location ( ) . end
694
+ let left_loc = left. get_location ( ) ;
695
+ let right_loc = right. get_location ( ) ;
696
+ SourceRange :: new (
697
+ & left_loc. file_path ,
698
+ left_loc. range . start ..right_loc. range . end ,
699
+ )
632
700
}
633
701
Statement :: CallStatement { location, .. } => location. clone ( ) ,
634
702
Statement :: IfStatement { location, .. } => location. clone ( ) ,
@@ -637,7 +705,12 @@ impl Statement {
637
705
Statement :: RepeatLoopStatement { location, .. } => location. clone ( ) ,
638
706
Statement :: CaseStatement { location, .. } => location. clone ( ) ,
639
707
Statement :: ArrayAccess { reference, access } => {
640
- reference. get_location ( ) . start ..access. get_location ( ) . end
708
+ let reference_loc = reference. get_location ( ) ;
709
+ let access_loc = access. get_location ( ) ;
710
+ SourceRange :: new (
711
+ & reference_loc. file_path ,
712
+ reference_loc. range . start ..access_loc. range . end ,
713
+ )
641
714
}
642
715
Statement :: MultipliedStatement { location, .. } => location. clone ( ) ,
643
716
}
0 commit comments