filter leading/trailing lines( VS Code)

This commit is contained in:
Dallin Romney 2025-03-17 09:51:04 -07:00
parent 432221b87d
commit b913c36ccb
2 changed files with 46 additions and 3 deletions

View file

@ -343,7 +343,7 @@ class IntelliJIDE(
}
override suspend fun getSearchResults(query: String): String {
val command = GeneralCommandLine(ripgrep, "-i", "-C", "2", "--", query, ".")
val command = GeneralCommandLine(ripgrep, "-i", "-C", "2", "--heading", "-e", query, ".")
command.setWorkDirectory(project.basePath)
return ExecUtil.execAndGetOutput(command).stdout
}

View file

@ -545,6 +545,7 @@ class VsCodeIde implements IDE {
"-i", // Case-insensitive search
"-C",
"2", // Show 2 lines of context
"--heading", // Only show filepath once per result
"-e",
query, // Pattern to search for
".", // Directory to search in
@ -573,9 +574,51 @@ class VsCodeIde implements IDE {
}
async getSearchResults(query: string): Promise<string> {
const results = [];
const results: string[] = [];
for (const dir of await this.getWorkspaceDirs()) {
results.push(await this._searchDir(query, dir));
const dirResults = await this._searchDir(query, dir);
const keepLines: string[] = [];
function countLeadingSpaces(line: string) {
return line?.match(/^ */)?.[0].length ?? 0;
}
function formatLine(line: string, sectionIndent: number): string {
return line.replace(new RegExp(`^[ ]{0,${n}}`), "");
}
let leading = false;
let sectionIndent = 0;
let sectionTrim = 0;
for (const line of dirResults.split("\n").filter((l) => !!l)) {
if (line.startsWith("./") || line === "--") {
leading = true;
keepLines.push(line);
continue;
}
if (leading) {
// Exclude leading single-char lines
if (line.trim().length > 1) {
// Record spacing at first non-single char line
leading = false;
sectionIndent = countLeadingSpaces(line);
keepLines.push(formatLine(line, sectionTrim));
}
continue;
}
// Exclude trailing
// TODO may exclude wanted results for lines that look like
// ./filename
// thisThing
// relevantThing
//
if (countLeadingSpaces(line) >= sectionIndent) {
keepLines.push(formatLine(line, sectionIndent));
}
}
results.push(keepLines.join("\n"));
}
return results.join("\n\n");