Skip to content

Fix Unicode offsets in completion replacement ranges - #4188

Open
rubemoon wants to merge 1 commit into
Shopify:mainfrom
rubemoon:fix/unicode-completion-offset
Open

Fix Unicode offsets in completion replacement ranges#4188
rubemoon wants to merge 1 commit into
Shopify:mainfrom
rubemoon:fix/unicode-completion-offset

Conversation

@rubemoon

@rubemoon rubemoon commented Aug 9, 2026

Copy link
Copy Markdown

Motivation

Closes #4180

Completion replacement ranges are incorrect when Unicode characters appear before the completion target on the same line.

For example:

test = "moo"
"🐮 says #{te}"

When completing te to test, the replacement range was returned as characters 13..15 instead of 11..13, causing the resulting text to be corrupted.

Implementation

Pass the document's negotiated code units cache to the completion listener and use it when converting Prism locations into LSP ranges.

A completion-specific range_from_prism_location helper now uses cached_start_code_units_column and cached_end_code_units_column so replacement ranges respect the position encoding negotiated with the client.

The shared Common#range_from_location helper remains unchanged, avoiding changes to other LSP features that also use that helper.

The conversion is applied consistently to Prism-based replacement ranges used by completion items.

Automated Tests

Added a regression test that reproduces the issue with a Unicode character before a local variable completion target.

Validation performed:

  • Unicode regression test: 1 run, 2 assertions, 0 failures
  • Completion test suite: 59 runs, 145 assertions, 0 failures
  • Full test suite: 18,541 runs, 67,651 assertions, 0 failures, 0 errors
  • bundle exec rubocop
  • bundle exec srb tc
  • git diff --check

Manual Tests

  1. Start Ruby LSP in Visual Studio Code.
  2. Create a Ruby file with:
test = "moo"
"🐮 says #{te}"
  1. Place the cursor immediately after te.
  2. Trigger completion.
  3. Select the test local variable completion.

Expected result:

test = "moo"
"🐮 says #{test}"

The completion should replace only te, without overwriting or duplicating surrounding characters.

@rubemoon
rubemoon requested a review from a team as a code owner August 9, 2026 00:47
@rubemoon

rubemoon commented Aug 9, 2026

Copy link
Copy Markdown
Author

I have signed the CLA!

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.

Incorrect completion inside interpolated string when prefix contains Unicode characters

1 participant