<commit_before>package org.jetbrains.plugins.scala
package lang
package refactoring
package util

import _root_.com.intellij.codeInsight.unwrap.ScopeHighlighter
import _root_.com.intellij.openapi.ui.popup.{LightweightWindowEvent, JBPopupAdapter, JBPopupFactory}
import _root_.java.util.Comparator
import _root_.javax.swing.event.{ListSelectionEvent, ListSelectionListener}
import _root_.java.awt.Component
import _root_.javax.swing.{DefaultListCellRenderer, DefaultListModel, JList}
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.codeInsight.highlighting.HighlightManager
import com.intellij.openapi.editor.colors.{EditorColorsManager, EditorColors}

import com.intellij.openapi.util.TextRange
import com.intellij.psi._
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.{ScCaseClause, ScLiteralPattern, ScReferencePattern}
import org.jetbrains.plugins.scala.lang.psi.types.result.TypingContext
import psi.api.expr._
import psi.impl.ScalaPsiElementFactory
import com.intellij.codeInsight.PsiEquivalenceUtil
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.{ScClassParameter, ScParameter}
import scala.collection.mutable.ArrayBuffer
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.openapi.vfs.ReadonlyStatusHandler
import com.intellij.openapi.project.Project
import org.jetbrains.plugins.scala.util.ScalaUtils
import psi.types._
import psi.api.statements.ScFunction
import lang.resolve.ScalaResolveResult
import psi.api.expr.xml.ScXmlExpr
import org.jetbrains.plugins.scala.lang.psi.{ScalaPsiUtil, ScalaPsiElement}
import org.jetbrains.plugins.scala.lang.psi.api.base.ScLiteral
import com.intellij.openapi.editor.{VisualPosition, Editor}
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.plugins.scala.lang.psi.api.{ScalaRecursiveElementVisitor, ScalaFile}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.ScEarlyDefinitions
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.types.ScDesignatorType
import org.jetbrains.plugins.scala.lang.psi.types.ScFunctionType
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScMember, ScTemplateDefinition, ScClass, ScTypeDefinition}
import com.intellij.openapi.application.ApplicationManager
import com.intellij.refactoring.util.CommonRefactoringUtil
import com.intellij.refactoring.HelpID
import java.util
import scala.annotation.tailrec
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.templates.ScTemplateBody


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

object ScalaRefactoringUtil {      
  def trimSpacesAndComments(editor: Editor, file: PsiFile, trimComments: Boolean = true) {
    var start = editor.getSelectionModel.getSelectionStart
    var end = editor.getSelectionModel.getSelectionEnd
    if (start == end) return
    while (file.findElementAt(start).isInstanceOf[PsiWhiteSpace] ||
            (file.findElementAt(start).isInstanceOf[PsiComment] && trimComments) ||
            file.getText.charAt(start) == '\n' ||
            file.getText.charAt(start) == ' ') start = start + 1
    while (file.findElementAt(end - 1).isInstanceOf[PsiWhiteSpace] ||
            (file.findElementAt(end - 1).isInstanceOf[PsiComment] && trimComments) ||
           file.getText.charAt(end - 1) == '\n' ||
           file.getText.charAt(end - 1) == ' ') end = end - 1
    editor.getSelectionModel.setSelection(start, end)
  }

  def getExprFrom(expr: ScExpression): ScExpression = {
    var e = unparExpr(expr)
    e match {
      case x: ScReferenceExpression => {
        x.resolve() match {
          case _: ScReferencePattern => return e
          case _ =>
        }
      }
      case _ =>
    }
    var hasNlToken = false
    val text = e.getText
    var i = text.length - 1
    while (i >= 0 && (text(i) == ' ' || text(i) == '\n')) {
      if (text(i) == '\n') hasNlToken = true
      i = i - 1
    }
    if (hasNlToken) e = ScalaPsiElementFactory.createExpressionFromText(text.substring(0, i + 1), e.getManager)
    e.getParent match {
      case x: ScMethodCall if x.args.exprs.size > 0 =>
        ScalaPsiElementFactory.createExpressionFromText(e.getText + " _", e.getManager)
      case _ => e
    }
  }

