fix(tui): scope file autocomplete to session (#33458)
This commit is contained in:
parent
975b1132f1
commit
ef2357915e
6 changed files with 70 additions and 49 deletions
14
packages/tui/src/context/location.tsx
Normal file
14
packages/tui/src/context/location.tsx
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import type { LocationRef } from "@opencode-ai/sdk/v2"
|
||||
import { createContext, useContext, type Accessor, type ParentProps } from "solid-js"
|
||||
|
||||
const context = createContext<Accessor<LocationRef | undefined>>()
|
||||
|
||||
export function LocationProvider(props: ParentProps<{ location?: LocationRef }>) {
|
||||
return <context.Provider value={() => props.location}>{props.children}</context.Provider>
|
||||
}
|
||||
|
||||
export function useLocation() {
|
||||
const value = useContext(context)
|
||||
if (!value) throw new Error("Location context must be used within a LocationProvider")
|
||||
return value
|
||||
}
|
||||
|
|
@ -1,31 +1,15 @@
|
|||
import path from "path"
|
||||
import { createContext, useContext, type ParentProps } from "solid-js"
|
||||
import { abbreviateHome } from "../runtime"
|
||||
import { useLocation } from "./location"
|
||||
import { useTuiPaths } from "./runtime"
|
||||
|
||||
const context = createContext<{
|
||||
path: () => string
|
||||
format: (input?: string) => string
|
||||
}>()
|
||||
|
||||
export function PathFormatterProvider(props: ParentProps<{ path: string | undefined }>) {
|
||||
const paths = useTuiPaths()
|
||||
return (
|
||||
<context.Provider
|
||||
value={{
|
||||
path: () => props.path || paths.cwd,
|
||||
format: (input) => formatPath(input, props.path || paths.cwd, paths.home),
|
||||
}}
|
||||
>
|
||||
{props.children}
|
||||
</context.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function usePathFormatter() {
|
||||
const value = useContext(context)
|
||||
if (!value) throw new Error("PathFormatter context must be used within a PathFormatterProvider")
|
||||
return value
|
||||
const paths = useTuiPaths()
|
||||
const location = useLocation()
|
||||
return {
|
||||
path: () => location()?.directory || paths.cwd,
|
||||
format: (input?: string) => formatPath(input, location()?.directory || paths.cwd, paths.home),
|
||||
}
|
||||
}
|
||||
|
||||
function formatPath(input: string | undefined, base: string, home: string) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue