GeezerVison is showing github.com — fix(tui): scope file autocomplete to session (#33458) · msegeya/opencode@ef23579
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/workflows/test.yml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 65 | 65 |
| 66 | 66 | - name: Run unit tests | 67 | 67 | timeout-minutes: 20 | 68 | | - run: bun turbo test --output-logs=errors-only --log-order=grouped --log-prefix=none | | 68 | + run: GITHUB_ACTIONS=false bun turbo test | 69 | 69 | env: | 70 | 70 | OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: ${{ runner.os == 'Windows' && 'true' || 'false' }} | 71 | 71 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/src/app.tsx +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ import { SDKProvider, useSDK } from "./context/sdk" | 35 | 35 | import { StartupLoading } from "./component/startup-loading" | 36 | 36 | import { SyncProvider, useSync } from "./context/sync" | 37 | 37 | import { DataProvider } from "./context/data" | | 38 | +import { LocationProvider } from "./context/location" | 38 | 39 | import { LocalProvider, useLocal } from "./context/local" | 39 | 40 | import { DialogModel } from "./component/dialog-model" | 40 | 41 | import { useConnected } from "./component/use-connected" | @@ -303,10 +304,12 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) { | 303 | 304 | <PromptHistoryProvider> | 304 | 305 | <PromptRefProvider> | 305 | 306 | <EditorContextProvider> | 306 | | - <App | 307 | | - onSnapshot={input.onSnapshot} | 308 | | - pluginHost={input.pluginHost} | 309 | | - /> | | 307 | + <LocationProvider> | | 308 | + <App | | 309 | + onSnapshot={input.onSnapshot} | | 310 | + pluginHost={input.pluginHost} | | 311 | + /> | | 312 | + </LocationProvider> | 310 | 313 | </EditorContextProvider> | 311 | 314 | </PromptRefProvider> | 312 | 315 | </PromptHistoryProvider> | |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/src/component/prompt/autocomplete.tsx +33-18Lines changed: 33 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { useData } from "../../context/data" | 13 | 13 | import { getScrollAcceleration } from "../../util/scroll" | 14 | 14 | import { useTuiPaths } from "../../context/runtime" | 15 | 15 | import { useTuiConfig } from "../../config" | | 16 | +import { useLocation } from "../../context/location" | 16 | 17 | import { useTheme, selectedForeground } from "../../context/theme" | 17 | 18 | import { SplitBorder } from "../../ui/border" | 18 | 19 | import { useTerminalDimensions } from "@opentui/solid" | @@ -21,6 +22,7 @@ import type { PromptInfo } from "../../prompt/history" | 21 | 22 | import { useFrecency } from "../../prompt/frecency" | 22 | 23 | import { useBindings, useCommandSlashes, useOpencodeModeStack } from "../../keymap" | 23 | 24 | import { displayCharAt, mentionTriggerIndex } from "../../prompt/display" | | 25 | +import type { FileSystemEntry } from "@opencode-ai/sdk/v2" | 24 | 26 |
| 25 | 27 | function removeLineRange(input: string) { | 26 | 28 | const hashIndex = input.lastIndexOf("#") | @@ -94,6 +96,7 @@ export function Autocomplete(props: { | 94 | 96 | const frecency = useFrecency() | 95 | 97 | const tuiConfig = useTuiConfig() | 96 | 98 | const paths = useTuiPaths() | | 99 | + const location = useLocation() | 97 | 100 | const [store, setStore] = createStore({ | 98 | 101 | index: 0, | 99 | 102 | selected: 0, | @@ -236,16 +239,18 @@ export function Autocomplete(props: { | 236 | 239 | } | 237 | 240 | } | 238 | 241 |
| 239 | | - function createFilePart(item: string, lineRange?: { startLine: number; endLine?: number }) { | 240 | | - const baseDir = (sync.path.directory || paths.cwd).replace(/\/+$/, "") | 241 | | - const fullPath = path.isAbsolute(item) ? item : path.join(baseDir, item) | 242 | | - const urlObj = pathToFileURL(fullPath) | | 242 | + function createFilePart( | | 243 | + item: FileSystemEntry, | | 244 | + filePath: string, | | 245 | + lineRange?: { startLine: number; endLine?: number }, | | 246 | + ) { | | 247 | + const urlObj = pathToFileURL(filePath) | 243 | 248 | const filename = | 244 | | - lineRange && !item.endsWith("/") | 245 | | - ? `${item}#${lineRange.startLine}${lineRange.endLine ? `-${lineRange.endLine}` : ""}` | 246 | | - : item | | 249 | + lineRange && item.type !== "directory" | | 250 | + ? `${item.path}#${lineRange.startLine}${lineRange.endLine ? `-${lineRange.endLine}` : ""}` | | 251 | + : item.path | 247 | 252 |
| 248 | | - if (lineRange && !item.endsWith("/")) { | | 253 | + if (lineRange && item.type !== "directory") { | 249 | 254 | urlObj.searchParams.set("start", String(lineRange.startLine)) | 250 | 255 | if (lineRange.endLine !== undefined) { | 251 | 256 | urlObj.searchParams.set("end", String(lineRange.endLine)) | @@ -254,10 +259,9 @@ export function Autocomplete(props: { | 254 | 259 |
| 255 | 260 | return { | 256 | 261 | filename, | 257 | | - url: urlObj.href, | 258 | 262 | part: { | 259 | 263 | type: "file" as const, | 260 | | - mime: "text/plain", | | 264 | + mime: item.mime, | 261 | 265 | filename, | 262 | 266 | url: urlObj.href, | 263 | 267 | source: { | @@ -267,7 +271,7 @@ export function Autocomplete(props: { | 267 | 271 | end: 0, | 268 | 272 | value: "", | 269 | 273 | }, | 270 | | - path: item, | | 274 | + path: item.path, | 271 | 275 | }, | 272 | 276 | }, | 273 | 277 | } | @@ -284,7 +288,7 @@ export function Autocomplete(props: { | 284 | 288 | }) | 285 | 289 |
| 286 | 290 | function normalizeMentionPath(filePath: string) { | 287 | | - const baseDir = sync.path.directory || paths.cwd | | 291 | + const baseDir = location()?.directory || sync.path.directory || paths.cwd | 288 | 292 | const absolute = path.resolve(filePath) | 289 | 293 | const relative = path.relative(baseDir, absolute) | 290 | 294 |
| @@ -301,7 +305,11 @@ export function Autocomplete(props: { | 301 | 305 | startLine: input.lineStart, | 302 | 306 | endLine: input.lineEnd > input.lineStart ? input.lineEnd : undefined, | 303 | 307 | } | 304 | | - const { filename, part } = createFilePart(item, lineRange) | | 308 | + const { filename, part } = createFilePart( | | 309 | + { path: item, type: "file", mime: "text/plain" }, | | 310 | + input.filePath, | | 311 | + lineRange, | | 312 | + ) | 305 | 313 | const index = store.visible === "@" ? store.index : props.input().cursorOffset | 306 | 314 |
| 307 | 315 | setStore("visible", false) | @@ -310,17 +318,20 @@ export function Autocomplete(props: { | 310 | 318 | } | 311 | 319 |
| 312 | 320 | const [files] = createResource( | 313 | | - () => search(), | 314 | | - async (query) => { | | 321 | + () => ({ query: search(), location: location() }), | | 322 | + async (input) => { | 315 | 323 | if (!store.visible || store.visible === "/") return [] | 316 | 324 | if (referenceMatch()) return [] | 317 | | - const { lineRange, baseQuery } = extractLineRange(query ?? "") | | 325 | + const { lineRange, baseQuery } = extractLineRange(input.query ?? "") | 318 | 326 |
| 319 | 327 | // Get files from SDK | 320 | 328 | const result = await sdk.client.v2.fs.find({ | 321 | 329 | query: baseQuery, | 322 | 330 | limit: "20", | 323 | | - location: { workspace: project.workspace.current() }, | | 331 | + location: { | | 332 | + directory: input.location?.directory, | | 333 | + workspace: input.location?.workspaceID ?? project.workspace.current(), | | 334 | + }, | 324 | 335 | }) | 325 | 336 |
| 326 | 337 | const options: AutocompleteOption[] = [] | @@ -331,7 +342,11 @@ export function Autocomplete(props: { | 331 | 342 | const width = props.anchor().width - 4 | 332 | 343 | options.push( | 333 | 344 | ...result.data.data.map((item): AutocompleteOption => { | 334 | | - const { filename, url, part } = createFilePart(item.path, lineRange) | | 345 | + const { filename, part } = createFilePart( | | 346 | + item, | | 347 | + path.join(result.data.location.directory, item.path), | | 348 | + lineRange, | | 349 | + ) | 335 | 350 | return { | 336 | 351 | display: Locale.truncateMiddle(filename, width), | 337 | 352 | value: filename, | |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import type { LocationRef } from "@opencode-ai/sdk/v2" | | 2 | +import { createContext, useContext, type Accessor, type ParentProps } from "solid-js" | | 3 | + | | 4 | +const context = createContext<Accessor<LocationRef | undefined>>() | | 5 | + | | 6 | +export function LocationProvider(props: ParentProps<{ location?: LocationRef }>) { | | 7 | + return <context.Provider value={() => props.location}>{props.children}</context.Provider> | | 8 | +} | | 9 | + | | 10 | +export function useLocation() { | | 11 | + const value = useContext(context) | | 12 | + if (!value) throw new Error("Location context must be used within a LocationProvider") | | 13 | + return value | | 14 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/src/context/path-format.tsx +7-23Lines changed: 7 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import path from "path" | 2 | | -import { createContext, useContext, type ParentProps } from "solid-js" | 3 | 2 | import { abbreviateHome } from "../runtime" | | 3 | +import { useLocation } from "./location" | 4 | 4 | import { useTuiPaths } from "./runtime" | 5 | 5 |
| 6 | | -const context = createContext<{ | 7 | | - path: () => string | 8 | | - format: (input?: string) => string | 9 | | -}>() | 10 | | - | 11 | | -export function PathFormatterProvider(props: ParentProps<{ path: string | undefined }>) { | 12 | | - const paths = useTuiPaths() | 13 | | - return ( | 14 | | - <context.Provider | 15 | | - value={{ | 16 | | - path: () => props.path || paths.cwd, | 17 | | - format: (input) => formatPath(input, props.path || paths.cwd, paths.home), | 18 | | - }} | 19 | | - > | 20 | | - {props.children} | 21 | | - </context.Provider> | 22 | | - ) | 23 | | -} | 24 | | - | 25 | 6 | export function usePathFormatter() { | 26 | | - const value = useContext(context) | 27 | | - if (!value) throw new Error("PathFormatter context must be used within a PathFormatterProvider") | 28 | | - return value | | 7 | + const paths = useTuiPaths() | | 8 | + const location = useLocation() | | 9 | + return { | | 10 | + path: () => location()?.directory || paths.cwd, | | 11 | + format: (input?: string) => formatPath(input, location()?.directory || paths.cwd, paths.home), | | 12 | + } | 29 | 13 | } | 30 | 14 |
| 31 | 15 | function formatPath(input: string | undefined, base: string, home: string) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/src/routes/session/index.tsx +8-3Lines changed: 8 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,7 +80,8 @@ import { usePluginRuntime } from "../../plugin/runtime" | 80 | 80 | import { DialogRetryAction } from "../../component/dialog-retry-action" | 81 | 81 | import { getRevertDiffFiles } from "../../util/revert-diff" | 82 | 82 | import { OPENCODE_BASE_MODE, useBindings, useCommandShortcut, useOpencodeKeymap } from "../../keymap" | 83 | | -import { PathFormatterProvider, usePathFormatter } from "../../context/path-format" | | 83 | +import { usePathFormatter } from "../../context/path-format" | | 84 | +import { LocationProvider } from "../../context/location" | 84 | 85 |
| 85 | 86 | addDefaultParsers(parsers.parsers) | 86 | 87 |
| @@ -193,6 +194,10 @@ export function Session() { | 193 | 194 | const { theme } = useTheme() | 194 | 195 | const promptRef = usePromptRef() | 195 | 196 | const session = createMemo(() => sync.session.get(route.sessionID)) | | 197 | + const location = createMemo(() => { | | 198 | + const current = session() | | 199 | + return current ? { directory: current.directory, workspaceID: current.workspaceID } : undefined | | 200 | + }) | 196 | 201 |
| 197 | 202 | createEffect(() => { | 198 | 203 | const title = Locale.truncate(session()?.title ?? "", 50) | @@ -1138,7 +1143,7 @@ export function Session() { | 1138 | 1143 | createEffect(on(() => route.sessionID, toBottom)) | 1139 | 1144 |
| 1140 | 1145 | return ( | 1141 | | - <PathFormatterProvider path={session()?.directory}> | | 1146 | + <LocationProvider location={location()}> | 1142 | 1147 | <context.Provider | 1143 | 1148 | value={{ | 1144 | 1149 | get width() { | @@ -1338,7 +1343,7 @@ export function Session() { | 1338 | 1343 | </Show> | 1339 | 1344 | </box> | 1340 | 1345 | </context.Provider> | 1341 | | - </PathFormatterProvider> | | 1346 | + </LocationProvider> | 1342 | 1347 | ) | 1343 | 1348 | } | 1344 | 1349 |
| |
|