<commit_before>package org.jetbrains.plugins
package scala
package annotator
package intention


import org.jetbrains.plugins.scala.lang.psi.api.toplevel.packaging.ScPackaging
import com.intellij.codeInsight.hint.{HintManager, HintManagerImpl, QuestionAction}
import com.intellij.codeInsight.CodeInsightUtilBase

import com.intellij.ide.util.FQNameCellRenderer
import com.intellij.openapi.editor.{LogicalPosition, Editor}
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.command.CommandProcessor
import com.intellij.psi._
import lang.psi.api.toplevel.templates.ScTemplateBody
import lang.psi.api.toplevel.typedef.{ScTypeDefinition, ScObject, ScClass}
import lang.psi.impl.toplevel.typedef.ScTypeDefinitionImpl
import lang.resolve.ResolveUtils
import java.awt.Point
import com.intellij.psi.util.PsiTreeUtil
import lang.psi.ScImportsHolder
import scala.util.ScalaUtils
import com.intellij.openapi.application.ApplicationManager
import com.intellij.codeInspection.HintAction
import com.intellij.openapi.project.Project
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
import lang.formatting.settings.ScalaCodeStyleSettings
import lang.psi.api.base.ScReferenceElement
import lang.psi.api.ScalaFile
import com.intellij.codeInsight.completion.JavaCompletionUtil
import com.intellij.openapi.ui.popup.util.BaseListPopupStep
import com.intellij.codeInsight.daemon.QuickFixBundle
import com.intellij.util.ObjectUtils
import javax.swing.{Icon, JList}
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction
import com.intellij.openapi.ui.popup.{JBPopupFactory, PopupStep, PopupChooserBuilder}
import lang.psi.api.expr.{ScInfixExpr, ScPrefixExpr, ScPostfixExpr, ScMethodCall}

/**
 * User: <NAME>
 * Date: 15.07.2009
 */

