void (final @NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) { final Ref<Boolean> toLowerCase = new Ref<>(Boolean.FALSE); runForCaret(editor, caret, c -> { if (!c.hasSelection()) { c.selectWordAtCaret(true); } int selectionStartOffset = c.getSelectionStart(); int selectionEndOffset = c.getSelectionEnd(); String originalText = editor.getDocument().getText(new TextRange(selectionStartOffset, selectionEndOffset)); if (!originalText.equals(toCase(editor, selectionStartOffset, selectionEndOffset, true))) { toLowerCase.set(Boolean.TRUE); } }); runForCaret(editor, caret, c -> { VisualPosition caretPosition = c.getVisualPosition(); int selectionStartOffset = c.getSelectionStart(); int selectionEndOffset = c.getSelectionEnd(); String originalText = editor.getDocument().getText(new TextRange(selectionStartOffset, selectionEndOffset)); String result = toCase(editor, selectionStartOffset, selectionEndOffset, toLowerCase.get()); editor.getDocument().replaceString(selectionStartOffset, selectionEndOffset, result); c.moveToVisualPosition(caretPosition); //Restore selection for TextComponentEditorImpl/TextAreaDocument etc. if (!c.hasSelection()) { c.setSelection(selectionStartOffset, selectionEndOffset + result.length() - originalText.length()); } }); }