use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.
the class HtmlUtil method isHtmlTagContainingFile.
public static boolean isHtmlTagContainingFile(PsiElement element) {
if (element == null) {
return false;
}
final PsiFile containingFile = element.getContainingFile();
if (containingFile != null) {
final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
if (tag instanceof HtmlTag) {
return true;
}
final XmlDocument document = PsiTreeUtil.getParentOfType(element, XmlDocument.class, false);
if (document instanceof HtmlDocumentImpl) {
return true;
}
final FileViewProvider provider = containingFile.getViewProvider();
Language language;
if (provider instanceof TemplateLanguageFileViewProvider) {
language = ((TemplateLanguageFileViewProvider) provider).getTemplateDataLanguage();
} else {
language = provider.getBaseLanguage();
}
return language == XHTMLLanguage.INSTANCE;
}
return false;
}
use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.
the class InsertRequiredAttributeFix method invoke.
@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
XmlTag myTag = (XmlTag) startElement;
ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(myTag);
final XmlElementDescriptor descriptor = myTag.getDescriptor();
if (descriptor == null) {
return;
}
final XmlAttributeDescriptor attrDescriptor = descriptor.getAttributeDescriptor(myAttrName, myTag);
final boolean indirectSyntax = XmlExtension.getExtension(myTag.getContainingFile()).isIndirectSyntax(attrDescriptor);
boolean insertShorthand = myTag instanceof HtmlTag && attrDescriptor != null && HtmlUtil.isBooleanAttribute(attrDescriptor, myTag);
PsiElement anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.EMPTY_TAG_END_FINDER.findChild(treeElement));
final boolean anchorIsEmptyTag = anchor != null;
if (anchor == null) {
anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.START_TAG_END_FINDER.findChild(treeElement));
}
if (anchor == null)
return;
final Template template = TemplateManager.getInstance(project).createTemplate("", "");
if (indirectSyntax) {
if (anchorIsEmptyTag)
template.addTextSegment(">");
template.addTextSegment("<jsp:attribute name=\"" + myAttrName + "\">");
} else {
template.addTextSegment(" " + myAttrName + (!insertShorthand ? "=\"" : ""));
}
Expression expression = new Expression() {
final TextResult result = new TextResult("");
@Override
public Result calculateResult(ExpressionContext context) {
return result;
}
@Override
public Result calculateQuickResult(ExpressionContext context) {
return null;
}
@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
final LookupElement[] items = new LookupElement[myValues.length];
for (int i = 0; i < items.length; i++) {
items[i] = LookupElementBuilder.create(myValues[i]);
}
return items;
}
};
if (!insertShorthand)
template.addVariable(NAME_TEMPLATE_VARIABLE, expression, expression, true);
if (indirectSyntax) {
template.addTextSegment("</jsp:attribute>");
template.addEndVariable();
if (anchorIsEmptyTag)
template.addTextSegment("</" + myTag.getName() + ">");
} else if (!insertShorthand) {
template.addTextSegment("\"");
}
final PsiElement anchor1 = anchor;
final Runnable runnable = () -> ApplicationManager.getApplication().runWriteAction(() -> {
int textOffset = anchor1.getTextOffset();
if (!anchorIsEmptyTag && indirectSyntax)
++textOffset;
editor.getCaretModel().moveToOffset(textOffset);
if (anchorIsEmptyTag && indirectSyntax) {
editor.getDocument().deleteString(textOffset, textOffset + 2);
}
TemplateManager.getInstance(project).startTemplate(editor, template);
});
if (!ApplicationManager.getApplication().isUnitTestMode()) {
Runnable commandRunnable = () -> CommandProcessor.getInstance().executeCommand(project, runnable, getText(), getFamilyName());
ApplicationManager.getApplication().invokeLater(commandRunnable);
} else {
runnable.run();
}
}
use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.
the class XmlAttributeReferenceCompletionProvider method addVariants.
private static void addVariants(final CompletionResultSet result, final XmlAttribute[] attributes, final XmlAttributeDescriptor[] descriptors, XmlAttribute attribute, @Nullable InsertHandler<LookupElement> replacementInsertHandler) {
final XmlTag tag = attribute.getParent();
final PsiFile file = tag.getContainingFile();
final XmlExtension extension = XmlExtension.getExtension(file);
final String prefix = attribute.getName().contains(":") && ((XmlAttributeImpl) attribute).getRealLocalName().length() > 0 ? attribute.getNamespacePrefix() + ":" : null;
for (XmlAttributeDescriptor descriptor : descriptors) {
if (isValidVariant(attribute, descriptor, attributes, extension)) {
String name = descriptor.getName(tag);
InsertHandler<LookupElement> insertHandler = XmlAttributeInsertHandler.INSTANCE;
if (tag instanceof HtmlTag && HtmlUtil.isShortNotationOfBooleanAttributePreferred() && HtmlUtil.isBooleanAttribute(descriptor, tag)) {
insertHandler = null;
}
if (replacementInsertHandler != null) {
insertHandler = replacementInsertHandler;
} else if (descriptor instanceof NamespaceAwareXmlAttributeDescriptor) {
final String namespace = ((NamespaceAwareXmlAttributeDescriptor) descriptor).getNamespace(tag);
if (file instanceof XmlFile && namespace != null && namespace.length() > 0 && !name.contains(":") && tag.getPrefixByNamespace(namespace) == null) {
insertHandler = new XmlAttributeInsertHandler(namespace);
}
}
if (prefix == null || name.startsWith(prefix)) {
if (prefix != null && name.length() > prefix.length()) {
name = descriptor.getName(tag).substring(prefix.length());
}
LookupElementBuilder element = LookupElementBuilder.create(name);
if (descriptor instanceof PsiPresentableMetaData) {
element = element.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
}
final int separator = name.indexOf(':');
if (separator > 0) {
element = element.withLookupString(name.substring(separator + 1));
}
element = element.withCaseSensitivity(!(descriptor instanceof HtmlAttributeDescriptorImpl)).withInsertHandler(insertHandler);
result.addElement(descriptor.isRequired() ? PrioritizedLookupElement.withPriority(element.appendTailText("(required)", true), 100) : HtmlUtil.isOwnHtmlAttribute(descriptor) ? PrioritizedLookupElement.withPriority(element, 50) : element);
}
}
}
}
use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.
the class HtmlMissingClosingTagInspection method checkTag.
@Override
protected void checkTag(@NotNull XmlTag tag, @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
return;
}
final PsiElement child = tag.getLastChild();
if (child instanceof PsiErrorElement) {
return;
}
final XmlToken tagNameElement = XmlTagUtil.getStartTagNameElement(tag);
if (tagNameElement == null) {
return;
}
final String tagName = tagNameElement.getText();
if (HtmlUtil.isSingleHtmlTag(tagName) || XmlTagUtil.getEndTagNameElement(tag) != null) {
return;
}
holder.registerProblem(tagNameElement, XmlErrorMessages.message("element.missing.end.tag"), new MissingClosingTagFix(tagName));
}
use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.
the class HtmlUnknownAttributeInspectionBase method checkAttribute.
@Override
protected void checkAttribute(@NotNull final XmlAttribute attribute, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
final XmlTag tag = attribute.getParent();
if (tag instanceof HtmlTag) {
XmlElementDescriptor elementDescriptor = tag.getDescriptor();
if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
return;
}
XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
if (attributeDescriptor == null && !attribute.isNamespaceDeclaration()) {
final String name = attribute.getName();
if (!XmlUtil.attributeFromTemplateFramework(name, tag) && (!isCustomValuesEnabled() || !isCustomValue(name))) {
boolean maySwitchToHtml5 = HtmlUtil.isCustomHtml5Attribute(name) && !HtmlUtil.hasNonHtml5Doctype(tag);
LocalQuickFix[] quickfixes = new LocalQuickFix[maySwitchToHtml5 ? 3 : 2];
quickfixes[0] = new AddCustomHtmlElementIntentionAction(ATTRIBUTE_KEY, name, XmlBundle.message("add.custom.html.attribute", name));
quickfixes[1] = new RemoveAttributeIntentionAction(name);
if (maySwitchToHtml5) {
quickfixes[2] = new SwitchToHtml5WithHighPriorityAction();
}
registerProblemOnAttributeName(attribute, XmlErrorMessages.message("attribute.is.not.allowed.here", attribute.getName()), holder, quickfixes);
}
}
}
}
Aggregations