raspu / Highlightr

iOS & OSX Syntax Highlighter.
MIT License
1.66k stars 261 forks source link

RTL Writing direction doesn't work #61

Closed lukakerr closed 3 years ago

lukakerr commented 5 years ago

I'm using macOS. I've noticed that when setting the writing direction in a NSTextView by right-clicking, and choosing Writing Direction > Right to Left, whenever the text changes the direction gets un-set, but when all text is removed, the cursor correctly appears on the right hand side of the text view.

I had a dig in to the code and believe this is happening in the highlight(_ range: NSRange) method of the CodeAttributedString class. I'm not sure exactly what is causing it, but think it has to do with the ranges being updated.

For example

If highlight() is called with the range {0, 1}, this represents a location of 0 and length of 1. The problem is that if the writing direction is Right to Left, and a single character is typed, then the range would be the exact same {0, 1}, since it's in location 0 and length 1, but this time from the right hand side of the text view.

As a result, whatever is typed in the Right to Left direction gets updated correctly, but gets put in the left hand side of the text view as if the direction is Left to Right.

I think fixing this may be fairly easy, as it should just require getting the writing direction and setting again somewhere, but it may not be trivial if some calculations need to be done around the range.

Below is a simple demo

highlightr

lukakerr commented 5 years ago

Just FYI, I managed to fix this on my fork - https://github.com/lukakerr/Highlightr/commit/c2ccb5d3f8c5e5bfe1a4bdb1b00d3bbbe2233987 (also updated to Swift 4.2 as well). After testing, Right to Left direction seems to work for me on macOS.

I'm not 100% sure if this fix is correct, as I've never developed for iOS and am not even sure how to get the writing direction on iOS. If you think it's fine, I can open a PR.

RobertoMachorro commented 3 years ago

Hi @lukakerr , are you doing a pull request from your fork?