Skip to content

Make server-side search respect Context Lines feature of Search Editor #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/providers/FileSystemProvider/TextSearchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,20 @@ export class TextSearchProvider implements vscode.TextSearchProvider {

const uri = DocumentContentProvider.getUri(file.doc, "", "", true, options.folder);
const content = decoder.decode(await vscode.workspace.fs.readFile(uri)).split("\n");
const contentLength = content.length;
// Find all lines that we have matches on
const lines = file.matches
.map((match: SearchMatch) => searchMatchToLine(content, match, file.doc, api.configName))
.map((match: SearchMatch) =>
token.isCancellationRequested ? null : searchMatchToLine(content, match, file.doc, api.configName)
)
.filter(notNull);
// Filter out duplicates and compute all matches for each one
[...new Set(lines)].forEach((line) => {
// Remove duplicates and make them quickly searchable
const matchedLines = new Set(lines);
// Compute all matches for each one
matchedLines.forEach((line) => {
if (token.isCancellationRequested) {
return;
}
const text = content[line];
const regex = new RegExp(
query.isRegExp ? query.pattern : query.pattern.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"),
Expand All @@ -345,6 +353,19 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
counter++;
}
if (matchRanges.length && previewRanges.length) {
if (options.beforeContext) {
// Add preceding context lines that aren't themselves result lines
const previewFrom = Math.max(line - options.beforeContext, 0);
for (let i = previewFrom; i < line; i++) {
if (!matchedLines.has(i)) {
progress.report({
uri,
text: content[i],
lineNumber: i + 1,
});
}
}
}
progress.report({
uri,
ranges: matchRanges,
Expand All @@ -353,6 +374,19 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
matches: previewRanges,
},
});
if (options.afterContext) {
// Add following context lines that aren't themselves result lines
const previewTo = Math.min(line + options.afterContext, contentLength - 1);
for (let i = line + 1; i <= previewTo; i++) {
if (!matchedLines.has(i)) {
progress.report({
uri,
text: content[i],
lineNumber: i + 1,
});
}
}
}
}
});
};
Expand Down