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

import com.intellij.debugger.NoDataException;
import com.intellij.debugger.PositionManager;
import com.intellij.debugger.SourcePosition;
import com.intellij.debugger.engine.CompoundPositionManager;
import com.intellij.debugger.engine.DebugProcess;
import com.intellij.debugger.engine.DebugProcessImpl;
import com.intellij.debugger.engine.jdi.VirtualMachineProxy;
import com.intellij.debugger.requests.ClassPrepareRequestor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.DirectoryIndex;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.Processor;
import com.intellij.util.Query;
import com.intellij.util.containers.HashSet;
import com.sun.jdi.AbsentInformationException;
import com.sun.jdi.Location;
import com.sun.jdi.ReferenceType;
import com.sun.jdi.request.ClassPrepareRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.scala.ScalaLoader;
import org.jetbrains.plugins.scala.caches.ScalaCachesManager;
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement;
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile;
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScForStatement;
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScFunctionExpr;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.templates.ScExtendsBlock;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTrait;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTypeDefinition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author ilyas
 */
public class ScalaPositionManager implements PositionManager {

  private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.engine.PositionManagerImpl");

  private final DebugProcess myDebugProcess;

  public ScalaPositionManager(DebugProcess debugProcess) {
    myDebugProcess = debugProcess;
  }

  public DebugProcess getDebugProcess() {
    return myDebugProcess;
  }

  @NotNull
  public List<Location> locationsOfLine(ReferenceType type,
                                        SourcePosition position) throws NoDataException {
    try {
      int line = position.getLine() + 1;
      List<Location> locations = getDebugProcess().getVirtualMachineProxy().versionHigher("1.4") ?
          type.locationsOfLine(DebugProcessImpl.JAVA_STRATUM, null, line) : type.locationsOfLine(line);
      if (locations == null || locations.isEmpty()) throw new NoDataException();
      return locations;
    }
    catch (AbsentInformationException e) {
      throw new NoDataException();
    }
  }

  private ScalaPsiElement findReferenceTypeSourceImage(SourcePosition position) {
    PsiFile file = position.getFile();
    if (!(file instanceof ScalaFile)) return null;
    PsiElement element = file.findElementAt(position.getOffset());
    if (element == null) return null;
    while (true) {
      if (element == null) break;
      if (element instanceof ScForStatement || element instanceof ScTypeDefinition || element instanceof ScFunctionExpr) break;
      if (element instanceof ScExtendsBlock && ((ScExtendsBlock) element).isAnonymousClass()) break;
      element = element.getParent();
    }

    return (ScalaPsiElement) element;
  }

  private ScTypeDefinition findEnclosingTypeDefinition(SourcePosition position) {
    PsiFile file = position.getFile();
    if (!(file instanceof ScalaFile)) return null;
    PsiElement element = file.findElementAt(position.getOffset());
    if (element == null) return null;
    return PsiTreeUtil.getParentOfType(element, ScTypeDefinition.class);
  }

  private static String getSpecificName(String name, Class<? extends PsiClass> clazzClass) {
    if (ScObject.class.isAssignableFrom(clazzClass)) return name + "$";
    if (ScTrait.class.isAssignableFrom(clazzClass)) return name + "$class";
    return name;
  }


