<commit_before>package scala.tools.eclipse.quickfix

import java.util.ArrayList

import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.mutable.Buffer
import scala.tools.eclipse.javaelements.ScalaCompilationUnit
import scala.tools.eclipse.testsetup.TestProjectSetup
import scala.tools.nsc.interactive.Response

import org.eclipse.core.runtime.CoreException
import org.eclipse.core.runtime.IStatus
import org.eclipse.jdt.core.compiler.IProblem
import org.eclipse.jdt.internal.ui.text.correction.AssistContext
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor
import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation
import org.eclipse.jdt.ui.JavaUI
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
import org.eclipse.jdt.ui.text.java.IProblemLocation
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue

object QuickFixesTests extends TestProjectSetup("quickfix") {

  def assertStatusOk(status: IStatus) {
    if (!status.isOK()) {
      if (status.getException() == null) { // find a status with an exception
        val children = status.getChildren();
        for (child <- children) {
          if (child.getException() != null) {
            throw new CoreException(child);
          }
        }
      }
    }
  }

  def assertNumberOfProblems(nProblems: Int, problems: Array[IProblem]) {
    // check if numbers match but we want reasonable error message
    if (problems.length != nProblems) {
      val buf = new StringBuffer("Wrong number of problems, is: ")
      buf.append(problems.length).append(", expected: ").append(nProblems).append('\n')
      for (problem <- problems) {
        buf.append(problem).append(" at ")
        buf.append('[').append(problem.getSourceStart()).append(" ,").append(problem.getSourceEnd()).append(']')
        buf.append('\n')
      }

      assertEquals(buf.toString, nProblems, problems.length)
    }
  }
  
  def withQuickFixes(pathToSource: String)(expectedQuickFixes: String*) {
    withManyQuickFixesPerLine(pathToSource)(expectedQuickFixes.map(List(_)).toList)
  }

  def withManyQuickFixesPerLine(pathToSource: String)(expectedQuickFixesList: List[List[String]]) {
    // get our compilation unit
    val unit = compilationUnit(pathToSource).asInstanceOf[ScalaCompilationUnit]

    // first, 'open' the file by telling the compiler to load it
    project.withSourceFile(unit) { (src, compiler) =>

      // do a compiler reload before checking for problems
      val dummy = new Response[Unit]
      compiler.askReload(List(src), dummy)
      dummy.get

      val problems = compiler.problemsOf(unit)
      assertTrue("No problems found.", problems.size > 0)
      assertNumberOfProblems(expectedQuickFixesList.size, problems.toArray)
      
      val editor = JavaUI.openInEditor(unit.getCompilationUnit)
      
      // check each problem quickfix
      for ((problem, expectedQuickFixes) <- problems zip expectedQuickFixesList) {
        // here we will accumulate proposals
        var proposals: ArrayList[IJavaCompletionProposal] = new ArrayList()

        // get all corrections for the problem
        val offset = problem.getSourceStart<commit_msg>Fix whitespace change<commit_after>package scala.tools.eclipse.quickfix

import java.util.ArrayList

import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.mutable.Buffer
import scala.tools.eclipse.javaelements.ScalaCompilationUnit
import scala.tools.eclipse.testsetup.TestProjectSetup
import scala.tools.nsc.interactive.Response

import org.eclipse.core.runtime.CoreException
import org.eclipse.core.runtime.IStatus
import org.eclipse.jdt.core.compiler.IProblem
import org.eclipse.jdt.internal.ui.text.correction.AssistContext
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor
import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation
import org.eclipse.jdt.ui.JavaUI
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
import org.eclipse.jdt.ui.text.java.IProblemLocation
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue

object QuickFixesTests extends TestProjectSetup("quickfix") {

  def assertStatusOk(status: IStatus) {
    if (!status.isOK()) {
      if (status.getException() == null) { // find a status with an exception
        val children = status.getChildren();
        for (child <- children) {
          if (child.getException() != null) {
            throw new CoreException(child);
          }
        }
      }
    }
  }

  def assertNumberOfProblems(nProblems: Int, problems: Array[IProblem]) {
    // check if numbers match but we want reasonable error message
    if (problems.length != nProblems) {
      val buf = new StringBuffer("Wrong number of problems, is: ")
      buf.append(problems.length).append(", expected: ").append(nProblems).append('\n')
      for (problem <- problems) {
        buf.append(problem).append(" at ")
        buf.append('[').append(problem.getSourceStart()).append(" ,").append(problem.getSourceEnd()).append(']')
        buf.append('\n')
      }

      assertEquals(buf.toString, nProblems, problems.length)
    }
  }
  
  def withQuickFixes(pathToSource: String)(expectedQuickFixes: String*) {
    withManyQuickFixesPerLine(pathToSource)(expectedQuickFixes.map(List(_)).toList)
  }

  def withManyQuickFixesPerLine(pathToSource: String)(expectedQuickFixesList: List[List[String]]) {
    // get our compilation unit
    val unit = compilationUnit(pathToSource).asInstanceOf[ScalaCompilationUnit]

    // first, 'open' the file by telling the compiler to load it
    project.withSourceFile(unit) { (src, compiler) =>

      // do a compiler reload before checking for problems
      val dummy = new Response[Unit]
      compiler.askReload(List(src), dummy)
      dummy.get

      val problems = compiler.problemsOf(unit)
      assertTrue("No problems found.", problems.size > 0)
      assertNumberOfProblems(expectedQuickFixesList.size, problems.toArray)

      val editor = JavaUI.openInEditor(unit.getCompilationUnit)

      // check each problem quickfix
      for ((problem, expectedQuickFixes) <- problems zip expectedQuickFixesList) {
        // here we will accumulate proposals
        var proposals: ArrayList[IJavaCompletionProposal] = new ArrayList()

        // get all corrections for the problem
        val offset = problem.getSourceStart
{'fraction_non_alphanumeric': 0.10732165206508135, 'fraction_numerical': 0.00031289111389236547, 'mean_word_length': 3.781600598354525, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4234027', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1815, 'n_words': 387}