Skip to content

Inserting space between CJK characters and '@' causing autocompletion to show up #20852

Closed as not planned

Description

@Ero98

Description

Typing space right before '@' when Chinese/CJK/non-ascii characters exist before it causing autocompletion to show up.
Image
And highlighting is broken too, the more Chinese characters occuring before '@', the more tailing character after '@' not correctly highlighted:
Image

Seems to have the same root cause as #3734, The cursor position calculation is a bit messed up when dealing with non-ascii/multi-byte characters.

The problem seems to be related to '@' triggering code here:
https://github.com/anomalyco/opencode/blob/dev/packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx#L535

When user input anything, OpenCode will extract the string before cursor to check if it contains an '@' symbol, and check out what follows the '@' symbol, then to decide whether or not to pop up an autocompletion list. The cursor position it fetches is EditBufferRenerable's cursorOffset, which I believe is in display-width(where CJK chars have a length of 2).

        // Check if autocomplete should reopen (e.g., after backspace deleted a space)
        const offset = props.input().cursorOffset
        if (offset === 0) return

But later cursorOffset was then used as normal string index to get the position of '@' and to slice the search keyword following '@':

        // Check for "@" trigger - find the nearest "@" before cursor with no whitespace between
        const text = value.slice(0, offset)
        const idx = text.lastIndexOf("@")
        if (idx === -1) return

        const between = text.slice(idx)
        const before = idx === 0 ? undefined : value[idx - 1]

I find that changing 'text' and 'idx' calculation logic to utilize EditBufferRenderable's API can fix the issue (but my algorithm is very inefficient, getTextRange is already O(n) for iterating over the input text tree, resulting in O(n²) time complexity):

        const text = props.input().getTextRange(0, offset)
        let idx = -1
        for (idx=offset - 1; idx >= 0; idx --) {
          const curChar = props.input().getTextRange(idx, idx + 1)
          if (curChar === "@") {
            break;
          }
        }
        if (idx === -1) return
        
        const between = props.input().getTextRange(idx + 1, offset)
        const before = idx === 0 ? undefined : props.input().getTextRange(idx - 1, idx)

This algorithm of mine utilizes EditBufferRenderable.getTextRange to get character from input using the correct cursor position. When applied, autocompletion is behaving normally, but highlighting is still broken.

Image Image

But my algorithm causes severe performance issue. The ideal way to solve this is that OpenTUI's EditBufferRenderable exposing a function like "getting specific character's last display-width offset", within a certain text range. That way we can do something like:

        const text = props.input().getTextRange(0, offset)
        const idx = props.input().getCharLastOffset("@", 0, offset)
        if (idx === -1) return
        
        const between = props.input().getTextRange(idx + 1, offset)
        const before = idx === 0 ? undefined : props.input().getTextRange(idx - 1, idx)

As for highlighting, it seems to be related to OpenTUI's ExtmarksController's function wrapDeletion and wrapInsertion, having the same issue as stated above, I haven't look into it closely.

Plugins

No response

OpenCode version

1.3.13

Steps to reproduce

  1. Open OpenCode.
  2. Paste this character '啊' into prompt input.
  3. Type '@' and select 'general'.
  4. Use the mouse to position the cursor over '@'.
  5. Press space key.

Screenshot and/or share link

Typing space between Chinese character and '@' symbol causing autocompletion to show up:
Image
When typing '@' right after normal ascii letter, autocompletion will not show up as Chinese characters do(possiblly unrelated discovery):
Image
Image

If you managed to position the cursor over '@'(by mouse) when input is pure ascii characters, typing space will not cause autocompletion to show up:
Image

Operating System

Windows 11

Terminal

Windows Terminal

Activity

  1. added
    bugSomething isn't working
    on Apr 3, 2026
  2. added
    opentuiThis relates to changes in v1.0, now that opencode uses opentui
    on Apr 3, 2026
  3. changed the title [-]Inserting space between Chinese characters and '@' causing autocompletion to show up[/-] [+]Inserting space between CJK characters and '@' causing autocompletion to show up[/+] on Apr 7, 2026
  4. removed
    bugSomething isn't working
    opentuiThis relates to changes in v1.0, now that opencode uses opentui
    on May 3, 2026
  5. XiaoPengYouCode commented on May 10, 2026

    @XiaoPengYouCode

    Still reproducible in v1.14.46 on macOS. When Chinese characters precede @, the cursorOffset (display-width based) gets passed to value.slice() as a byte index, causing lastIndexOf('@') to return -1 entirely — so @ autocomplete never triggers at all, not just misbehaves.

  6. github-actions commented on Jul 10, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions