forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNavigation.cs
More file actions
152 lines (124 loc) · 3.85 KB
/
Copy pathBlockNavigation.cs
File metadata and controls
152 lines (124 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class BlockNavigation : ObservableObject
{
public record Block(int Start, int End)
{
public bool IsInRange(int line)
{
return line >= Start && line <= End;
}
}
public string Indicator
{
get
{
if (_blocks.Count == 0)
return "-/-";
if (_current >= 0 && _current < _blocks.Count)
return $"{_current + 1}/{_blocks.Count}";
return $"-/{_blocks.Count}";
}
}
public BlockNavigation(List<Models.TextDiffLine> lines)
{
_blocks.Clear();
_current = -1;
if (lines.Count == 0)
return;
var lineIdx = 0;
var blockStartIdx = 0;
var isReadingBlock = false;
var blocks = new List<Block>();
foreach (var line in lines)
{
lineIdx++;
if (line.Type is Models.TextDiffLineType.Added or Models.TextDiffLineType.Deleted or Models.TextDiffLineType.None)
{
if (!isReadingBlock)
{
isReadingBlock = true;
blockStartIdx = lineIdx;
}
}
else
{
if (isReadingBlock)
{
blocks.Add(new Block(blockStartIdx, lineIdx - 1));
isReadingBlock = false;
}
}
}
if (isReadingBlock)
blocks.Add(new Block(blockStartIdx, lines.Count));
_blocks.AddRange(blocks);
}
public Block GetCurrentBlock()
{
if (_current >= 0 && _current < _blocks.Count)
return _blocks[_current];
return null;
}
public Block GotoFirst()
{
if (_blocks.Count == 0)
return null;
_current = 0;
OnPropertyChanged(nameof(Indicator));
return _blocks[_current];
}
public Block GotoPrev()
{
if (_blocks.Count == 0)
return null;
if (_current == -1)
_current = 0;
else if (_current > 0)
_current--;
OnPropertyChanged(nameof(Indicator));
return _blocks[_current];
}
public Block GotoNext()
{
if (_blocks.Count == 0)
return null;
if (_current < _blocks.Count - 1)
_current++;
OnPropertyChanged(nameof(Indicator));
return _blocks[_current];
}
public Block GotoLast()
{
if (_blocks.Count == 0)
return null;
_current = _blocks.Count - 1;
OnPropertyChanged(nameof(Indicator));
return _blocks[_current];
}
public void UpdateByCaretPosition(int caretLine)
{
if (_current >= 0 && _current < _blocks.Count)
{
var block = _blocks[_current];
if (block.IsInRange(caretLine))
return;
}
_current = -1;
for (var i = 0; i < _blocks.Count; i++)
{
var block = _blocks[i];
if (block.Start > caretLine)
break;
_current = i;
if (block.End >= caretLine)
break;
}
OnPropertyChanged(nameof(Indicator));
}
private int _current;
private readonly List<Block> _blocks = [];
}
}