<commit_before>package org.jetbrains.plugins.scala
package lang.completion.handlers

import com.intellij.codeInsight.completion._
import com.intellij.codeInsight.lookup.LookupElement
import org.jetbrains.plugins.scala.lang.resolve.ResolveUtils.ScalaLookupObject
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.api.base.ScReferenceElement
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScReferenceExpression
import lang.completion.ScalaCompletionUtil
import lang.psi.api.toplevel.imports.{ScImportSelectors, ScImportStmt}
import lang.psi.ScalaPsiUtil
import lang.psi.api.ScalaFile
import lang.resolve.ResolveUtils
import lang.psi.api.toplevel.typedef.ScObject
import com.intellij.codeInsight.AutoPopupController
import com.intellij.openapi.util.Condition
import com.intellij.psi.{PsiFile, PsiDocumentManager, PsiMember, PsiClass}

/**
 * @author <NAME>
 */

class ScalaClassNameInsertHandler extends ScalaInsertHandler {
  override def handleInsert(context: InsertionContext, item: LookupElement) {
    val startOffset = context.getStartOffset
    var ref: ScReferenceElement = PsiTreeUtil.findElementOfClassAtOffset(context.getFile, startOffset, classOf[ScReferenceElement], false)
    val useFullyQualifiedName = PsiTreeUtil.getParentOfType(ref, classOf[ScImportStmt]) != null &&
      PsiTreeUtil.getParentOfType(ref, classOf[ScImportSelectors]) == null //do not complete in sel
    if (ref == null) return
    val file = ref.getContainingFile
    val patchedObject = ScalaCompletionUtil.getScalaLookupObject(item)
    if (patchedObject == null) return

    patchedObject match {
      case ScalaLookupObject(cl: PsiClass, _, _, isInRef) =>
        while (ref.getParent != null && ref.getParent.isInstanceOf[ScReferenceElement] &&
                (ref.getParent.asInstanceOf[ScReferenceElement].qualifier match {
                  case Some(r) => r != ref
                  case _ => true
                }))
          ref = ref.getParent.asInstanceOf[ScReferenceElement]
        val addDot = if (cl.isInstanceOf[ScObject] && isInRef) "." else ""
        val newRef = (useFullyQualifiedName, ref) match {
          case (false, ref: ScReferenceExpression) =>
            ScalaPsiElementFactory.createExpressionFromText(cl.getName + addDot, cl.getManager).asInstanceOf[ScReferenceExpression]
          case (false, _) =>
            ScalaPsiElementFactory.createReferenceFromText(cl.getName + addDot, cl.getManager)
          case (true, _) =>
            ScalaPsiElementFactory.createReferenceFromText(cl.getQualifiedName + addDot, cl.getManager)
        }
        ref.getNode.getTreeParent.replaceChild(ref.getNode, newRef.getNode)
        newRef.bindToElement(cl)
        if (addDot == ".") {
          context.setLaterRunnable(new Runnable {
            def run() {
              AutoPopupController.getInstance(context.getProject).scheduleAutoPopup(
                context.getEditor, new Condition[PsiFile] {
                  def value(t: PsiFile): Boolean = t == context.getFile
                }
              )
            }
          })
        }
      case ScalaLookupObject(namedElement, _, _, _) =>
        val containingClass = ScalaPsiUtil.nameContext(namedElement) match {
          case memb: PsiMember =>
            memb.getContainingClass
          case _ => null
        }
        super.handleInsert(context, item)
        if (containingClass != null) {
          val document = context.getEditor.getDocument
          PsiDocumentManager.getInstance(file.getProject).commitDocument(document)
          file match {
            case scalaFile: ScalaFile =>
              val elem = scalaFile.findElementAt(startOffset)
              val usedQuickfix = item.getUserData(ResolveUtils.usedImportStaticQuickfixKey)
              val shouldImport = item.getUserData(ResolveUtils.shouldImportKey)
              def qualifyReference(ref: ScReferenceExpression) {
                val newRef = ScalaPsiElementFactory.createExpressionFromText(
                  containingClass.getName + "." + ref.getText,
                  containingClass.getManager).asInstanceOf[ScReferenceExpression]
                ref.getNode.getTreeParent.replaceChild(ref.getNode, newRef.getNode)
                newRef.qualifier.get.asInstanceOf[ScReferenceExpression].bindToElement(containingClass)
              }
              elem.getParent match {
                case ref: ScReferenceExpression if usedQuickfix == null || !usedQuickfix.booleanValue() =>
                  if (shouldImport != null && shouldImport.booleanValue()) {
                    qualifyReference(ref)
                  }
                case ref: ScReferenceExpression =>
                  if (shouldImport == null || !shouldImport.booleanValue()) {
                    qualifyReference(ref)
                  } else {
                    //import static
                    ref.bindToElement(namedElement)
                  }
                case _ =>
              }
            case _ =>
          }
        }
      case _ =>
    }
  }
}<commit_msg>Commit document before class name insert handler (X)<commit_after>package org.jetbrains.plugins.scala
package lang.completion.handlers

