|
28 | 28 |
|
29 | 29 | from .SyntaxHighlighterStyle import SyntaxHighlighterStyle |
30 | 30 | from CodeReview.Diff.RawTextDocumentDiff import chunk_type |
| 31 | +from CodeReview.Math.Functions import number_of_digits |
31 | 32 | from CodeReview.Tools.IteratorTools import pairwise, iter_with_last_flag |
32 | 33 | from CodeReview.Tools.StringTools import remove_trailing_newline |
33 | 34 | import CodeReview.GUI.DiffViewer.DiffWidgetConfig as DiffWidgetConfig |
@@ -368,35 +369,68 @@ def _insert_metadata(self, cursor, document_model): |
368 | 369 |
|
369 | 370 | ############################################## |
370 | 371 |
|
| 372 | + def _append_document_on_side(self, side, document_model, |
| 373 | + aligned_mode, |
| 374 | + complete_mode, |
| 375 | + line_number_mode, |
| 376 | + ): |
| 377 | + |
| 378 | + cursor = self._cursors[side] |
| 379 | + |
| 380 | + if line_number_mode: |
| 381 | + previous_line_number = -1 |
| 382 | + last_line_number = document_model[-1].line_slice.upper |
| 383 | + line_number_pattern = '{:' + str(number_of_digits(last_line_number)) + '} | ' |
| 384 | + |
| 385 | + if document_model.metadata is not None: |
| 386 | + cursor.begin_block(side, chunk_type.header) |
| 387 | + self._insert_metadata(cursor, document_model) |
| 388 | + |
| 389 | + for text_block in document_model: |
| 390 | + cursor.begin_block(side, text_block.frame_type, aligned_mode) |
| 391 | + if text_block.frame_type != chunk_type.equal_block or complete_mode: |
| 392 | + for text_fragment, last_text_fragment in iter_with_last_flag(text_block): |
| 393 | + text_format = self._syntax_highlighter_style[text_fragment.token_type] |
| 394 | + if (text_block.frame_type == chunk_type.replace and |
| 395 | + text_fragment.frame_type != chunk_type.equal): |
| 396 | + text_format.setBackground(DiffWidgetConfig.intra_difference_background_colour) |
| 397 | + if line_number_mode: |
| 398 | + line_slice = text_block.line_slice |
| 399 | + line_number = line_slice.start |
| 400 | + for line in text_fragment.text.line_iterator(): |
| 401 | + if line_number > previous_line_number: |
| 402 | + cursor.insert_text(line_number_pattern.format(line_number +1)) |
| 403 | + cursor.insert_text(str(line), text_format, line_number == line_slice.upper) |
| 404 | + previous_line_number = line_number |
| 405 | + line_number += 1 |
| 406 | + else: |
| 407 | + cursor.insert_text(str(text_fragment), text_format, last_text_fragment) |
| 408 | + if aligned_mode: |
| 409 | + padding = '\n'*(text_block.alignment_padding() -1) # same as last_text_fragment=True |
| 410 | + cursor.insert_text(padding) |
| 411 | + |
| 412 | + ############################################## |
| 413 | + |
371 | 414 | def append_document_models(self, document_models, |
372 | 415 | aligned_mode=True, |
373 | 416 | complete_mode=True, |
374 | | - line_number_mode=True): |
| 417 | + line_number_mode=True, |
| 418 | + ): |
375 | 419 |
|
376 | 420 | for side, document_model in enumerate(document_models): |
377 | | - cursor = self._cursors[side] |
378 | | - if document_model.metadata is not None: |
379 | | - cursor.begin_block(side, chunk_type.header) |
380 | | - self._insert_metadata(cursor, document_model) |
381 | | - for text_block in document_model: |
382 | | - cursor.begin_block(side, text_block.frame_type, aligned_mode) |
383 | | - if text_block.frame_type != chunk_type.equal_block or complete_mode: |
384 | | - for text_fragment, last_text_fragment in iter_with_last_flag(text_block): |
385 | | - text_format = self._syntax_highlighter_style[text_fragment.token_type] |
386 | | - if (text_block.frame_type == chunk_type.replace and |
387 | | - text_fragment.frame_type != chunk_type.equal): |
388 | | - text_format.setBackground(DiffWidgetConfig.intra_difference_background_colour) |
389 | | - cursor.insert_text(str(text_fragment), text_format, last_text_fragment) |
390 | | - if aligned_mode: |
391 | | - padding = '\n'*(text_block.alignment_padding() -1) # same as last_text_fragment=True |
392 | | - cursor.insert_text(padding) |
| 421 | + self._append_document_on_side(side, document_model, |
| 422 | + aligned_mode, complete_mode, line_number_mode) |
393 | 423 |
|
394 | 424 | ############################################## |
395 | 425 |
|
396 | | - def set_document_models(self, document_models, aligned_mode=True, complete_mode=True): |
| 426 | + def set_document_models(self, document_models, |
| 427 | + aligned_mode=True, |
| 428 | + complete_mode=True, |
| 429 | + line_number_mode=True, |
| 430 | + ): |
397 | 431 |
|
398 | 432 | self.clear() |
399 | | - self.append_document_models(document_models, aligned_mode, complete_mode) |
| 433 | + self.append_document_models(document_models, aligned_mode, complete_mode, line_number_mode) |
400 | 434 | for cursor in self._cursors: |
401 | 435 | cursor.end() |
402 | 436 |
|
|
0 commit comments