-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOutputLineParser.cs
More file actions
189 lines (171 loc) · 5.67 KB
/
OutputLineParser.cs
File metadata and controls
189 lines (171 loc) · 5.67 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System.Text;
using System.Text.Json;
namespace FindInFiles {
internal sealed class OutputLineParser {
private readonly Control owner;
private readonly OutputLineRender render;
private readonly List<OutputLine> cachedLines = new();
public int MaxLinesAfterMatch = 0;
public int MaxCachedLine = 0;
public int TotalMatchCount = 0;
private int linesAfterMatch = 0;
private bool afterMatch = false;
public OutputLineParser(Control owner, OutputLineRender render) {
this.owner = owner;
this.render = render;
}
public void Clear() {
TotalMatchCount = 0;
linesAfterMatch = 0;
afterMatch = false;
cachedLines.Clear();
}
public void Reset(int cache) {
Clear();
MaxCachedLine = cache;
}
public void Flush() {
if (cachedLines.Count != 0) {
RenderOutput();
}
render.Invalidate();
}
private void RenderOutput() {
render.RenderOutput(cachedLines);
cachedLines.Clear();
}
private static string? GetText(JsonElement element) {
string? text;
if (element.TryGetProperty("text", out var prop)) {
text = prop.GetString();
} else {
text = element.GetProperty("bytes").GetString();
var bytes = Convert.FromBase64String(text ?? "");
text = Encoding.UTF8.GetString(bytes);
}
return text;
}
public void Parse(string line) {
var root = JsonDocument.Parse(line).RootElement;
if (root.ValueKind != JsonValueKind.Object) {
return;
}
if (!root.TryGetProperty("type", out var type) || !root.TryGetProperty("data", out var data)) {
return;
}
OutputLine outputLine;
var dataType = type.GetString();
switch (dataType) {
case "begin": {
var path = data.GetProperty("path").GetProperty("text").GetString();
outputLine = new OutputLine { LineType = OutputLineType.Path, Text = path };
} break;
case "match": {
var text = GetText(data.GetProperty("lines"));
var number = data.GetProperty("line_number").GetInt32();
var submatches = data.GetProperty("submatches");
var matches = ParseSubMatches(text, submatches);
outputLine = new OutputLine { LineType = OutputLineType.Match, Text = text, Number = number, Matches = matches };
} break;
case "context": {
var text = GetText(data.GetProperty("lines"));
var number = data.GetProperty("line_number").GetInt32();
outputLine = new OutputLine { LineType = OutputLineType.Context, Text = text, Number = number };
} break;
case "end":
case "summary": {
var stats = data.GetProperty("stats");
var matched_lines = stats.GetProperty("matched_lines").GetInt32();
var matches = stats.GetProperty("matches").GetInt32();
var elapsed = stats.GetProperty("elapsed").GetProperty("human").GetString();
var summary = $"matched lines: {matched_lines}, matches: {matches}, elapsed: {elapsed}";
if (dataType == "end") {
var path = data.GetProperty("path").GetProperty("text").GetString();
path = Path.GetFileName(path);
summary = $"-- {path}, {summary}";
} else {
TotalMatchCount = matches;
var elapsed_total = data.GetProperty("elapsed_total").GetProperty("human").GetString();
summary = $"-- total {summary}, total elapsed: {elapsed_total}";
}
outputLine = new OutputLine { LineType = OutputLineType.Summary, Text = summary };
} break;
default:
return;
}
if (MaxLinesAfterMatch != 0) {
if (outputLine.LineType == OutputLineType.Context) {
if (afterMatch) {
++linesAfterMatch;
if (linesAfterMatch > MaxLinesAfterMatch) {
afterMatch = false;
cachedLines.Add(new OutputLine { LineType = OutputLineType.Separator, Text = "--" });
}
}
} else {
linesAfterMatch = 0;
afterMatch = outputLine.LineType == OutputLineType.Match;
}
}
cachedLines.Add(outputLine);
if (cachedLines.Count >= MaxCachedLine) {
owner.Invoke(RenderOutput);
}
}
private static MatchTextRange[]? ParseSubMatches(string? line, JsonElement submatches) {
if (string.IsNullOrEmpty(line) || submatches.ValueKind != JsonValueKind.Array) {
return null;
}
var count = submatches.GetArrayLength();
if (count == 0) { // invert
return null;
}
var matches = new MatchTextRange[count];
var ascii = Util.GetLeadingAsciiCount(line);
var startIndex = ascii;
var byteCount = ascii;
for (var index = 0; index < count;) {
ref var range = ref matches[index];
var match = submatches[index++];
var start = match.GetProperty("start").GetInt32();
var end = match.GetProperty("end").GetInt32() - start;
var text = match.GetProperty("match").GetProperty("text").GetString();
if (!string.IsNullOrEmpty(text)) {
end = text.Length;
range.Space = char.IsWhiteSpace(text[0]) || char.IsWhiteSpace(text[end - 1]);
// convert byte offset to character index
if (start > ascii) {
//start = line.IndexOf(text, startIndex, StringComparison.Ordinal);
//startIndex = start + end;
start = Util.GetCharacterIndex(line, startIndex, ref byteCount, start);
if (index < count) {
startIndex = start + end;
byteCount += Util.GetUTF8ByteCount(text);
}
}
}
range.Start = start;
range.Length = end;
}
return matches;
}
}
internal struct MatchTextRange {
public int Start;
public int Length;
public bool Space;
}
internal enum OutputLineType {
Path,
Match,
Context,
Separator,
Summary,
}
internal sealed class OutputLine {
public OutputLineType LineType;
public int Number;
public string? Text;
public MatchTextRange[]? Matches;
}
}