Position[] selectionRange = selection.getSelectionRange(false);
boolean selectionChanged = false;
// 1) New beginning of selection based on suffix-matching and
// backspaceCount
Position selectionStart = selectionRange[0];
int selectionStartColumn = selectionStart.getColumn();
String textBefore = selectionStart.getLine().getText().substring(0, selectionStartColumn);
if (!textBefore.endsWith(preContentSuffix)) {
Log.warn(getClass(),
"expected suffix [" + preContentSuffix + "] do not match [" + textBefore + "]");
return;
}
int matchCount = preContentSuffix.length();
int leftOffset = backspaceCount + matchCount;
if (leftOffset > 0) {
selectionStart = getPosition(selectionStart, -leftOffset);
selectionChanged = true;
}
// 2) Calculate end of selection
Position selectionEnd = selectionRange[1];
if (deleteCount > 0) {
selectionEnd = getPosition(selectionEnd, deleteCount);
selectionChanged = true;
}
// 3) Set selection it was changed.
if (selectionChanged) {
selection.setSelection(selectionStart.getLineInfo(), selectionStart.getColumn(),
selectionEnd.getLineInfo(), selectionEnd.getColumn());
}
// 4) Replace selection
EditorDocumentMutator mutator = editor.getEditorDocumentMutator();
if (selection.hasSelection() || autocompletionText.length() > 0) {
mutator.insertText(selectionStart.getLine(), selectionStart.getLineNumber(),
selectionStart.getColumn(), autocompletionText);
}
// 5) Move cursor / set final selection
selectionEnd = getPosition(selectionStart, jumpLength);
if (selectionCount == 0) {
selection.setCursorPosition(selectionEnd.getLineInfo(), selectionEnd.getColumn());
} else {
selectionStart = getPosition(selectionStart, jumpLength - selectionCount);
selection.setSelection(selectionStart.getLineInfo(), selectionStart.getColumn(),
selectionEnd.getLineInfo(), selectionEnd.getColumn());
}
}