void (@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) { if (!EditorModificationUtil.checkModificationAllowed(editor) || !FileModificationService.getInstance().preparePsiElementsForWrite(file)) return; final List<MavenId> ids; if (ApplicationManager.getApplication().isUnitTestMode()) { ids = AddGradleDslDependencyAction.TEST_THREAD_LOCAL.get(); } else { ids = MavenArtifactSearchDialog.searchForArtifact(project, Collections.emptyList()); } if (ids.isEmpty()) return; WriteCommandAction.writeCommandAction(project, file) .withName(GradleBundle.message("gradle.codeInsight.action.add_maven_dependency.text")).run(() -> { GradleVersion gradleVersion = GradleUtil.getGradleVersion(project, file); String scope = GradleUtil.isSupportedImplementationScope(gradleVersion) ? "implementation" : "compile"; GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project); List<GrMethodCall> closableBlocks = PsiTreeUtil.getChildrenOfTypeAsList(file, GrMethodCall.class); GrCall dependenciesBlock = ContainerUtil.find(closableBlocks, call -> { GrExpression expression = call.getInvokedExpression(); return "dependencies".equals(expression.getText()); }); if (dependenciesBlock == null) { StringBuilder buf = new StringBuilder(); for (MavenId mavenId : ids) { buf.append(String.format("%s '%s'\n", scope, getMavenArtifactKey(mavenId))); } dependenciesBlock = (GrCall)factory.createStatementFromText("dependencies{\n" + buf + "}"); file.add(dependenciesBlock); } else { GrClosableBlock closableBlock = ArrayUtil.getFirstElement(dependenciesBlock.getClosureArguments()); if (closableBlock != null) { for (MavenId mavenId : ids) { closableBlock.addStatementBefore( factory.createStatementFromText(String.format("%s '%s'\n", scope, getMavenArtifactKey(mavenId))), null); } } } }); }