public String getSelectedText() { if (!hasSelection()) return ""; int startIndex = getSelectionStart().toIndex(getLines()); int endIndex = getSelectionEnd().toIndex(getLines()); return text.substring(startIndex, endIndex); }