neoclide / coc-highlight

Document highlight and document colors LSP support for coc.nvim
221 stars 12 forks source link

Color highlighting at wrong position in lines with characters requiring UTF-16 surrogate pairs #45

Closed FallenWarrior2k closed 1 year ago

FallenWarrior2k commented 3 years ago

A basic example to repro: pasting 🗎 red into an empty buffer with coc-highlight enabled will result in the following. image

Inspecting the character with ga, we find that we're dealing with U+1F5CE, which becomes 0xD83D 0xDDCE in UTF-16. My assumption here is that, due to "🗎".length === 2, the highlighting gets applied to the wrong columns, as the code assumes there's two characters, even though they only take up one column of screen space.

MDN's page on String.length suggests a function like the following to count the actual characters:

function getCharacterLength(str) {
  return [...str].length;
}

I am not familiar with the codebase, but I might contribute a fix myself if it seems easy enough.

Lastly, I want to note that a similar issue exists with the post-yank highlighting in coc-yank. If you want me to open an issue over there as well, I can do so, but I decided against it for now because I didn't want to open two issues with essentially the same body text.

chemzqm commented 3 years ago

Issue with coc.nvim, since I can't find vim's function to calculate utf16 offset

FallenWarrior2k commented 3 years ago

Ah well. Thanks for your quick response anyway and sorry if the way I wrote the issue came off as arrogant. I shouldn't have assumed it was something simple like string length. My bad.

chemzqm commented 1 year ago

Fixed on coc.nvim by https://github.com/neoclide/coc.nvim/commit/c019fd7002d5fe903389e9d2587e301a790f88f6