|
2 | 2 | using System.Text;
|
3 | 3 | using System.Text.RegularExpressions;
|
4 | 4 |
|
| 5 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 6 | + |
5 | 7 | using Avalonia;
|
6 | 8 | using Avalonia.Media.Imaging;
|
7 | 9 |
|
@@ -59,16 +61,70 @@ public bool IsInRange(int idx)
|
59 | 61 | }
|
60 | 62 | }
|
61 | 63 |
|
62 | | - public partial class TextDiff |
| 64 | + public class TextDiffChangeBlock |
| 65 | + { |
| 66 | + public TextDiffChangeBlock(int startLine, int endLine) |
| 67 | + { |
| 68 | + StartLine = startLine; |
| 69 | + EndLine = endLine; |
| 70 | + } |
| 71 | + |
| 72 | + public int StartLine { get; set; } = 0; |
| 73 | + public int EndLine { get; set; } = 0; |
| 74 | + |
| 75 | + public bool IsInRange(int line) |
| 76 | + { |
| 77 | + return line >= StartLine && line <= EndLine; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public partial class TextDiff : ObservableObject |
63 | 82 | {
|
64 | 83 | public string File { get; set; } = string.Empty;
|
65 | 84 | public List<TextDiffLine> Lines { get; set; } = new List<TextDiffLine>();
|
66 | 85 | public Vector ScrollOffset { get; set; } = Vector.Zero;
|
67 | 86 | public int MaxLineNumber = 0;
|
68 | 87 |
|
| 88 | + public int CurrentChangeBlockIdx |
| 89 | + { |
| 90 | + get => _currentChangeBlockIdx; |
| 91 | + set => SetProperty(ref _currentChangeBlockIdx, value); |
| 92 | + } |
| 93 | + |
69 | 94 | public string Repo { get; set; } = null;
|
70 | 95 | public DiffOption Option { get; set; } = null;
|
71 | 96 |
|
| 97 | + public List<TextDiffChangeBlock> ChangeBlocks { get; set; } = []; |
| 98 | + |
| 99 | + public void ProcessChangeBlocks() |
| 100 | + { |
| 101 | + ChangeBlocks.Clear(); |
| 102 | + int lineIdx = 0, blockStartIdx = 0; |
| 103 | + bool isNewBlock = true; |
| 104 | + foreach (var line in Lines) |
| 105 | + { |
| 106 | + lineIdx++; |
| 107 | + if (line.Type == Models.TextDiffLineType.Added || |
| 108 | + line.Type == Models.TextDiffLineType.Deleted || |
| 109 | + line.Type == Models.TextDiffLineType.None) // Empty |
| 110 | + { |
| 111 | + if (isNewBlock) |
| 112 | + { |
| 113 | + isNewBlock = false; |
| 114 | + blockStartIdx = lineIdx; |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + if (!isNewBlock) |
| 120 | + { |
| 121 | + ChangeBlocks.Add(new TextDiffChangeBlock(blockStartIdx, lineIdx - 1)); |
| 122 | + isNewBlock = true; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
72 | 128 | public TextDiffSelection MakeSelection(int startLine, int endLine, bool isCombined, bool isOldSide)
|
73 | 129 | {
|
74 | 130 | var rs = new TextDiffSelection();
|
@@ -626,6 +682,8 @@ private bool ProcessIndicatorForPatchSingleSide(StringBuilder builder, TextDiffL
|
626 | 682 | return true;
|
627 | 683 | }
|
628 | 684 |
|
| 685 | + private int _currentChangeBlockIdx = -1; // NOTE: Use -1 as "not set". |
| 686 | + |
629 | 687 | [GeneratedRegex(@"^@@ \-(\d+),?\d* \+(\d+),?\d* @@")]
|
630 | 688 | private static partial Regex REG_INDICATOR();
|
631 | 689 | }
|
|
0 commit comments