Search in sources :

Example 1 with MavenVersionComparable

use of org.jetbrains.idea.maven.dom.MavenVersionComparable in project intellij-community by JetBrains.

the class MavenVersionCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    if (parameters.getCompletionType() != CompletionType.BASIC)
        return;
    PsiElement element = parameters.getPosition();
    PsiElement xmlText = element.getParent();
    if (!(xmlText instanceof XmlText))
        return;
    PsiElement tagElement = xmlText.getParent();
    if (!(tagElement instanceof XmlTag))
        return;
    XmlTag tag = (XmlTag) tagElement;
    Project project = element.getProject();
    DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
    if (!(domElement instanceof GenericDomValue))
        return;
    DomElement parent = domElement.getParent();
    if (parent instanceof MavenDomArtifactCoordinates && ((GenericDomValue) domElement).getConverter() instanceof MavenArtifactCoordinatesVersionConverter) {
        MavenDomArtifactCoordinates coordinates = (MavenDomArtifactCoordinates) parent;
        String groupId = coordinates.getGroupId().getStringValue();
        String artifactId = coordinates.getArtifactId().getStringValue();
        if (StringUtil.isEmptyOrSpaces(artifactId))
            return;
        CompletionResultSet newResultSet = result.withRelevanceSorter(CompletionService.getCompletionService().emptySorter().weigh(new LookupElementWeigher("mavenVersionWeigher") {

            @Nullable
            @Override
            public Comparable weigh(@NotNull LookupElement element) {
                return new NegatingComparable(new MavenVersionComparable(element.getLookupString()));
            }
        }));
        MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
        Set<String> versions;
        if (StringUtil.isEmptyOrSpaces(groupId)) {
            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(version));
        }
        newResultSet.addElement(LookupElementBuilder.create(RepositoryUtils.ReleaseVersionId));
        newResultSet.addElement(LookupElementBuilder.create(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) MavenDomPlugin(org.jetbrains.idea.maven.dom.model.MavenDomPlugin) MavenArtifactCoordinatesVersionConverter(org.jetbrains.idea.maven.dom.converters.MavenArtifactCoordinatesVersionConverter) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) LookupElementWeigher(com.intellij.codeInsight.lookup.LookupElementWeigher) XmlText(com.intellij.psi.xml.XmlText) MavenDomArtifactCoordinates(org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) GenericDomValue(com.intellij.util.xml.GenericDomValue)

Example 2 with MavenVersionComparable

use of org.jetbrains.idea.maven.dom.MavenVersionComparable in project intellij-community by JetBrains.

the class MavenSearcher method sort.

private void sort(List<RESULT_TYPE> result) {
    for (RESULT_TYPE each : result) {
        if (each.versions.size() > 1) {
            TreeMap<MavenVersionComparable, MavenArtifactInfo> tree = new TreeMap<>(Collections.reverseOrder());
            for (MavenArtifactInfo artifactInfo : each.versions) {
                tree.put(new MavenVersionComparable(artifactInfo.getVersion()), artifactInfo);
            }
            each.versions.clear();
            each.versions.addAll(tree.values());
        }
    }
    Collections.sort(result, (o1, o2) -> makeSortKey(o1).compareTo(makeSortKey(o2)));
}
Also used : MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) MavenArtifactInfo(org.jetbrains.idea.maven.model.MavenArtifactInfo)

Example 3 with MavenVersionComparable

use of org.jetbrains.idea.maven.dom.MavenVersionComparable 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)

Example 4 with MavenVersionComparable

use of org.jetbrains.idea.maven.dom.MavenVersionComparable in project intellij-community by JetBrains.

the class MavenVersionsRemoteManager method execute.

@NotNull
@Override
public List<String> execute(@NotNull RepositoryLibraryDescription repositoryLibraryDescription, ProgressIndicator indicator) {
    MavenEmbeddersManager manager = MavenProjectsManager.getInstance(myProject).getEmbeddersManager();
    MavenEmbedderWrapper embedder = manager.getEmbedder(MavenEmbeddersManager.FOR_GET_VERSIONS, null, null);
    embedder.customizeForGetVersions();
    try {
        List<MavenRemoteRepository> remoteRepositories = convertRepositories(repositoryLibraryDescription.getRemoteRepositories());
        List<String> versions = embedder.retrieveVersions(repositoryLibraryDescription.getGroupId(), repositoryLibraryDescription.getArtifactId(), remoteRepositories);
        Collections.sort(versions, (o1, o2) -> {
            MavenVersionComparable v1 = new MavenVersionComparable(o1);
            MavenVersionComparable v2 = new MavenVersionComparable(o2);
            return v2.compareTo(v1);
        });
        return versions;
    } catch (MavenProcessCanceledException e) {
        return Collections.emptyList();
    } finally {
        manager.release(embedder);
    }
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) MavenRemoteRepository(org.jetbrains.idea.maven.model.MavenRemoteRepository) MavenEmbeddersManager(org.jetbrains.idea.maven.project.MavenEmbeddersManager) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MavenVersionComparable

