From 67888c7cfe2427183d943c22ce972725d89eb849 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 3 Apr 2025 16:04:20 -0700 Subject: [PATCH] grep search cleanup, glob search tool --- .../providers/SearchContextProvider.ts | 4 +- core/tools/implementations/globSearch.ts | 4 +- core/tools/implementations/grepSearch.ts | 5 +- core/util/grepSearch.ts | 61 +++++++++++++++++++ extensions/vscode/package-lock.json | 4 +- extensions/vscode/src/VsCodeIde.ts | 50 ++------------- 6 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 core/util/grepSearch.ts diff --git a/core/context/providers/SearchContextProvider.ts b/core/context/providers/SearchContextProvider.ts index bc44964ac..ec75994c3 100644 --- a/core/context/providers/SearchContextProvider.ts +++ b/core/context/providers/SearchContextProvider.ts @@ -3,6 +3,7 @@ import { ContextProviderDescription, ContextProviderExtras, } from "../../index.js"; +import { formatGrepSearchResults } from "../../util/grepSearch.js"; import { BaseContextProvider } from "../index.js"; class SearchContextProvider extends BaseContextProvider { @@ -19,10 +20,11 @@ class SearchContextProvider extends BaseContextProvider { extras: ContextProviderExtras, ): Promise { const results = await extras.ide.getSearchResults(query); + const formatted = formatGrepSearchResults(results); return [ { description: "Search results", - content: `Results of searching codebase for "${query}":\n\n${results}`, + content: `Results of searching codebase for "${query}":\n\n${formatted}`, name: "Search results", }, ]; diff --git a/core/tools/implementations/globSearch.ts b/core/tools/implementations/globSearch.ts index 2d74c533a..c0b214357 100644 --- a/core/tools/implementations/globSearch.ts +++ b/core/tools/implementations/globSearch.ts @@ -1,12 +1,12 @@ import { ToolImpl } from "."; export const fileGlobSearchImpl: ToolImpl = async (args, extras) => { - // const content = await extras.ide.getSearchResults(args.query); + const results = await extras.ide.getFileResults(args.pattern); return [ { name: "File results", description: "Results from file glob search", - content: "", + content: results.join("\n"), }, ]; }; diff --git a/core/tools/implementations/grepSearch.ts b/core/tools/implementations/grepSearch.ts index cdbf747ea..d618791e8 100644 --- a/core/tools/implementations/grepSearch.ts +++ b/core/tools/implementations/grepSearch.ts @@ -1,12 +1,13 @@ import { ToolImpl } from "."; +import { formatGrepSearchResults } from "../../util/grepSearch"; export const grepSearchImpl: ToolImpl = async (args, extras) => { - const content = await extras.ide.getSearchResults(args.query); + const results = await extras.ide.getSearchResults(args.query); return [ { name: "Search results", description: "Results from exact search", - content, + content: formatGrepSearchResults(results), }, ]; }; diff --git a/core/util/grepSearch.ts b/core/util/grepSearch.ts new file mode 100644 index 000000000..a30982e77 --- /dev/null +++ b/core/util/grepSearch.ts @@ -0,0 +1,61 @@ +export function formatGrepSearchResults(results: string): string { + const keepLines: string[] = []; + + function countLeadingSpaces(line: string) { + return line?.match(/^ */)?.[0].length ?? 0; + } + + const processResult = (lines: string[]) => { + // Skip results in which only the file path was kept + const resultPath = lines[0]; + const resultContent = lines.slice(1); + if (resultContent.length === 0) { + return; + } + + // Add path + keepLines.push(resultPath); + + // Find the minimum indentation of content lines + let minIndent = Infinity; + for (const line of resultContent) { + const indent = countLeadingSpaces(line); + if (indent < minIndent) { + minIndent = indent; + } + } + + // Make all lines line up to 2-space indent + const changeIndentBy = 2 - minIndent; + if (changeIndentBy === 0) { + keepLines.push(...resultContent); + } else if (changeIndentBy < 0) { + keepLines.push( + ...resultContent.map((line) => line.substring(-changeIndentBy)), + ); + } else { + keepLines.push( + ...resultContent.map((line) => " ".repeat(changeIndentBy) + line), + ); + } + }; + + let resultLines: string[] = []; + for (const line of results.split("\n").filter((l) => !!l)) { + if (line.startsWith("./") || line === "--") { + processResult(resultLines); // process previous result + resultLines = [line]; + continue; + } + + // Exclude leading zero- or single-char lines + if (resultLines.length === 1 && line.trim().length <= 1) { + continue; + } + + resultLines.push(line); + } + processResult(resultLines); + + return keepLines.join("\n"); +} diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index c01a1eb12..a26290f1a 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "1.1.18", + "version": "1.1.19", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "1.1.18", + "version": "1.1.19", "license": "Apache-2.0", "dependencies": { "@continuedev/fetch": "^1.0.3", diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 5bc042ba5..ba8f791ca 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -556,13 +556,13 @@ class VsCodeIde implements IDE { } async getFileResults(pattern: string): Promise { - vscode.workspace.findFiles(""); - return []; + // vscode.workspace.findFiles(""); + return ["file1", "file2"]; } async getSearchResults(query: string): Promise { if (vscode.env.remoteName) { - return "Exact search not supported for remote workspaces"; + return "Ripgrep not supported, this workspace is remote"; } const results: string[] = []; for (const dir of await this.getWorkspaceDirs()) { @@ -604,50 +604,10 @@ class VsCodeIde implements IDE { }); }); - 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,${sectionIndent}}`), ""); - // } - - 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(line); - } - continue; - } - // Exclude trailing - // TODO may exclude wanted results for lines that look like - // ./filename - // thisThing - // relevantThing - // - if (countLeadingSpaces(line) >= sectionIndent) { - keepLines.push(line); - } - } - results.push(keepLines.join("\n")); + results.push(dirResults); } - return results.join("\n\n"); + return results.join("\n"); } async getProblems(fileUri?: string | undefined): Promise {