Search in sources :

Example 1 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute 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 2 with XmlAttribute

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

the class XmlNsPrefixAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (PsiUtilCore.getElementType(element) != XmlTokenType.XML_NAME)
        return;
    PsiElement parent = element.getParent();
    if (!(parent instanceof XmlTag) && !(parent instanceof XmlAttribute))
        return;
    TextRange elementRange = element.getTextRange();
    List<SchemaPrefixReference> references = ContainerUtil.findAll(parent.getReferences(), SchemaPrefixReference.class);
    for (SchemaPrefixReference ref : references) {
        TextRange rangeInElement = ref.getRangeInElement();
        if (rangeInElement.isEmpty())
            continue;
        TextRange range = rangeInElement.shiftRight(ref.getElement().getTextRange().getStartOffset());
        if (!range.intersects(elementRange))
            continue;
        holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) TextRange(com.intellij.openapi.util.TextRange) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with XmlAttribute

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

the class XmlUnusedNamespaceInspection method getLocationReferences.

private static PsiReference[] getLocationReferences(String namespace, XmlTag tag) {
    XmlAttribute locationAttr = tag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
    if (locationAttr == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    XmlAttributeValue value = locationAttr.getValueElement();
    return value == null ? PsiReference.EMPTY_ARRAY : getLocationReferences(namespace, value);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 4 with XmlAttribute

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

the class XmlUnusedNamespaceInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            PsiFile file = holder.getFile();
            if (!(file instanceof XmlFile))
                return;
            XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
            if (refCountHolder == null)
                return;
            if (!attribute.isNamespaceDeclaration()) {
                checkUnusedLocations(attribute, holder, refCountHolder);
                return;
            }
            String namespace = attribute.getValue();
            String declaredPrefix = getDeclaredPrefix(attribute);
            if (namespace != null && !refCountHolder.isInUse(declaredPrefix)) {
                ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
                for (ImplicitUsageProvider provider : implicitUsageProviders) {
                    if (provider.isImplicitUsage(attribute))
                        return;
                }
                XmlAttributeValue value = attribute.getValueElement();
                assert value != null;
                holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.declaration"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, false, !refCountHolder.isUsedNamespace(namespace)));
                XmlTag parent = attribute.getParent();
                if (declaredPrefix.isEmpty()) {
                    XmlAttribute location = getDefaultLocation(parent);
                    if (location != null) {
                        holder.registerProblem(location, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                } else if (!refCountHolder.isUsedNamespace(namespace)) {
                    for (PsiReference reference : getLocationReferences(namespace, parent)) {
                        if (!XmlHighlightVisitor.hasBadResolve(reference, false))
                            holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                }
            }
        }
    };
}
Also used : XmlRefCountHolder(com.intellij.xml.util.XmlRefCountHolder) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with XmlAttribute

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

the class HtmlLocalInspectionTool method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlToken(final XmlToken token) {
            IElementType tokenType = token.getTokenType();
            if (tokenType == XmlTokenType.XML_NAME || tokenType == XmlTokenType.XML_TAG_NAME) {
                PsiElement element = token.getPrevSibling();
                while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
                if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
                    PsiElement parent = element.getParent();
                    if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
                        XmlTag tag = (XmlTag) parent;
                        checkTag(tag, holder, isOnTheFly);
                    }
                }
            }
        }

        @Override
        public void visitXmlAttribute(final XmlAttribute attribute) {
            checkAttribute(attribute, holder, isOnTheFly);
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) IElementType(com.intellij.psi.tree.IElementType) OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6