Search in sources :

Example 6 with MavenDomDependency

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

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

the class MavenPomXmlCompletionTagListenerContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    if (TemplateManager.getInstance(parameters.getOriginalFile().getProject()).getActiveTemplate(parameters.getEditor()) != null) {
        // Don't brake the template.
        return;
    }
    PsiFile psiFile = parameters.getOriginalFile();
    if (!(psiFile instanceof XmlFile))
        return;
    if (!MavenDomUtil.isProjectFile(psiFile))
        return;
    DomFileDescription<?> description = DomManager.getDomManager(psiFile.getProject()).getDomFileDescription((XmlFile) psiFile);
    if (!(description instanceof MavenDomProjectModelDescription))
        return;
    result.runRemainingContributors(parameters, r -> {
        final LookupElement lookupElement = r.getLookupElement();
        if (myHandledTags.contains(lookupElement.getLookupString())) {
            LookupElement decorator = LookupElementDecorator.withInsertHandler(lookupElement, new InsertHandler<LookupElementDecorator<LookupElement>>() {

                @Override
                public void handleInsert(final InsertionContext context, LookupElementDecorator<LookupElement> item) {
                    lookupElement.handleInsert(context);
                    Object object = lookupElement.getObject();
                    if ("dependency".equals(lookupElement.getLookupString()) && object instanceof XmlTag && "maven-4.0.0.xsd".equals(((XmlTag) object).getContainingFile().getName())) {
                        context.commitDocument();
                        CaretModel caretModel = context.getEditor().getCaretModel();
                        PsiElement psiElement = context.getFile().findElementAt(caretModel.getOffset());
                        XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
                        if (xmlTag != null) {
                            DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement(xmlTag);
                            if (domElement instanceof MavenDomDependency) {
                                String s = "\n<groupId></groupId>\n<artifactId></artifactId>\n";
                                context.getDocument().insertString(caretModel.getOffset(), s);
                                caretModel.moveToOffset(caretModel.getOffset() + s.length() - "</artifactId>\n".length());
                                context.commitDocument();
                                new ReformatCodeProcessor(context.getProject(), context.getFile(), xmlTag.getTextRange(), false).run();
                                MavenDependencyCompletionUtil.invokeCompletion(context, CompletionType.BASIC);
                            }
                        }
                    }
                }
            });
            r = r.withLookupElement(decorator);
        }
        result.passResult(r);
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) XmlFile(com.intellij.psi.xml.XmlFile) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) DomElement(com.intellij.util.xml.DomElement) MavenDomProjectModelDescription(org.jetbrains.idea.maven.dom.MavenDomProjectModelDescription) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 8 with MavenDomDependency

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

the class MavenDuplicateDependenciesInspection method collect.

private static void collect(MultiMap<DependencyConflictId, MavenDomDependency> duplicates, @NotNull MavenDomDependencies dependencies) {
    for (MavenDomDependency dependency : dependencies.getDependencies()) {
        DependencyConflictId mavenId = DependencyConflictId.create(dependency);
        if (mavenId == null)
            continue;
        duplicates.putValue(mavenId, dependency);
    }
}
Also used : DependencyConflictId(org.jetbrains.idea.maven.dom.DependencyConflictId) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 9 with MavenDomDependency

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

the class MavenDuplicateDependenciesInspection method addProblem.

private static void addProblem(@NotNull MavenDomDependency dependency, @NotNull Collection<MavenDomDependency> dependencies, @NotNull DomElementAnnotationHolder holder) {
    StringBuilder sb = new StringBuilder();
    Set<MavenDomProjectModel> processed = new HashSet<>();
    for (MavenDomDependency domDependency : dependencies) {
        if (dependency.equals(domDependency))
            continue;
        MavenDomProjectModel model = domDependency.getParentOfType(MavenDomProjectModel.class, false);
        if (model != null && !processed.contains(model)) {
            if (processed.size() > 0)
                sb.append(", ");
            sb.append(createLinkText(model, domDependency));
            processed.add(model);
        }
    }
    holder.createProblem(dependency, HighlightSeverity.WARNING, MavenDomBundle.message("MavenDuplicateDependenciesInspection.has.duplicates", sb.toString()));
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) HashSet(com.intellij.util.containers.hash.HashSet) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 10 with MavenDomDependency

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

the class AddMavenDependencyQuickFix method invoke.

public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
    if (!myRef.isValid())
        return;
    MavenProject mavenProject = MavenDomUtil.findContainingProject(file);
    if (mavenProject == null)
        return;
    final List<MavenId> ids = MavenArtifactSearchDialog.searchForClass(project, getReferenceText());
    if (ids.isEmpty())
        return;
    final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(project, mavenProject.getFile());
    if (model == null)
        return;
    new WriteCommandAction(project, "Add Maven Dependency", DomUtil.getFile(model)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            boolean isTestSource = false;
            VirtualFile virtualFile = file.getOriginalFile().getVirtualFile();
            if (virtualFile != null) {
                isTestSource = ProjectRootManager.getInstance(project).getFileIndex().isInTestSourceContent(virtualFile);
            }
            for (MavenId each : ids) {
                MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, each);
                if (isTestSource) {
                    dependency.getScope().setStringValue("test");
                }
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Result(com.intellij.openapi.application.Result) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Aggregations

MavenDomDependency (org.jetbrains.idea.maven.dom.model.MavenDomDependency)12 MavenDomProjectModel (org.jetbrains.idea.maven.dom.model.MavenDomProjectModel)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 XmlTag (com.intellij.psi.xml.XmlTag)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 DomElement (com.intellij.util.xml.DomElement)2 Nullable (org.jetbrains.annotations.Nullable)2 DependencyConflictId (org.jetbrains.idea.maven.dom.DependencyConflictId)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 LookupElementDecorator (com.intellij.codeInsight.lookup.LookupElementDecorator)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)1 Project (com.intellij.openapi.project.Project)1 NavigatableAdapter (com.intellij.pom.NavigatableAdapter)1 XmlCodeStyleSettings (com.intellij.psi.formatter.xml.XmlCodeStyleSettings)1