Skip to content

fix(vscode-companion): don't override cursorPosition=0 to text.length#2971

Open
chinesepowered wants to merge 1 commit intoQwenLM:mainfrom
chinesepowered:fix/vscode-completion-cursor-zero
Open

fix(vscode-companion): don't override cursorPosition=0 to text.length#2971
chinesepowered wants to merge 1 commit intoQwenLM:mainfrom
chinesepowered:fix/vscode-completion-cursor-zero

Conversation

@chinesepowered
Copy link
Copy Markdown
Contributor

Fix VS Code companion autocomplete misfiring when cursor is at position 0.

TLDR

useCompletionTrigger.ts rewrote a legitimate cursorPosition === 0 into text.length whenever the text was non-empty, causing trigger detection to search the entire text (including content after the cursor) for @ and / characters. Users who placed the cursor at the start of existing text would see stale, incorrect autocomplete suggestions based on content that came after their cursor. The fix removes the override.

Screenshots / Video Demo

N/A — behavior change; the concrete repro is described below.

Dive Deeper

The relevant section of useCompletionTrigger.ts (line 295-300 before the fix):

// Find trigger character before cursor
// Use text length if cursorPosition is 0 but we have text (edge case for first character)
const effectiveCursorPosition =
  cursorPosition === 0 && text.length > 0 ? text.length : cursorPosition;

const textBeforeCursor = text.substring(0, effectiveCursorPosition);

The walker above (line 292) already handles the "cursor not found" case by setting cursorPosition = text.length:

// If we found the node, use the calculated offset; otherwise use text length
cursorPosition = found ? offset : text.length;

So by the time we reach the override, cursorPosition === 0 is unambiguous — it means the walker did find the cursor's text node and resolved its offset to 0 (cursor genuinely at the start of non-empty text). Rewriting 0 → text.length caused textBeforeCursor = text.substring(0, text.length) to span the entire text, so any @ or / anywhere in the text would falsely register as a trigger match.

Repro: In the VS Code companion webview, type hello @someone into the input, then press Home to put the cursor at position 0. Before the fix, autocomplete would incorrectly trigger on the @someone further right in the text. After the fix, no trigger fires (correctly — there's nothing before position 0).

The fix is a one-liner: trust cursorPosition as the walker computed it.

// After
// Find trigger character before cursor
const textBeforeCursor = text.substring(0, cursorPosition);

Note: the element-node path on line 270 uses its own offset || text.length fallback, so the element-node case is unchanged. This PR only touches the text-node path's downstream override.

Modified file:

  • packages/vscode-ide-companion/src/webview/hooks/useCompletionTrigger.ts — removed the cursorPosition === 0 override

Reviewer Test Plan

  1. Open the VS Code companion webview
  2. Type hello @someone into the input
  3. Press Home to move cursor to position 0
  4. Verify no @ autocomplete appears (before the fix, it would)
  5. Type @ at position 0 — cursor moves to position 1, @ autocomplete should appear correctly
  6. Regression check: normal @// completions elsewhere in the text continue to work

Testing Matrix

macOS Windows Linux
npm run ? pass ?
npx ? ? ?
Docker ? ? ?
Podman ? - -
Seatbelt ? - -

Completion trigger detection overrode a legitimate cursorPosition=0 to
text.length when the text was non-empty. The walker on the text-node
path already sets cursorPosition to text.length when the node is not
found (line 292), so by the time we reach the override, position 0
means the cursor is genuinely at the start. Overriding it caused
substring(0, text.length) to search the ENTIRE text for @ and /,
pulling trigger matches from content AFTER the cursor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant