Xcode Source Editor Extension - single character selection range

Currently XCSourceTextRange uses closed range to represend text selections and there is no way to distinguish single char selection and "insert mode" (no selection). I have an empty range in both cases (start.column == end.column), and after source editor command invoked the character is no longer selected.


As a solution we could use a half closed range to represent any selection:

start.column == 0 && end.column == 0 // no selection
start.column == 0 && end.column == 1 // one character selected


Is this the case or i've missed something?

Thanks!

Answered by mescher in 186053022

Fixed in Xcode 8.1, thanks. (tested on beta 2)

Hey! Your code logic isn't right.

start.column is the offset of te cursor from the BEGGINING of the line, so column zero would be exactly the begining of the line, even if there are any spaces or tabs.


Column zero would be right before the first letter of this sentence.

So if you want to know if theres no selection you should instead check:

start.column == end.column // No selection

Or if you want to check for only one character or n number of characters selected you should do this:

start.column == (end.column - 1) // only one character selected
start.column == (end.column - x) // only X number of characters selected


BTW, I finished my xcode extension two days ago and I'm still not able to use it in Xcode 8 final because of a **** bug. 😠 but that's another problem.


Hope this helps 😉

Thank you for your reply!

Yes, you're right about how it should work. This is what i was saying, my code logic the same as yours.

The thing is it doesn't work this way, in your second example when i should get:

start.column == (end.column - 1) // only one character selected

what i really get from invocation is:

start.column == end.column // No selection


Can you confirm, please, that in your extension end.column is not equal to star.column when you have single character selected?

What version do you use? Xcode 8 or 8.1 beta? I should try 8.1.

Accepted Answer

Fixed in Xcode 8.1, thanks. (tested on beta 2)

Xcode Source Editor Extension - single character selection range
 
 
Q