Search in sources :

Example 11 with XmlToken

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

the class XmlTagUtil method getEndTagNameElement.

@Nullable
public static XmlToken getEndTagNameElement(@NotNull XmlTag tag) {
    final ASTNode node = tag.getNode();
    if (node == null)
        return null;
    ASTNode current = node.getLastChildNode();
    ASTNode prev = current;
    while (current != null) {
        final IElementType elementType = prev.getElementType();
        if ((elementType == XmlTokenType.XML_NAME || elementType == XmlTokenType.XML_TAG_NAME) && current.getElementType() == XmlTokenType.XML_END_TAG_START) {
            return (XmlToken) prev.getPsi();
        }
        prev = current;
        current = current.getTreePrev();
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) XmlToken(com.intellij.psi.xml.XmlToken) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with XmlToken

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

the class SpellcheckingStrategy method getTokenizer.

@NotNull
public Tokenizer getTokenizer(PsiElement element) {
    if (element instanceof PsiWhiteSpace) {
        return EMPTY_TOKENIZER;
    }
    if (element instanceof PsiLanguageInjectionHost && InjectedLanguageUtil.hasInjections((PsiLanguageInjectionHost) element)) {
        return EMPTY_TOKENIZER;
    }
    if (element instanceof PsiNameIdentifierOwner)
        return new PsiIdentifierOwnerTokenizer();
    if (element instanceof PsiComment) {
        if (SuppressionUtil.isSuppressionComment(element)) {
            return EMPTY_TOKENIZER;
        }
        return myCommentTokenizer;
    }
    if (element instanceof XmlAttributeValue)
        return myXmlAttributeTokenizer;
    if (element instanceof PsiPlainText) {
        PsiFile file = element.getContainingFile();
        FileType fileType = file == null ? null : file.getFileType();
        if (fileType instanceof CustomSyntaxTableFileType) {
            return new CustomFileTypeTokenizer(((CustomSyntaxTableFileType) fileType).getSyntaxTable());
        }
        return TEXT_TOKENIZER;
    }
    if (element instanceof XmlToken) {
        if (((XmlToken) element).getTokenType() == XmlTokenType.XML_DATA_CHARACTERS) {
            PsiElement injection = InjectedLanguageManager.getInstance(element.getProject()).findInjectedElementAt(element.getContainingFile(), element.getTextOffset());
            if (injection == null) {
                return TEXT_TOKENIZER;
            }
        }
    }
    return EMPTY_TOKENIZER;
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlToken(com.intellij.psi.xml.XmlToken) FileType(com.intellij.openapi.fileTypes.FileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with XmlToken

use of com.intellij.psi.xml.XmlToken in project android by JetBrains.

the class AndroidXmlDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof ProvidedDocumentationPsiElement) {
        return ((ProvidedDocumentationPsiElement) element).getDocumentation();
    }
    if (element instanceof LazyValueResourceElementWrapper) {
        LazyValueResourceElementWrapper wrapper = (LazyValueResourceElementWrapper) element;
        ValueResourceInfo resourceInfo = wrapper.getResourceInfo();
        ResourceType type = resourceInfo.getType();
        String name = resourceInfo.getName();
        Module module = ModuleUtilCore.findModuleForPsiElement(element);
        if (module == null) {
            return null;
        }
        AndroidFacet facet = AndroidFacet.getInstance(element);
        if (facet == null) {
            return null;
        }
        ResourceUrl url;
        ResourceUrl originalUrl = originalElement != null ? ResourceUrl.parse(originalElement.getText()) : null;
        if (originalUrl != null && name.equals(originalUrl.name)) {
            url = originalUrl;
        } else {
            boolean isFramework = false;
            if (originalUrl != null) {
                isFramework = originalUrl.framework;
            } else {
                // Figure out if this resource is a framework file.
                // We really should store that info in the ValueResourceInfo instances themselves.
                // For now, attempt to figure it out
                SystemResourceManager systemResourceManager = facet.getSystemResourceManager();
                VirtualFile containingFile = resourceInfo.getContainingFile();
                if (systemResourceManager != null) {
                    VirtualFile parent = containingFile.getParent();
                    if (parent != null) {
                        VirtualFile resDir = parent.getParent();
                        if (resDir != null) {
                            isFramework = systemResourceManager.isResourceDir(resDir);
                        }
                    }
                }
            }
            url = ResourceUrl.create(type, name, isFramework, false);
        }
        return generateDoc(element, url);
    } else if (element instanceof MyResourceElement) {
        return getResourceDocumentation(element, ((MyResourceElement) element).myResource);
    } else if (element instanceof XmlAttributeValue) {
        return getResourceDocumentation(element, ((XmlAttributeValue) element).getValue());
    }
    if (originalElement instanceof XmlToken) {
        XmlToken token = (XmlToken) originalElement;
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_START_DELIMITER) {
            PsiElement next = token.getNextSibling();
            if (next instanceof XmlToken) {
                token = (XmlToken) next;
            }
        } else if (token.getTokenType() == XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            PsiElement prev = token.getPrevSibling();
            if (prev instanceof XmlToken) {
                token = (XmlToken) prev;
            }
        }
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_TOKEN) {
            String documentation = getResourceDocumentation(originalElement, token.getText());
            if (documentation != null) {
                return documentation;
            }
        } else if (token.getTokenType() == XML_DATA_CHARACTERS) {
            String text = token.getText().trim();
            String documentation = getResourceDocumentation(originalElement, text);
            if (documentation != null) {
                return documentation;
            }
        }
    }
    if (element instanceof PomTargetPsiElement && originalElement != null) {
        final PomTarget target = ((PomTargetPsiElement) element).getTarget();
        if (target instanceof DomAttributeChildDescription) {
            synchronized (ANDROID_ATTRIBUTE_DOCUMENTATION_CACHE_KEY) {
                return generateDocForXmlAttribute((DomAttributeChildDescription) target, originalElement);
            }
        }
    }
    if (element instanceof MyDocElement) {
        return ((MyDocElement) element).myDocumentation;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DomAttributeChildDescription(com.intellij.util.xml.reflect.DomAttributeChildDescription) XmlToken(com.intellij.psi.xml.XmlToken) PomTarget(com.intellij.pom.PomTarget) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) ValueResourceInfo(org.jetbrains.android.resourceManagers.ValueResourceInfo)

