204 def persist_writes(self, layout: SnapshotFileLayout):
205
206 if not self.writes:
207 return
208
209
210 sorted_writes = sorted(
211 self.writes.values(),
212 key=lambda x: (x.call_stack.location.file_name, x.call_stack.location.line),
213 )
214
215
216 first_write = sorted_writes[0]
217 current_file = layout.sourcefile_for_call(first_write.call_stack.location)
218 content = SourceFile(current_file.name, layout.fs.file_read(current_file))
219 delta_line_numbers = 0
220
221 for write in sorted_writes:
222
223 file_path = layout.sourcefile_for_call(write.call_stack.location)
224
225 if file_path != current_file:
226 layout.fs.file_write(current_file, content.as_string)
227 current_file = file_path
228 content = SourceFile(
229 current_file.name, layout.fs.file_read(current_file)
230 )
231 delta_line_numbers = 0
232
233
234 line = write.call_stack.location.line + delta_line_numbers
235 if isinstance(write.snapshot.format, LiteralTodoStub):
236 kind: TodoStub = write.snapshot.actual
237 content.replace_on_line(line, f".{kind.name}_TODO(", f".{kind.name}(")
238 else:
239 to_be_literal = content.parse_to_be_like(line)
240
241 literal_change = to_be_literal.set_literal_and_get_newline_delta(
242 write.snapshot
243 )
244 delta_line_numbers += literal_change
245
246
247 layout.fs.file_write(current_file, content.as_string)
248
249