Browse Source

Fix file tagging in multi line inputs (#3865)

Greg Pstrucha 3 months ago
parent
commit
68039d4c71
1 changed files with 12 additions and 15 deletions
  1. 12 15
      packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx

+ 12 - 15
packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx

@@ -52,14 +52,14 @@ export function Autocomplete(props: {
     // Track props.value to make memo reactive to text changes
     props.value // <- there surely is a better way to do this, like making .input() reactive
 
-    const val = props.input().getTextRange(store.index + 1, props.input().visualCursor.offset + 1)
+    const val = props.input().getTextRange(store.index + 1, props.input().cursorOffset + 1)
 
     return val
   })
 
   function insertPart(text: string, part: PromptInfo["parts"][number]) {
     const input = props.input()
-    const currentCursorOffset = input.visualCursor.offset
+    const currentCursorOffset = input.cursorOffset
 
     const charAfterCursor = props.value.at(currentCursorOffset)
     const needsSpace = charAfterCursor !== " "
@@ -344,7 +344,7 @@ export function Autocomplete(props: {
     command.keybinds(false)
     setStore({
       visible: mode,
-      index: props.input().visualCursor.offset,
+      index: props.input().cursorOffset,
       position: {
         x: props.anchor().x,
         y: props.anchor().y,
@@ -368,8 +368,8 @@ export function Autocomplete(props: {
       get visible() {
         return store.visible
       },
-      onInput(value: string) {
-        if (store.visible && Bun.stringWidth(value) <= store.index) hide()
+      onInput() {
+        if (store.visible && props.input().cursorOffset <= store.index) hide()
       },
       onKeyDown(e: KeyEvent) {
         if (store.visible) {
@@ -381,23 +381,20 @@ export function Autocomplete(props: {
         }
         if (!store.visible) {
           if (e.name === "@") {
-            const cursorOffset = props.input().visualCursor.offset
+            const cursorOffset = props.input().cursorOffset
             const charBeforeCursor =
               cursorOffset === 0
                 ? undefined
                 : props.input().getTextRange(cursorOffset - 1, cursorOffset)
-
-            if (
-              charBeforeCursor === " " ||
-              charBeforeCursor === "\n" ||
-              charBeforeCursor === undefined
-            ) {
-              show("@")
-            }
+            const canTrigger =
+              charBeforeCursor === undefined ||
+              charBeforeCursor === "" ||
+              /\s/.test(charBeforeCursor)
+            if (canTrigger) show("@")
           }
 
           if (e.name === "/") {
-            if (props.input().visualCursor.offset === 0) show("/")
+            if (props.input().cursorOffset === 0) show("/")
           }
         }
       },