Search in sources :

Example 1 with XmlTag

use of com.intellij.psi.xml.XmlTag in project intellij-community by JetBrains.

the class MavenPluginParamInfo method getParamInfoList.

public static ParamInfoList getParamInfoList(@NotNull XmlTag paramTag) {
    XmlTag configurationTag = paramTag;
    DomElement domElement;
    Map m = getMap().get(paramTag.getName());
    while (true) {
        if (m == null)
            return ParamInfoList.EMPTY;
        configurationTag = configurationTag.getParentTag();
        if (configurationTag == null)
            return ParamInfoList.EMPTY;
        String tagName = configurationTag.getName();
        if ("configuration".equals(tagName)) {
            domElement = DomManager.getDomManager(configurationTag.getProject()).getDomElement(configurationTag);
            if (domElement instanceof MavenDomConfiguration) {
                break;
            }
            if (domElement != null)
                return ParamInfoList.EMPTY;
        }
        m = (Map) m.get(tagName);
    }
    Map<Pair<String, String>, Map<String, ParamInfo>> pluginsMap = m;
    MavenDomConfiguration domCfg = (MavenDomConfiguration) domElement;
    MavenDomPlugin domPlugin = domCfg.getParentOfType(MavenDomPlugin.class, true);
    if (domPlugin == null)
        return ParamInfoList.EMPTY;
    String pluginGroupId = domPlugin.getGroupId().getStringValue();
    String pluginArtifactId = domPlugin.getArtifactId().getStringValue();
    Map<String, ParamInfo> goalsMap;
    if (pluginGroupId == null) {
        goalsMap = pluginsMap.get(Pair.create("org.apache.maven.plugins", pluginArtifactId));
        if (goalsMap == null) {
            goalsMap = pluginsMap.get(Pair.create("org.codehaus.mojo", pluginArtifactId));
        }
    } else {
        goalsMap = pluginsMap.get(Pair.create(pluginGroupId, pluginArtifactId));
    }
    if (goalsMap == null)
        return ParamInfoList.EMPTY;
    DomElement parent = domCfg.getParent();
    if (parent instanceof MavenDomPluginExecution) {
        SmartList<ParamInfo> infos = null;
        MavenDomGoals goals = ((MavenDomPluginExecution) parent).getGoals();
        for (MavenDomGoal goal : goals.getGoals()) {
            ParamInfo info = goalsMap.get(goal.getStringValue());
            if (info != null) {
                if (infos == null) {
                    infos = new SmartList<>();
                }
                infos.add(info);
            }
        }
        if (infos != null) {
            ParamInfo defaultInfo = goalsMap.get(null);
            if (defaultInfo != null) {
                infos.add(defaultInfo);
            }
            return new ParamInfoList(domCfg, infos);
        }
    }
    ParamInfo defaultInfo = goalsMap.get(null);
    if (defaultInfo != null) {
        return new ParamInfoList(domCfg, Collections.singletonList(defaultInfo));
    }
    return ParamInfoList.EMPTY;
}
Also used : DomElement(com.intellij.util.xml.DomElement) XmlTag(com.intellij.psi.xml.XmlTag) Pair(com.intellij.openapi.util.Pair)

Example 2 with XmlTag

use of com.intellij.psi.xml.XmlTag in project intellij-community by JetBrains.

the class MavenGroovyPomCompletionContributor method fillCompletionVariants.

@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("pom.xml", XMLLanguage.INSTANCE, buf);
    psiFile.putUserData(ORIGINAL_POM_FILE, virtualFile);
    List<Object> variants = ContainerUtil.newArrayList();
    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(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()) {
        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);
            }
        }
    }
    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);
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) MavenProject(org.jetbrains.idea.maven.project.MavenProject) GenericDomValueReference(com.intellij.util.xml.impl.GenericDomValueReference) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) MavenProjectIndicesManager(org.jetbrains.idea.maven.indices.MavenProjectIndicesManager) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Project(com.intellij.openapi.project.Project) MavenProject(org.jetbrains.idea.maven.project.MavenProject) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with XmlTag

use of com.intellij.psi.xml.XmlTag in project intellij-community by JetBrains.

the class MavenNavigationUtil method createNavigatableForDependency.

@Nullable
public static Navigatable createNavigatableForDependency(final Project project, final VirtualFile file, final MavenArtifact artifact) {
    return new NavigatableAdapter() {

        public void navigate(boolean requestFocus) {
            if (!file.isValid())
                return;
            MavenDomProjectModel projectModel = MavenDomUtil.getMavenDomProjectModel(project, file);
            if (projectModel == null)
                return;
            MavenDomDependency dependency = findDependency(projectModel, artifact.getGroupId(), artifact.getArtifactId());
            if (dependency == null)
                return;
            XmlTag artifactId = dependency.getArtifactId().getXmlTag();
            if (artifactId == null)
                return;
            navigate(project, artifactId.getContainingFile().getVirtualFile(), artifactId.getTextOffset() + artifactId.getName().length() + 2, requestFocus);
        }
    };
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) NavigatableAdapter(com.intellij.pom.NavigatableAdapter) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with XmlTag

