Skip to content

Commit 555ede4

Browse files
authored
tui: autocomplete: expand directory on Tab, select on Enter (anomalyco#6975)
Signed-off-by: yuguorui <yuguorui@pku.edu.cn>
1 parent fc39b9e commit 555ede4

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type AutocompleteOption = {
5555
aliases?: string[]
5656
disabled?: boolean
5757
description?: string
58+
isDirectory?: boolean
5859
onSelect?: () => void
5960
}
6061

@@ -200,8 +201,10 @@ export function Autocomplete(props: {
200201
url = urlObj.toString()
201202
}
202203

204+
const isDir = item.endsWith("/")
203205
return {
204206
display: Locale.truncateMiddle(filename, width),
207+
isDirectory: isDir,
205208
onSelect: () => {
206209
insertPart(filename, {
207210
type: "file",
@@ -511,6 +514,27 @@ export function Autocomplete(props: {
511514
selected.onSelect?.()
512515
}
513516

517+
function expandDirectory() {
518+
const selected = options()[store.selected]
519+
if (!selected) return
520+
521+
const input = props.input()
522+
const currentCursorOffset = input.cursorOffset
523+
524+
const displayText = selected.display.trimEnd()
525+
const path = displayText.startsWith("@") ? displayText.slice(1) : displayText
526+
527+
input.cursorOffset = store.index
528+
const startCursor = input.logicalCursor
529+
input.cursorOffset = currentCursorOffset
530+
const endCursor = input.logicalCursor
531+
532+
input.deleteRange(startCursor.row, startCursor.col, endCursor.row, endCursor.col)
533+
input.insertText("@" + path)
534+
535+
setStore("selected", 0)
536+
}
537+
514538
function show(mode: "@" | "/") {
515539
command.keybinds(false)
516540
setStore({
@@ -575,11 +599,21 @@ export function Autocomplete(props: {
575599
e.preventDefault()
576600
return
577601
}
578-
if (name === "return" || name === "tab") {
602+
if (name === "return") {
579603
select()
580604
e.preventDefault()
581605
return
582606
}
607+
if (name === "tab") {
608+
const selected = options()[store.selected]
609+
if (selected?.isDirectory) {
610+
expandDirectory()
611+
} else {
612+
select()
613+
}
614+
e.preventDefault()
615+
return
616+
}
583617
}
584618
if (!store.visible) {
585619
if (e.name === "@") {

0 commit comments

Comments
 (0)