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

import java.util
import java.io.File
import collection.JavaConverters._
import com.intellij.openapi.externalSystem.model.{ProjectKeys, DataNode}
import com.intellij.openapi.externalSystem.service.project.{ProjectStructureHelper, PlatformFacade}
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.libraries.Library
import org.jetbrains.plugins.gradle.model.data.ScalaModelData
import ScalaGradleDataService._
import configuration._

/**
 * @author <NAME>
 */
class ScalaGradleDataService(platformFacade: PlatformFacade, helper: ProjectStructureHelper)
        extends AbstractDataService[ScalaModelData, Library](ScalaModelData.KEY) {

  def doImportData(toImport: util.Collection[DataNode[ScalaModelData]], project: Project) {
    toImport.asScala.foreach { dataNode =>
      val module = {
        val moduleName = dataNode.getData(ProjectKeys.MODULE).getName
        helper.findIdeModule(moduleName, project)
      }

      val scalaData = dataNode.getData

      val compilerClasspath = scalaData.getScalaClasspath.asScala.toSet
//      val compilerOptions = compilerOptionsFrom(scalaData) TODO handle

      val scalaSdk = findScalaSdkIn(project, compilerClasspath).orElse {
        val standardLibrary = findScalaStandardLibraryIn(project, compilerClasspath)
        standardLibrary.map(_.convertToScalaSdkWith(compilerClasspath.toSeq))
      }

      scalaSdk.foreach { properSdk =>
        val existingScalaSdk = module.scalaSdk

        existingScalaSdk match {
          case None =>
            module.attach(properSdk)
          case Some(sdk) if sdk.library != properSdk.library =>
            module.detach(sdk)
            module.attach(properSdk)
          case _ =>
        }
      }
    }
  }

  def doRemoveData(toRemove: util.Collection[_ <: Library], project: Project) {
    // TODO
  }
}

object ScalaGradleDataService {
  def findScalaSdkIn(project: Project, compilerClasspath: Set[File]): Option[ScalaSdk] =
    project.scalaSdks.find(_.compilerClasspath.toSet == compilerClasspath)

  def findScalaStandardLibraryIn(project: Project, compilerClasspath: Set[File]): Option[Library] = {
    val compilerStandardLibraryFile = compilerClasspath.find(_.getName.startsWith("scala-library"))

    compilerStandardLibraryFile.flatMap(file => project.libraries.find(_.classes.contains(file)))
  }

  def compilerOptionsFrom(data: ScalaModelData): Seq[String] = {
    val options = data.getScalaCompileOptions

    val presentations = Seq(
      options.isDeprecation -> "-deprecation",
      options.isUnchecked -> "-unchecked",
      options.isOptimize -> "-optimise",
      !isEmpty(options.getDebugLevel) -> s"-g:${options.getDebugLevel}",
      !isEmpty(options.getEncoding) -> s"-encoding ${options.getEncoding}",
      !isEmpty(data.getTargetCompatibility) -> s"-target:jvm-${data.getTargetCompatibility}")

    presentations.flatMap((include _).tupled)
  }

  private def isEmpty(s: String) = s == null || s.isEmpty

  private def include(b: Boolean, s: String): Seq[String] = if (b) Seq(s) else Seq.empty
}<commit_msg>Gradle: Scala SDK import is simplified<commit_after>package org.jetbrains.plugins.scala
package config

import java.util
import collection.JavaConverters._
import com.intellij.openapi.externalSystem.model.DataNode
import com.intellij.openapi.externalSystem.service.project.{ProjectStructureHelper, PlatformFacade}
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.libraries.Library
import org.jetbrains.plugins.gradle.model.data.ScalaModelData
import configuration._

/**
 * @author <NAME>
 */
class ScalaGradleDataService(platformFacade: PlatformFacade, helper: ProjectStructureHelper)
        extends AbstractDataService[ScalaModelData, Library](ScalaModelData.KEY) {

  def doImportData(toImport: util.Collection[DataNode[ScalaModelData]], project: Project) {
    toImport.asScala.foreach { dataNode =>
    }
  }

  private def doImport(scalaNode: DataNode[ScalaModelData], project: Project) {
    val scalaData = scalaNode.getData

    val compilerOptions = ScalaGradleDataService.compilerOptionsFrom(scalaData) // TODO

    val compilerClasspath = scalaData.getScalaClasspath.asScala.toSeq

    val compilerVersion = compilerClasspath.find(_.getName.startsWith("scala-library")).collect {
      case JarVersion(version) => version
    }

    compilerVersion.foreach { version =>
      val matchedLibrary = project.libraries.find(_.scalaVersion == Some(version))

      for (library <- matchedLibrary if !library.isScalaSdk) {
        val languageLevel = ScalaLanguageLevel.from(version, true)
        library.convertToScalaSdkWith(languageLevel, compilerClasspath)
      }
    }
  }

  def doRemoveData(toRemove: util.Collection[_ <: Library], project: Project) {

  }
}

private object ScalaGradleDataService {
  private def compilerOptionsFrom(data: ScalaModelData): Seq[String] = {
    val options = data.getScalaCompileOptions

    val presentations = Seq(
      options.isDeprecation -> "-deprecation",
      options.isUnchecked -> "-unchecked",
      options.isOptimize -> "-optimise",
      !isEmpty(options.getDebugLevel) -> s"-g:${options.getDebugLevel}",
      !isEmpty(options.getEncoding) -> s"-encoding ${options.getEncoding}",
      !isEmpty(data.getTargetCompatibility) -> s"-target:jvm-${data.getTargetCompatibility}")

    presentations.flatMap((include _).tupled)
  }

  private def isEmpty(s: String) = s == null || s.isEmpty

  private def include(b: Boolean, s: String): Seq[String] = if (b) Seq(s) else Seq.empty
}
{'fraction_non_alphanumeric': 0.10885082084225553, 'fraction_numerical': 0.0, 'mean_word_length': 4.267857142857143, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 26, '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': '4442007', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1642, 'n_words': 296}