void (@NotNull JTextComponent 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(); if (macro != null) { int position = textComponent.getCaretPosition(); int selectionStart = textComponent.getSelectionStart(); int selectionEnd = textComponent.getSelectionEnd(); try { if (selectionStart < selectionEnd) { textComponent.getDocument().remove(selectionStart, selectionEnd - selectionStart); position = selectionStart; } final String nameToInsert = (macro.startsWith("$") || macro.startsWith("%")) ? macro : "$" + macro + "$"; textComponent.getDocument().insertString(position, nameToInsert, null); textComponent.setCaretPosition(position + nameToInsert.length()); } catch (BadLocationException ignored) { } } } IdeFocusManager.findInstance().requestFocus(textComponent, true); }