fix(app): open project search (#11783)
This commit is contained in:
@@ -4,10 +4,11 @@ import { FileIcon } from "@opencode-ai/ui/file-icon"
|
|||||||
import { List } from "@opencode-ai/ui/list"
|
import { List } from "@opencode-ai/ui/list"
|
||||||
import { getDirectory, getFilename } from "@opencode-ai/util/path"
|
import { getDirectory, getFilename } from "@opencode-ai/util/path"
|
||||||
import fuzzysort from "fuzzysort"
|
import fuzzysort from "fuzzysort"
|
||||||
import { createMemo } from "solid-js"
|
import { createMemo, createResource, createSignal } from "solid-js"
|
||||||
import { useGlobalSDK } from "@/context/global-sdk"
|
import { useGlobalSDK } from "@/context/global-sdk"
|
||||||
import { useGlobalSync } from "@/context/global-sync"
|
import { useGlobalSync } from "@/context/global-sync"
|
||||||
import { useLanguage } from "@/context/language"
|
import { useLanguage } from "@/context/language"
|
||||||
|
import type { ListRef } from "@opencode-ai/ui/list"
|
||||||
|
|
||||||
interface DialogSelectDirectoryProps {
|
interface DialogSelectDirectoryProps {
|
||||||
title?: string
|
title?: string
|
||||||
@@ -15,18 +16,47 @@ interface DialogSelectDirectoryProps {
|
|||||||
onSelect: (result: string | string[] | null) => void
|
onSelect: (result: string | string[] | null) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Row = {
|
||||||
|
absolute: string
|
||||||
|
search: string
|
||||||
|
}
|
||||||
|
|
||||||
export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
||||||
const sync = useGlobalSync()
|
const sync = useGlobalSync()
|
||||||
const sdk = useGlobalSDK()
|
const sdk = useGlobalSDK()
|
||||||
const dialog = useDialog()
|
const dialog = useDialog()
|
||||||
const language = useLanguage()
|
const language = useLanguage()
|
||||||
|
|
||||||
const home = createMemo(() => sync.data.path.home)
|
const [filter, setFilter] = createSignal("")
|
||||||
|
|
||||||
const start = createMemo(() => sync.data.path.home || sync.data.path.directory)
|
let list: ListRef | undefined
|
||||||
|
|
||||||
|
const missingBase = createMemo(() => !(sync.data.path.home || sync.data.path.directory))
|
||||||
|
|
||||||
|
const [fallbackPath] = createResource(
|
||||||
|
() => (missingBase() ? true : undefined),
|
||||||
|
async () => {
|
||||||
|
return sdk.client.path
|
||||||
|
.get()
|
||||||
|
.then((x) => x.data)
|
||||||
|
.catch(() => undefined)
|
||||||
|
},
|
||||||
|
{ initialValue: undefined },
|
||||||
|
)
|
||||||
|
|
||||||
|
const home = createMemo(() => sync.data.path.home || fallbackPath()?.home || "")
|
||||||
|
|
||||||
|
const start = createMemo(
|
||||||
|
() => sync.data.path.home || sync.data.path.directory || fallbackPath()?.home || fallbackPath()?.directory,
|
||||||
|
)
|
||||||
|
|
||||||
const cache = new Map<string, Promise<Array<{ name: string; absolute: string }>>>()
|
const cache = new Map<string, Promise<Array<{ name: string; absolute: string }>>>()
|
||||||
|
|
||||||
|
const clean = (value: string) => {
|
||||||
|
const first = (value ?? "").split(/\r?\n/)[0] ?? ""
|
||||||
|
return first.replace(/[\u0000-\u001F\u007F]/g, "").trim()
|
||||||
|
}
|
||||||
|
|
||||||
function normalize(input: string) {
|
function normalize(input: string) {
|
||||||
const v = input.replaceAll("\\", "/")
|
const v = input.replaceAll("\\", "/")
|
||||||
if (v.startsWith("//") && !v.startsWith("///")) return "//" + v.slice(2).replace(/\/+/g, "/")
|
if (v.startsWith("//") && !v.startsWith("///")) return "//" + v.slice(2).replace(/\/+/g, "/")
|
||||||
@@ -64,24 +94,67 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
function display(path: string) {
|
function parentOf(input: string) {
|
||||||
|
const v = trimTrailing(input)
|
||||||
|
if (v === "/") return v
|
||||||
|
if (v === "//") return v
|
||||||
|
if (/^[A-Za-z]:\/$/.test(v)) return v
|
||||||
|
|
||||||
|
const i = v.lastIndexOf("/")
|
||||||
|
if (i <= 0) return "/"
|
||||||
|
if (i === 2 && /^[A-Za-z]:/.test(v)) return v.slice(0, 3)
|
||||||
|
return v.slice(0, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
function modeOf(input: string) {
|
||||||
|
const raw = normalizeDriveRoot(input.trim())
|
||||||
|
if (!raw) return "relative" as const
|
||||||
|
if (raw.startsWith("~")) return "tilde" as const
|
||||||
|
if (rootOf(raw)) return "absolute" as const
|
||||||
|
return "relative" as const
|
||||||
|
}
|
||||||
|
|
||||||
|
function display(path: string, input: string) {
|
||||||
const full = trimTrailing(path)
|
const full = trimTrailing(path)
|
||||||
|
if (modeOf(input) === "absolute") return full
|
||||||
|
|
||||||
|
return tildeOf(full) || full
|
||||||
|
}
|
||||||
|
|
||||||
|
function tildeOf(absolute: string) {
|
||||||
|
const full = trimTrailing(absolute)
|
||||||
const h = home()
|
const h = home()
|
||||||
if (!h) return full
|
if (!h) return ""
|
||||||
|
|
||||||
const hn = trimTrailing(h)
|
const hn = trimTrailing(h)
|
||||||
const lc = full.toLowerCase()
|
const lc = full.toLowerCase()
|
||||||
const hc = hn.toLowerCase()
|
const hc = hn.toLowerCase()
|
||||||
if (lc === hc) return "~"
|
if (lc === hc) return "~"
|
||||||
if (lc.startsWith(hc + "/")) return "~" + full.slice(hn.length)
|
if (lc.startsWith(hc + "/")) return "~" + full.slice(hn.length)
|
||||||
return full
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
function scoped(filter: string) {
|
function row(absolute: string): Row {
|
||||||
|
const full = trimTrailing(absolute)
|
||||||
|
const tilde = tildeOf(full)
|
||||||
|
|
||||||
|
const withSlash = (value: string) => {
|
||||||
|
if (!value) return ""
|
||||||
|
if (value.endsWith("/")) return value
|
||||||
|
return value + "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
const search = Array.from(
|
||||||
|
new Set([full, withSlash(full), tilde, withSlash(tilde), getFilename(full)].filter(Boolean)),
|
||||||
|
).join("\n")
|
||||||
|
return { absolute: full, search }
|
||||||
|
}
|
||||||
|
|
||||||
|
function scoped(value: string) {
|
||||||
const base = start()
|
const base = start()
|
||||||
if (!base) return
|
if (!base) return
|
||||||
|
|
||||||
const raw = normalizeDriveRoot(filter.trim())
|
const raw = normalizeDriveRoot(value)
|
||||||
if (!raw) return { directory: trimTrailing(base), path: "" }
|
if (!raw) return { directory: trimTrailing(base), path: "" }
|
||||||
|
|
||||||
const h = home()
|
const h = home()
|
||||||
@@ -122,21 +195,25 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const directories = async (filter: string) => {
|
const directories = async (filter: string) => {
|
||||||
const input = scoped(filter)
|
const value = clean(filter)
|
||||||
if (!input) return [] as string[]
|
const scopedInput = scoped(value)
|
||||||
|
if (!scopedInput) return [] as string[]
|
||||||
|
|
||||||
const raw = normalizeDriveRoot(filter.trim())
|
const raw = normalizeDriveRoot(value)
|
||||||
const isPath = raw.startsWith("~") || !!rootOf(raw) || raw.includes("/")
|
const isPath = raw.startsWith("~") || !!rootOf(raw) || raw.includes("/")
|
||||||
|
|
||||||
const query = normalizeDriveRoot(input.path)
|
const query = normalizeDriveRoot(scopedInput.path)
|
||||||
|
|
||||||
if (!isPath) {
|
const find = () =>
|
||||||
const results = await sdk.client.find
|
sdk.client.find
|
||||||
.files({ directory: input.directory, query, type: "directory", limit: 50 })
|
.files({ directory: scopedInput.directory, query, type: "directory", limit: 50 })
|
||||||
.then((x) => x.data ?? [])
|
.then((x) => x.data ?? [])
|
||||||
.catch(() => [])
|
.catch(() => [])
|
||||||
|
|
||||||
return results.map((rel) => join(input.directory, rel)).slice(0, 50)
|
if (!isPath) {
|
||||||
|
const results = await find()
|
||||||
|
|
||||||
|
return results.map((rel) => join(scopedInput.directory, rel)).slice(0, 50)
|
||||||
}
|
}
|
||||||
|
|
||||||
const segments = query.replace(/^\/+/, "").split("/")
|
const segments = query.replace(/^\/+/, "").split("/")
|
||||||
@@ -145,17 +222,10 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
|
|
||||||
const cap = 12
|
const cap = 12
|
||||||
const branch = 4
|
const branch = 4
|
||||||
let paths = [input.directory]
|
let paths = [scopedInput.directory]
|
||||||
for (const part of head) {
|
for (const part of head) {
|
||||||
if (part === "..") {
|
if (part === "..") {
|
||||||
paths = paths.map((p) => {
|
paths = paths.map(parentOf)
|
||||||
const v = trimTrailing(p)
|
|
||||||
if (v === "/") return v
|
|
||||||
if (/^[A-Za-z]:\/$/.test(v)) return v
|
|
||||||
const i = v.lastIndexOf("/")
|
|
||||||
if (i <= 0) return "/"
|
|
||||||
return v.slice(0, i)
|
|
||||||
})
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +235,27 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const out = (await Promise.all(paths.map((p) => match(p, tail, 50)))).flat()
|
const out = (await Promise.all(paths.map((p) => match(p, tail, 50)))).flat()
|
||||||
return Array.from(new Set(out)).slice(0, 50)
|
const deduped = Array.from(new Set(out))
|
||||||
|
const base = raw.startsWith("~") ? trimTrailing(scopedInput.directory) : ""
|
||||||
|
const expand = !raw.endsWith("/")
|
||||||
|
if (!expand || !tail) {
|
||||||
|
const items = base ? Array.from(new Set([base, ...deduped])) : deduped
|
||||||
|
return items.slice(0, 50)
|
||||||
|
}
|
||||||
|
|
||||||
|
const needle = tail.toLowerCase()
|
||||||
|
const exact = deduped.filter((p) => getFilename(p).toLowerCase() === needle)
|
||||||
|
const target = exact[0]
|
||||||
|
if (!target) return deduped.slice(0, 50)
|
||||||
|
|
||||||
|
const children = await match(target, "", 30)
|
||||||
|
const items = Array.from(new Set([...deduped, ...children]))
|
||||||
|
return (base ? Array.from(new Set([base, ...items])) : items).slice(0, 50)
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = async (value: string) => {
|
||||||
|
const results = await directories(value)
|
||||||
|
return results.map(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolve(absolute: string) {
|
function resolve(absolute: string) {
|
||||||
@@ -179,24 +269,52 @@ export function DialogSelectDirectory(props: DialogSelectDirectoryProps) {
|
|||||||
search={{ placeholder: language.t("dialog.directory.search.placeholder"), autofocus: true }}
|
search={{ placeholder: language.t("dialog.directory.search.placeholder"), autofocus: true }}
|
||||||
emptyMessage={language.t("dialog.directory.empty")}
|
emptyMessage={language.t("dialog.directory.empty")}
|
||||||
loadingMessage={language.t("common.loading")}
|
loadingMessage={language.t("common.loading")}
|
||||||
items={directories}
|
items={items}
|
||||||
key={(x) => x}
|
key={(x) => x.absolute}
|
||||||
|
filterKeys={["search"]}
|
||||||
|
ref={(r) => (list = r)}
|
||||||
|
onFilter={(value) => setFilter(clean(value))}
|
||||||
|
onKeyEvent={(e, item) => {
|
||||||
|
if (e.key !== "Tab") return
|
||||||
|
if (e.shiftKey) return
|
||||||
|
if (!item) return
|
||||||
|
|
||||||
|
e.preventDefault()
|
||||||
|
e.stopPropagation()
|
||||||
|
|
||||||
|
const value = display(item.absolute, filter())
|
||||||
|
list?.setFilter(value.endsWith("/") ? value : value + "/")
|
||||||
|
}}
|
||||||
onSelect={(path) => {
|
onSelect={(path) => {
|
||||||
if (!path) return
|
if (!path) return
|
||||||
resolve(path)
|
resolve(path.absolute)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{(absolute) => {
|
{(item) => {
|
||||||
const path = display(absolute)
|
const path = display(item.absolute, filter())
|
||||||
|
if (path === "~") {
|
||||||
return (
|
return (
|
||||||
<div class="w-full flex items-center justify-between rounded-md">
|
<div class="w-full flex items-center justify-between rounded-md">
|
||||||
<div class="flex items-center gap-x-3 grow min-w-0">
|
<div class="flex items-center gap-x-3 grow min-w-0">
|
||||||
<FileIcon node={{ path: absolute, type: "directory" }} class="shrink-0 size-4" />
|
<FileIcon node={{ path: item.absolute, type: "directory" }} class="shrink-0 size-4" />
|
||||||
|
<div class="flex items-center text-14-regular min-w-0">
|
||||||
|
<span class="text-text-strong whitespace-nowrap">~</span>
|
||||||
|
<span class="text-text-weak whitespace-nowrap">/</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div class="w-full flex items-center justify-between rounded-md">
|
||||||
|
<div class="flex items-center gap-x-3 grow min-w-0">
|
||||||
|
<FileIcon node={{ path: item.absolute, type: "directory" }} class="shrink-0 size-4" />
|
||||||
<div class="flex items-center text-14-regular min-w-0">
|
<div class="flex items-center text-14-regular min-w-0">
|
||||||
<span class="text-text-weak whitespace-nowrap overflow-hidden overflow-ellipsis truncate min-w-0">
|
<span class="text-text-weak whitespace-nowrap overflow-hidden overflow-ellipsis truncate min-w-0">
|
||||||
{getDirectory(path)}
|
{getDirectory(path)}
|
||||||
</span>
|
</span>
|
||||||
<span class="text-text-strong whitespace-nowrap">{getFilename(path)}</span>
|
<span class="text-text-strong whitespace-nowrap">{getFilename(path)}</span>
|
||||||
|
<span class="text-text-weak whitespace-nowrap">/</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export interface ListProps<T> extends FilteredListProps<T> {
|
|||||||
export interface ListRef {
|
export interface ListRef {
|
||||||
onKeyDown: (e: KeyboardEvent) => void
|
onKeyDown: (e: KeyboardEvent) => void
|
||||||
setScrollRef: (el: HTMLDivElement | undefined) => void
|
setScrollRef: (el: HTMLDivElement | undefined) => void
|
||||||
|
setFilter: (value: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void }) {
|
export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void }) {
|
||||||
@@ -80,7 +81,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
container.scrollTop = Math.max(0, Math.min(target, max))
|
container.scrollTop = Math.max(0, Math.min(target, max))
|
||||||
}
|
}
|
||||||
|
|
||||||
const { filter, grouped, flat, active, setActive, onKeyDown, onInput } = useFilteredList<T>(props)
|
const { filter, grouped, flat, active, setActive, onKeyDown, onInput, refetch } = useFilteredList<T>(props)
|
||||||
|
|
||||||
const searchProps = () => (typeof props.search === "object" ? props.search : {})
|
const searchProps = () => (typeof props.search === "object" ? props.search : {})
|
||||||
const searchAction = () => searchProps().action
|
const searchAction = () => searchProps().action
|
||||||
@@ -89,21 +90,29 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
|
|
||||||
const moved = (event: MouseEvent) => event.movementX !== 0 || event.movementY !== 0
|
const moved = (event: MouseEvent) => event.movementX !== 0 || event.movementY !== 0
|
||||||
|
|
||||||
createEffect(() => {
|
const applyFilter = (value: string, options?: { ref?: boolean }) => {
|
||||||
if (props.filter !== undefined) {
|
const prev = filter()
|
||||||
onInput(props.filter)
|
setInternalFilter(value)
|
||||||
}
|
onInput(value)
|
||||||
})
|
props.onFilter?.(value)
|
||||||
|
|
||||||
createEffect((prev) => {
|
if (!options?.ref) return
|
||||||
if (!props.search) return
|
|
||||||
const current = internalFilter()
|
// Force a refetch even if the value is unchanged.
|
||||||
if (prev !== current) {
|
// This is important for programmatic changes like Tab completion.
|
||||||
onInput(current)
|
if (prev === value) {
|
||||||
props.onFilter?.(current)
|
refetch()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return current
|
queueMicrotask(() => refetch())
|
||||||
}, "")
|
}
|
||||||
|
|
||||||
|
createEffect(() => {
|
||||||
|
if (props.filter === undefined) return
|
||||||
|
if (props.filter === internalFilter()) return
|
||||||
|
setInternalFilter(props.filter)
|
||||||
|
onInput(props.filter)
|
||||||
|
})
|
||||||
|
|
||||||
createEffect(
|
createEffect(
|
||||||
on(
|
on(
|
||||||
@@ -163,6 +172,8 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
const index = selected ? all.indexOf(selected) : -1
|
const index = selected ? all.indexOf(selected) : -1
|
||||||
props.onKeyEvent?.(e, selected)
|
props.onKeyEvent?.(e, selected)
|
||||||
|
|
||||||
|
if (e.defaultPrevented) return
|
||||||
|
|
||||||
if (e.key === "Enter" && !e.isComposing) {
|
if (e.key === "Enter" && !e.isComposing) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
if (selected) handleSelect(selected, index)
|
if (selected) handleSelect(selected, index)
|
||||||
@@ -174,6 +185,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
props.ref?.({
|
props.ref?.({
|
||||||
onKeyDown: handleKey,
|
onKeyDown: handleKey,
|
||||||
setScrollRef,
|
setScrollRef,
|
||||||
|
setFilter: (value) => applyFilter(value, { ref: true }),
|
||||||
})
|
})
|
||||||
|
|
||||||
const renderAdd = () => {
|
const renderAdd = () => {
|
||||||
@@ -247,7 +259,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
data-slot="list-search-input"
|
data-slot="list-search-input"
|
||||||
type="text"
|
type="text"
|
||||||
value={internalFilter()}
|
value={internalFilter()}
|
||||||
onChange={setInternalFilter}
|
onChange={(value) => applyFilter(value)}
|
||||||
onKeyDown={handleKey}
|
onKeyDown={handleKey}
|
||||||
placeholder={searchProps().placeholder}
|
placeholder={searchProps().placeholder}
|
||||||
spellcheck={false}
|
spellcheck={false}
|
||||||
@@ -260,7 +272,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
<IconButton
|
<IconButton
|
||||||
icon="circle-x"
|
icon="circle-x"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
onClick={() => setInternalFilter("")}
|
onClick={() => applyFilter("")}
|
||||||
aria-label={i18n.t("ui.list.clearFilter")}
|
aria-label={i18n.t("ui.list.clearFilter")}
|
||||||
/>
|
/>
|
||||||
</Show>
|
</Show>
|
||||||
@@ -295,6 +307,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
data-active={props.key(item) === active()}
|
data-active={props.key(item) === active()}
|
||||||
data-selected={item === props.current}
|
data-selected={item === props.current}
|
||||||
onClick={() => handleSelect(item, i())}
|
onClick={() => handleSelect(item, i())}
|
||||||
|
onKeyDown={handleKey}
|
||||||
type="button"
|
type="button"
|
||||||
onMouseMove={(event) => {
|
onMouseMove={(event) => {
|
||||||
if (!moved(event)) return
|
if (!moved(event)) return
|
||||||
|
|||||||
Reference in New Issue
Block a user