<commit_before>package scala.tools.eclipse
package quickfix

import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
import org.eclipse.jdt.ui.{ISharedImages, JavaUI}
import org.eclipse.jface.text.contentassist.IContextInformation
import org.eclipse.jface.text.{TextUtilities, IDocument}
import org.eclipse.swt.graphics.{Point, Image}

import scala.tools.eclipse.refactoring.EditorHelpers
import scala.tools.eclipse.util.HasLogger
import scala.tools.refactoring.implementations.AddImportStatement

case class ImportCompletionProposal(val importName: String) extends IJavaCompletionProposal with HasLogger {
  
  /**
   * Fixed relevance at 100 for now.
   */
  def getRelevance = 100
  
  /**
   * Inserts the proposed completion into the given document.
   *
   * @param document the document into which to insert the proposed completion
   */
  def apply(document: IDocument) {
    // First, try to insert with an AST transformation, if that fails, use the (old) method
    try {
      applyByASTTransformation(document)
    } catch {
      case t => {
        logger.error("failed to update import by AST transformation, fallback to text implementation", t)
        applyByTextTransformation(document)
      }
    }
  }
  
  /**
   * Inserts the proposed completion into the given document.
   *
   * @param document the document into which to insert the proposed completion
   */
  private def applyByASTTransformation(document: IDocument) {
    
    EditorHelpers.withScalaFileAndSelection { (scalaSourceFile, textSelection) =>
    
      val changes = scalaSourceFile.withSourceFile { (sourceFile, compiler) =>
       
         compiler.askOption {() => 
           
           val refactoring = new AddImportStatement { val global = compiler }
           refactoring.addImport(scalaSourceFile.file, importName)
           
         } getOrElse Nil
        
      }(Nil)
      
      EditorHelpers.applyChangesToFileWhileKeepingSelection(document, textSelection, scalaSourceFile.file, changes)
      
      None
    }
  }
  
  /**
   * Inserts the proposed completion into the given document. (text based transformation)
   *
   * @param document the document into which to insert the proposed completion
   */<commit_msg>Make sure the AST is up to date before adding an import.<commit_after>package scala.tools.eclipse
package quickfix

import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
import org.eclipse.jdt.ui.{ISharedImages, JavaUI}
import org.eclipse.jface.text.contentassist.IContextInformation
import org.eclipse.jface.text.{TextUtilities, IDocument}
import org.eclipse.swt.graphics.{Point, Image}

import scala.tools.eclipse.refactoring.EditorHelpers
import scala.tools.eclipse.util.HasLogger
import scala.tools.refactoring.implementations.AddImportStatement

case class ImportCompletionProposal(val importName: String) extends IJavaCompletionProposal with HasLogger {
  
  /**
   * Fixed relevance at 100 for now.
   */
  def getRelevance = 100
  
  /**
   * Inserts the proposed completion into the given document.
   *
   * @param document the document into which to insert the proposed completion
   */
  def apply(document: IDocument) {
    // First, try to insert with an AST transformation, if that fails, use the (old) method
    try {
      applyByASTTransformation(document)
    } catch {
      case t => {
        logger.error("failed to update import by AST transformation, fallback to text implementation", t)
        applyByTextTransformation(document)
      }
    }
  }
  
  /**
   * Inserts the proposed completion into the given document.
   *
   * @param document the document into which to insert the proposed completion
   */
  private def applyByASTTransformation(document: IDocument) {
    
    EditorHelpers.withScalaFileAndSelection { (scalaSourceFile, textSelection) =>
    
      val changes = scalaSourceFile.withSourceFile { (sourceFile, compiler) =>
       
         val r = new compiler.Response[compiler.Tree]
         compiler.askLoadedTyped(sourceFile, r)
         (r.get match {
           case Right(error) =>
             logger.error(error)
             None
           case _ =>
             compiler.askOption {() =>
               val refactoring = new AddImportStatement { val global = compiler }
               refactoring.addImport(scalaSourceFile.file, importName)
             }
         }) getOrElse Nil
        
      }(Nil)
      
      EditorHelpers.applyChangesToFileWhileKeepingSelection(document, textSelection, scalaSourceFile.file, changes)
      
      None
    }
  }
  
  /**
   * Inserts the proposed completion into the given document. (text based transformation)
   *
   * @param document the document into which to insert the proposed completion
   */
{'fraction_non_alphanumeric': 0.07721518987341772, 'fraction_numerical': 0.002531645569620253, 'mean_word_length': 3.024617996604414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2565928', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1276, 'n_words': 358}