<commit_before>/*
 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package com.intellij.codeInsight.completion;

import com.intellij.codeInsight.lookup.MutableLookupElement;
import com.intellij.lang.html.HTMLLanguage;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.util.text.CharArrayUtil;
import com.intellij.xml.util.HtmlUtil;

/**
* @author peter
*/
public class XmlAttributeInsertHandler implements InsertHandler<MutableLookupElement> {
  public static final XmlAttributeInsertHandler INSTANCE = new XmlAttributeInsertHandler();

  public void handleInsert(InsertionContext context, MutableLookupElement item) {
    final Editor editor = context.getEditor();

    final Document document = editor.getDocument();
    final int caretOffset = editor.getCaretModel().getOffset();
    if (PsiDocumentManager.getInstance(editor.getProject()).getPsiFile(document).getLanguage() == HTMLLanguage.INSTANCE &&
        HtmlUtil.isSingleHtmlAttribute((String)item.getObject())) {
      return;
    }

    final CharSequence chars = document.getCharsSequence();
    if (!CharArrayUtil.regionMatches(chars, caretOffset, "=\"") && !CharArrayUtil.regionMatches(chars, caretOffset, "='")) {
      if (caretOffset >= document.getTextLength() || "/> \n\t\r".indexOf(document.getCharsSequence().charAt(caretOffset)) < 0) {
        document.insertString(caretOffset, "=\"\" ");
      }
      else {
        document.insertString(caretOffset, "=\"\"");
      }

      if ('=' == context.getCompletionChar()) {
        context.setAddCompletionChar(false); // IDEA-19449<commit_msg>Intellij cannot recognize Javascript variable for BlackBird Javascript logging library (IDEADEV-37157)<commit_after>/*
 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package com.intellij.codeInsight.completion;

import com.intellij.codeInsight.lookup.MutableLookupElement;
import com.intellij.lang.html.HTMLLanguage;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiElement;
import com.intellij.util.text.CharArrayUtil;
import com.intellij.xml.util.HtmlUtil;

/**
* @author peter
*/
public class XmlAttributeInsertHandler implements InsertHandler<MutableLookupElement> {
  public static final XmlAttributeInsertHandler INSTANCE = new XmlAttributeInsertHandler();

  public void handleInsert(InsertionContext context, MutableLookupElement item) {
    final Editor editor = context.getEditor();

    final Document document = editor.getDocument();
    final int caretOffset = editor.getCaretModel().getOffset();
    PsiFile file = PsiDocumentManager.getInstance(editor.getProject()).getPsiFile(document);
    if (file.getLanguage() == HTMLLanguage.INSTANCE &&
        HtmlUtil.isSingleHtmlAttribute((String)item.getObject())) {
      return;
    }

    final CharSequence chars = document.getCharsSequence();
    if (!CharArrayUtil.regionMatches(chars, caretOffset, "=\"") && !CharArrayUtil.regionMatches(chars, caretOffset, "='")) {
      PsiElement fileContext = file.getContext();
      String toInsert= "=\"\"";

      if(fileContext != null) {
        if (fileContext.getText().startsWith("\"")) toInsert = "=''";
      }
      
      if (caretOffset >= document.getTextLength() || "/> \n\t\r".indexOf(document.getCharsSequence().charAt(caretOffset)) < 0) {
        document.insertString(caretOffset, toInsert + " ");
      }
      else {
        document.insertString(caretOffset, toInsert);
      }

      if ('=' == context.getCompletionChar()) {
        context.setAddCompletionChar(false); // IDEA-19449
{'fraction_non_alphanumeric': 0.126842907981698, 'fraction_numerical': 0.00838840874428063, 'mean_word_length': 4.472878998609179, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5939372', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1134, 'n_words': 213}