import com.intellij.codeInsight.completion._
import com.intellij.codeInsight.lookup.LookupElement
import org.jetbrains.plugins.scala.lang.resolve.ResolveUtils.ScalaLookupObject
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.api.base.ScReferenceElement
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScReferenceExpression
import lang.completion.ScalaCompletionUtil
import lang.psi.api.toplevel.imports.{ScImportSelectors, ScImportStmt}
import lang.psi.ScalaPsiUtil
import lang.psi.api.ScalaFile
import lang.resolve.ResolveUtils
import lang.psi.api.toplevel.typedef.ScObject
import com.intellij.codeInsight.AutoPopupController
import com.intellij.openapi.util.Condition
import com.intellij.psi.{PsiFile, PsiDocumentManager, PsiMember, PsiClass}

/**
 * @author <NAME>
 */

class ScalaClassNameInsertHandler extends ScalaInsertHandler {
  override def handleInsert(context: InsertionContext, item: LookupElement) {
    PsiDocumentManager.getInstance(context.getProject).commitDocument(context.getDocument)
    val startOffset = context.getStartOffset
    var ref: ScReferenceElement = PsiTreeUtil.findElementOfClassAtOffset(context.getFile, startOffset, classOf[ScReferenceElement], false)
    val useFullyQualifiedName = PsiTreeUtil.getParentOfType(ref, classOf[ScImportStmt]) != null &&
      PsiTreeUtil.getParentOfType(ref, classOf[ScImportSelectors]) == null //do not complete in sel
    if (ref == null) return
    val file = ref.getContainingFile
    val patchedObject = ScalaCompletionUtil.getScalaLookupObject(item)
    if (patchedObject == null) return

    patchedObject match {
      case ScalaLookupObject(cl: PsiClass, _, _, isInRef) =>
        while (ref.getParent != null && ref.getParent.isInstanceOf[ScReferenceElement] &&
                (ref.getParent.asInstanceOf[ScReferenceElement].qualifier match {
                  case Some(r) => r != ref
                  case _ => true
                }))
          ref = ref.getParent.asInstanceOf[ScReferenceElement]
        val addDot = if (cl.isInstanceOf[ScObject] && isInRef) "." else ""
        val newRef = (useFullyQualifiedName, ref) match {
          case (false, ref: ScReferenceExpression) =>
            ScalaPsiElementFactory.createExpressionFromText(cl.getName + addDot, cl.getManager).asInstanceOf[ScReferenceExpression]
          case (false, _) =>
            ScalaPsiElementFactory.createReferenceFromText(cl.getName + addDot, cl.getManager)
          case (true, _) =>
            ScalaPsiElementFactory.createReferenceFromText(cl.getQualifiedName + addDot, cl.getManager)
        }
        ref.getNode.getTreeParent.replaceChild(ref.getNode, newRef.getNode)
        newRef.bindToElement(cl)
        if (addDot == ".") {
          context.setLaterRunnable(new Runnable {
            def run() {
              AutoPopupController.getInstance(context.getProject).scheduleAutoPopup(
                context.getEditor, new Condition[PsiFile] {
                  def value(t: PsiFile): Boolean = t == context.getFile
                }
              )
            }
          })
        }
      case ScalaLookupObject(namedElement, _, _, _) =>
        val containingClass = ScalaPsiUtil.nameContext(namedElement) match {
          case memb: PsiMember =>
            memb.getContainingClass
          case _ => null
        }
        super.handleInsert(context, item)
        if (containingClass != null) {
          val document = context.getEditor.getDocument
          PsiDocumentManager.getInstance(file.getProject).commitDocument(document)
          file match {
            case scalaFile: ScalaFile =>
              val elem = scalaFile.findElementAt(startOffset)
              val usedQuickfix = item.getUserData(ResolveUtils.usedImportStaticQuickfixKey)
              val shouldImport = item.getUserData(ResolveUtils.shouldImportKey)
              def qualifyReference(ref: ScReferenceExpression) {
                val newRef = ScalaPsiElementFactory.createExpressionFromText(
                  containingClass.getName + "." + ref.getText,
                  containingClass.getManager).asInstanceOf[ScReferenceExpression]
                ref.getNode.getTreeParent.replaceChild(ref.getNode, newRef.getNode)
                newRef.qualifier.get.asInstanceOf[ScReferenceExpression].bindToElement(containingClass)
              }
              elem.getParent match {
                case ref: ScReferenceExpression if usedQuickfix == null || !usedQuickfix.booleanValue() =>
                  if (shouldImport != null && shouldImport.booleanValue()) {
                    qualifyReference(ref)
                  }
                case ref: ScReferenceExpression =>
                  if (shouldImport == null || !shouldImport.booleanValue()) {
                    qualifyReference(ref)
                  } else {
                    //import static
                    ref.bindToElement(namedElement)
                  }
                case _ =>
              }
            case _ =>
          }
        }
      case _ =>
    }
  }
}
{'fraction_non_alphanumeric': 0.08924793229467205, 'fraction_numerical': 0.0, 'mean_word_length': 2.6145290232881475, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4944948', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2753, 'n_words': 472}