<commit_before>package org.jetbrains.plugins.scala.lang.psi

import com.intellij.openapi.application.{ApplicationAdapter, ApplicationManager}
import com.intellij.openapi.util.Key
import com.intellij.psi._
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportTypeFix
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.formatting.settings.ScalaCodeStyleSettings
import org.jetbrains.plugins.scala.lang.psi.api.base.ScReferenceElement
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScBindingPattern
import org.jetbrains.plugins.scala.lang.psi.api.base.types.{ScParameterizedTypeElement, ScSimpleTypeElement, ScTypeElement, ScTypeProjection}
import org.jetbrains.plugins.scala.lang.psi.api.statements.{ScTypeAlias, ScTypeAliasDefinition}
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.types.{ScType, ScTypePresentation}
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil
import org.jetbrains.plugins.scala.lang.resolve.ResolveTargets._
import org.jetbrains.plugins.scala.lang.resolve.processor.BaseProcessor
import org.jetbrains.plugins.scala.util.ScEquivalenceUtil

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer


object TypeAdjuster extends ApplicationAdapter {

  ApplicationManager.getApplication.addApplicationListener(this)

  private val markedElements = ArrayBuffer[SmartPsiElementPointer[PsiElement]]()

  override def writeActionFinished(action: scala.Any): Unit = {
    if (markedElements.nonEmpty) {
      adjustMarkedElements()
    }
  }

  def adjustFor(elements: Seq[PsiElement], addImports: Boolean = true, useTypeAliases: Boolean = true): Unit = {

    val typeElements = elements.toVector.flatMap(collectAdjustableTypeElements).distinct

    val rInfos = toReplacementInfos(typeElements, useTypeAliases)

    replaceAndAddImports(rInfos, addImports)
  }

  def markToAdjust(element: PsiElement) = {
    if (element != null && element.isValid) {
      val manager = SmartPointerManager.getInstance(element.getProject)
      markedElements += manager.createSmartPsiElementPointer(element)
    }
  }

  private def adjustMarkedElements() = {
    val elements = markedElements.flatMap(_.getElement.toOption).filter(_.isValid)
    markedElements.clear()<commit_msg>commit documents before performing postponed adjust types EA-80617 fixed<commit_after>package org.jetbrains.plugins.scala.lang.psi

import com.intellij.openapi.application.{ApplicationAdapter, ApplicationManager}
import com.intellij.openapi.util.Key
import com.intellij.psi._
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.plugins.scala.annotator.intention.ScalaImportTypeFix
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.formatting.settings.ScalaCodeStyleSettings
import org.jetbrains.plugins.scala.lang.psi.api.base.ScReferenceElement
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScBindingPattern
import org.jetbrains.plugins.scala.lang.psi.api.base.types.{ScParameterizedTypeElement, ScSimpleTypeElement, ScTypeElement, ScTypeProjection}
import org.jetbrains.plugins.scala.lang.psi.api.statements.{ScTypeAlias, ScTypeAliasDefinition}
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.types.{ScType, ScTypePresentation}
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil
import org.jetbrains.plugins.scala.lang.resolve.ResolveTargets._
import org.jetbrains.plugins.scala.lang.resolve.processor.BaseProcessor
import org.jetbrains.plugins.scala.util.ScEquivalenceUtil

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer


object TypeAdjuster extends ApplicationAdapter {

  ApplicationManager.getApplication.addApplicationListener(this)

  private val markedElements = ArrayBuffer[SmartPsiElementPointer[PsiElement]]()

  override def writeActionFinished(action: scala.Any): Unit = {
    if (markedElements.nonEmpty) {
      val project = markedElements.head.getProject
      PsiDocumentManager.getInstance(project).commitAllDocuments()
      adjustMarkedElements()
    }
  }

  def adjustFor(elements: Seq[PsiElement], addImports: Boolean = true, useTypeAliases: Boolean = true): Unit = {

    val typeElements = elements.toVector.flatMap(collectAdjustableTypeElements).distinct

    val rInfos = toReplacementInfos(typeElements, useTypeAliases)

    replaceAndAddImports(rInfos, addImports)
  }

  def markToAdjust(element: PsiElement) = {
    if (element != null && element.isValid) {
      val manager = SmartPointerManager.getInstance(element.getProject)
      markedElements += manager.createSmartPsiElementPointer(element)
    }
  }

  private def adjustMarkedElements() = {
    val elements = markedElements.flatMap(_.getElement.toOption).filter(_.isValid)
    markedElements.clear()
{'fraction_non_alphanumeric': 0.09483624673498091, 'fraction_numerical': 0.0010046212577858146, 'mean_word_length': 5.904299583911234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6887123', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1429, 'n_words': 213}