use of com.intellij.xml.XmlExtension in project intellij-community by JetBrains.
the class XmlUnboundNsPrefixInspection method checkUnboundNamespacePrefix.
private static void checkUnboundNamespacePrefix(final XmlElement element, final XmlTag context, String namespacePrefix, final XmlToken token, final ProblemsHolder holder, boolean isOnTheFly) {
if (namespacePrefix.isEmpty() && (!(element instanceof XmlTag) || !(element.getParent() instanceof XmlDocument)) || XML.equals(namespacePrefix)) {
return;
}
final String namespaceByPrefix = context.getNamespaceByPrefix(namespacePrefix);
if (!namespaceByPrefix.isEmpty()) {
return;
}
PsiFile psiFile = context.getContainingFile();
if (!(psiFile instanceof XmlFile))
return;
final XmlFile containingFile = (XmlFile) psiFile;
if (!HighlightingLevelManager.getInstance(containingFile.getProject()).shouldInspect(containingFile))
return;
final XmlExtension extension = XmlExtension.getExtension(containingFile);
if (extension.getPrefixDeclaration(context, namespacePrefix) != null) {
return;
}
final String localizedMessage = isOnTheFly ? XmlErrorMessages.message("unbound.namespace", namespacePrefix) : XmlErrorMessages.message("unbound.namespace.no.param");
if (namespacePrefix.isEmpty()) {
final XmlTag tag = (XmlTag) element;
if (!XmlUtil.JSP_URI.equals(tag.getNamespace())) {
LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
reportTagProblem(tag, localizedMessage, null, ProblemHighlightType.INFORMATION, fix, holder);
}
return;
}
final int prefixLength = namespacePrefix.length();
final TextRange range = new TextRange(0, prefixLength);
final HighlightInfoType infoType = extension.getHighlightInfoType(containingFile);
final ProblemHighlightType highlightType = infoType == HighlightInfoType.ERROR ? ProblemHighlightType.ERROR : ProblemHighlightType.LIKE_UNKNOWN_SYMBOL;
if (element instanceof XmlTag) {
LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(context, namespacePrefix, token) : null;
reportTagProblem(element, localizedMessage, range, highlightType, fix, holder);
} else if (element instanceof XmlAttribute) {
LocalQuickFix fix = isOnTheFly ? XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(element, namespacePrefix, token) : null;
XmlAttribute attribute = (XmlAttribute) element;
holder.registerProblem(attribute.getNameElement(), localizedMessage, highlightType, range, fix);
} else {
holder.registerProblem(element, localizedMessage, highlightType, range);
}
}
use of com.intellij.xml.XmlExtension in project intellij-community by JetBrains.
the class IdReferenceProvider method getReferencesByElement.
@Override
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
if (element instanceof XmlAttributeValue) {
final XmlExtension extension = XmlExtension.getExtensionByElement(element);
if (extension != null && extension.hasDynamicComponents(element)) {
return PsiReference.EMPTY_ARRAY;
}
final PsiElement parentElement = element.getParent();
if (!(parentElement instanceof XmlAttribute))
return PsiReference.EMPTY_ARRAY;
final String name = ((XmlAttribute) parentElement).getName();
final String ns = ((XmlAttribute) parentElement).getParent().getNamespace();
final boolean jsfNs = Arrays.asList(XmlUtil.JSF_CORE_URIS).contains(ns) || Arrays.asList(XmlUtil.JSF_HTML_URIS).contains(ns);
if (FOR_ATTR_NAME.equals(name)) {
return new PsiReference[] { jsfNs && element.getText().indexOf(':') == -1 ? new IdRefReference(element) : new IdRefReference(element) {
@Override
public boolean isSoft() {
final XmlAttributeDescriptor descriptor = ((XmlAttribute) parentElement).getDescriptor();
return descriptor != null && !descriptor.hasIdRefType();
}
} };
} else {
final boolean allowReferences = !ourNamespacesWithoutNameReference.contains(ns);
if (ID_ATTR_NAME.equals(name) && allowReferences || STYLE_ID_ATTR_NAME.equals(name) || NAME_ATTR_NAME.equals(name) && allowReferences) {
final AttributeValueSelfReference attributeValueSelfReference;
if (jsfNs) {
attributeValueSelfReference = new AttributeValueSelfReference(element);
} else {
if (hasOuterLanguageElement(element))
return PsiReference.EMPTY_ARRAY;
attributeValueSelfReference = new GlobalAttributeValueSelfReference(element, true);
}
return new PsiReference[] { attributeValueSelfReference };
}
}
}
return PsiReference.EMPTY_ARRAY;
}
use of com.intellij.xml.XmlExtension in project intellij-community by JetBrains.
the class TagNameVariantCollector method getTagDescriptors.
public static List<XmlElementDescriptor> getTagDescriptors(final XmlTag element, final Collection<String> namespaces, @Nullable List<String> nsInfo) {
XmlElementDescriptor elementDescriptor = null;
String elementNamespace = element.getNamespacePrefix().isEmpty() ? null : element.getNamespace();
final Map<String, XmlElementDescriptor> descriptorsMap = new HashMap<>();
PsiElement context = element.getParent();
PsiElement curElement = element.getParent();
while (curElement instanceof XmlTag) {
final XmlTag declarationTag = (XmlTag) curElement;
final String namespace = declarationTag.getNamespace();
if (!descriptorsMap.containsKey(namespace)) {
final XmlElementDescriptor descriptor = declarationTag.getDescriptor();
if (descriptor != null) {
descriptorsMap.put(namespace, descriptor);
if (elementDescriptor == null) {
elementDescriptor = descriptor;
if (elementNamespace == null) {
elementNamespace = namespace;
}
}
}
}
curElement = curElement.getContext();
}
final Set<XmlNSDescriptor> visited = new HashSet<>();
final XmlExtension extension = XmlExtension.getExtension(element.getContainingFile());
final ArrayList<XmlElementDescriptor> variants = new ArrayList<>();
for (final String namespace : namespaces) {
final int initialSize = variants.size();
processVariantsInNamespace(namespace, element, variants, elementDescriptor, elementNamespace, descriptorsMap, visited, context instanceof XmlTag ? (XmlTag) context : element, extension);
if (nsInfo != null) {
for (int i = initialSize; i < variants.size(); i++) {
XmlElementDescriptor descriptor = variants.get(i);
nsInfo.add(descriptor instanceof XmlElementDescriptorImpl && !(descriptor instanceof RelaxedHtmlFromSchemaElementDescriptor) ? ((XmlElementDescriptorImpl) descriptor).getNamespaceByContext(element) : namespace);
}
}
}
final boolean hasPrefix = StringUtil.isNotEmpty(element.getNamespacePrefix());
return ContainerUtil.filter(variants, descriptor -> {
if (descriptor instanceof AnyXmlElementDescriptor) {
return false;
} else if (hasPrefix && descriptor instanceof XmlElementDescriptorImpl && !namespaces.contains(((XmlElementDescriptorImpl) descriptor).getNamespace())) {
return false;
}
return true;
});
}
use of com.intellij.xml.XmlExtension in project intellij-community by JetBrains.
the class XmlTagImpl method knownNamespaces.
@Override
public String[] knownNamespaces() {
final PsiElement parentElement = getParent();
BidirectionalMap<String, String> map = getNamespaceMap();
Set<String> known = Collections.emptySet();
if (map != null) {
known = new HashSet<>(map.values());
}
if (parentElement instanceof XmlTag) {
if (known.isEmpty())
return ((XmlTag) parentElement).knownNamespaces();
ContainerUtil.addAll(known, ((XmlTag) parentElement).knownNamespaces());
} else {
XmlExtension xmlExtension = XmlExtension.getExtensionByElement(this);
if (xmlExtension != null) {
final XmlFile xmlFile = xmlExtension.getContainingFile(this);
if (xmlFile != null) {
final XmlTag rootTag = xmlFile.getRootTag();
if (rootTag != null && rootTag != this) {
if (known.isEmpty())
return rootTag.knownNamespaces();
ContainerUtil.addAll(known, rootTag.knownNamespaces());
}
}
}
}
return ArrayUtil.toStringArray(known);
}
use of com.intellij.xml.XmlExtension in project intellij-community by JetBrains.
the class ExtendedTagInsertHandler method handleInsert.
@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
final XmlFile contextFile = (XmlFile) context.getFile();
final XmlExtension extension = XmlExtension.getExtension(contextFile);
final XmlFile file = extension.getContainingFile(contextFile);
final Project project = context.getProject();
assert file != null;
final PsiElement psiElement = file.findElementAt(context.getStartOffset());
assert psiElement != null;
if (isNamespaceBound(psiElement)) {
doDefault(context, item);
return;
}
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(document);
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {
@Override
public void run(final String namespacePrefix) {
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement element = file.findElementAt(context.getStartOffset());
if (element != null) {
qualifyWithPrefix(namespacePrefix, element, document);
PsiDocumentManager.getInstance(project).commitDocument(document);
}
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
doDefault(context, item);
}
};
try {
final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
} else {
// qualify && complete
runAfter.run(prefixByNamespace);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
Aggregations