@Nullable public static String findUri(PsiFile file, int offset) { PsiElement element = file.findElementAt(offset); if (element == null || element instanceof PsiWhiteSpace ) { return null; } PsiReference currentRef = file.getViewProvider().findReferenceAt(offset, file.getLanguage()); if (currentRef == null) currentRef = file.getViewProvider().findReferenceAt(offset); if (currentRef instanceof URLReference || currentRef instanceof DependentNSReference) { return currentRef.getCanonicalText(); } return null; }
protected PsiReference createUrlReference(final PsiElement dtdUrlElement) { return new URLReference(XmlDoctypeImpl.this) { @NotNull public Object[] getVariants() { return findChildByRoleAsPsiElement(XmlChildRole.XML_DOCTYPE_PUBLIC) != null ? super.getVariants(): EMPTY_ARRAY; } @NotNull public String getCanonicalText() { return extractValue(dtdUrlElement); } public TextRange getRangeInElement() { return TextRange.from(dtdUrlElement.getTextRange().getStartOffset() - getTextRange().getStartOffset() + 1, Math.max(dtdUrlElement.getTextRange().getLength() - 2, 0)); } }; }
protected PsiReference createUrlReference(final PsiElement dtdUrlElement) { return new URLReference(XmlDoctypeImpl.this) { @Override @NotNull public String getCanonicalText() { return extractValue(dtdUrlElement); } @Override public TextRange getRangeInElement() { return TextRange.from(dtdUrlElement.getTextRange().getStartOffset() - getTextRange().getStartOffset() + 1, Math.max(dtdUrlElement.getTextRange().getLength() - 2, 0)); } }; }
private static void checkUnusedLocations(XmlAttribute attribute, ProblemsHolder holder, @NotNull XmlRefCountHolder refCountHolder) { if (XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(attribute.getNamespace())) { if (XmlUtil.NO_NAMESPACE_SCHEMA_LOCATION_ATT.equals(attribute.getLocalName())) { if (refCountHolder.isInUse("")) return; holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceLocationFix("")); } else if (XmlUtil.SCHEMA_LOCATION_ATT.equals(attribute.getLocalName())) { XmlAttributeValue value = attribute.getValueElement(); if (value == null) return; PsiReference[] references = value.getReferences(); for (int i = 0, referencesLength = references.length; i < referencesLength; i++) { PsiReference reference = references[i]; if (reference instanceof URLReference) { String ns = getNamespaceFromReference(reference); if (ArrayUtil.indexOf(attribute.getParent().knownNamespaces(), ns) == -1 && !refCountHolder.isUsedNamespace(ns)) { if (!XmlHighlightVisitor.hasBadResolve(reference, false)) { holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceLocationFix(ns)); } for (int j = i + 1; j < referencesLength; j++) { PsiReference nextRef = references[j]; if (nextRef instanceof URLReference) break; if (!XmlHighlightVisitor.hasBadResolve(nextRef, false)) { holder.registerProblemForReference(nextRef, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceLocationFix(ns)); } } } } } } } }
public static PsiReference[] getUrlReferencesForAttributeValue(final XmlAttributeValue element) { return getReferencesForAttributeValue(element, new PairFunction<String, Integer, PsiReference>() { @Nullable @Override public PsiReference fun(String token, Integer offset) { if (HtmlUtil.hasHtmlPrefix(token)) { final TextRange range = TextRange.from(offset, token.length()); final URLReference urlReference = new URLReference(element, range, true); return new DependentNSReference(element, range, urlReference, true); } return null; } }); }
private static void checkUnusedLocations(XmlAttribute attribute, ProblemsHolder holder) { if (XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(attribute.getNamespace())) { XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder(attribute); if (refCountHolder == null) return; if (XmlUtil.NO_NAMESPACE_SCHEMA_LOCATION_ATT.equals(attribute.getLocalName())) { if (refCountHolder.isInUse("")) return; holder.registerProblem(attribute, NAMESPACE_LOCATION_IS_NEVER_USED, ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceLocationFix("")); } else if (XmlUtil.SCHEMA_LOCATION_ATT.equals(attribute.getLocalName())) { XmlAttributeValue value = attribute.getValueElement(); if (value == null) return; PsiReference[] references = value.getReferences(); for (int i = 0, referencesLength = references.length; i < referencesLength; i++) { PsiReference reference = references[i]; if (reference instanceof URLReference) { String ns = getNamespaceFromReference(reference); if (ArrayUtil.indexOf(attribute.getParent().knownNamespaces(), ns) == -1 && !refCountHolder.isUsedNamespace(ns)) { if (!XmlHighlightVisitor.hasBadResolve(reference, false)) { holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, NAMESPACE_LOCATION_IS_NEVER_USED, new RemoveNamespaceLocationFix(ns)); } for (int j = i + 1; j < referencesLength; j++) { PsiReference nextRef = references[j]; if (nextRef instanceof URLReference) break; if (!XmlHighlightVisitor.hasBadResolve(nextRef, false)) { holder.registerProblemForReference(nextRef, ProblemHighlightType.LIKE_UNUSED_SYMBOL, NAMESPACE_LOCATION_IS_NEVER_USED, new RemoveNamespaceLocationFix(ns)); } } } } } } } }
@Nullable public static String findUri(PsiFile file, int offset) { PsiReference currentRef = file.getViewProvider().findReferenceAt(offset, file.getLanguage()); if (currentRef == null) currentRef = file.getViewProvider().findReferenceAt(offset); if (currentRef instanceof URLReference || currentRef instanceof DependentNSReference) { return currentRef.getCanonicalText(); } return null; }
public static PsiReference[] getUrlReferencesForAttributeValue(final XmlAttributeValue element) { return getReferencesForAttributeValue(element, (token, offset) -> { if(HtmlUtil.hasHtmlPrefix(token)) { final TextRange range = TextRange.from(offset, token.length()); final URLReference urlReference = new URLReference(element, range, true); return new DependentNSReference(element, range, urlReference, true); } return null; }); }