  def addPossibleTypes(scType: ScType, expr: ScExpression): Array[ScType] = {
    val types = new ArrayBuffer[ScType]
    if (scType != null && scType != psi.types.Unit) types += scType
    expr.getTypeWithoutImplicits(TypingContext.empty).foreach(types +=)
    expr.getTypeIgnoreBaseType(TypingContext.empty).foreach(types +=)
    if (scType == psi.types.Unit) types += scType
    if (types.isEmpty) types += psi.types.Any
    types.toArray
  }

  def getExpression(project: Project, editor: Editor, file: PsiFile, startOffset: Int, endOffset: Int): Option[(ScExpression, ScType)] = {
    val element = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, classOf[ScExpression])
    if (element == null || element.getTextRange.getStartOffset != startOffset || element.getTextRange.getEndOffset != endOffset) {
      val rangeText = file.getText.substring(startOffset, endOffset)
      val expr = ScalaPsiElementFactory.createOptionExpressionFromText(rangeText, file.getManager)
      expr match {
        case Some(expression: ScInfixExpr) => {
          val op1 = expression.operation
          if (ScalaRefactoringUtil.ensureFileWritable(project, file)) {
            var res: Option[(ScExpression, ScType)] = None
            ScalaUtils.runWriteAction(new Runnable {
              def run() {
                val document = editor.getDocument
                document.insertString(endOffset, ")")
                document.insertString(startOffset, "(")
                val documentManager: PsiDocumentManager = PsiDocumentManager.getInstance(project)
                documentManager.commitDocument(document)
                val newOpt = getExpression(project, editor, file, startOffset, endOffset + 2)
                newOpt match {
                  case Some((expression: ScExpression, typez)) => {
                    expression.getParent match {
                      case inf: ScInfixExpr => {
                        val op2 = inf.operation
                        import parser.util.ParserUtils.priority<commit_msg>Bug with suggesting Unit type in introduce variable fixed #SCL-2149 fixed<commit_after>package org.jetbrains.plugins.scala
package lang
package refactoring
package util

import _root_.com.intellij.codeInsight.unwrap.ScopeHighlighter
import _root_.com.intellij.openapi.ui.popup.{LightweightWindowEvent, JBPopupAdapter, JBPopupFactory}
import _root_.java.util.Comparator
import _root_.javax.swing.event.{ListSelectionEvent, ListSelectionListener}
import _root_.java.awt.Component
import _root_.javax.swing.{DefaultListCellRenderer, DefaultListModel, JList}
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.codeInsight.highlighting.HighlightManager
import com.intellij.openapi.editor.colors.{EditorColorsManager, EditorColors}

import com.intellij.openapi.util.TextRange
import com.intellij.psi._
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.{ScCaseClause, ScLiteralPattern, ScReferencePattern}
import org.jetbrains.plugins.scala.lang.psi.types.result.TypingContext
import psi.api.expr._
import psi.impl.ScalaPsiElementFactory
import com.intellij.codeInsight.PsiEquivalenceUtil
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.{ScClassParameter, ScParameter}
import scala.collection.mutable.ArrayBuffer
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.openapi.vfs.ReadonlyStatusHandler
import com.intellij.openapi.project.Project
import org.jetbrains.plugins.scala.util.ScalaUtils
import psi.types._
import psi.api.statements.ScFunction
import lang.resolve.ScalaResolveResult
import psi.api.expr.xml.ScXmlExpr
import org.jetbrains.plugins.scala.lang.psi.{ScalaPsiUtil, ScalaPsiElement}
import org.jetbrains.plugins.scala.lang.psi.api.base.ScLiteral
import com.intellij.openapi.editor.{VisualPosition, Editor}
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.plugins.scala.lang.psi.api.{ScalaRecursiveElementVisitor, ScalaFile}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.ScEarlyDefinitions
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.lang.psi.types.ScDesignatorType
import org.jetbrains.plugins.scala.lang.psi.types.ScFunctionType
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScMember, ScTemplateDefinition, ScClass, ScTypeDefinition}
import com.intellij.openapi.application.ApplicationManager
import com.intellij.refactoring.util.CommonRefactoringUtil
import com.intellij.refactoring.HelpID
import java.util
import scala.annotation.tailrec
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.templates.ScTemplateBody


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

object ScalaRefactoringUtil {      
  def trimSpacesAndComments(editor: Editor, file: PsiFile, trimComments: Boolean = true) {
    var start = editor.getSelectionModel.getSelectionStart
    var end = editor.getSelectionModel.getSelectionEnd
    if (start == end) return
    while (file.findElementAt(start).isInstanceOf[PsiWhiteSpace] ||
            (file.findElementAt(start).isInstanceOf[PsiComment] && trimComments) ||
            file.getText.charAt(start) == '\n' ||
            file.getText.charAt(start) == ' ') start = start + 1
    while (file.findElementAt(end - 1).isInstanceOf[PsiWhiteSpace] ||
            (file.findElementAt(end - 1).isInstanceOf[PsiComment] && trimComments) ||
           file.getText.charAt(end - 1) == '\n' ||
           file.getText.charAt(end - 1) == ' ') end = end - 1
    editor.getSelectionModel.setSelection(start, end)
  }

  def getExprFrom(expr: ScExpression): ScExpression = {
    var e = unparExpr(expr)
    e match {
      case x: ScReferenceExpression => {
        x.resolve() match {
          case _: ScReferencePattern => return e
          case _ =>
        }
      }
      case _ =>
    }
    var hasNlToken = false
    val text = e.getText
    var i = text.length - 1
    while (i >= 0 && (text(i) == ' ' || text(i) == '\n')) {
      if (text(i) == '\n') hasNlToken = true
      i = i - 1
    }
    if (hasNlToken) e = ScalaPsiElementFactory.createExpressionFromText(text.substring(0, i + 1), e.getManager)
    e.getParent match {
      case x: ScMethodCall if x.args.exprs.size > 0 =>
        ScalaPsiElementFactory.createExpressionFromText(e.getText + " _", e.getManager)
      case _ => e
    }
  }

  def addPossibleTypes(scType: ScType, expr: ScExpression): Array[ScType] = {
    val types = new ArrayBuffer[ScType]
    if (scType != null) types += scType
    expr.getTypeWithoutImplicits(TypingContext.empty).foreach(types +=)
    expr.getTypeIgnoreBaseType(TypingContext.empty).foreach(types +=)
    if (types.isEmpty) types += psi.types.Any
    val unit = psi.types.Unit
    val result = if (types.contains(unit)) (types.distinct - unit) :+ unit else types.distinct
    result.toArray
  }

  def getExpression(project: Project, editor: Editor, file: PsiFile, startOffset: Int, endOffset: Int): Option[(ScExpression, ScType)] = {
    val element = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, classOf[ScExpression])
    if (element == null || element.getTextRange.getStartOffset != startOffset || element.getTextRange.getEndOffset != endOffset) {
      val rangeText = file.getText.substring(startOffset, endOffset)
      val expr = ScalaPsiElementFactory.createOptionExpressionFromText(rangeText, file.getManager)
      expr match {
        case Some(expression: ScInfixExpr) => {
          val op1 = expression.operation
          if (ScalaRefactoringUtil.ensureFileWritable(project, file)) {
            var res: Option[(ScExpression, ScType)] = None
            ScalaUtils.runWriteAction(new Runnable {
              def run() {
                val document = editor.getDocument
                document.insertString(endOffset, ")")
                document.insertString(startOffset, "(")
                val documentManager: PsiDocumentManager = PsiDocumentManager.getInstance(project)
                documentManager.commitDocument(document)
                val newOpt = getExpression(project, editor, file, startOffset, endOffset + 2)
                newOpt match {
                  case Some((expression: ScExpression, typez)) => {
                    expression.getParent match {
                      case inf: ScInfixExpr => {
                        val op2 = inf.operation
                        import parser.util.ParserUtils.priority
{'fraction_non_alphanumeric': 0.10680379746835443, 'fraction_numerical': 0.003955696202531646, 'mean_word_length': 3.666297526762643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3319560', 'n_tokens_mistral': 3895, 'n_tokens_neox': 3759, 'n_words': 688}