  public ClassPrepareRequest createPrepareRequest(final ClassPrepareRequestor requestor, final SourcePosition position) throws NoDataException {
    ScalaPsiElement sourceImage = findReferenceTypeSourceImage(position);
    String qName = null;
    if (sourceImage instanceof ScTypeDefinition) {
      qName = getSpecificName(((ScTypeDefinition) sourceImage).getQualifiedName(), ((ScTypeDefinition) sourceImage).getClass());
    } else if (sourceImage instanceof ScFunctionExpr || sourceImage instanceof ScForStatement
        || sourceImage instanceof ScExtendsBlock) {
      ScTypeDefinition typeDefinition = findEnclosingTypeDefinition(position);
      if (typeDefinition != null) {
        final String fqn = typeDefinition.getQualifiedName();
        qName = fqn + "$*";
      }
    }
    // Enclosinc closure not found
    if (qName == null) {
      ScTypeDefinition typeDefinition = findEnclosingTypeDefinition(position);<commit_msg>yet another one debugger fix<commit_after>package org.jetbrains.plugins.scala.debugger;

import com.intellij.debugger.NoDataException;
import com.intellij.debugger.PositionManager;
import com.intellij.debugger.SourcePosition;
import com.intellij.debugger.engine.CompoundPositionManager;
import com.intellij.debugger.engine.DebugProcess;
import com.intellij.debugger.engine.DebugProcessImpl;
import com.intellij.debugger.engine.jdi.VirtualMachineProxy;
import com.intellij.debugger.requests.ClassPrepareRequestor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.DirectoryIndex;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.Processor;
import com.intellij.util.Query;
import com.intellij.util.containers.HashSet;
import com.sun.jdi.AbsentInformationException;
import com.sun.jdi.Location;
import com.sun.jdi.ReferenceType;
import com.sun.jdi.request.ClassPrepareRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.scala.ScalaLoader;
import org.jetbrains.plugins.scala.caches.ScalaCachesManager;
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement;
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile;
import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.ScCaseClauses;
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScForStatement;
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScFunctionExpr;
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScBlockExpr;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.templates.ScExtendsBlock;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTrait;
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTypeDefinition;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author ilyas
 */
public class ScalaPositionManager implements PositionManager {

  private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.engine.PositionManagerImpl");

  private final DebugProcess myDebugProcess;

  public ScalaPositionManager(DebugProcess debugProcess) {
    myDebugProcess = debugProcess;
  }

  public DebugProcess getDebugProcess() {
    return myDebugProcess;
  }

  @NotNull
  public List<Location> locationsOfLine(ReferenceType type,
                                        SourcePosition position) throws NoDataException {
    try {
      int line = position.getLine() + 1;
      List<Location> locations = getDebugProcess().getVirtualMachineProxy().versionHigher("1.4") ?
          type.locationsOfLine(DebugProcessImpl.JAVA_STRATUM, null, line) : type.locationsOfLine(line);
      if (locations == null || locations.isEmpty()) throw new NoDataException();
      return locations;
    }
    catch (AbsentInformationException e) {
      throw new NoDataException();
    }
  }

  private ScalaPsiElement findReferenceTypeSourceImage(SourcePosition position) {
    PsiFile file = position.getFile();
    if (!(file instanceof ScalaFile)) return null;
    PsiElement element = file.findElementAt(position.getOffset());
    if (element == null) return null;
    while (true) {
      if (element == null) break;
      if (element instanceof ScForStatement || element instanceof ScTypeDefinition || element instanceof ScFunctionExpr)
        break;
      if (element instanceof ScExtendsBlock && ((ScExtendsBlock) element).isAnonymousClass()) break;
      if (element instanceof ScCaseClauses && element.getParent() instanceof ScBlockExpr) break;
      element = element.getParent();
    }

    return (ScalaPsiElement) element;
  }

  private ScTypeDefinition findEnclosingTypeDefinition(SourcePosition position) {
    PsiFile file = position.getFile();
    if (!(file instanceof ScalaFile)) return null;
    PsiElement element = file.findElementAt(position.getOffset());
    if (element == null) return null;
    return PsiTreeUtil.getParentOfType(element, ScTypeDefinition.class);
  }

  private static String getSpecificName(String name, Class<? extends PsiClass> clazzClass) {
    if (ScObject.class.isAssignableFrom(clazzClass)) return name + "$";
    if (ScTrait.class.isAssignableFrom(clazzClass)) return name + "$class";
    return name;
  }


  public ClassPrepareRequest createPrepareRequest(final ClassPrepareRequestor requestor, final SourcePosition position) throws NoDataException {
    ScalaPsiElement sourceImage = findReferenceTypeSourceImage(position);
    String qName = null;
    if (sourceImage instanceof ScTypeDefinition) {
      qName = getSpecificName(((ScTypeDefinition) sourceImage).getQualifiedName(), ((ScTypeDefinition) sourceImage).getClass());
    } else if (sourceImage instanceof ScFunctionExpr ||
        sourceImage instanceof ScForStatement ||
        sourceImage instanceof ScExtendsBlock ||
        sourceImage instanceof ScCaseClauses && sourceImage.getParent() instanceof ScBlockExpr) {
      ScTypeDefinition typeDefinition = findEnclosingTypeDefinition(position);
      if (typeDefinition != null) {
        final String fqn = typeDefinition.getQualifiedName();
        qName = fqn + "$*";
      }
    }
    // Enclosinc closure not found
    if (qName == null) {
      ScTypeDefinition typeDefinition = findEnclosingTypeDefinition(position);
{'fraction_non_alphanumeric': 0.09897641299510458, 'fraction_numerical': 0.000534045393858478, 'mean_word_length': 4.885804085908853, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4230406', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3203, 'n_words': 631}