Search in sources :

Example 41 with XmlAttributeValue

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

the class AndroidColorAnnotator method annotateXml.

private static void annotateXml(PsiElement element, AnnotationHolder holder, String value) {
    if (value.startsWith("#")) {
        final PsiFile file = element.getContainingFile();
        if (file != null && AndroidResourceUtil.isInResourceSubdirectory(file, null)) {
            if (element instanceof XmlTag) {
                Annotation annotation = holder.createInfoAnnotation(element, null);
                annotation.setGutterIconRenderer(new MyRenderer(element, null));
            } else {
                assert element instanceof XmlAttributeValue;
                Color color = ResourceHelper.parseColor(value);
                if (color != null) {
                    Annotation annotation = holder.createInfoAnnotation(element, null);
                    annotation.setGutterIconRenderer(new MyRenderer(element, null));
                }
            }
        }
    } else if (value.startsWith(COLOR_RESOURCE_PREFIX)) {
        annotateResourceReference(ResourceType.COLOR, holder, element, value.substring(COLOR_RESOURCE_PREFIX.length()), false);
    } else if (value.startsWith(ANDROID_COLOR_RESOURCE_PREFIX)) {
        annotateResourceReference(ResourceType.COLOR, holder, element, value.substring(ANDROID_COLOR_RESOURCE_PREFIX.length()), true);
    } else if (value.startsWith(DRAWABLE_PREFIX)) {
        annotateResourceReference(ResourceType.DRAWABLE, holder, element, value.substring(DRAWABLE_PREFIX.length()), false);
    } else if (value.startsWith(ANDROID_DRAWABLE_PREFIX)) {
        annotateResourceReference(ResourceType.DRAWABLE, holder, element, value.substring(ANDROID_DRAWABLE_PREFIX.length()), true);
    } else if (value.startsWith(MIPMAP_PREFIX)) {
        annotateResourceReference(ResourceType.MIPMAP, holder, element, value.substring(MIPMAP_PREFIX.length()), false);
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Annotation(com.intellij.lang.annotation.Annotation) XmlTag(com.intellij.psi.xml.XmlTag)

Example 42 with XmlAttributeValue

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

the class IssueIdDocumentationProvider method generateDoc.

@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    // Check whether the element is attribute value
    if (!(element instanceof XmlAttributeValue)) {
        return null;
    }
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return null;
    }
    // Check whether the current XML file is lint.xml file
    final DomFileElement<LintDomElement> fileElement = DomManager.getDomManager(element.getProject()).getFileElement((XmlFile) file, LintDomElement.class);
    if (fileElement == null) {
        return null;
    }
    final Issue issue = IssueIdConverter.getIdSet().get(((XmlAttributeValue) element).getValue());
    if (issue == null) {
        return null;
    }
    return issue.getExplanation(TextFormat.HTML);
}
Also used : Issue(com.android.tools.lint.detector.api.Issue) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with XmlAttributeValue

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

the class AndroidInlineUtil method getInlinableStyleDataFromContext.

@Nullable
static MyStyleData getInlinableStyleDataFromContext(@Nullable PsiElement context) {
    if (context instanceof LazyValueResourceElementWrapper) {
        context = ((LazyValueResourceElementWrapper) context).computeElement();
    }
    if (context == null || !context.getManager().isInProject(context)) {
        return null;
    }
    final XmlAttributeValue attrValue = PsiTreeUtil.getParentOfType(context, XmlAttributeValue.class, false);
    final XmlTag tag = attrValue != null ? PsiTreeUtil.getParentOfType(attrValue, XmlTag.class) : null;
    if (tag == null) {
        return null;
    }
    final MyStyleData data = getInlinableStyleData(tag);
    return data != null && PsiEquivalenceUtil.areElementsEquivalent(data.myReferredElement, attrValue) ? data : null;
}
Also used : LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with XmlAttributeValue

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

the class ResourceManager method findIdDeclarations.

