Skip to content

Commit

Permalink
Make server-side search respect Context Lines feature of Search Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
gjsjohnmurray committed Dec 28, 2023
1 parent da772e5 commit 248e048
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/providers/FileSystemProvider/TextSearchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
.map((match: SearchMatch) => searchMatchToLine(content, match, file.doc, api.configName))
.filter(notNull);
// Filter out duplicates and compute all matches for each one
[...new Set(lines)].forEach((line) => {
[...new Set(lines)].forEach((line, _index, matchedLines) => {
const text = content[line];
const previewFrom = Math.max(line - options.beforeContext || 0, 0);
const previewTo = Math.min(line + options.afterContext || 0, content.length - 1);
const regex = new RegExp(
query.isRegExp ? query.pattern : query.pattern.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"),
query.isCaseSensitive ? "g" : "gi"
Expand All @@ -344,6 +346,16 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
counter++;
}
if (matchRanges.length && previewRanges.length) {
// Add preceding context lines that aren't result lines
for (let i = previewFrom; i < line; i++) {
if (!matchedLines.includes(i)) {
progress.report({
uri,
text: content[i],
lineNumber: i + 1,
});
}
}
progress.report({
uri,
ranges: matchRanges,
Expand All @@ -352,6 +364,16 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
matches: previewRanges,
},
});
// Add following context lines that aren't result lines
for (let i = line + 1; i <= previewTo; i++) {
if (!matchedLines.includes(i)) {
progress.report({
uri,
text: content[i],
lineNumber: i + 1,
});
}
}
}
});
};
Expand Down

0 comments on commit 248e048

Please sign in to comment.