forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRipgrep.re
325 lines (290 loc) · 7.54 KB
/
Ripgrep.re
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
open Kernel;
open Utility;
module Time = Revery_Core.Time;
module Match = {
module Log = (val Log.withNamespace("Oni2.Core.Ripgrep.Match"));
type t = {
file: string,
text: string,
lineNumber: int,
charStart: int,
charEnd: int,
};
let fromJsonString = str => {
Yojson.Basic.Util.(
try({
let json = Yojson.Basic.from_string(str);
if (member("type", json) == `String("match")) {
let data = member("data", json);
let submatches = data |> member("submatches") |> to_list;
let matches =
submatches
|> List.map(submatch =>
{
file:
data |> member("path") |> member("text") |> to_string,
text:
data |> member("lines") |> member("text") |> to_string,
lineNumber: data |> member("line_number") |> to_int,
charStart: submatch |> member("start") |> to_int,
charEnd: submatch |> member("end") |> to_int,
}
);
Some(matches);
} else {
None; // Not a "match" message
};
}) {
| Type_error(message, _) =>
Log.warn("Error decoding JSON: " ++ message);
None;
| Yojson.Json_error(message) =>
Log.warn("Error parsing JSON: " ++ message);
None;
}
);
};
};
module Log = (val Log.withNamespace("Oni2.Core.Ripgrep"));
type t = {
search:
(
~filesExclude: list(string),
~directory: string,
~onUpdate: list(string) => unit,
~onComplete: unit => unit,
~onError: string => unit
) =>
dispose,
findInFiles:
(
~searchExclude: list(string),
~searchInclude: list(string),
~directory: string,
~query: string,
~onUpdate: list(Match.t) => unit,
~onComplete: unit => unit,
~onError: string => unit,
~enableRegex: bool=?,
~caseSensitive: bool=?,
unit
) =>
dispose,
}
and dispose = unit => unit;
/**
RipgrepProcessingJob is the logic for processing a [Bytes.t]
and sending it back to whatever is listening to Ripgrep.
Each iteration of work, it will split up a [Bytes.t] (a chunk of output),
get a list of files, and send that to the callback.
*/
module RipgrepProcessingJob = {
type pendingWork = {
onUpdate: list(string) => unit,
queue: Queue.t(Bytes.t),
};
let pendingWorkPrinter = pending =>
Printf.sprintf("Byte chunks left: %n", Queue.length(pending.queue));
type t = Job.t(pendingWork, unit);
let doWork = (pending, completed) => {
let queue =
switch (Queue.pop(pending.queue)) {
| (None, queue) => queue
| (Some(bytes), queue) =>
let items =
bytes
|> Bytes.to_string
|> String.trim
|> String.split_on_char('\n');
pending.onUpdate(items);
queue;
};
(Queue.isEmpty(pending.queue), {...pending, queue}, completed);
};
let create = (~onUpdate) => {
Job.create(
~f=doWork,
~initialCompletedWork=(),
~name="RipgrepProcessingJob",
~pendingWorkPrinter,
~budget=Time.ms(2),
{onUpdate, queue: Queue.empty},
);
};
let queueWork = (bytes: Bytes.t, currentJob: t) => {
Job.map(
(pending, completed) => {
let queue = Queue.push(bytes, pending.queue);
(false, {...pending, queue}, completed);
},
currentJob,
);
};
};
let process = (rgPath, args, onUpdate, onComplete, onError) => {
let on_exit = (_, ~exit_status: int64, ~term_signal as _) => {
Log.debugf(m =>
m("Process completed - exit code: %n", exit_status |> Int64.to_int)
);
};
Log.debugf(m =>
m(
"Starting process: %s with args: |%s|",
rgPath,
args |> String.concat(" "),
)
);
let processResult =
Luv.Pipe.init()
|> ResultEx.flatMap(pipe => {
LuvEx.Process.spawn(
~on_exit,
~redirect=[
Luv.Process.to_parent_pipe(
~fd=Luv.Process.stdout,
~parent_pipe=pipe,
(),
),
],
rgPath,
[rgPath, ...args],
)
|> Result.map(process => (process, pipe))
});
switch (processResult) {
| Error(err) =>
let errMsg = err |> Luv.Error.strerror;
Log.error(errMsg);
onError(errMsg);
(() => ());
| Ok((process, pipe)) =>
let job = ref(RipgrepProcessingJob.create(~onUpdate));
let isRipgrepProcessDone = ref(false);
let disposeTick = ref(None);
let disposeAll = () => {
Log.info("disposeAll");
disposeTick^ |> Option.iter(f => f());
let _: result(unit, Luv.Error.t) = Luv.Process.kill(process, 2);
isRipgrepProcessDone := true;
();
};
let allocator = Utility.LuvEx.allocator("Ripgrep");
Luv.Stream.read_start(
~allocate=allocator,
pipe,
fun
| Error(`EOF) => {
Luv.Handle.close(pipe, ignore);
isRipgrepProcessDone := true;
}
| Error(msg) => {
disposeAll();
let errMsg = msg |> Luv.Error.strerror;
onError(errMsg);
}
| Ok(buffer) => {
let bytes = Luv.Buffer.to_bytes(buffer);
job := RipgrepProcessingJob.queueWork(bytes, job^);
},
);
disposeTick :=
Some(
Revery.Tick.interval(
~name="Ripgrep - Processing Ticker",
_ =>
if (!Job.isComplete(job^)) {
job := Job.tick(job^);
} else if (isRipgrepProcessDone^) {
onComplete();
disposeAll();
},
Time.zero,
),
);
disposeAll;
};
};
/**
Search through files of the directory passed in and sort the results in
order of the last time they were accessed, alternative sort order includes
path, modified, created
*/
let search =
(
~executablePath,
~filesExclude,
~directory,
~onUpdate,
~onComplete,
~onError,
) => {
let dedup = {
let seen = Hashtbl.create(1000);
List.filter(str => {
switch (Hashtbl.find_opt(seen, str)) {
| Some(_) => false
| None =>
Hashtbl.add(seen, str, true);
true;
}
});
};
let globs =
filesExclude
|> List.map(x => "!" ++ x)
|> List.map(x => ["-g", x])
|> List.concat;
let args = globs @ ["--smart-case", "--hidden", "--files", "--", directory];
process(
executablePath,
args,
items => items |> dedup |> onUpdate,
onComplete,
onError,
);
};
let findInFiles =
(
~executablePath,
~searchExclude,
~searchInclude,
~directory,
~query,
~onUpdate,
~onComplete,
~onError,
~enableRegex=false,
~caseSensitive=false,
(),
) => {
let excludeArgs =
searchExclude
|> List.filter(str => !StringEx.isEmpty(str))
|> List.concat_map(x => ["-g", "!" ++ x]);
let includeArgs =
searchInclude
|> List.filter(str => !StringEx.isEmpty(str))
|> List.concat_map(x => ["-g", x]);
let args =
excludeArgs
@ includeArgs
@ (enableRegex ? [] : ["--fixed-strings"])
@ (caseSensitive ? ["--case-sensitive"] : ["--ignore-case"])
@ ["--hidden", "--json", "--", query, directory];
process(
executablePath,
args,
items => {
items
|> List.filter_map(Match.fromJsonString)
|> ListEx.safeConcat
|> onUpdate
},
onComplete,
onError,
);
};
let make = (~executablePath) => {
search: search(~executablePath),
findInFiles: findInFiles(~executablePath),
};