private void bindMessageToTag(
      final XmlTag tag,
      final HighlightInfoType warning,
      final int messageLength,
      final String localizedMessage,
      IntentionAction... quickFixActions) {
    XmlToken childByRole = XmlTagUtil.getStartTagNameElement(tag);

    bindMessageToAstNode(childByRole, warning, 0, messageLength, localizedMessage, quickFixActions);
    childByRole = XmlTagUtil.getEndTagNameElement(tag);
    bindMessageToAstNode(childByRole, warning, 0, messageLength, localizedMessage, quickFixActions);
  }
Пример #2
0
  private static void addMessagesForTag(
      XmlTag tag,
      String message,
      Validator.ValidationHost.ErrorType type,
      AnnotationHolder myHolder,
      IntentionAction... actions) {
    XmlToken childByRole = XmlTagUtil.getStartTagNameElement(tag);

    addMessagesForTreeChild(childByRole, type, message, myHolder, actions);

    childByRole = XmlTagUtil.getEndTagNameElement(tag);
    addMessagesForTreeChild(childByRole, type, message, myHolder, actions);
  }
Пример #3
0
  public static Pair<TextRange, PsiElement> getProblemRange(final XmlTag tag) {
    final PsiElement startToken = XmlTagUtil.getStartTagNameElement(tag);
    if (startToken == null) {
      return Pair.create(tag.getTextRange(), (PsiElement) tag);
    }

    return Pair.create(
        startToken.getTextRange().shiftRight(-tag.getTextRange().getStartOffset()),
        (PsiElement) tag);
  }
 public void annotate(final PsiElement psiElement, final AnnotationHolder holder) {
   if (psiElement instanceof XmlToken) {
     final PsiElement parent = psiElement.getParent();
     if (parent instanceof XmlTag) {
       final XmlTag tag = (XmlTag) parent;
       final XmlToken start = XmlTagUtil.getStartTagNameElement(tag);
       XmlToken endTagName = XmlTagUtil.getEndTagNameElement(tag);
       if (endTagName != null
           && !(tag instanceof HtmlTag)
           && !tag.getName().equals(endTagName.getText())) {
         registerProblem(holder, tag, start, endTagName);
       } else if (endTagName == null
           && !(tag instanceof HtmlTag && HtmlUtil.isSingleHtmlTag(tag.getName()))) {
         final PsiErrorElement errorElement =
             PsiTreeUtil.getChildOfType(tag, PsiErrorElement.class);
         endTagName = findEndTagName(errorElement);
         if (endTagName != null) {
           registerProblem(holder, tag, start, endTagName);
         }
       }
     } else if (parent instanceof PsiErrorElement) {
       if (XmlTokenType.XML_NAME == ((XmlToken) psiElement).getTokenType()) {
         final PsiFile psiFile = psiElement.getContainingFile();
         if (psiFile != null
             && (HTMLLanguage.INSTANCE == psiFile.getViewProvider().getBaseLanguage()
                 || HTMLLanguage.INSTANCE == parent.getLanguage())) {
           final String message =
               XmlErrorMessages.message("xml.parsing.closing.tag.mathes.nothing");
           if (message.equals(((PsiErrorElement) parent).getErrorDescription())) {
             final Annotation annotation = holder.createWarningAnnotation(parent, message);
             annotation.registerFix(new RemoveExtraClosingTagIntentionAction());
           }
         }
       }
     }
   }
 }
  @Override
  protected void checkTag(
      @NotNull final XmlTag tag, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag)
        || !XmlHighlightVisitor.shouldBeValidated(tag)
        || isInSpecialHtml5Namespace(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 AddCustomTagOrAttributeIntentionAction action =
            new AddCustomTagOrAttributeIntentionAction(
                TAG_KEY, name, XmlEntitiesInspection.UNKNOWN_TAG);

        // 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<LocalQuickFix>();
        quickfixes.add(action);
        if (isOnTheFly) {
          ContainerUtil.addIfNotNull(
              CreateNSDeclarationIntentionFix.createFix(startTagName, ""), quickfixes);
        }
        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()]));
        }
      }
    }
  }