@mrsf/cli 0.4.2 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/fuzzy.js +152 -15
- package/package.json +1 -1
package/dist/lib/fuzzy.js
CHANGED
|
@@ -110,6 +110,37 @@ export function normalizedMatch(lines, needle) {
|
|
|
110
110
|
function tokenize(text) {
|
|
111
111
|
return text.split(/\s+/).filter((t) => t.length > 0);
|
|
112
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Build a frequency map for a token array. Used for fast multiset
|
|
115
|
+
* intersection during the fuzzySearch prefilter.
|
|
116
|
+
*/
|
|
117
|
+
function buildTokenFreq(tokens) {
|
|
118
|
+
const freq = new Map();
|
|
119
|
+
for (const tok of tokens) {
|
|
120
|
+
freq.set(tok, (freq.get(tok) ?? 0) + 1);
|
|
121
|
+
}
|
|
122
|
+
return freq;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Multiset intersection size: sum over needle tokens of
|
|
126
|
+
* min(needleFreq[token], windowFreq[token]). This is an upper bound on
|
|
127
|
+
* the longest common subsequence length, so it lets us cheaply skip
|
|
128
|
+
* windows that cannot possibly reach the score threshold.
|
|
129
|
+
*/
|
|
130
|
+
function multisetIntersection(needleFreq, windowFreq) {
|
|
131
|
+
let common = 0;
|
|
132
|
+
// Iterate the smaller map for speed.
|
|
133
|
+
const [smaller, larger] = needleFreq.size <= windowFreq.size
|
|
134
|
+
? [needleFreq, windowFreq]
|
|
135
|
+
: [windowFreq, needleFreq];
|
|
136
|
+
for (const [tok, cnt] of smaller) {
|
|
137
|
+
const other = larger.get(tok);
|
|
138
|
+
if (other !== undefined) {
|
|
139
|
+
common += cnt < other ? cnt : other;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return common;
|
|
143
|
+
}
|
|
113
144
|
/**
|
|
114
145
|
* Longest Common Subsequence length of two token arrays.
|
|
115
146
|
*/
|
|
@@ -199,38 +230,144 @@ export function fuzzySearch(lines, needle, threshold = 0.6, hintLine) {
|
|
|
199
230
|
const needleLines = needle.split("\n");
|
|
200
231
|
const needleLineCount = needleLines.length;
|
|
201
232
|
const candidates = [];
|
|
233
|
+
// Pre-tokenize: do this once per call instead of per window. Per-line
|
|
234
|
+
// tokenization gives identical results to tokenize(lines.join("\n")) since
|
|
235
|
+
// tokenize splits on /\s+/ and filters empties.
|
|
236
|
+
const lineTokens = new Array(lines.length);
|
|
237
|
+
const lineTokenLengths = new Array(lines.length);
|
|
238
|
+
for (let i = 0; i < lines.length; i++) {
|
|
239
|
+
const toks = lines[i] ? tokenize(lines[i]) : [];
|
|
240
|
+
lineTokens[i] = toks;
|
|
241
|
+
lineTokenLengths[i] = toks.length;
|
|
242
|
+
}
|
|
243
|
+
const needleTokens = tokenize(needle);
|
|
244
|
+
const needleTokenCount = needleTokens.length;
|
|
245
|
+
const needleFreq = buildTokenFreq(needleTokens);
|
|
246
|
+
// The combined score is 0.6 * tScore + 0.4 * lScore. Since lScore <= 1.0,
|
|
247
|
+
// a window can only reach `threshold` if tScore >= (threshold - 0.4) / 0.6.
|
|
248
|
+
// Use this as a hard prefilter before doing the expensive LCS / Levenshtein.
|
|
249
|
+
const minTScore = Math.max(0, (threshold - 0.4) / 0.6);
|
|
202
250
|
// Window sizes: ±30% of original line count, minimum 1
|
|
203
251
|
const minWindow = Math.max(1, Math.floor(needleLineCount * 0.7));
|
|
204
252
|
const maxWindow = Math.min(lines.length - 1, Math.ceil(needleLineCount * 1.3) + 1);
|
|
253
|
+
// Track total token count per window using a rolling sum so we don't
|
|
254
|
+
// re-walk the line-token arrays for every (winSize, startLine) pair.
|
|
205
255
|
for (let winSize = minWindow; winSize <= maxWindow; winSize++) {
|
|
256
|
+
if (winSize < 1)
|
|
257
|
+
continue;
|
|
258
|
+
if (winSize > lines.length - 1)
|
|
259
|
+
continue;
|
|
260
|
+
// Initial window token sum for startLine = 1.
|
|
261
|
+
let runningTokenCount = 0;
|
|
262
|
+
for (let i = 1; i < 1 + winSize && i < lines.length; i++) {
|
|
263
|
+
runningTokenCount += lineTokenLengths[i];
|
|
264
|
+
}
|
|
206
265
|
for (let startLine = 1; startLine + winSize - 1 < lines.length; startLine++) {
|
|
207
|
-
const
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
266
|
+
const endLineExclusive = startLine + winSize;
|
|
267
|
+
const M = runningTokenCount;
|
|
268
|
+
// Cheap skip: empty windows or empty needle can't fuzzy-match.
|
|
269
|
+
if (needleTokenCount > 0 && M > 0) {
|
|
270
|
+
// Build a frequency map for the window tokens. Cheaper than LCS:
|
|
271
|
+
// O(M) instead of O(M * needleTokenCount). We allocate a fresh map
|
|
272
|
+
// per window; tried a rolling map and the bookkeeping was a wash
|
|
273
|
+
// with realistic windowSize but if profiling demands it can be
|
|
274
|
+
// converted.
|
|
275
|
+
const windowFreq = new Map();
|
|
276
|
+
for (let li = startLine; li < endLineExclusive; li++) {
|
|
277
|
+
const arr = lineTokens[li];
|
|
278
|
+
for (let ti = 0; ti < arr.length; ti++) {
|
|
279
|
+
const tok = arr[ti];
|
|
280
|
+
windowFreq.set(tok, (windowFreq.get(tok) ?? 0) + 1);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
// Multiset intersection is an upper bound on LCS length. If we
|
|
284
|
+
// can't reach minTScore even with a perfect LCS, skip the window.
|
|
285
|
+
const common = multisetIntersection(needleFreq, windowFreq);
|
|
286
|
+
const tScoreUpperBound = common / Math.max(needleTokenCount, M);
|
|
287
|
+
if (tScoreUpperBound >= minTScore) {
|
|
288
|
+
// Build the flat window token array only when we actually need
|
|
289
|
+
// to compute LCS.
|
|
290
|
+
const windowTokens = new Array(M);
|
|
291
|
+
let wi = 0;
|
|
292
|
+
for (let li = startLine; li < endLineExclusive; li++) {
|
|
293
|
+
const arr = lineTokens[li];
|
|
294
|
+
for (let ti = 0; ti < arr.length; ti++) {
|
|
295
|
+
windowTokens[wi++] = arr[ti];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const lcs = lcsLength(needleTokens, windowTokens);
|
|
299
|
+
const tScore = lcs / Math.max(needleTokenCount, M);
|
|
300
|
+
if (tScore >= minTScore) {
|
|
301
|
+
// Defer expensive string join + Levenshtein until we know the
|
|
302
|
+
// tScore could plausibly reach threshold.
|
|
303
|
+
const windowLines = lines.slice(startLine, endLineExclusive);
|
|
304
|
+
const windowText = windowLines.join("\n");
|
|
305
|
+
let lScore;
|
|
306
|
+
if (needle.length < 500 && windowText.length < 500) {
|
|
307
|
+
lScore = levenshteinScore(needle, windowText);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
lScore = tScore; // fall back to token score only
|
|
311
|
+
}
|
|
312
|
+
const score = tScore * 0.6 + lScore * 0.4;
|
|
313
|
+
if (score >= threshold) {
|
|
314
|
+
candidates.push({
|
|
315
|
+
text: windowText,
|
|
316
|
+
line: startLine,
|
|
317
|
+
endLine: startLine + winSize - 1,
|
|
318
|
+
startColumn: 0,
|
|
319
|
+
endColumn: windowLines[windowLines.length - 1].length,
|
|
320
|
+
score,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// Slide window by one: drop startLine token count, add endLine token count.
|
|
327
|
+
const nextStart = startLine + 1;
|
|
328
|
+
const nextEndExclusive = nextStart + winSize;
|
|
329
|
+
if (nextEndExclusive <= lines.length) {
|
|
330
|
+
runningTokenCount += lineTokenLengths[endLineExclusive] - lineTokenLengths[startLine];
|
|
219
331
|
}
|
|
220
332
|
}
|
|
221
333
|
}
|
|
222
|
-
// For single-line needles, also try substring matching within each line
|
|
334
|
+
// For single-line needles, also try substring matching within each line.
|
|
335
|
+
// Prefilter each line by token overlap before running the inner sliding
|
|
336
|
+
// window — long lines without enough needle-token coverage are skipped
|
|
337
|
+
// wholesale, since no substring of such a line can exceed its parent
|
|
338
|
+
// line's token-overlap with the needle.
|
|
223
339
|
if (needleLineCount === 1 && needle.length < 200) {
|
|
340
|
+
// The minimum number of needle tokens that must appear in a line for any
|
|
341
|
+
// substring of that line to plausibly reach minTScore. tScore =
|
|
342
|
+
// lcs / max(needleN, subN), so lcs >= minTScore * needleN is required,
|
|
343
|
+
// and lcs <= number of common tokens.
|
|
344
|
+
const requiredLineCommon = Math.max(0, Math.ceil(needleTokenCount * minTScore));
|
|
224
345
|
for (let lineNum = 1; lineNum < lines.length; lineNum++) {
|
|
225
346
|
const line = lines[lineNum];
|
|
226
347
|
if (!line)
|
|
227
348
|
continue;
|
|
228
|
-
|
|
349
|
+
if (needleTokenCount > 0 && requiredLineCommon > 0) {
|
|
350
|
+
const lineFreq = buildTokenFreq(lineTokens[lineNum]);
|
|
351
|
+
const lineCommon = multisetIntersection(needleFreq, lineFreq);
|
|
352
|
+
if (lineCommon < requiredLineCommon)
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
// Sliding window within line. Step by 1 by default for accuracy, but
|
|
356
|
+
// for very long lines and long needles, we coarsen the column step to
|
|
357
|
+
// avoid quadratic-in-line-length behaviour. The natural `len = winLen`
|
|
358
|
+
// is always swept densely so we don't miss the most likely match.
|
|
229
359
|
const winLen = needle.length;
|
|
230
360
|
const minWinLen = Math.max(3, Math.floor(winLen * 0.7));
|
|
231
361
|
const maxWinLen = Math.min(line.length, Math.ceil(winLen * 1.3));
|
|
232
362
|
for (let len = minWinLen; len <= maxWinLen; len++) {
|
|
233
|
-
|
|
363
|
+
// Coarsen the column step proportionally to needle length once the
|
|
364
|
+
// sub-window is no longer the natural needle length. This trades a
|
|
365
|
+
// tiny bit of recall for an order-of-magnitude speedup on long
|
|
366
|
+
// lines with many candidate offsets.
|
|
367
|
+
const colStep = len === winLen
|
|
368
|
+
? 1
|
|
369
|
+
: Math.max(1, Math.floor(winLen / 16));
|
|
370
|
+
for (let col = 0; col + len <= line.length; col += colStep) {
|
|
234
371
|
const sub = line.substring(col, col + len);
|
|
235
372
|
const score = combinedScore(needle, sub);
|
|
236
373
|
if (score >= threshold) {
|