void (@NotNull EditorTextField textComponent, @NotNull Predicate<? super Macro> filter, @Nullable Map<String, String> userMacros) { MacrosDialog dialog = new MacrosDialog(textComponent, filter, userMacros); if (dialog.showAndGet()) { String macro = dialog.getSelectedMacroName(); Editor editor = textComponent.getEditor(); if (macro != null && editor != null) { int selectionStart = editor.getSelectionModel().getSelectionStart(); int selectionEnd = editor.getSelectionModel().getSelectionEnd(); final String nameToInsert = (macro.startsWith("$") || macro.startsWith("%")) ? macro : "$" + macro + "$"; int position = selectionStart < selectionEnd ? selectionStart : editor.getCaretModel().getOffset(); WriteCommandAction.writeCommandAction(textComponent.getProject()).run(() -> { if (selectionStart < selectionEnd) { editor.getDocument().deleteString(selectionStart, selectionEnd - selectionStart); } textComponent.getDocument().insertString(position, nameToInsert); }); textComponent.setCaretPosition(position + nameToInsert.length()); } } IdeFocusManager.findInstance().requestFocus(textComponent, true); }