@@ -8,6 +8,7 @@ use std::{
8
8
use core:: slice:: Iter ;
9
9
10
10
use petgraph:: stable_graph:: NodeIndex ;
11
+ use slog_scope:: debug;
11
12
12
13
use crate :: graph:: CachedStableGraph ;
13
14
use crate :: source_mapper:: SourceMapper ;
@@ -88,27 +89,16 @@ impl<'a> MergeViewBuilder<'a> {
88
89
// );
89
90
90
91
// last_offset_set.insert((first, None), version_char_offsets.1);
91
- self . last_offset_set . insert (
92
- FilialTuple {
93
- child : first,
94
- parent : None ,
95
- } ,
96
- 0 ,
97
- ) ;
92
+ self . set_last_offset_for_tuple ( None , first, 0 ) ;
98
93
99
94
// stack to keep track of the depth first traversal
100
95
let mut stack = VecDeque :: < NodeIndex > :: new ( ) ;
101
96
102
97
self . create_merge_views ( & mut merge_list, & mut extra_lines, & mut stack) ;
103
98
104
99
// now we add a view of the remainder of the root file
105
- let offset = * self
106
- . last_offset_set
107
- . get ( & FilialTuple {
108
- child : first,
109
- parent : None ,
110
- } )
111
- . unwrap ( ) ;
100
+
101
+ let offset = self . get_last_offset_for_tuple ( None , first) . unwrap ( ) ;
112
102
113
103
let len = first_source. len ( ) ;
114
104
merge_list. push_back ( & first_source[ min ( offset, len) ..] ) ;
@@ -135,8 +125,8 @@ impl<'a> MergeViewBuilder<'a> {
135
125
. parent_child_edge_iterator
136
126
. entry ( * n)
137
127
. or_insert_with ( || {
138
- let edge_metas = self . graph . get_edge_metas ( parent, child) ;
139
- Box :: new ( edge_metas )
128
+ let child_positions = self . graph . get_child_positions ( parent, child) ;
129
+ Box :: new ( child_positions )
140
130
} )
141
131
. next ( )
142
132
. unwrap ( ) ;
@@ -147,15 +137,16 @@ impl<'a> MergeViewBuilder<'a> {
147
137
let ( char_for_line, char_following_line) = self . char_offset_for_line ( edge. line , parent_source) ;
148
138
149
139
let offset = * self
150
- . last_offset_set
151
- . insert (
152
- FilialTuple {
153
- child : parent,
154
- parent : stack. back ( ) . copied ( ) ,
155
- } ,
156
- char_following_line,
157
- )
140
+ . set_last_offset_for_tuple ( stack. back ( ) . copied ( ) , parent, char_following_line)
158
141
. get_or_insert ( 0 ) ;
142
+
143
+ debug ! ( "creating view to start child file" ;
144
+ "parent" => parent_path. to_str( ) . unwrap( ) , "child" => child_path. to_str( ) . unwrap( ) ,
145
+ "grandparent" => stack. back( ) . copied( ) . map( |g| self . graph. get_node( g) . to_str( ) . unwrap( ) . to_string( ) ) , // self.graph.get_node().to_str().unwrap(),
146
+ "last_parent_offset" => offset, "line" => edge. line, "char_for_line" => char_for_line,
147
+ "char_following_line" => char_following_line,
148
+ ) ;
149
+
159
150
merge_list. push_back ( & parent_source[ offset..char_for_line] ) ;
160
151
self . add_opening_line_directive ( & child_path, child, merge_list, extra_lines) ;
161
152
@@ -173,13 +164,7 @@ impl<'a> MergeViewBuilder<'a> {
173
164
}
174
165
} ;
175
166
merge_list. push_back ( & child_source[ ..offset] ) ;
176
- self . last_offset_set . insert (
177
- FilialTuple {
178
- child,
179
- parent : Some ( parent) ,
180
- } ,
181
- 0 ,
182
- ) ;
167
+ self . set_last_offset_for_tuple ( Some ( parent) , child, 0 ) ;
183
168
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
184
169
self . add_closing_line_directive ( edge. line + 2 , & parent_path, parent, merge_list, extra_lines) ;
185
170
// if the next pair's parent is not the current pair's parent, we need to bubble up
@@ -193,29 +178,17 @@ impl<'a> MergeViewBuilder<'a> {
193
178
self . create_merge_views ( merge_list, extra_lines, stack) ;
194
179
stack. pop_back ( ) ;
195
180
196
- let offset = * self
197
- . last_offset_set
198
- . get ( & FilialTuple {
199
- child,
200
- parent : Some ( parent) ,
201
- } )
202
- . unwrap ( ) ;
181
+ let offset = self . get_last_offset_for_tuple ( Some ( parent) , child) . unwrap ( ) ;
203
182
let child_source = self . sources . get ( & child_path) . unwrap ( ) ;
204
183
// this evaluates to false once the file contents have been exhausted aka offset = child_source.len() + 1
205
184
let end_offset = match child_source. ends_with ( '\n' ) {
206
- true => 1 , /* child_source.len()-1 */
207
- false => 0 , /* child_source.len() */
185
+ true => 1 ,
186
+ false => 0 ,
208
187
} ;
209
188
if offset < child_source. len ( ) - end_offset {
210
189
// if ends in \n\n, we want to exclude the last \n for some reason. Ask optilad
211
- merge_list. push_back ( & child_source[ offset../* std::cmp::max( */ child_source. len ( ) -end_offset/* , offset) */ ] ) ;
212
- self . last_offset_set . insert (
213
- FilialTuple {
214
- child,
215
- parent : Some ( parent) ,
216
- } ,
217
- 0 ,
218
- ) ;
190
+ merge_list. push_back ( & child_source[ offset..child_source. len ( ) - end_offset] ) ;
191
+ self . set_last_offset_for_tuple ( Some ( parent) , child, 0 ) ;
219
192
}
220
193
221
194
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
@@ -234,20 +207,26 @@ impl<'a> MergeViewBuilder<'a> {
234
207
false => child_source. len ( ) ,
235
208
} ;
236
209
merge_list. push_back ( & child_source[ ..offset] ) ;
237
- self . last_offset_set . insert (
238
- FilialTuple {
239
- child,
240
- parent : Some ( parent) ,
241
- } ,
242
- 0 ,
243
- ) ;
210
+ self . set_last_offset_for_tuple ( Some ( parent) , child, 0 ) ;
244
211
// +2 because edge.line is 0 indexed but #line is 1 indexed and references the *following* line
245
212
self . add_closing_line_directive ( edge. line + 2 , & parent_path, parent, merge_list, extra_lines) ;
246
213
}
247
214
}
248
215
}
249
216
}
250
217
218
+ fn set_last_offset_for_tuple ( & mut self , parent : Option < NodeIndex > , child : NodeIndex , offset : usize ) -> Option < usize > {
219
+ debug ! ( "inserting last offset" ;
220
+ "parent" => parent. map( |p| self . graph. get_node( p) . to_str( ) . unwrap( ) . to_string( ) ) ,
221
+ "child" => self . graph. get_node( child) . to_str( ) . unwrap( ) . to_string( ) ,
222
+ "offset" => offset) ;
223
+ self . last_offset_set . insert ( FilialTuple { child, parent } , offset)
224
+ }
225
+
226
+ fn get_last_offset_for_tuple ( & self , parent : Option < NodeIndex > , child : NodeIndex ) -> Option < usize > {
227
+ self . last_offset_set . get ( & FilialTuple { child, parent } ) . copied ( )
228
+ }
229
+
251
230
// returns the character offset + 1 of the end of line number `line` and the character
252
231
// offset + 1 for the end of the line after the previous one
253
232
fn char_offset_for_line ( & self , line_num : usize , source : & str ) -> ( usize , usize ) {
0 commit comments