@Override
  public void invoke(@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(() -> {
      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("implementation '%s'\n", 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("implementation '%s'\n", getMavenArtifactKey(mavenId))), null);
          }
        }
      }
    });
  }
@Override
  public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file)