protected AbstractStringManipAction(boolean setupHandler) {
super(null);
if (setupHandler) {
this.setupHandler(new EditorWriteActionHandler() {
public void executeWriteAction(Editor editor, DataContext dataContext) {
final SelectionModel selectionModel = editor.getSelectionModel();
String selectedText = selectionModel.getSelectedText();
boolean allLinSelected = false;
if (selectedText == null) {
selectionModel.selectLineAtCaret();
selectedText = selectionModel.getSelectedText();
allLinSelected = true;
if (selectedText == null) {
return;
}
} else if (selectedText.endsWith("\n")) {
allLinSelected = true;
}
String[] textParts = selectedText.split("\n");
if (selectionModel.hasBlockSelection()) {
int[] blockStarts = selectionModel.getBlockSelectionStarts();
int[] blockEnds = selectionModel.getBlockSelectionEnds();
int plusOffset = 0;
for (int i = 0; i < textParts.length; i++) {
String newTextPart = transform(textParts[i]);
if (allLinSelected) {
newTextPart += "\n";
}
editor.getDocument().replaceString(blockStarts[i] + plusOffset, blockEnds[i] + plusOffset,
newTextPart);
int realOldTextLength = blockEnds[i] - blockStarts[i];
plusOffset += newTextPart.length() - realOldTextLength;
}
} else {
for (int i = 0; i < textParts.length; i++) {
textParts[i] = transform(textParts[i]);
}
final String s = StringUtils.join(textParts, '\n');
editor.getDocument().replaceString(selectionModel.getSelectionStart(),
selectionModel.getSelectionEnd(), s);
if (allLinSelected) {
editor.getDocument().insertString(selectionModel.getSelectionEnd(), "\n");
}
}
}
});
}