grep search cleanup, glob search tool

This commit is contained in:
Dallin Romney 2025-04-03 16:04:20 -07:00
parent b9e4737c77
commit 67888c7cfe
6 changed files with 76 additions and 52 deletions

View file

@ -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<ContextItem[]> {
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",
},
];

View file

@ -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"),
},
];
};

View file

@ -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),
},
];
};

61
core/util/grepSearch.ts Normal file
View file

@ -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");
}

View file

@ -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",

View file

@ -556,13 +556,13 @@ class VsCodeIde implements IDE {
}
async getFileResults(pattern: string): Promise<string[]> {
vscode.workspace.findFiles("");
return [];
// vscode.workspace.findFiles("");
return ["file1", "file2"];
}
async getSearchResults(query: string): Promise<string> {
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<Problem[]> {