Handle surrogate pair when converting from LSP encoding #1785
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The generic LSP completer adds 1 to the code unit position before conversion to get a 1-based index:
ycmd/ycmd/completers/language_server/language_server_completer.py
Lines 3489 to 3490 in a51329a
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
:ycmd/ycmd/completers/language_server/language_server_protocol.py
Lines 826 to 827 in a51329a
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