<commit_before>package org.jetbrains.plugins.scala.codeInspection.prefixMutableCollections

import org.jetbrains.plugins.scala.lang.resolve.ScalaResolveResult
import com.intellij.psi.PsiClass
import org.jetbrains.plugins.scala.extensions.toPsiClassExt
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings
import org.jetbrains.plugins.scala.codeInspection.{AbstractFix, AbstractInspection}
import com.intellij.openapi.project.Project
import com.intellij.codeInspection.{ProblemDescriptor, ProblemsHolder}
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScStableCodeReferenceElement, ScReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScReferenceExpression
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports.ScImportSelector

/**
 * @author Alefas
 * @since 26.05.12
 */

class ReferenceMustBePrefixedInspection extends AbstractInspection("ReferenceMustBePrefixed", "Reference must be prefixed") {
  def actionFor(holder: ProblemsHolder) = {
    case ref: ScReferenceElement if ref.qualifier == None && !ref.getParent.isInstanceOf[ScImportSelector] =>
      ref.bind() match {
        case Some(r: ScalaResolveResult) =>
          r.getActualElement match {
            case clazz: PsiClass =>
              val qualName = clazz.qualifiedName
              if (ScalaProjectSettings.getInstance(holder.getProject).hasImportWithPrefix(qualName)) {
                holder.registerProblem(ref, getDisplayName, new AddPrefixFix(ref, clazz))
              }
            case _ =>
          }
        case _ =>
      }
  }
}

class AddPrefixFix(ref: ScReferenceElement, clazz: PsiClass) extends AbstractFix("Add prefix to reference", ref) {
  def doApplyFix(project: Project, descriptor: ProblemDescriptor) {
    if (!ref.isValid || !clazz.isValid) return
    val parts = clazz.qualifiedName.split('.')
    if (parts.length < 2) return
    val newRefText = parts.takeRight(2).mkString(".")
    ref match {
      case ref: ScStableCodeReferenceElement =>
        val newRef = ScalaPsiElementFactory.createReferenceFromText(newRefText, ref.getManager)
        ref.replace(newRef)
        newRef.bindToElement(clazz)
      case ref: ScReferenceExpression =>
        val newRef = ScalaPsiElementFactory.createExpressionFromText(newRefText, ref.getManager).asInstanceOf[ScReferenceExpression]
        ref.replace(newRef)
        newRef.bindToElement(clazz)
      case _ => return
    }
  }
}
<commit_msg>SCL-4404 Errorneous 'Reference must be prefixed' warning<commit_after>package org.jetbrains.plugins.scala.codeInspection.prefixMutableCollections

import org.jetbrains.plugins.scala.lang.resolve.ScalaResolveResult
import com.intellij.psi.PsiClass
import org.jetbrains.plugins.scala.extensions.toPsiClassExt
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings
import org.jetbrains.plugins.scala.codeInspection.{AbstractFix, AbstractInspection}
import com.intellij.openapi.project.Project
import com.intellij.codeInspection.{ProblemDescriptor, ProblemsHolder}
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScStableCodeReferenceElement, ScReferenceElement}
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScReferenceExpression
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.imports.ScImportSelector

/**
 * @author Alefas
 * @since 26.05.12
 */

class ReferenceMustBePrefixedInspection extends AbstractInspection("ReferenceMustBePrefixed", "Reference must be prefixed") {
  def actionFor(holder: ProblemsHolder) = {
    case ref: ScReferenceElement if ref.qualifier == None && !ref.getParent.isInstanceOf[ScImportSelector] =>
      ref.bind() match {
        case Some(r: ScalaResolveResult) if r.nameShadow.isEmpty =>
          r.getActualElement match {
            case clazz: PsiClass =>
              val qualName = clazz.qualifiedName
              if (ScalaProjectSettings.getInstance(holder.getProject).hasImportWithPrefix(qualName)) {
                holder.registerProblem(ref, getDisplayName, new AddPrefixFix(ref, clazz))
              }
            case _ =>
          }
        case _ =>
      }
  }
}

class AddPrefixFix(ref: ScReferenceElement, clazz: PsiClass) extends AbstractFix("Add prefix to reference", ref) {
  def doApplyFix(project: Project, descriptor: ProblemDescriptor) {
    if (!ref.isValid || !clazz.isValid) return
    val parts = clazz.qualifiedName.split('.')
    if (parts.length < 2) return
    val newRefText = parts.takeRight(2).mkString(".")
    ref match {
      case ref: ScStableCodeReferenceElement =>
        val newRef = ScalaPsiElementFactory.createReferenceFromText(newRefText, ref.getManager)
        ref.replace(newRef)
        newRef.bindToElement(clazz)
      case ref: ScReferenceExpression =>
        val newRef = ScalaPsiElementFactory.createExpressionFromText(newRefText, ref.getManager).asInstanceOf[ScReferenceExpression]
        ref.replace(newRef)
        newRef.bindToElement(clazz)
      case _ => return
    }
  }
}

{'fraction_non_alphanumeric': 0.09747009217493627, 'fraction_numerical': 0.003922337713277113, 'mean_word_length': 4.009823182711198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '836701', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1478, 'n_words': 254}