<commit_before>package com.intellij.codeInsight.completion;

import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlToken;
import com.intellij.psi.xml.XmlTokenType;
import com.intellij.psi.filters.ElementFilter;
import com.intellij.util.ArrayUtil;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Oct 13, 2004
 * Time: 6:50:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class HtmlCompletionData extends XmlCompletionData {
  private static CompletionData ourStyleCompletionData;
  private boolean myCaseInsensitive;
  private static CompletionData ourScriptCompletionData;

  public HtmlCompletionData() {
    this(true);
  }

  protected HtmlCompletionData(boolean _caseInsensitive) {
    myCaseInsensitive = _caseInsensitive;
  }

  private boolean equalNames(String str,String str2) {
    if (!myCaseInsensitive) return str.equals(str2);
    return str.equalsIgnoreCase(str2);
  }

  protected boolean isCaseInsensitive() {
    return true;
  }

  protected ElementFilter createTagCompletionFilter() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        String name = ((XmlTag)context).getName();
        if (equalNames(name, "style") ||
            equalNames(name,"script")) {
          return false;
        }

        XmlAttribute parentOfType = PsiTreeUtil.getParentOfType((PsiElement)element,XmlAttribute.class);
        if ( parentOfType != null && equalNames(parentOfType.getName(), "style")) return false;
        return true;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  protected ElementFilter createAttributeCompletion() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        if (equalNames(((XmlAttribute)context).getName(), "style")) return false;
        return true;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  protected ElementFilter createAttributeValueCompletionFilter() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        if (equalNames(((XmlAttribute)context.getParent()).getName(), "style")) return false;
        return true;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  private boolean isScriptContext(PsiElement element) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
    if(attribute != null ||
       ((element instanceof XmlToken) && ((XmlToken)element).getTokenType() == XmlTokenType.XML_NAME)) {
      return false; // we could recognize
    }

    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);

    return isScriptTag(tag);

  }

  private boolean isScriptTag(XmlTag tag) {
    if (tag!=null) {
      String tagName = tag.getName();
      if (myCaseInsensitive) tagName = tagName.toLowerCase();

      return tagName.equals("script");
    }

    return false;
  }

  private boolean isStyleTag(XmlTag tag) {
    if (tag!=null) {
      String tagName = tag.getName();
      if (myCaseInsensitive) tagName = tagName.toLowerCase();

      return tagName.equals("style");
    }

    return false;
  }

  public CompletionVariant[] findVariants(PsiElement position, CompletionContext context) {
    CompletionVariant[] variants = super.findVariants(position, context);

    if (ourStyleCompletionData!=null && isStyleContext(position)) {
      final CompletionVariant[] styleVariants = ourStyleCompletionData.findVariants(position, context);

      variants = ArrayUtil.mergeArrays(variants,styleVariants, CompletionVariant.class);
    }

    if (ourScriptCompletionData!=null && isScriptContext(position)) {
      final CompletionVariant[] scriptVariants = ourScriptCompletionData.findVariants(position, context);

      variants = ArrayUtil.mergeArrays(variants,scriptVariants, CompletionVariant.class);
    }

    return variants;
  }

  private boolean isStyleContext(PsiElement position) {
    XmlAttribute parentOfType = PsiTreeUtil.getParentOfType(position, XmlAttribute.class, false);

    if (parentOfType != null) {
      String name = parentOfType.getName();
      if (myCaseInsensitive) name = name.toLowerCase();
      return name.equals("style");
    }

    return isStyleTag(PsiTreeUtil.getParentOfType(position, XmlTag.class, false));
  }

  public void addKeywordVariants(Set set, CompletionContext context, PsiElement position) {
    super.addKeywordVariants(set, context, position);

    if (ourStyleCompletionData!=null && isStyleContext(position)) {
      ourStyleCompletionData.addKeywordVariants(set, context, position);
    } else if (ourScriptCompletionData!=null && isScriptContext(position)) {
      ourScriptCompletionData.addKeywordVariants(set, context, position);
    }
  }

  public static void setStyleCompletionData(CompletionData cssCompletionData) {
    ourStyleCompletionData = cssCompletionData;
  }

  public void registerVariant(CompletionVariant variant) {
    super.registerVariant(variant);<commit_msg>(no message)<commit_after>package com.intellij.codeInsight.completion;

import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlToken;
import com.intellij.psi.xml.XmlTokenType;
import com.intellij.psi.filters.ElementFilter;
import com.intellij.util.ArrayUtil;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Oct 13, 2004
 * Time: 6:50:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class HtmlCompletionData extends XmlCompletionData {
  private static CompletionData ourStyleCompletionData;
  private boolean myCaseInsensitive;
  private static CompletionData ourScriptCompletionData;

  public HtmlCompletionData() {
    this(true);
  }

  protected HtmlCompletionData(boolean _caseInsensitive) {
    myCaseInsensitive = _caseInsensitive;
  }

  private boolean equalNames(String str,String str2) {
    if (!myCaseInsensitive) return str.equals(str2);
    return str.equalsIgnoreCase(str2);
  }

  protected boolean isCaseInsensitive() {
    return true;
  }

  protected ElementFilter createTagCompletionFilter() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        String name = ((XmlTag)context).getName();
        if (equalNames(name, "style") ||
            equalNames(name,"script")) {
          return false;
        }

        XmlAttribute parentOfType = PsiTreeUtil.getParentOfType((PsiElement)element,XmlAttribute.class);
        if ( parentOfType != null && equalNames(parentOfType.getName(), "style")) return false;
        return true;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  protected ElementFilter createAttributeCompletion() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        if (equalNames(((XmlAttribute)context).getName(), "style")) return false;
        return true;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  protected ElementFilter createAttributeValueCompletionFilter() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        if (equalNames(((XmlAttribute)context.getParent()).getName(), "style")) return false;
        return true;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  private boolean isScriptContext(PsiElement element) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class, false);
    if(attribute != null ||
       ((element instanceof XmlToken) && ((XmlToken)element).getTokenType() == XmlTokenType.XML_NAME)) {
      return false; // we could recognize
    }

    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);

    return isScriptTag(tag);

  }

  private boolean isScriptTag(XmlTag tag) {
    if (tag!=null) {
      String tagName = tag.getName();
      if (myCaseInsensitive) tagName = tagName.toLowerCase();

      return tagName.equals("script");
    }

    return false;
  }

  private boolean isStyleTag(XmlTag tag) {
    if (tag!=null) {
      String tagName = tag.getName();
      if (myCaseInsensitive) tagName = tagName.toLowerCase();

      return tagName.equals("style");
    }

    return false;
  }

  public CompletionVariant[] findVariants(PsiElement position, CompletionContext context) {
    CompletionVariant[] variants = super.findVariants(position, context);

    if (ourStyleCompletionData!=null && isStyleContext(position)) {
      final CompletionVariant[] styleVariants = ourStyleCompletionData.findVariants(position, context);

      variants = ArrayUtil.mergeArrays(variants,styleVariants, CompletionVariant.class);
    }

    if (ourScriptCompletionData!=null && isScriptContext(position)) {
      final CompletionVariant[] scriptVariants = ourScriptCompletionData.findVariants(position, context);

      variants = ArrayUtil.mergeArrays(variants,scriptVariants, CompletionVariant.class);
    }

    return variants;
  }

  private boolean isStyleContext(PsiElement position) {
    XmlAttribute parentOfType = PsiTreeUtil.getParentOfType(position, XmlAttribute.class, false);

    if (parentOfType != null) {
      String name = parentOfType.getName();
      if (myCaseInsensitive) name = name.toLowerCase();
      return name.equals("style");
    }

    return isStyleTag(PsiTreeUtil.getParentOfType(position, XmlTag.class, false));
  }

  public void addKeywordVariants(Set<CompletionVariant> set, CompletionContext context, PsiElement position) {
    super.addKeywordVariants(set, context, position);

    if (ourStyleCompletionData!=null && isStyleContext(position)) {
      ourStyleCompletionData.addKeywordVariants(set, context, position);
    } else if (ourScriptCompletionData!=null && isScriptContext(position)) {
      ourScriptCompletionData.addKeywordVariants(set, context, position);
    }
  }

  public static void setStyleCompletionData(CompletionData cssCompletionData) {
    ourStyleCompletionData = cssCompletionData;
  }

  public void registerVariant(CompletionVariant variant) {
    super.registerVariant(variant);
{'fraction_non_alphanumeric': 0.09900990099009901, 'fraction_numerical': 0.0026153558752101624, 'mean_word_length': 3.975371747211896, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '138748', 'n_tokens_mistral': 3039, 'n_tokens_neox': 2941, 'n_words': 658}