use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class HtmlUnknownTagInspectionBase method checkTag.
@Override
protected void checkTag(@NotNull final XmlTag tag, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
return;
}
XmlElementDescriptor descriptorFromContext = XmlUtil.getDescriptorFromContext(tag);
PsiElement parent = tag.getParent();
XmlElementDescriptor parentDescriptor = parent instanceof XmlTag ? ((XmlTag) parent).getDescriptor() : null;
XmlElementDescriptor ownDescriptor = isAbstractDescriptor(descriptorFromContext) ? tag.getDescriptor() : descriptorFromContext;
if (isAbstractDescriptor(ownDescriptor) || (parentDescriptor instanceof HtmlElementDescriptorImpl && ownDescriptor instanceof HtmlElementDescriptorImpl && isAbstractDescriptor(descriptorFromContext))) {
final String name = tag.getName();
if (!isCustomValuesEnabled() || !isCustomValue(name)) {
final AddCustomHtmlElementIntentionAction action = new AddCustomHtmlElementIntentionAction(TAG_KEY, name, XmlBundle.message("add.custom.html.tag", name));
// todo: support "element is not allowed" message for html5
// some tags in html5 cannot be found in xhtml5.xsd if they are located in incorrect context, so they get any-element descriptor (ex. "canvas: tag)
final String message = isAbstractDescriptor(ownDescriptor) ? XmlErrorMessages.message("unknown.html.tag", name) : XmlErrorMessages.message("element.is.not.allowed.here", name);
final PsiElement startTagName = XmlTagUtil.getStartTagNameElement(tag);
assert startTagName != null;
final PsiElement endTagName = XmlTagUtil.getEndTagNameElement(tag);
List<LocalQuickFix> quickfixes = new ArrayList<>();
quickfixes.add(action);
if (isOnTheFly) {
PsiFile file = startTagName.getContainingFile();
if (file instanceof XmlFile) {
quickfixes.add(XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(startTagName, "", null));
}
// People using non-HTML as their template data language (but having not changed this in the IDE)
// will most likely see 'unknown html tag' error, because HTML is usually the default.
// So if they check quick fixes for this error they'll discover Change Template Data Language feature.
ContainerUtil.addIfNotNull(quickfixes, createChangeTemplateDataFix(file));
}
if (HtmlUtil.isHtml5Tag(name) && !HtmlUtil.hasNonHtml5Doctype(tag)) {
quickfixes.add(new SwitchToHtml5WithHighPriorityAction());
}
ProblemHighlightType highlightType = tag.getContainingFile().getContext() == null ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING : ProblemHighlightType.INFORMATION;
if (startTagName.getTextLength() > 0) {
holder.registerProblem(startTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
}
if (endTagName != null) {
holder.registerProblem(endTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
}
}
}
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlUnusedNamespaceInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
PsiFile file = holder.getFile();
if (!(file instanceof XmlFile))
return;
XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
if (refCountHolder == null)
return;
if (!attribute.isNamespaceDeclaration()) {
checkUnusedLocations(attribute, holder, refCountHolder);
return;
}
String namespace = attribute.getValue();
String declaredPrefix = getDeclaredPrefix(attribute);
if (namespace != null && !refCountHolder.isInUse(declaredPrefix)) {
ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
for (ImplicitUsageProvider provider : implicitUsageProviders) {
if (provider.isImplicitUsage(attribute))
return;
}
XmlAttributeValue value = attribute.getValueElement();
assert value != null;
holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.declaration"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, false, !refCountHolder.isUsedNamespace(namespace)));
XmlTag parent = attribute.getParent();
if (declaredPrefix.isEmpty()) {
XmlAttribute location = getDefaultLocation(parent);
if (location != null) {
holder.registerProblem(location, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
}
} else if (!refCountHolder.isUsedNamespace(namespace)) {
for (PsiReference reference : getLocationReferences(namespace, parent)) {
if (!XmlHighlightVisitor.hasBadResolve(reference, false))
holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
}
}
}
}
};
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlConstraintsTest method getXSModel.
private XSModel getXSModel(String... files) {
myFixture.configureByFiles(files);
XmlFile file = (XmlFile) myFixture.getFile();
ValidateXmlActionHandler handler = new ValidateXmlActionHandler(false) {
@Override
protected SAXParser createParser() throws SAXException, ParserConfigurationException {
SAXParser parser = super.createParser();
parser.getXMLReader().setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE, true);
return parser;
}
};
handler.setErrorReporter(new TestErrorReporter(handler));
handler.doValidate(file);
XMLGrammarPool grammarPool = ValidateXmlActionHandler.getGrammarPool(file);
assert grammarPool != null;
Grammar[] grammars = grammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
XSGrammar grammar = (XSGrammar) grammars[0];
return grammar.toXSModel();
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class XmlConstraintsTest method configure.
private Map<String, XmlElementDescriptor> configure(String... files) {
myFixture.configureByFiles(files);
XmlTag tag = ((XmlFile) getFile()).getRootTag();
assertNotNull(tag);
XmlElementDescriptor descriptor = tag.getDescriptor();
assertNotNull(descriptor);
XmlElementDescriptor[] descriptors = descriptor.getElementsDescriptors(tag);
Map<String, XmlElementDescriptor> map = ContainerUtil.newMapFromValues(Arrays.asList(descriptors).iterator(), new Convertor<XmlElementDescriptor, String>() {
@Override
public String convert(XmlElementDescriptor o) {
return o.getName();
}
});
map.put(tag.getName(), tag.getDescriptor());
return map;
}
use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.
the class HtmlFileReferenceHelper method getContexts.
@NotNull
@Override
public Collection<PsiFileSystemItem> getContexts(Project project, @NotNull VirtualFile vFile) {
final PsiFile file = PsiManager.getInstance(project).findFile(vFile);
final Module module = file != null ? ModuleUtilCore.findModuleForPsiElement(file) : null;
if (module == null || !(file instanceof XmlFile))
return Collections.emptyList();
final String basePath = HtmlUtil.getHrefBase((XmlFile) file);
if (basePath != null && !HtmlUtil.hasHtmlPrefix(basePath)) {
for (VirtualFile virtualFile : getBaseRoots(module)) {
final VirtualFile base = virtualFile.findFileByRelativePath(basePath);
final PsiDirectory result = base != null ? PsiManager.getInstance(project).findDirectory(base) : null;
if (result != null) {
return Collections.<PsiFileSystemItem>singletonList(result);
}
}
}
return Collections.emptyList();
}
Aggregations