private static Map<String, XmlTag> findScopesWithItemRef(@Nullable PsiFile file) { if (!(file instanceof XmlFile)) return Collections.emptyMap(); final Map<String, XmlTag> result = new THashMap<String, XmlTag>(); file.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(final XmlTag tag) { super.visitXmlTag(tag); XmlAttribute refAttr = tag.getAttribute(ITEM_REF); if (refAttr != null && tag.getAttribute(ITEM_SCOPE) != null) { getReferencesForAttributeValue(refAttr.getValueElement(), new PairFunction<String, Integer, PsiReference>() { @Nullable @Override public PsiReference fun(String t, Integer v) { result.put(t, tag); return null; } }); } } }); return result; }
public static void processTemplates(final PsiFile psiFile, final Processor<XmlAttribute> processor) { if (psiFile instanceof XmlFile) { psiFile.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { // TODO: Fix to correct handlebars script type if (HtmlUtil.isScriptTag(tag) && "text/x-handlebars".equals(tag.getAttributeValue("type"))) { final XmlAttribute id = tag.getAttribute("id"); if (id != null) { processor.process(id); return; } } super.visitXmlTag(tag); } }); } }
@Override public void checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull final GlobalInspectionContext globalContext, @NotNull final ProblemDescriptionsProcessor problemDescriptionsProcessor) { HighlightInfoHolder myHolder = new HighlightInfoHolder(file) { @Override public boolean add(@Nullable HighlightInfo info) { if (info != null) { GlobalInspectionUtil.createProblem( file, info, new TextRange(info.startOffset, info.endOffset), null, manager, problemDescriptionsProcessor, globalContext ); } return true; } }; final XmlHighlightVisitor highlightVisitor = new XmlHighlightVisitor(); highlightVisitor.analyze(file, true, myHolder, new Runnable() { @Override public void run() { file.accept(new XmlRecursiveElementVisitor() { @Override public void visitElement(PsiElement element) { highlightVisitor.visit(element); super.visitElement(element); } }); } }); }
private static void collectProcessNames(XmlElement xmlElement, final Set<String> result) { xmlElement.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlAttribute(XmlAttribute attribute) { if ("process".equals(attribute.getLocalName())) { final String value = attribute.getValue(); if (value != null) { result.add(value.toLowerCase()); } } } }); }
@Override public XmlAttributeValue computeXmlElement() { final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(myFile); if (!(psiFile instanceof XmlFile)) { return null; } final Ref<XmlAttributeValue> result = Ref.create(); psiFile.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlAttributeValue(XmlAttributeValue attributeValue) { if (!result.isNull()) { return; } if (AndroidResourceUtil.isIdDeclaration(attributeValue)) { final String idInAttr = AndroidResourceUtil.getResourceNameByReferenceText(attributeValue.getValue()); if (myName.equals(idInAttr)) { result.set(attributeValue); } } } }); return result.get(); }
public void collectPossibleStyleApplications(PsiFile layoutFile, final List<UsageInfo> usages) { layoutFile.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { super.visitXmlTag(tag); if (isPossibleApplicationOfStyle(tag)) { usages.add(new UsageInfo(tag)); } } }); }
@Override public void checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull final GlobalInspectionContext globalContext, @NotNull final ProblemDescriptionsProcessor problemDescriptionsProcessor) { HighlightInfoHolder myHolder = new HighlightInfoHolder(file) { @Override public boolean add(@Nullable HighlightInfo info) { if(info != null) { GlobalInspectionUtil.createProblem(file, info, new TextRange(info.startOffset, info.endOffset), null, manager, problemDescriptionsProcessor, globalContext); } return true; } }; final XmlHighlightVisitor highlightVisitor = new XmlHighlightVisitor(); highlightVisitor.analyze(file, true, myHolder, new Runnable() { @Override public void run() { file.accept(new XmlRecursiveElementVisitor() { @Override public void visitElement(PsiElement element) { highlightVisitor.visit(element); super.visitElement(element); } }); } }); }
private static Map<String, XmlTag> findScopesWithItemRef(@Nullable final PsiFile file) { if(!(file instanceof XmlFile)) { return Collections.emptyMap(); } return CachedValuesManager.getCachedValue(file, new CachedValueProvider<Map<String, XmlTag>>() { @Nullable @Override public Result<Map<String, XmlTag>> compute() { final Map<String, XmlTag> result = new THashMap<>(); file.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(final XmlTag tag) { super.visitXmlTag(tag); XmlAttribute refAttr = tag.getAttribute(ITEM_REF); if(refAttr != null && tag.getAttribute(ITEM_SCOPE) != null) { getReferencesForAttributeValue(refAttr.getValueElement(), (t, v) -> { result.put(t, tag); return null; }); } } }); return Result.create(result, file); } }); }
@Override protected CachedValue<Map<String, String>> compute(final XmlFile file, final Object p) { return CachedValuesManager.getManager(file.getProject()).createCachedValue(new CachedValueProvider<Map<String, String>>() { @Override public Result<Map<String, String>> compute() { final Map<String, String> cachedComponentImports = new THashMap<String, String>(); final List<PsiFile> dependencies = new ArrayList<PsiFile>(); dependencies.add(file); file.acceptChildren(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { XmlElementDescriptor descriptor = tag.getDescriptor(); if(descriptor != null) { PsiElement declaration = descriptor.getDeclaration(); if(declaration instanceof XmlFile) { declaration = XmlBackedJSClassImpl.getXmlBackedClass((XmlFile) declaration); } if(declaration instanceof JSClass) { JSClass jsClass = (JSClass) declaration; cachedComponentImports.put(jsClass.getName(), jsClass.getQualifiedName()); dependencies.add(declaration.getContainingFile()); } } super.visitXmlTag(tag); } }); return new Result<Map<String, String>>(cachedComponentImports, dependencies.toArray()); } }, false); }
@Override @NotNull public Map<ResourceEntry, Set<MyResourceInfo>> map(@NotNull FileContent inputData) { if (!isSimilarFile(inputData)) { return Collections.emptyMap(); } final PsiFile file = inputData.getPsiFile(); if (!(file instanceof XmlFile)) { return Collections.emptyMap(); } final Map<ResourceEntry, Set<MyResourceInfo>> result = new HashMap<ResourceEntry, Set<MyResourceInfo>>(); file.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(XmlTag tag) { super.visitXmlTag(tag); final String resName = tag.getAttributeValue(NAME_ATTRIBUTE_VALUE); if (resName == null) { return; } final String tagName = tag.getName(); final String resTypeStr; if ("item".equals(tagName)) { resTypeStr = tag.getAttributeValue(TYPE_ATTRIBUTE_VALUE); } else { resTypeStr = AndroidCommonUtils.getResourceTypeByTagName(tagName); } final ResourceType resType = resTypeStr != null ? ResourceType.getEnum(resTypeStr) : null; if (resType == null) { return; } final int offset = tag.getTextRange().getStartOffset(); if (resType == ResourceType.ATTR) { final XmlTag parentTag = tag.getParentTag(); final String contextName = parentTag != null ? parentTag.getAttributeValue(NAME_ATTRIBUTE_VALUE) : null; processResourceEntry(new ResourceEntry(resTypeStr, resName, contextName != null ? contextName : ""), result, offset); } else { processResourceEntry(new ResourceEntry(resTypeStr, resName, ""), result, offset); } } }); return result; }
/** * Obtain all IDs from layout * * @param file * @return */ public static ArrayList<Element> getIDsFromLayout(final PsiFile file, final ArrayList<Element> elements) { file.accept(new XmlRecursiveElementVisitor() { @Override public void visitElement(final PsiElement element) { super.visitElement(element); if (element instanceof XmlTag) { XmlTag tag = (XmlTag) element; if (tag.getName().equalsIgnoreCase("include")) { XmlAttribute layout = tag.getAttribute("layout", null); if (layout != null) { Project project = file.getProject(); PsiFile include = findLayoutResource(project, getLayoutName(layout.getValue())); if (include != null) { getIDsFromLayout(include, elements); return; } } } // get element ID XmlAttribute id = tag.getAttribute("android:id", null); if (id == null) { return; // missing android:id attribute } String value = id.getValue(); if (value == null) { return; // empty value } // check if there is defined custom class String name = tag.getName(); XmlAttribute clazz = tag.getAttribute("class", null); if (clazz != null) { name = clazz.getValue(); } elements.add(new Element(name, value)); } } }); return elements; }
@Override @NotNull public Map<File, SingleLayout> map(@NotNull final FileContent inputData) { Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() { @Override public Module compute() { return ModuleUtilCore.findModuleForFile(inputData.getFile(), inputData.getProject()); } }); if (module == null) { return Collections.emptyMap(); } final HoldrModel holdrModel = HoldrModel.getInstance(module); if (holdrModel == null) { return Collections.emptyMap(); } // For some bizarre reason, just inputData.getPsiFile() will sometimes not allow you to get the parent directory. final PsiFile file = inputData.getPsiFile().getManager().findFile(inputData.getFile()); if (file == null) { return Collections.emptyMap(); } if (!AndroidResourceUtil.isInResourceSubdirectory(file, "layout")) { return Collections.emptyMap(); } if (!(file instanceof XmlFile)) { return Collections.emptyMap(); } final File path = new File(inputData.getFile().getPath()); final Map<File, SingleLayout> result = new HashMap<File, SingleLayout>(); final ParserUtils.IncludeIgnoreState state = new ParserUtils.IncludeIgnoreState(); final SingleLayout.Builder layoutBuilder = SingleLayout.of(path); final TagParser tagParser = new TagParser(); file.accept(new XmlRecursiveElementVisitor() { @Override public void visitXmlTag(final XmlTag tag) { tagParser.tag = tag; ParserUtils.parseTag(holdrModel.getConfig(), layoutBuilder, state, tagParser); super.visitXmlTag(tag); state.tagEnd(tag.getName()); } }); SingleLayout layout = layoutBuilder.build(); result.put(path, layout); return result; }