core: add optional dirs parameter to file search API

Allow users to exclude directories from file search results by setting dirs=false parameter in /find/file endpoint
This commit is contained in:
Dax Raad
2025-11-03 11:53:38 -05:00
parent 66eb846e6f
commit 07bb75f086
2 changed files with 18 additions and 6 deletions

View File

@@ -165,7 +165,11 @@ export namespace File {
const project = Instance.project const project = Instance.project
if (project.vcs !== "git") return [] if (project.vcs !== "git") return []
const diffOutput = await $`git diff --numstat HEAD`.cwd(Instance.directory).quiet().nothrow().text() const diffOutput = await $`git diff --numstat HEAD`
.cwd(Instance.directory)
.quiet()
.nothrow()
.text()
const changedFiles: Info[] = [] const changedFiles: Info[] = []
@@ -257,9 +261,14 @@ export namespace File {
if (project.vcs === "git") { if (project.vcs === "git") {
let diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text() let diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text()
if (!diff.trim()) diff = await $`git diff --staged ${file}`.cwd(Instance.directory).quiet().nothrow().text() if (!diff.trim())
diff = await $`git diff --staged ${file}`.cwd(Instance.directory).quiet().nothrow().text()
if (diff.trim()) { if (diff.trim()) {
const original = await $`git show HEAD:${file}`.cwd(Instance.directory).quiet().nothrow().text() const original = await $`git show HEAD:${file}`
.cwd(Instance.directory)
.quiet()
.nothrow()
.text()
const patch = structuredPatch(file, file, original, content, "old", "new", { const patch = structuredPatch(file, file, original, content, "old", "new", {
context: Infinity, context: Infinity,
ignoreWhitespace: true, ignoreWhitespace: true,
@@ -307,12 +316,12 @@ export namespace File {
}) })
} }
export async function search(input: { query: string; limit?: number }) { export async function search(input: { query: string; limit?: number; dirs?: boolean }) {
log.info("search", { query: input.query }) log.info("search", { query: input.query })
const limit = input.limit ?? 100 const limit = input.limit ?? 100
const result = await state().then((x) => x.files()) const result = await state().then((x) => x.files())
if (!input.query) return result.dirs.toSorted().slice(0, limit) if (!input.query) return input.dirs !== false ? result.dirs.toSorted().slice(0, limit) : []
const items = [...result.files, ...result.dirs] const items = input.dirs !== false ? [...result.files, ...result.dirs] : result.files
const sorted = fuzzysort.go(input.query, items, { limit: limit }).map((r) => r.target) const sorted = fuzzysort.go(input.query, items, { limit: limit }).map((r) => r.target)
log.info("search", { query: input.query, results: sorted.length }) log.info("search", { query: input.query, results: sorted.length })
return sorted return sorted

View File

@@ -1106,13 +1106,16 @@ export namespace Server {
"query", "query",
z.object({ z.object({
query: z.string(), query: z.string(),
dirs: z.boolean().optional(),
}), }),
), ),
async (c) => { async (c) => {
const query = c.req.valid("query").query const query = c.req.valid("query").query
const dirs = c.req.valid("query").dirs
const results = await File.search({ const results = await File.search({
query, query,
limit: 10, limit: 10,
dirs,
}) })
return c.json(results) return c.json(results)
}, },