use of org.jetbrains.idea.maven.dom.MavenVersionComparable in project intellij-community by JetBrains.

the class RepositoryAttachDialog method updateComboboxSelection.

private void updateComboboxSelection(boolean force) {
    final String prevFilter = myFilterString;
    final JTextComponent field = (JTextComponent) myCombobox.getEditor().getEditorComponent();
    final int caret = field.getCaretPosition();
    myFilterString = field.getText();
    if (!force && Comparing.equal(myFilterString, prevFilter))
        return;
    int prevSize = myShownItems.size();
    myShownItems.clear();
    myInUpdate = true;
    final boolean itemSelected = myCoordinates.containsKey(myFilterString) && Comparing.strEqual((String) myCombobox.getSelectedItem(), myFilterString, false);
    final boolean filtered;
    if (itemSelected) {
        myShownItems.addAll(myCoordinates.keySet());
        filtered = false;
    } else {
        final String[] parts = myFilterString.split(" ");
        main: for (String coordinate : myCoordinates.keySet()) {
            for (String part : parts) {
                if (!StringUtil.containsIgnoreCase(coordinate, part))
                    continue main;
            }
            myShownItems.add(coordinate);
        }
        filtered = !myShownItems.isEmpty();
        if (!filtered) {
            myShownItems.addAll(myCoordinates.keySet());
        }
        myCombobox.setSelectedItem(null);
    }
    // use maven version sorter
    ArrayList<Comparable> comparables = new ArrayList<>(myShownItems.size());
    for (String item : myShownItems) {
        comparables.add(new MavenVersionComparable(item));
    }
    Collections.sort(comparables);
    myShownItems.clear();
    for (Comparable comparable : comparables) {
        myShownItems.add(comparable.toString());
    }
    ((CollectionComboBoxModel) myCombobox.getModel()).update();
    myInUpdate = false;
    field.setText(myFilterString);
    field.setCaretPosition(caret);
    updateInfoLabel();
    if (filtered) {
        if (prevSize < 10 && myShownItems.size() > prevSize && myCombobox.isPopupVisible()) {
            myCombobox.setPopupVisible(false);
        }
        if (!myCombobox.isPopupVisible()) {
            myCombobox.setPopupVisible(true);
        }
    }
}
Also used : MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) MavenVersionComparable(org.jetbrains.idea.maven.dom.MavenVersionComparable) CollectionComboBoxModel(com.intellij.ui.CollectionComboBoxModel) JTextComponent(javax.swing.text.JTextComponent)

Aggregations

MavenVersionComparable (org.jetbrains.idea.maven.dom.MavenVersionComparable)5 NotNull (org.jetbrains.annotations.NotNull)3 NegatingComparable (com.intellij.codeInsight.completion.impl.NegatingComparable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 LookupElementWeigher (com.intellij.codeInsight.lookup.LookupElementWeigher)2 MavenProjectIndicesManager (org.jetbrains.idea.maven.indices.MavenProjectIndicesManager)2 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlText (com.intellij.psi.xml.XmlText)1 CollectionComboBoxModel (com.intellij.ui.CollectionComboBoxModel)1 DomElement (com.intellij.util.xml.DomElement)1 GenericDomValue (com.intellij.util.xml.GenericDomValue)1 JTextComponent (javax.swing.text.JTextComponent)1 MavenArtifactCoordinatesVersionConverter (org.jetbrains.idea.maven.dom.converters.MavenArtifactCoordinatesVersionConverter)1 MavenDomArtifactCoordinates (org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates)1 MavenDomPlugin (org.jetbrains.idea.maven.dom.model.MavenDomPlugin)1 MavenArtifactInfo (org.jetbrains.idea.maven.model.MavenArtifactInfo)1 MavenRemoteRepository (org.jetbrains.idea.maven.model.MavenRemoteRepository)1 MavenEmbeddersManager (org.jetbrains.idea.maven.project.MavenEmbeddersManager)1