From b913c36ccbcec86bb26d0c93b17dd29301aae399 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 17 Mar 2025 09:51:04 -0700 Subject: [PATCH] filter leading/trailing lines( VS Code) --- .../continue/IntelliJIde.kt | 2 +- extensions/vscode/src/VsCodeIde.ts | 47 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt index ade988875..f6a9e5be6 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/IntelliJIde.kt @@ -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 } diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 1aaa10283..9d194f22a 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -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 { - 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");