use of com.intellij.psi.xml.XmlTag in project intellij-community by JetBrains.

the class UndeclaredTestInspection method checkClass.

@Nullable
public ProblemDescriptor[] checkClass(@NotNull final PsiClass aClass, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    if (TestNGUtil.hasTest(aClass) && PsiClassUtil.isRunnableClass(aClass, true)) {
        final Project project = aClass.getProject();
        final String qName = aClass.getQualifiedName();
        if (qName == null)
            return null;
        final String packageQName = StringUtil.getPackageName(qName);
        final List<String> names = new ArrayList<>();
        for (int i = 0; i < qName.length(); i++) {
            if (qName.charAt(i) == '.') {
                names.add(qName.substring(0, i));
            }
        }
        names.add(qName);
        Collections.reverse(names);
        for (final String name : names) {
            final boolean isFullName = qName.equals(name);
            final boolean[] found = new boolean[] { false };
            PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, (file, startOffset, endOffset) -> {
                if (file.findReferenceAt(startOffset) != null) {
                    if (!isFullName) {
                        //special package tag required
                        final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
                        if (tag == null || !tag.getName().equals("package")) {
                            return true;
                        }
                        final XmlAttribute attribute = tag.getAttribute("name");
                        if (attribute == null)
                            return true;
                        final String value = attribute.getValue();
                        if (value == null)
                            return true;
                        if (!value.endsWith(".*") && !value.equals(packageQName))
                            return true;
                    }
                    found[0] = true;
                    return false;
                }
                return true;
            }, new TestNGSearchScope(project));
            if (found[0])
                return null;
        }
        final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
        LOG.assertTrue(nameIdentifier != null);
        return new ProblemDescriptor[] { manager.createProblemDescriptor(nameIdentifier, "Undeclared test \'" + aClass.getName() + "\'", isOnTheFly, new LocalQuickFix[] { new RegisterClassFix(aClass), new CreateTestngFix() }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING) };
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with XmlTag

use of com.intellij.psi.xml.XmlTag in project intellij-community by JetBrains.

the class HtmlUnknownBooleanAttributeInspectionBase method checkAttribute.

@Override
protected void checkAttribute(@NotNull final XmlAttribute attribute, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (attribute.getValueElement() == null) {
        final XmlTag tag = attribute.getParent();
        if (tag instanceof HtmlTag) {
            XmlElementDescriptor elementDescriptor = tag.getDescriptor();
            if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
                return;
            }
            XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
            if (attributeDescriptor != null && !(attributeDescriptor instanceof AnyXmlAttributeDescriptor)) {
                String name = attribute.getName();
                if (!HtmlUtil.isBooleanAttribute(attributeDescriptor, null) && (!isCustomValuesEnabled() || !isCustomValue(name))) {
                    final boolean html5 = HtmlUtil.isHtml5Context(tag);
                    LocalQuickFix[] quickFixes = !html5 ? new LocalQuickFix[] { new AddCustomHtmlElementIntentionAction(BOOLEAN_ATTRIBUTE_KEY, name, XmlBundle.message("add.custom.html.boolean.attribute", name)), XmlQuickFixFactory.getInstance().addAttributeValueFix(attribute), new RemoveAttributeIntentionAction(name) } : new LocalQuickFix[] { XmlQuickFixFactory.getInstance().addAttributeValueFix(attribute) };
                    String error = null;
                    if (html5) {
                        if (attributeDescriptor instanceof XmlEnumerationDescriptor && ((XmlEnumerationDescriptor) attributeDescriptor).getValueDeclaration(attribute, "") == null) {
                            error = XmlErrorMessages.message("wrong.value", "attribute");
                        }
                    } else {
                        error = XmlErrorMessages.message("attribute.is.not.boolean", attribute.getName());
                    }
                    if (error != null) {
                        registerProblemOnAttributeName(attribute, error, holder, quickFixes);
                    }
                }
            }
        }
    }
}
Also used : AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) XmlEnumerationDescriptor(com.intellij.xml.impl.XmlEnumerationDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlTag (com.intellij.psi.xml.XmlTag)673 XmlFile (com.intellij.psi.xml.XmlFile)151 PsiElement (com.intellij.psi.PsiElement)130 XmlAttribute (com.intellij.psi.xml.XmlAttribute)121 Nullable (org.jetbrains.annotations.Nullable)99 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)89 NotNull (org.jetbrains.annotations.NotNull)89 PsiFile (com.intellij.psi.PsiFile)71 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)52 VirtualFile (com.intellij.openapi.vfs.VirtualFile)50 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)50 Project (com.intellij.openapi.project.Project)48 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)47 ArrayList (java.util.ArrayList)43 XmlDocument (com.intellij.psi.xml.XmlDocument)39 DomElement (com.intellij.util.xml.DomElement)35 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 TextRange (com.intellij.openapi.util.TextRange)32 Result (com.intellij.openapi.application.Result)24 Document (com.intellij.openapi.editor.Document)24