Skip to content

Handle surrogate pair when converting from LSP encoding #1785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bullno1
Copy link

@bullno1 bullno1 commented Jun 6, 2025

The generic LSP completer adds 1 to the code unit position before conversion to get a 1-based index:

lsp.UTF16CodeUnitsToCodepoints( line_value,
location[ 'character' ] + 1 ) )

However, this is problematic when the original index is pointing at a surrogate pair.
For example: given the string: 😊, the language server sends index 0.
Adding 1 will point it to the low surrogate and the following conversion will fail with a UnicodeError:
bytes_included = value_as_utf16_bytes[ : code_unit_offset * 2 ]
return len( bytes_included.decode( 'utf-16-le' ) )

This is because the surrogate pair is sliced in half.

What this PR does is checking whether the high byte at the offset is a low surrogate and advance the offset further to skip the entire pair.

This only happens for positions sent from the language server so it is not a reversible conversion.
I added a separate test case from test_CodepointsToUTF16CodeUnitsAndReverse


This change is Reviewable

@bullno1 bullno1 force-pushed the handle-utf16-surrogate-pair branch from 1c73ef0 to 8fbddc5 Compare June 6, 2025 19:58
Copy link
Member

@puremourning puremourning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

thanks!

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: 1 of 2 LGTMs obtained (waiting on @bullno1)

@puremourning
Copy link
Member

@Mergifyio rebase

Copy link
Contributor

mergify bot commented Jun 16, 2025

rebase

✅ Branch has been successfully rebased

@puremourning puremourning force-pushed the handle-utf16-surrogate-pair branch from 8fbddc5 to ecd1bb8 Compare June 16, 2025 20:07
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.

2 participants