Skip to content

Commit 29b7871

Browse files
committed
structure
1 parent 968b696 commit 29b7871

4 files changed

Lines changed: 59 additions & 49 deletions

File tree

packages/opencode/src/cli/cmd/tui/app.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ import { TuiPluginRuntime } from "@/cli/cmd/tui/plugin/runtime"
6464
import { createTuiApi } from "@/cli/cmd/tui/plugin/api"
6565
import type { RouteMap } from "@/cli/cmd/tui/plugin/api"
6666
import { FormatError, FormatUnknownError } from "@/cli/error"
67-
import { COMMAND_PALETTE_DIALOG, CommandPaletteDialog } from "./component/command-palette"
67+
import { CommandPaletteDialog } from "./component/command-palette"
6868
import {
69+
COMMAND_PALETTE_COMMAND,
6970
OPENCODE_BASE_MODE,
7071
OpencodeKeymapProvider,
7172
createOpencodeModeStack,
@@ -403,7 +404,7 @@ function App(props: { onSnapshot?: () => Promise<string[]> }) {
403404
const appCommands = createMemo(() =>
404405
[
405406
{
406-
name: COMMAND_PALETTE_DIALOG,
407+
name: COMMAND_PALETTE_COMMAND,
407408
title: "Show command palette",
408409
hidden: true,
409410
run: () => {

packages/opencode/src/cli/cmd/tui/component/command-palette.tsx

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
import { createMemo, type Accessor } from "solid-js"
1+
import { createMemo } from "solid-js"
22
import { DialogSelect, type DialogSelectRef } from "@tui/ui/dialog-select"
33
import { type DialogContext } from "@tui/ui/dialog"
4-
import { formatKeyBindings, type OpenTuiKeymap, useKeymapSelector, useOpencodeKeymap } from "../keymap"
4+
import {
5+
COMMAND_PALETTE_COMMAND,
6+
formatKeyBindings,
7+
type OpenTuiKeymap,
8+
useKeymapSelector,
9+
useOpencodeKeymap,
10+
} from "../keymap"
511
import { useTuiConfig } from "../context/tui-config"
612

7-
type SlashEntry = {
8-
display: string
9-
description?: string
10-
aliases?: string[]
11-
onSelect: () => void
12-
}
13-
14-
export const COMMAND_PALETTE_DIALOG = "command.palette.show"
1513
type PaletteCommandEntry = ReturnType<OpenTuiKeymap["getCommandEntries"]>[number]
1614

1715
function isVisiblePaletteCommand(entry: PaletteCommandEntry) {
18-
return entry.command.hidden !== true && entry.command.name !== COMMAND_PALETTE_DIALOG
16+
return entry.command.hidden !== true && entry.command.name !== COMMAND_PALETTE_COMMAND
1917
}
2018

2119
function isSuggestedPaletteCommand(entry: PaletteCommandEntry) {
@@ -80,36 +78,3 @@ export function CommandPaletteDialog() {
8078

8179
return <DialogSelect ref={(value) => (ref = value)} title="Commands" options={list()} />
8280
}
83-
84-
export function useCommandSlashes(): Accessor<readonly SlashEntry[]> {
85-
const keymap = useOpencodeKeymap()
86-
const entries = useKeymapSelector((keymap: OpenTuiKeymap) =>
87-
keymap
88-
.getCommandEntries({
89-
visibility: "reachable",
90-
namespace: "palette",
91-
})
92-
.filter(isVisiblePaletteCommand),
93-
)
94-
95-
return createMemo<SlashEntry[]>(() =>
96-
entries().flatMap((entry) => {
97-
const slashName = entry.command.slashName
98-
if (typeof slashName !== "string" || !slashName) return []
99-
const slashAliases = entry.command.slashAliases
100-
return {
101-
display: `/${slashName}`,
102-
description:
103-
typeof entry.command.desc === "string"
104-
? entry.command.desc
105-
: typeof entry.command.title === "string"
106-
? entry.command.title
107-
: undefined,
108-
aliases: Array.isArray(slashAliases)
109-
? slashAliases.filter((alias): alias is string => typeof alias === "string").map((alias) => `/${alias}`)
110-
: undefined,
111-
onSelect: () => keymap.dispatchCommand(entry.command.name),
112-
}
113-
}),
114-
)
115-
}

packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import { getScrollAcceleration } from "../../util/scroll"
1212
import { useTuiConfig } from "../../context/tui-config"
1313
import { useTheme, selectedForeground } from "@tui/context/theme"
1414
import { SplitBorder } from "@tui/component/border"
15-
import { useCommandSlashes } from "../command-palette"
1615
import { useTerminalDimensions } from "@opentui/solid"
1716
import { Locale } from "@/util/locale"
1817
import type { PromptInfo } from "./history"
1918
import { useFrecency } from "./frecency"
20-
import { useBindings, useOpencodeModeStack } from "../../keymap"
19+
import { useBindings, useCommandSlashes, useOpencodeModeStack } from "../../keymap"
2120

2221
function removeLineRange(input: string) {
2322
const hashIndex = input.lastIndexOf("#")

packages/opencode/src/cli/cmd/tui/keymap.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import {
1010
useKeymap,
1111
useKeymapSelector,
1212
} from "@opentui/keymap/solid"
13-
import type { Accessor } from "solid-js"
13+
import { createMemo, type Accessor } from "solid-js"
1414
import type { TuiConfig } from "./config/tui"
1515
import { useTuiConfig } from "./context/tui-config"
1616

1717
export const LEADER_TOKEN = "leader"
1818
export const OPENCODE_BASE_MODE = "base"
19+
export const COMMAND_PALETTE_COMMAND = "command.palette.show"
1920

2021
const OPENCODE_MODE_KEY = "opencode.mode"
2122

@@ -26,9 +27,20 @@ export { useBindings, useKeymapSelector }
2627

2728
export type OpenTuiKeymap = ReturnType<typeof useKeymap>
2829
type OpencodeModeStack = ReturnType<typeof createOpencodeModeStack>
30+
type CommandSlashEntry = {
31+
display: string
32+
description?: string
33+
aliases?: string[]
34+
onSelect: () => void
35+
}
36+
type CommandEntry = ReturnType<OpenTuiKeymap["getCommandEntries"]>[number]
2937

3038
const modeStacks = new WeakMap<OpenTuiKeymap, OpencodeModeStack>()
3139

40+
function isVisiblePaletteCommand(entry: CommandEntry) {
41+
return entry.command.hidden !== true && entry.command.name !== COMMAND_PALETTE_COMMAND
42+
}
43+
3244
export function createOpencodeModeStack(keymap: OpenTuiKeymap) {
3345
keymap.setData(OPENCODE_MODE_KEY, OPENCODE_BASE_MODE)
3446

@@ -145,3 +157,36 @@ export function useCommandShortcut(command: string): Accessor<string> {
145157
export function useLeaderActive(): Accessor<boolean> {
146158
return useKeymapSelector((keymap: OpenTuiKeymap) => keymap.getPendingSequence()[0]?.tokenName === LEADER_TOKEN)
147159
}
160+
161+
export function useCommandSlashes(): Accessor<readonly CommandSlashEntry[]> {
162+
const keymap = useOpencodeKeymap()
163+
const entries = useKeymapSelector((keymap: OpenTuiKeymap) =>
164+
keymap
165+
.getCommandEntries({
166+
visibility: "reachable",
167+
namespace: "palette",
168+
})
169+
.filter(isVisiblePaletteCommand),
170+
)
171+
172+
return createMemo<CommandSlashEntry[]>(() =>
173+
entries().flatMap((entry) => {
174+
const slashName = entry.command.slashName
175+
if (typeof slashName !== "string" || !slashName) return []
176+
const slashAliases = entry.command.slashAliases
177+
return {
178+
display: `/${slashName}`,
179+
description:
180+
typeof entry.command.desc === "string"
181+
? entry.command.desc
182+
: typeof entry.command.title === "string"
183+
? entry.command.title
184+
: undefined,
185+
aliases: Array.isArray(slashAliases)
186+
? slashAliases.filter((alias): alias is string => typeof alias === "string").map((alias) => `/${alias}`)
187+
: undefined,
188+
onSelect: () => keymap.dispatchCommand(entry.command.name),
189+
}
190+
}),
191+
)
192+
}

0 commit comments

Comments
 (0)