Search in sources :

Example 6 with MavenProjectIndicesManager

use of org.jetbrains.idea.maven.indices.MavenProjectIndicesManager in project intellij-community by JetBrains.

the class MavenDependenciesCompletionProvider method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement xmlText = parameters.getPosition().getParent();
    if (!(xmlText instanceof XmlText))
        return;
    PsiElement eDependencyTag = xmlText.getParent();
    if (!(eDependencyTag instanceof XmlTag))
        return;
    XmlTag dependencyTag = (XmlTag) eDependencyTag;
    if (!"dependency".equals(dependencyTag.getName()))
        return;
    if (!PsiImplUtil.isLeafElementOfType(xmlText.getPrevSibling(), XmlTokenType.XML_TAG_END) || !PsiImplUtil.isLeafElementOfType(xmlText.getNextSibling(), XmlTokenType.XML_END_TAG_START)) {
        return;
    }
    Project project = dependencyTag.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(dependencyTag);
    if (!(domElement instanceof MavenDomDependency)) {
        return;
    }
    MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
    for (String groupId : indicesManager.getGroupIds()) {
        for (String artifactId : indicesManager.getArtifactIds(groupId)) {
            LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId).withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
            result.addElement(builder);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager) DomElement(com.intellij.util.xml.DomElement) XmlText(com.intellij.psi.xml.XmlText) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 7 with MavenProjectIndicesManager

use of org.jetbrains.idea.maven.indices.MavenProjectIndicesManager in project intellij-community by JetBrains.

the class MavenDependencyCompletionUtil method addTypeAndClassifierAndVersion.

public static void addTypeAndClassifierAndVersion(@NotNull InsertionContext context, @NotNull MavenDomDependency dependency, @NotNull String groupId, @NotNull String artifactId) {
    if (!StringUtil.isEmpty(dependency.getVersion().getStringValue()))
        return;
    Project project = context.getProject();
    if (!isInsideManagedDependency(dependency)) {
        MavenDomProjectModel model = DomUtil.<MavenDomProjectModel>getFileElement(dependency).getRootElement();
        MavenDomDependency managedDependency = findManagedDependency(model, project, groupId, artifactId);
        if (managedDependency != null) {
            if (dependency.getClassifier().getXmlTag() == null && dependency.getType().getXmlTag() == null) {
                String classifier = managedDependency.getClassifier().getRawText();
                if (StringUtil.isNotEmpty(classifier)) {
                    dependency.getClassifier().setStringValue(classifier);
                }
                String type = managedDependency.getType().getRawText();
                if (StringUtil.isNotEmpty(type)) {
                    dependency.getType().setStringValue(type);
                }
            }
            return;
        }
    }
    MavenProjectIndicesManager manager = MavenProjectIndicesManager.getInstance(project);
    Set<String> versions = manager.getVersions(groupId, artifactId);
    if (versions.size() == 1) {
        dependency.getVersion().setStringValue(ContainerUtil.getFirstItem(versions));
        return;
    }
    dependency.getVersion().setStringValue("");
    int versionPosition = dependency.getVersion().getXmlTag().getValue().getTextRange().getStartOffset();
    context.getEditor().getCaretModel().moveToOffset(versionPosition);
    if (versions.size() > 0) {
        invokeCompletion(context, CompletionType.BASIC);
    }
}
Also used : Project(com.intellij.openapi.project.Project) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager)

Example 8 with MavenProjectIndicesManager

use of org.jetbrains.idea.maven.indices.MavenProjectIndicesManager in project intellij-community by JetBrains.

the class MavenGroovyPomCompletionContributor method completeVersions.

private static void completeVersions(@NotNull CompletionResultSet completionResultSet, @NotNull Project project, @Nullable String groupId, @Nullable String artifactId, @NotNull String prefix) {
    if (StringUtil.isEmptyOrSpaces(artifactId))
        return;
    CompletionResultSet newResultSet = completionResultSet.withRelevanceSorter(CompletionService.getCompletionService().emptySorter().weigh(new LookupElementWeigher("mavenVersionWeigher") {

        @Nullable
        @Override
        public Comparable weigh(@NotNull LookupElement element) {
            return new NegatingComparable(new MavenVersionComparable(StringUtil.trimStart(element.getLookupString(), prefix)));
        }
    }));
    MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
    Set<String> versions;
    if (StringUtil.isEmptyOrSpaces(groupId)) {
        versions = Collections.emptySet();
    //if (!(coordinates instanceof MavenDomPlugin)) return;
    //
    //versions = indicesManager.getVersions(MavenArtifactUtil.DEFAULT_GROUPS[0], artifactId);
    //for (int i = 0; i < MavenArtifactUtil.DEFAULT_GROUPS.length; i++) {
    //  versions = Sets.union(versions, indicesManager.getVersions(MavenArtifactUtil.DEFAULT_GROUPS[i], artifactId));
    //}
    } else {
        versions = indicesManager.getVersions(groupId, artifactId);
    }
    for (String version : versions) {
        newResultSet.addElement(LookupElementBuilder.create(prefix + version));
    }
    newResultSet.addElement(LookupElementBuilder.create(prefix + RepositoryUtils.ReleaseVersionId));
    newResultSet.addElement(LookupElementBuilder.create(prefix + RepositoryUtils.LatestVersionId));
}
Also used : NegatingComparable(com.intellij.codeInsight.completion.impl.NegatingComparable) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager) MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) LookupElementWeigher(com.intellij.codeInsight.lookup.LookupElementWeigher) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MavenProjectIndicesManager (org.jetbrains.idea.maven.indices.MavenProjectIndicesManager)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 Project (com.intellij.openapi.project.Project)4 XmlTag (com.intellij.psi.xml.XmlTag)3 NotNull (org.jetbrains.annotations.NotNull)3 NegatingComparable (com.intellij.codeInsight.completion.impl.NegatingComparable)2 LookupElementWeigher (com.intellij.codeInsight.lookup.LookupElementWeigher)2 PsiElement (com.intellij.psi.PsiElement)2 XmlText (com.intellij.psi.xml.XmlText)2 DomElement (com.intellij.util.xml.DomElement)2 MavenVersionComparable (org.jetbrains.idea.maven.dom.MavenVersionComparable)2 MavenId (org.jetbrains.idea.maven.model.MavenId)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)1 HashSet (com.intellij.util.containers.hash.HashSet)1 GenericDomValue (com.intellij.util.xml.GenericDomValue)1 GenericDomValueReference (com.intellij.util.xml.impl.GenericDomValueReference)1 MavenArtifactCoordinatesVersionConverter (org.jetbrains.idea.maven.dom.converters.MavenArtifactCoordinatesVersionConverter)1 MavenDomArtifactCoordinates (org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates)1 MavenDomDependency (org.jetbrains.idea.maven.dom.model.MavenDomDependency)1