@Override
  public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    final PsiElement position = parameters.getPosition();
    if (!(position instanceof LeafElement)) return;

    Project project = position.getProject();
    VirtualFile virtualFile = parameters.getOriginalFile().getVirtualFile();
    if(virtualFile == null) return;

    MavenProject mavenProject = MavenProjectsManager.getInstance(project).findProject(virtualFile);
    if (mavenProject == null) return;

    List<String> methodCallInfo = MavenGroovyPomUtil.getGroovyMethodCalls(position);
    if (methodCallInfo.isEmpty()) return;

    StringBuilder buf = new StringBuilder();
    for (String s : methodCallInfo) {
      buf.append('<').append(s).append('>');
    }
    for (String s : ContainerUtil.reverse(methodCallInfo)) {
      buf.append('<').append(s).append("/>");
    }

    PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(MavenConstants.POM_XML, XMLLanguage.INSTANCE, buf);
    psiFile.putUserData(ORIGINAL_POM_FILE, virtualFile);
    List<Object> variants = new ArrayList<>();


    String lastMethodCall = ContainerUtil.getLastItem(methodCallInfo);
    Ref<Boolean> completeDependency = Ref.create(false);
    Ref<Boolean> completeVersion = Ref.create(false);
    psiFile.accept(new PsiRecursiveElementVisitor(true) {
      @Override
      public void visitElement(@NotNull PsiElement element) {
        super.visitElement(element);
        if (!completeDependency.get() && element.getParent() instanceof XmlTag &&
            "dependency".equals(((XmlTag)element.getParent()).getName())) {
          if ("artifactId".equals(lastMethodCall) || "groupId".equals(lastMethodCall)) {
            completeDependency.set(true);
          }
          else if ("version".equals(lastMethodCall) || "dependency".equals(lastMethodCall)) {
            completeVersion.set(true);
            //completeDependency.set(true);
          }
        }

        if (!completeDependency.get() && !completeVersion.get()) {
          PsiReference[] references = getReferences(element);
          for (PsiReference each : references) {
            if (each instanceof GenericDomValueReference) {
              Collections.addAll(variants, each.getVariants());
            }
          }
        }
      }
    });
    for (Object variant : variants) {
      if (variant instanceof LookupElement) {
        result.addElement((LookupElement)variant);
      }
      else {
        result.addElement(LookupElementBuilder.create(variant));
      }
    }

    if (completeDependency.get()) {
      DependencySearchService searchService = DependencySearchService.getInstance(project);

      for (String groupId : searchService.getGroupIds("")) {
        for (String artifactId : searchService.getArtifactIds(groupId)) {
          LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId)
            .withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
          result.addElement(builder);
        }
      }
    }

    if (completeVersion.get()) {
      consumeDependencyElement(position, closableBlock -> {

        String groupId = null;
        String artifactId = null;
        for (GrMethodCall methodCall : PsiTreeUtil.findChildrenOfType(closableBlock, GrMethodCall.class)) {
          GroovyPsiElement[] arguments = methodCall.getArgumentList().getAllArguments();
          if (arguments.length != 1) continue;
          PsiReference reference = arguments[0].getReference();
          if (reference == null) continue;

          String callExpression = methodCall.getInvokedExpression().getText();
          String argumentValue = reference.getCanonicalText();
          if ("groupId".equals(callExpression)) {
            groupId = argumentValue;
          }
          else if ("artifactId".equals(callExpression)) {
            artifactId = argumentValue;
          }
        }
        completeVersions(result, project, groupId, artifactId, "");
      }, element -> {
        if (element.getParent() instanceof PsiLiteral) {
          Object value = ((PsiLiteral)element.getParent()).getValue();
          if (value == null) return;

          String[] mavenCoordinates = value.toString().split(":");
          if (mavenCoordinates.length < 3) return;

          String prefix = mavenCoordinates[0] + ':' + mavenCoordinates[1] + ':';
          completeVersions(result, project, mavenCoordinates[0], mavenCoordinates[1], prefix);
        }
      });
    }
  }
@Override
  public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result)