atom-archive / xray

An experimental next-generation Electron-based text editor
MIT License
8.48k stars 235 forks source link

Improve cursor movement when inserting #115

Closed MoritzKn closed 6 years ago

MoritzKn commented 6 years ago

Attempts to fix #113

Still not perfect: the rust char returned by chars() represent "Unicode Scalar Values" which may not be considered characters 1 to 1:

It's important to remember that char represents a Unicode Scalar Value, and may not match your idea of what a 'character' is. Iteration over grapheme clusters may be what you actually want.

https://stackoverflow.com/questions/46290655/get-the-string-length-in-characters-in-rust

See this demo in the rust play ground

Xray currently displays:

bildschirmfoto 2018-07-17 um 15 34 39

The problem seems to be that the perceived length of a string can't really be determined by the string in isolation. A good example is the 👍🏽 string in the demo: in the editor it is displayed as two separate characters (the skip tone modifier and the actual emoji) but in the output it is displayed as one (the emoji with changed skin tone). As a user I would expect the cursor to move two characters in the first case but one in the second.

However since Xray seems to be unable to display complex multibyte unicode characters anyway I'd consider this change to be sufficient for now.

nathansobo commented 6 years ago

Awesome! Thanks for digging in and investigating this. I think we can probably deal with grapheme clustering in the rendering layer, but you're correct that we don't render those kinds of characters correctly yet anyways.