refactor(tui): simplify location and command contexts

This commit is contained in:
Dax Raad 2026-07-14 23:01:03 -04:00
commit 2cb2f9d538
6 changed files with 78 additions and 108 deletions

View file

@ -1,63 +1,35 @@
import { createMemo } from "solid-js"
import { DialogSelect, type DialogSelectRef } from "../ui/dialog-select"
import { type DialogContext } from "../ui/dialog"
import {
COMMAND_PALETTE_COMMAND,
formatKeyBindings,
type OpenTuiKeymap,
useKeymapSelector,
useOpencodeKeymap,
} from "../keymap"
import { useConfig } from "../config"
import { COMMAND_PALETTE_COMMAND } from "../keymap"
import { Keymap, type KeymapCommand } from "../context/keymap"
type PaletteCommandEntry = ReturnType<OpenTuiKeymap["getCommandEntries"]>[number]
function isVisiblePaletteCommand(command: PaletteCommandEntry["command"]) {
return command.hidden !== true && command.name !== COMMAND_PALETTE_COMMAND
}
function isSuggestedPaletteCommand(entry: PaletteCommandEntry) {
const suggested = entry.command.suggested
function isSuggestedPaletteCommand(command: KeymapCommand) {
const suggested = command.suggested
if (typeof suggested === "boolean") return suggested
if (typeof suggested === "function") return suggested() === true
return false
}
export function CommandPaletteDialog() {
const config = useConfig().data
const keymap = useOpencodeKeymap()
const entries = useKeymapSelector((keymap: OpenTuiKeymap) => {
const query = {
namespace: "palette",
}
const reachable = keymap.getCommandEntries({
...query,
visibility: "reachable",
filter: isVisiblePaletteCommand,
})
const registeredBindings = keymap.getCommandBindings({
visibility: "registered",
commands: reachable.map((entry) => entry.command.name),
})
return reachable.map((entry) => ({
...entry,
bindings: registeredBindings.get(entry.command.name) ?? entry.bindings,
}))
})
const commands = Keymap.useCommands()
const shortcuts = Keymap.useShortcuts()
const options = createMemo(() =>
entries().map((entry) => ({
title: typeof entry.command.title === "string" ? entry.command.title : entry.command.name,
description: typeof entry.command.desc === "string" ? entry.command.desc : undefined,
category: typeof entry.command.category === "string" ? entry.command.category : undefined,
footer: formatKeyBindings(entry.bindings, config),
value: entry.command.name,
suggested: isSuggestedPaletteCommand(entry),
onSelect: (dialog: DialogContext) => {
dialog.clear()
keymap.dispatchCommand(entry.command.name)
},
})),
commands().flatMap((command) => {
if (!command.id || !command.palette || command.hidden || command.id === COMMAND_PALETTE_COMMAND) return []
return {
title: command.title ?? command.id,
description: command.description,
category: command.group,
footer: shortcuts.all(command.id),
value: command.id,
suggested: isSuggestedPaletteCommand(command),
onSelect: (dialog: DialogContext) => {
dialog.clear()
command.run()
},
}
}),
)
let ref: DialogSelectRef<string>

View file

@ -18,7 +18,7 @@ import { useTerminalDimensions } from "@opentui/solid"
import { Locale } from "../../util/locale"
import type { PromptInfo, PromptPartRef } from "../../prompt/history"
import { useFrecency } from "../../prompt/frecency"
import { useBindings, useCommandSlashes } from "../../keymap"
import { useBindings } from "../../keymap"
import { Keymap } from "../../context/keymap"
import { displayCharAt, mentionTriggerIndex } from "../../prompt/display"
import type { FileSystemEntry } from "@opencode-ai/client"
@ -86,8 +86,8 @@ export function Autocomplete(props: {
const editor = useEditorContext()
const client = useClient()
const data = useData()
const slashes = useCommandSlashes()
const keymap = Keymap.use()
const keymapCommands = Keymap.useCommands()
const { theme } = useTheme()
const dimensions = useTerminalDimensions()
const frecency = useFrecency()
@ -428,7 +428,15 @@ export function Autocomplete(props: {
)
const commands = createMemo((): AutocompleteOption[] => {
const results: AutocompleteOption[] = [...slashes()]
const results: AutocompleteOption[] = keymapCommands().flatMap((command) => {
if (!command.slash) return []
return {
display: `/${command.slash.name}`,
description: command.description ?? command.title,
aliases: command.slash.aliases?.map((alias) => `/${alias}`),
onSelect: command.run,
}
})
const commandNames = new Set<string>()
for (const serverCommand of data.location.command.list(location.current) ?? []) {