I agree, stupid that getSelection()does not exist. Your solution works fine, but you should assume that only the cursor is displayed there, and not the entire selected range of text. I have not figured out how to fill this hole.
EDIT: Oh of course:
int selStart = ic.getTextBeforeCursor(HIGH_NUMBER, 0).length();
String sel = ic.getSelectedText();
int selEnd = selStart + (sel==null? 0: sel.length());
source
share