Example 14 with XmlToken

use of com.intellij.psi.xml.XmlToken in project android by JetBrains.

the class AndroidXmlDocumentationProvider method getResourceDocumentation.

@Nullable
private static String getResourceDocumentation(PsiElement element, String value) {
    ResourceUrl url = ResourceUrl.parse(value);
    if (url != null) {
        return generateDoc(element, url);
    } else {
        XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
        XmlTag tag = null;
        // True if getting the documentation of the XML value (not the value of the name attribute)
        boolean isXmlValue = false;
        // get the XmlAttribute using the containing tag
        if (element instanceof XmlToken && XML_DATA_CHARACTERS.equals(((XmlToken) element).getTokenType())) {
            tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
            attribute = tag == null ? null : tag.getAttribute(ATTR_NAME);
            isXmlValue = true;
        }
        if (attribute == null) {
            return null;
        }
        if (ATTR_NAME.equals(attribute.getName())) {
            tag = tag != null ? tag : attribute.getParent();
            XmlTag parentTag = tag.getParentTag();
            if (parentTag == null) {
                return null;
            }
            if (TAG_RESOURCES.equals(parentTag.getName())) {
                // Handle ID definitions, http://developer.android.com/guide/topics/resources/more-resources.html#Id
                String typeName = tag.getName();
                if (TAG_ITEM.equals(typeName)) {
                    typeName = tag.getAttributeValue(ATTR_TYPE);
                }
                ResourceType type = ResourceType.getEnum(typeName);
                if (type != null) {
                    return generateDoc(element, type, value, false);
                }
            } else if (TAG_STYLE.equals(parentTag.getName())) {
                // String used to get attribute definitions
                String targetValue = value;
                if (isXmlValue && attribute.getValue() != null) {
                    // In this case, the target is the name attribute of the <item> tag, which contains the key of the attr enum
                    targetValue = attribute.getValue();
                }
                if (targetValue.startsWith(ANDROID_NS_NAME_PREFIX)) {
                    targetValue = targetValue.substring(ANDROID_NS_NAME_PREFIX.length());
                }
                // Handle style item definitions, http://developer.android.com/guide/topics/resources/style-resource.html
                AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, targetValue);
                if (attributeDefinition == null) {
                    return null;
                }
                // Return the doc of the value if searching for an enum value, otherwise return the doc of the enum itself
                return StringUtil.trim(isXmlValue ? attributeDefinition.getValueDoc(value) : attributeDefinition.getDocValue(null));
            }
        }
        // Display documentation for enum values defined in attrs.xml file, if it's present
        if (ANDROID_URI.equals(attribute.getNamespace())) {
            AttributeDefinition attributeDefinition = getAttributeDefinitionForElement(element, attribute.getLocalName());
            if (attributeDefinition == null) {
                return null;
            }
            return StringUtil.trim(attributeDefinition.getValueDoc(value));
        }
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) ResourceUrl(com.android.ide.common.resources.ResourceUrl) XmlTag(com.intellij.psi.xml.XmlTag) XmlToken(com.intellij.psi.xml.XmlToken) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with XmlToken

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

the class ExtensionPointLocator method isRegisteredExtension.

public static boolean isRegisteredExtension(@NotNull PsiClass psiClass) {
    String name = psiClass.getQualifiedName();
    if (name == null)
        return false;
    Project project = psiClass.getProject();
    GlobalSearchScope scope = getCandidatesScope(project);
    return !PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, new PsiNonJavaFileReferenceProcessor() {

        @Override
        public boolean process(PsiFile file, int startOffset, int endOffset) {
            PsiElement at = file.findElementAt(startOffset);
            String tokenText = at instanceof XmlToken ? at.getText() : null;
            if (!StringUtil.equals(name, tokenText))
                return true;
            XmlTag tag = PsiTreeUtil.getParentOfType(at, XmlTag.class);
            if (tag == null)
                return true;
            DomElement dom = DomUtil.getDomElement(tag);
            return !(dom instanceof Extension && ((Extension) dom).getExtensionPoint() != null);
        }
    }, scope);
}
Also used : Extension(org.jetbrains.idea.devkit.dom.Extension) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlToken (com.intellij.psi.xml.XmlToken)29 PsiElement (com.intellij.psi.PsiElement)18 XmlTag (com.intellij.psi.xml.XmlTag)12 TextRange (com.intellij.openapi.util.TextRange)6 IElementType (com.intellij.psi.tree.IElementType)6 Nullable (org.jetbrains.annotations.Nullable)5 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiErrorElement (com.intellij.psi.PsiErrorElement)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 ArrayList (java.util.ArrayList)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 ResourceType (com.android.resources.ResourceType)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 HtmlTag (com.intellij.psi.html.HtmlTag)2