// searches only declarations such as "@+id/..."
@NotNull
public List<XmlAttributeValue> findIdDeclarations(@NotNull final String id) {
    if (!isResourcePublic(ResourceType.ID.getName(), id)) {
        return Collections.emptyList();
    }
    final List<XmlAttributeValue> declarations = new ArrayList<XmlAttributeValue>();
    final Collection<VirtualFile> files = FileBasedIndex.getInstance().getContainingFiles(AndroidIdIndex.INDEX_ID, "+" + id, GlobalSearchScope.allScope(myProject));
    final Set<VirtualFile> fileSet = new HashSet<VirtualFile>(files);
    final PsiManager psiManager = PsiManager.getInstance(myProject);
    for (VirtualFile subdir : getResourceSubdirsToSearchIds()) {
        for (VirtualFile file : subdir.getChildren()) {
            if (fileSet.contains(file)) {
                final PsiFile psiFile = psiManager.findFile(file);
                if (psiFile instanceof XmlFile) {
                    psiFile.accept(new XmlRecursiveElementVisitor() {

                        @Override
                        public void visitXmlAttributeValue(XmlAttributeValue attributeValue) {
                            if (AndroidResourceUtil.isIdDeclaration(attributeValue)) {
                                final String idInAttr = AndroidResourceUtil.getResourceNameByReferenceText(attributeValue.getValue());
                                if (id.equals(idInAttr)) {
                                    declarations.add(attributeValue);
                                }
                            }
                        }
                    });
                }
            }
        }
    }
    return declarations;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with XmlAttributeValue

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

the class JavaFindUsagesHelper method getElementNames.

@NotNull
public static Set<String> getElementNames(@NotNull final PsiElement element) {
    if (element instanceof PsiDirectory) {
        // normalize a directory to a corresponding package
        PsiPackage aPackage = ReadAction.compute(() -> JavaDirectoryService.getInstance().getPackage((PsiDirectory) element));
        return aPackage == null ? Collections.emptySet() : getElementNames(aPackage);
    }
    final Set<String> result = new HashSet<>();
    ApplicationManager.getApplication().runReadAction(() -> {
        if (element instanceof PsiPackage) {
            ContainerUtil.addIfNotNull(result, ((PsiPackage) element).getQualifiedName());
        } else if (element instanceof PsiClass) {
            final String qname = ((PsiClass) element).getQualifiedName();
            if (qname != null) {
                result.add(qname);
                PsiClass topLevelClass = PsiUtil.getTopLevelClass(element);
                if (topLevelClass != null && !(topLevelClass instanceof PsiSyntheticClass)) {
                    String topName = topLevelClass.getQualifiedName();
                    assert topName != null : "topLevelClass : " + topLevelClass + "; element: " + element + " (" + qname + ") top level file: " + InjectedLanguageManager.getInstance(element.getProject()).getTopLevelFile(element);
                    if (qname.length() > topName.length()) {
                        result.add(topName + qname.substring(topName.length()).replace('.', '$'));
                    }
                }
            }
        } else if (element instanceof PsiMethod) {
            ContainerUtil.addIfNotNull(result, ((PsiMethod) element).getName());
        } else if (element instanceof PsiVariable) {
            ContainerUtil.addIfNotNull(result, ((PsiVariable) element).getName());
        } else if (element instanceof PsiMetaOwner) {
            final PsiMetaData metaData = ((PsiMetaOwner) element).getMetaData();
            if (metaData != null) {
                ContainerUtil.addIfNotNull(result, metaData.getName());
            }
        } else if (element instanceof PsiNamedElement) {
            ContainerUtil.addIfNotNull(result, ((PsiNamedElement) element).getName());
        } else if (element instanceof XmlAttributeValue) {
            ContainerUtil.addIfNotNull(result, ((XmlAttributeValue) element).getValue());
        } else {
            LOG.error("Unknown element type: " + element);
        }
    });
    return result;
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiMetaData(com.intellij.psi.meta.PsiMetaData) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5