<commit_before>package org.jetbrains.plugins.scala
package lang.refactoring.rename.inplace

import com.intellij.refactoring.rename.inplace.{InplaceRefactoring, MemberInplaceRenamer, MemberInplaceRenameHandler}
import com.intellij.psi._
import com.intellij.openapi.editor.Editor
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScTrait, ScObject, ScClass}
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction
import com.intellij.ui.components.JBList
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.psi.util.PsiUtilBase
import org.jetbrains.plugins.scala.lang.psi.api.base.ScReferenceElement
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.ScNamedElement


/**
 * Nikolay.Tropin
 * 6/20/13
 */
class ScalaMemberInplaceRenameHandler extends MemberInplaceRenameHandler{

  def renameProcessor(element: PsiElement): RenamePsiElementProcessor = {
    val processor = if (element != null) RenamePsiElementProcessor.forElement(element) else null
    if (processor != RenamePsiElementProcessor.DEFAULT) processor else null
  }

  override def isAvailable(element: PsiElement, editor: Editor, file: PsiFile): Boolean = {
    val processor = renameProcessor(element)
    editor.getSettings.isVariableInplaceRenameEnabled && processor != null && processor.canProcessElement(element)
  }

  protected override def createMemberRenamer(substituted: PsiElement,
                                             elementToRename: PsiNameIdentifierOwner,
                                             editor: Editor): MemberInplaceRenamer = {
    substituted match {
      case clazz: PsiClass =>
        val companion = ScalaPsiUtil.getBaseCompanionModule(clazz)
        if (companion.isDefined) new ScalaMemberInplaceRenamer(clazz, companion.get, editor)
        else new ScalaMemberInplaceRenamer(clazz, clazz, editor)
      case subst: PsiNamedElement => new ScalaMemberInplaceRenamer(elementToRename, subst, editor)
      case _ => throw new IllegalArgumentException("Substituted element for renaming has no name")
    }
  }

  override def doRename(elementToRename: PsiElement, editor: Editor, dataContext: DataContext): InplaceRefactoring = {
    def specialMethodPopup(fun: ScFunction) {
      val clazz = fun.containingClass
      val clazzType = clazz match {
        case _: ScObject => "object"
        case _: ScClass => "class"
        case _: ScTrait => "trait"
      }
      val title = ScalaBundle.message("rename.special.method.title")
      val renameClass = ScalaBundle.message("rename.special.method.rename.class", clazzType)
      val cancel = ScalaBundle.message("rename.special.method.cancel")
      val list: JBList = new JBList(renameClass, cancel)
      JBPopupFactory.getInstance.createListPopupBuilder(list)
              .setTitle(title)
              .setMovable(false)
              .setResizable(false)
              .setRequestFocus(true)
              .setItemChoosenCallback(new Runnable {
        def run() {
          list.getSelectedValue match {
            case s: String if s == renameClass =>
              new DialogRenamer().doDialogRename(clazz, editor.getProject, null, editor)
            case s: String if s == cancel =>
          }
        }
      }).createPopup.showInBestPositionFor(editor)
    }

    val atCaret = PsiUtilBase.getElementAtCaret(editor)<commit_msg>Check that language of an element is scala before inplace renaming<commit_after>package org.jetbrains.plugins.scala
package lang.refactoring.rename.inplace

import com.intellij.refactoring.rename.inplace.{InplaceRefactoring, MemberInplaceRenamer, MemberInplaceRenameHandler}
import com.intellij.psi._
import com.intellij.openapi.editor.Editor
import com.intellij.refactoring.rename.RenamePsiElementProcessor
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScTrait, ScObject, ScClass}
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiUtil
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction
import com.intellij.ui.components.JBList
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.psi.util.PsiUtilBase
import org.jetbrains.plugins.scala.lang.psi.api.base.ScReferenceElement
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.ScNamedElement

/**
 * Nikolay.Tropin
 * 6/20/13
 */
class ScalaMemberInplaceRenameHandler extends MemberInplaceRenameHandler{

  def renameProcessor(element: PsiElement): RenamePsiElementProcessor = {
    val processor = if (element != null && element.getLanguage.getID == "Scala") RenamePsiElementProcessor.forElement(element) else null
    if (processor != RenamePsiElementProcessor.DEFAULT) processor else null
  }

  override def isAvailable(element: PsiElement, editor: Editor, file: PsiFile): Boolean = {
    val processor = renameProcessor(element)
    editor.getSettings.isVariableInplaceRenameEnabled && processor != null && processor.canProcessElement(element)
  }

  protected override def createMemberRenamer(substituted: PsiElement,
                                             elementToRename: PsiNameIdentifierOwner,
                                             editor: Editor): MemberInplaceRenamer = {
    substituted match {
      case clazz: PsiClass =>
        val companion = ScalaPsiUtil.getBaseCompanionModule(clazz)
        if (companion.isDefined) new ScalaMemberInplaceRenamer(clazz, companion.get, editor)
        else new ScalaMemberInplaceRenamer(clazz, clazz, editor)
      case subst: PsiNamedElement => new ScalaMemberInplaceRenamer(elementToRename, subst, editor)
      case _ => throw new IllegalArgumentException("Substituted element for renaming has no name")
    }
  }

  override def doRename(elementToRename: PsiElement, editor: Editor, dataContext: DataContext): InplaceRefactoring = {
    def specialMethodPopup(fun: ScFunction): Unit = {
      val clazz = fun.containingClass
      val clazzType = clazz match {
        case _: ScObject => "object"
        case _: ScClass => "class"
        case _: ScTrait => "trait"
      }
      val title = ScalaBundle.message("rename.special.method.title")
      val renameClass = ScalaBundle.message("rename.special.method.rename.class", clazzType)
      val cancel = ScalaBundle.message("rename.special.method.cancel")
      val list: JBList = new JBList(renameClass, cancel)
      JBPopupFactory.getInstance.createListPopupBuilder(list)
              .setTitle(title)
              .setMovable(false)
              .setResizable(false)
              .setRequestFocus(true)
              .setItemChoosenCallback(new Runnable {
        def run(): Unit = {
          list.getSelectedValue match {
            case s: String if s == renameClass =>
              new DialogRenamer().doDialogRename(clazz, editor.getProject, null, editor)
            case s: String if s == cancel =>
          }
        }
      }).createPopup.showInBestPositionFor(editor)
    }

    val atCaret = PsiUtilBase.getElementAtCaret(editor)
{'fraction_non_alphanumeric': 0.08916132627472834, 'fraction_numerical': 0.0013931457230426303, 'mean_word_length': 3.5264817150063053, 'pattern_counts': {'":': 0, '<': 3, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3408733', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2050, 'n_words': 405}