void (@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) { if (!EditorModificationUtil.checkModificationAllowed(editor)) return; if (!FileModificationService.getInstance().preparePsiElementsForWrite(file)) return; final JBList<PluginDescriptor> list = new JBList<>(myPlugins); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setCellRenderer(new MyListCellRenderer()); Runnable runnable = () -> { final PluginDescriptor selected = list.getSelectedValue(); WriteCommandAction.writeCommandAction(project, file).withName(GradleBundle.message("gradle.codeInsight.action.apply_plugin.text")) .run(() -> { if (selected == null) return; GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project); GrStatement grStatement = factory.createStatementFromText(String.format("apply plugin: '%s'", selected.name()), null); PsiElement anchor = file.findElementAt(editor.getCaretModel().getOffset()); PsiElement currentElement = PsiTreeUtil.getParentOfType(anchor, GrClosableBlock.class, GroovyFile.class); if (currentElement != null) { currentElement.addAfter(grStatement, anchor); } else { file.addAfter(grStatement, file.findElementAt(editor.getCaretModel().getOffset() - 1)); } PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); Document document = documentManager.getDocument(file); if (document != null) { documentManager.commitDocument(document); } }); }; if (ApplicationManager.getApplication().isUnitTestMode()) { PluginDescriptor descriptor = ContainerUtil.find(myPlugins, value -> value.name().equals(AddGradleDslPluginAction.TEST_THREAD_LOCAL.get())); list.setSelectedValue(descriptor, false); runnable.run(); } else { JBPopupFactory.getInstance().createListPopupBuilder(list) .setTitle(GradleBundle.message("gradle.codeInsight.action.apply_plugin.popup.title")) .setItemChosenCallback(runnable) .setNamerForFiltering(o -> o.name()) .createPopup() .showInBestPositionFor(editor); } }