<commit_before>package org.jetbrains.plugins.scala.debugger

import com.intellij.debugger.SourcePosition
import com.intellij.debugger.engine.SourcePositionProvider
import com.intellij.debugger.impl.{PositionUtil, DebuggerContextImpl}
import com.intellij.debugger.ui.tree.{FieldDescriptor, LocalVariableDescriptor, NodeDescriptor}
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScBindingPattern
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScReferenceExpression
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil

import scala.annotation.tailrec

/**
 * @author Nikolay.Tropin
 */
class ScalaSourcePositionProvider extends SourcePositionProvider {
  override def computeSourcePosition(descriptor: NodeDescriptor,
                                     project: Project,
                                     context: DebuggerContextImpl,
                                     nearest: Boolean): SourcePosition = {

    val contextElement = PositionUtil.getContextElement(context)
    val isScala = contextElement match {
      case _: ScalaPsiElement => true
      case _ => false
    }
    if (!isScala) return null

    descriptor match {
      case _: FieldDescriptor | _: LocalVariableDescriptor =>
      case _ => return null
    }

    val name = descriptor.getName
    resolveReferenceWithName(name, contextElement) match {
      case bp: ScBindingPattern => SourcePosition.createFromElement(bp)
      case _ => null
    }
  }

  @tailrec
  private def resolveReferenceWithName(name: String, context: PsiElement): PsiElement = {
    if (!ScalaNamesUtil.isIdentifier(name)) return null

    val ref = ScalaPsiElementFactory.createExpressionWithContextFromText(name, context, context).asInstanceOf[ScReferenceExpression]

    ref.resolve() match {
      case null if name.contains("$") =>
        val fixedName = name.substring(0, name.indexOf('$'))
        resolveReferenceWithName(fixedName, context)
      case elem => elem
    }
  }
}
<commit_msg>don't try to resolve $outer and x$n in SourcePositionProvider<commit_after>package org.jetbrains.plugins.scala.debugger

import com.intellij.debugger.SourcePosition
import com.intellij.debugger.engine.SourcePositionProvider
import com.intellij.debugger.impl.{DebuggerContextImpl, PositionUtil}
import com.intellij.debugger.ui.tree.{FieldDescriptor, LocalVariableDescriptor, NodeDescriptor}
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScBindingPattern
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScReferenceExpression
import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil

import scala.annotation.tailrec

/**
 * @author Nikolay.Tropin
 */
class ScalaSourcePositionProvider extends SourcePositionProvider {
  override def computeSourcePosition(descriptor: NodeDescriptor,
                                     project: Project,
                                     context: DebuggerContextImpl,
                                     nearest: Boolean): SourcePosition = {

    val contextElement = PositionUtil.getContextElement(context)
    val isScala = contextElement match {
      case _: ScalaPsiElement => true
      case _ => false
    }
    if (!isScala) return null

    descriptor match {
      case _: FieldDescriptor | _: LocalVariableDescriptor =>
      case _ => return null
    }

    val name = descriptor.getName
    resolveReferenceWithName(name, contextElement) match {
      case bp: ScBindingPattern => SourcePosition.createFromElement(bp)
      case _ => null
    }
  }

  @tailrec
  private def resolveReferenceWithName(name: String, context: PsiElement): PsiElement = {
    if (!ScalaNamesUtil.isIdentifier(name)) return null
    if (name == "$outer" || name.startsWith("x$")) return null

    val ref = ScalaPsiElementFactory.createExpressionWithContextFromText(name, context, context).asInstanceOf[ScReferenceExpression]

    ref.resolve() match {
      case null if name.contains("$") =>
        val fixedName = name.substring(0, name.indexOf('$'))
        resolveReferenceWithName(fixedName, context)
      case elem => elem
    }
  }
}

{'fraction_non_alphanumeric': 0.08887906256909131, 'fraction_numerical': 0.00044218439089100157, 'mean_word_length': 3.6069246435845215, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2145882', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1223, 'n_words': 242}