class ScalaImportClassFix(private var classes: Array[PsiClass], ref: ScReferenceElement) extends {
    val project = ref.getProject
  } with  HintAction {
  def getText = ScalaBundle.message("import.with", classes(0).getQualifiedName)

  def getFamilyName = ScalaBundle.message("import.class")

  def isAvailable(project: Project, editor: Editor, file: PsiFile) = file.isInstanceOf[ScalaFile]

  def invoke(project: Project, editor: Editor, file: PsiFile) = {
    CommandProcessor.getInstance().runUndoTransparentAction(new Runnable {
      def run() {
        if (!ref.isValid) return
        classes = ScalaImportClassFix.getClasses(ref, project)
        new ScalaAddImportAction(editor, classes, ref).execute()
      }
    })
  }

  private val scalaSettings: ScalaCodeStyleSettings = CodeStyleSettingsManager.getSettings(project).getCustomSettings(classOf[ScalaCodeStyleSettings])
  def showHint(editor: Editor): Boolean = {
    if (!ref.isValid) return false
    ref.getContext match {
      case ref: ScReferenceElement => return false
      case postf: ScPostfixExpr if postf.operation == ref => return false
      case pref: ScPrefixExpr if pref.operation == ref => return false
      case inf: ScInfixExpr if inf.operation == ref => return false
      case _ => {
        classes = ScalaImportClassFix.getClasses(ref, project)
        classes.length match {
          case 0 => return false
          case 1 if scalaSettings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY  &&
                  !caretNear(editor) => {
            CommandProcessor.getInstance().runUndoTransparentAction(new Runnable {
              def run() {
                new ScalaAddImportAction(editor, classes, ref).execute()
              }
            })
            return false
          }
          case _ => {
            fixesAction(editor)
            return true
          }
        }
      }
    }
  }

  private def caretNear(editor: Editor): Boolean = ref.getTextRange.grown(1).contains(editor.getCaretModel.getOffset)<commit_msg>Fixed import hint for expressions qualifier.<commit_after>package org.jetbrains.plugins
package scala
package annotator
package intention


import org.jetbrains.plugins.scala.lang.psi.api.toplevel.packaging.ScPackaging
import com.intellij.codeInsight.hint.{HintManager, HintManagerImpl, QuestionAction}
import com.intellij.codeInsight.CodeInsightUtilBase

import com.intellij.ide.util.FQNameCellRenderer
import com.intellij.openapi.editor.{LogicalPosition, Editor}
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.command.CommandProcessor
import com.intellij.psi._
import lang.psi.api.toplevel.templates.ScTemplateBody
import lang.psi.api.toplevel.typedef.{ScTypeDefinition, ScObject, ScClass}
import lang.psi.impl.toplevel.typedef.ScTypeDefinitionImpl
import lang.resolve.ResolveUtils
import java.awt.Point
import com.intellij.psi.util.PsiTreeUtil
import lang.psi.ScImportsHolder
import scala.util.ScalaUtils
import com.intellij.openapi.application.ApplicationManager
import com.intellij.codeInspection.HintAction
import com.intellij.openapi.project.Project
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
import lang.formatting.settings.ScalaCodeStyleSettings
import lang.psi.api.base.ScReferenceElement
import lang.psi.api.ScalaFile
import com.intellij.codeInsight.completion.JavaCompletionUtil
import com.intellij.openapi.ui.popup.util.BaseListPopupStep
import com.intellij.codeInsight.daemon.QuickFixBundle
import com.intellij.util.ObjectUtils
import javax.swing.{Icon, JList}
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction
import com.intellij.openapi.ui.popup.{JBPopupFactory, PopupStep, PopupChooserBuilder}
import lang.psi.api.expr.{ScInfixExpr, ScPrefixExpr, ScPostfixExpr, ScMethodCall}

/**
 * User: <NAME>
 * Date: 15.07.2009
 */

class ScalaImportClassFix(private var classes: Array[PsiClass], ref: ScReferenceElement) extends {
    val project = ref.getProject
  } with  HintAction {
  def getText = ScalaBundle.message("import.with", classes(0).getQualifiedName)

  def getFamilyName = ScalaBundle.message("import.class")

  def isAvailable(project: Project, editor: Editor, file: PsiFile) = file.isInstanceOf[ScalaFile]

  def invoke(project: Project, editor: Editor, file: PsiFile) = {
    CommandProcessor.getInstance().runUndoTransparentAction(new Runnable {
      def run() {
        if (!ref.isValid) return
        classes = ScalaImportClassFix.getClasses(ref, project)
        new ScalaAddImportAction(editor, classes, ref).execute()
      }
    })
  }

  private val scalaSettings: ScalaCodeStyleSettings = CodeStyleSettingsManager.getSettings(project).getCustomSettings(classOf[ScalaCodeStyleSettings])
  def showHint(editor: Editor): Boolean = {
    if (!ref.isValid) return false
    if (ref.qualifier != None) return false
    ref.getContext match {
      case postf: ScPostfixExpr if postf.operation == ref => return false
      case pref: ScPrefixExpr if pref.operation == ref => return false
      case inf: ScInfixExpr if inf.operation == ref => return false
      case _ => {
        classes = ScalaImportClassFix.getClasses(ref, project)
        classes.length match {
          case 0 => return false
          case 1 if scalaSettings.ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY  &&
                  !caretNear(editor) => {
            CommandProcessor.getInstance().runUndoTransparentAction(new Runnable {
              def run() {
                new ScalaAddImportAction(editor, classes, ref).execute()
              }
            })
            return false
          }
          case _ => {
            fixesAction(editor)
            return true
          }
        }
      }
    }
  }

  private def caretNear(editor: Editor): Boolean = ref.getTextRange.grown(1).contains(editor.getCaretModel.getOffset)
{'fraction_non_alphanumeric': 0.0927494046043927, 'fraction_numerical': 0.0031754432389521038, 'mean_word_length': 4.02593085106383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, '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': '261645', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2247, 'n_words': 430}