forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathripgrepSearcher.js
More file actions
380 lines (320 loc) · 12.3 KB
/
ripgrepSearcher.js
File metadata and controls
380 lines (320 loc) · 12.3 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Modified version of https://github.com/atom/atom/blob/master/src/ripgrep-directory-searcher.js
//
// Copyright (c) 2011-2019 GitHub Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import { spawn } from 'child_process'
import path from 'path'
function cleanResultLine (resultLine) {
resultLine = getText(resultLine)
return resultLine[resultLine.length - 1] === '\n' ? resultLine.slice(0, -1) : resultLine
}
function getPositionFromColumn (lines, column) {
let currentLength = 0
let currentLine = 0
let previousLength = 0
while (column >= currentLength) {
previousLength = currentLength
currentLength += lines[currentLine].length + 1
currentLine++
}
return [currentLine - 1, column - previousLength]
}
function processUnicodeMatch (match) {
const text = getText(match.lines)
if (text.length === Buffer.byteLength(text)) {
// fast codepath for lines that only contain characters of 1 byte length.
return
}
let remainingBuffer = Buffer.from(text)
let currentLength = 0
let previousPosition = 0
function convertPosition (position) {
const currentBuffer = remainingBuffer.slice(0, position - previousPosition)
currentLength = currentBuffer.toString().length + currentLength
remainingBuffer = remainingBuffer.slice(position - previousPosition)
previousPosition = position
return currentLength
}
// Iterate over all the submatches to find the convert the start and end values
// (which come as bytes from ripgrep) to character positions.
// We can do this because submatches come ordered by position.
for (const submatch of match.submatches) {
submatch.start = convertPosition(submatch.start)
submatch.end = convertPosition(submatch.end)
}
}
// This function processes a ripgrep submatch to create the correct
// range. This is mostly needed for multi-line results, since the range
// will have differnt start and end rows and we need to calculate these
// based on the lines that ripgrep returns.
function processSubmatch (submatch, lineText, offsetRow) {
const lineParts = lineText.split('\n')
const start = getPositionFromColumn(lineParts, submatch.start)
const end = getPositionFromColumn(lineParts, submatch.end)
// Make sure that the lineText string only contains lines that are
// relevant to this submatch. This means getting rid of lines above
// the start row and below the end row.
for (let i = start[0]; i > 0; i--) {
lineParts.shift()
}
while (end[0] < lineParts.length - 1) {
lineParts.pop()
}
start[0] += offsetRow
end[0] += offsetRow
return {
range: [start, end],
lineText: cleanResultLine({ text: lineParts.join('\n') })
}
}
function getText (input) {
return 'text' in input ? input.text : Buffer.from(input.bytes, 'base64').toString()
}
class RipgrepDirectorySearcher {
constructor () {
this.rgPath = global.marktext.paths.ripgrepBinaryPath
}
// Performs a text search for files in the specified `Directory`s, subject to the
// specified parameters.
//
// Results are streamed back to the caller by invoking methods on the specified `options`,
// such as `didMatch`.
//
// * `directories` {Array} of absolute {string} paths to search.
// * `pattern` {string} to search with.
// * `options` {Object} with the following properties:
// * `didMatch` {Function} call with a search result structured as follows:
// * `searchResult` {Object} with the following keys:
// * `filePath` {String} absolute path to the matching file.
// * `matches` {Array} with object elements with the following keys:
// * `lineText` {String} The full text of the matching line (without a line terminator character).
// * `matchText` {String} The text that matched the `regex` used for the search.
// * `range` {Range} Identifies the matching region in the file. (Likely as an array of numeric arrays.)
// * `didSearchPaths` {Function} periodically call with the number of paths searched that contain results thus far.
// * `inclusions` {Array} of glob patterns (as strings) to search within. Note that this
// array may be empty, indicating that all files should be searched.
//
// Each item in the array is a file/directory pattern, e.g., `src` to search in the "src"
// directory or `*.js` to search all JavaScript files. In practice, this often comes from the
// comma-delimited list of patterns in the bottom text input of the ProjectFindView dialog.
// * `noIgnore` {boolean} whether to ignore ignore files like `.gitignore`.
// * `exclusions` {Array} similar to inclusions
// * `followSymlinks` {boolean} whether symlinks should be followed.
// * `isWholeWord` {boolean} whether to search for whole words.
// * `isRegexp` {boolean} whether `pattern` is a RegEx.
// * `isCaseSensitive` {boolean} whether to search case sensitive or not.
// * `maxFileSize` {number} the maximal file size.
// * `includeHidden` {boolean} whether to search in hidden files and directories.
// Returns a *thenable* `DirectorySearch` that includes a `cancel()` method. If `cancel()` is
// invoked before the `DirectorySearch` is determined, it will resolve the `DirectorySearch`.
search (directories, pattern, options) {
const numPathsFound = { num: 0 }
const allPromises = directories.map(
directory => this.searchInDirectory(directory, pattern, options, numPathsFound)
)
const promise = Promise.all(allPromises)
promise.cancel = () => {
for (const promise of allPromises) {
promise.cancel()
}
}
return promise
}
searchInDirectory (directoryPath, pattern, options, numPathsFound) {
let regexpStr = null
let textPattern = null
const args = ['--json']
if (options.isRegexp) {
regexpStr = this.prepareRegexp(pattern)
args.push('--regexp', regexpStr)
} else {
args.push('--fixed-strings')
textPattern = pattern
}
if (regexpStr && this.isMultilineRegexp(regexpStr)) {
args.push('--multiline')
}
if (options.isCaseSensitive) {
args.push('--case-sensitive')
} else {
args.push('--ignore-case')
}
if (options.isWholeWord) {
args.push('--word-regexp')
}
if (options.followSymlinks) {
args.push('--follow')
}
if (options.maxFileSize) {
args.push('--max-filesize', options.maxFileSize + '')
}
if (options.includeHidden) {
args.push('--hidden')
}
if (options.noIgnore) {
args.push('--no-ignore')
}
if (options.leadingContextLineCount) {
args.push('--before-context', options.leadingContextLineCount)
}
if (options.trailingContextLineCount) {
args.push('--after-context', options.trailingContextLineCount)
}
for (const inclusion of this.prepareGlobs(options.inclusions, directoryPath)) {
args.push('--iglob', inclusion)
}
for (const exclusion of this.prepareGlobs(options.exclusions, directoryPath)) {
args.push('--iglob', '!' + exclusion)
}
args.push('--')
if (textPattern) {
args.push(textPattern)
}
args.push(directoryPath)
let child = null
try {
child = spawn(this.rgPath, args, {
cwd: directoryPath,
stdio: ['pipe', 'pipe', 'pipe']
})
} catch (err) {
return Promise.reject(err)
}
const didMatch = options.didMatch || (() => {})
let cancelled = false
const returnedPromise = new Promise((resolve, reject) => {
let buffer = ''
let bufferError = ''
let pendingEvent
let pendingLeadingContext
let pendingTrailingContexts
child.on('close', (code, signal) => {
// code 1 is used when no results are found.
if (code !== null && code > 1) {
reject(new Error(bufferError))
} else {
resolve()
}
})
child.on('error', err => {
reject(err)
})
child.stderr.on('data', chunk => {
bufferError += chunk
})
child.stdout.on('data', chunk => {
if (cancelled) {
return
}
buffer += chunk
const lines = buffer.split('\n')
buffer = lines.pop()
for (const line of lines) {
const message = JSON.parse(line)
if (message.type === 'begin') {
pendingEvent = {
filePath: getText(message.data.path),
matches: []
}
pendingLeadingContext = []
pendingTrailingContexts = new Set()
} else if (message.type === 'match') {
const trailingContextLines = []
pendingTrailingContexts.add(trailingContextLines)
processUnicodeMatch(message.data)
for (const submatch of message.data.submatches) {
const { lineText, range } = processSubmatch(
submatch,
getText(message.data.lines),
message.data.line_number - 1
)
pendingEvent.matches.push({
matchText: getText(submatch.match),
lineText,
range,
leadingContextLines: [...pendingLeadingContext],
trailingContextLines
})
}
} else if (message.type === 'end') {
options.didSearchPaths(++numPathsFound.num)
didMatch(pendingEvent)
pendingEvent = null
}
}
})
})
returnedPromise.cancel = () => {
child.kill()
cancelled = true
}
return returnedPromise
}
// We need to prepare the "globs" that we receive from the user to make their behaviour more
// user-friendly (e.g when adding `src/` the user probably means `src/**/*`).
// This helper function takes care of that.
prepareGlobs (globs, projectRootPath) {
const output = []
for (let pattern of globs) {
// we need to replace path separators by slashes since globs should
// always use always slashes as path separators.
pattern = pattern.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
if (pattern.length === 0) {
continue
}
const projectName = path.basename(projectRootPath)
// The user can just search inside one of the opened projects. When we detect
// this scenario we just consider the glob to include every file.
if (pattern === projectName) {
output.push('**/*')
continue
}
if (pattern.startsWith(projectName + '/')) {
pattern = pattern.slice(projectName.length + 1)
}
if (pattern.endsWith('/')) {
pattern = pattern.slice(0, -1)
}
pattern = pattern.startsWith('**/') ? pattern : `**/${pattern}`
output.push(pattern)
output.push(pattern.endsWith('/**') ? pattern : `${pattern}/**`)
}
return output
}
prepareRegexp (regexpStr) {
// ripgrep handles `--` as the arguments separator, so we need to escape it if the
// user searches for that exact same string.
if (regexpStr === '--') {
return '\\-\\-'
}
// ripgrep is quite picky about unnecessarily escaped sequences, so we need to unescape
// them: https://github.com/BurntSushi/ripgrep/issues/434.
regexpStr = regexpStr.replace(/\\\//g, '/')
return regexpStr
}
isMultilineRegexp (regexpStr) {
if (regexpStr.includes('\\n')) {
return true
}
return false
}
}
export default RipgrepDirectorySearcher