<commit_before>package org.jetbrains.sbt
package project

import com.intellij.openapi.externalSystem.service.project.ExternalSystemProjectResolver
import com.intellij.openapi.externalSystem.model.task.{ExternalSystemTaskNotificationListener, ExternalSystemTaskNotificationEvent, ExternalSystemTaskId}
import com.intellij.openapi.externalSystem.model.project._
import com.intellij.openapi.module.StdModuleTypes
import com.intellij.openapi.externalSystem.model.{ExternalSystemException, DataNode}
import com.intellij.openapi.roots.DependencyScope
import java.io.File
import module.SbtModuleType
import settings._
import structure._
import data._

/**
 * @author <NAME>
 */
class SbtProjectResolver extends ExternalSystemProjectResolver[SbtExecutionSettings] {
  def resolveProjectInfo(id: ExternalSystemTaskId, projectPath: String, isPreview: Boolean, settings: SbtExecutionSettings, listener: ExternalSystemTaskNotificationListener): DataNode[ProjectData] = {
    val root = {
      val file = new File(projectPath)
      if (file.isDirectory) file.getPath else file.getParent
    }

    val runner = new SbtRunner(settings.ideaSystem, settings.vmOptions, settings.customLauncher, settings.customVm)

    val xml = runner.read(new File(root), !isPreview) { message =>
      listener.onStatusChange(new ExternalSystemTaskNotificationEvent(id, message.trim))
    } match {
      case Left(errors) => throw new ExternalSystemException(errors)
      case Right(node) => node
    }

    val data = StructureParser.parse(xml, new File(System.getProperty("user.home")))

    val externalSourceRoots = data.projects.flatMap(externalSourceRootsIn)

    if (externalSourceRoots.nonEmpty) {
      val warning = externalSourceRoots.map(file => s"<li>${file.getPath}</li>").mkString(
        "The following source roots are outside of the corresponding base directories:\n<ul>", "\n",
        "\n</ul>\nThese source roots cannot be included in the IDEA project model." +
                "<br>Please consider using shared SBT projects instead of shared source roots.")

      listener.onTaskOutput(id, WarningMessage(warning), false)
    }

    convert(root, data).toDataNode
  }

  private def convert(root: String, data: Structure): Node[ProjectData] = {
    val projects = data.projects
<commit_msg>SBT: show existing SBT warnings/errors even on successfull project extraction<commit_after>package org.jetbrains.sbt
package project

import com.intellij.openapi.externalSystem.service.project.ExternalSystemProjectResolver
import com.intellij.openapi.externalSystem.model.task.{ExternalSystemTaskNotificationListener, ExternalSystemTaskNotificationEvent, ExternalSystemTaskId}
import com.intellij.openapi.externalSystem.model.project._
import com.intellij.openapi.module.StdModuleTypes
import com.intellij.openapi.externalSystem.model.{ExternalSystemException, DataNode}
import com.intellij.openapi.roots.DependencyScope
import java.io.File
import module.SbtModuleType
import settings._
import structure._
import data._

/**
 * @author <NAME>
 */
class SbtProjectResolver extends ExternalSystemProjectResolver[SbtExecutionSettings] {
  def resolveProjectInfo(id: ExternalSystemTaskId, projectPath: String, isPreview: Boolean, settings: SbtExecutionSettings, listener: ExternalSystemTaskNotificationListener): DataNode[ProjectData] = {
    val root = {
      val file = new File(projectPath)
      if (file.isDirectory) file.getPath else file.getParent
    }

    val runner = new SbtRunner(settings.ideaSystem, settings.vmOptions, settings.customLauncher, settings.customVm)

    var warnings = new StringBuilder()

    val xml = runner.read(new File(root), !isPreview) { message =>
      if (message.startsWith("[error] ") || message.startsWith("[warn] ")) {
        warnings ++= message
      }

      listener.onStatusChange(new ExternalSystemTaskNotificationEvent(id, message.trim))
    } match {
      case Left(errors) => throw new ExternalSystemException(errors)
      case Right(node) => node
    }

    if (warnings.nonEmpty) {
      listener.onTaskOutput(id, WarningMessage(warnings.toString), false)
    }

    val data = StructureParser.parse(xml, new File(System.getProperty("user.home")))

    val externalSourceRoots = data.projects.flatMap(externalSourceRootsIn)

    if (externalSourceRoots.nonEmpty) {
      val warning = externalSourceRoots.map(file => s"<li>${file.getPath}</li>").mkString(
        "The following source roots are outside of the corresponding base directories:\n<ul>", "\n",
        "\n</ul>\nThese source roots cannot be included in the IDEA project model." +
                "<br>Please consider using shared SBT projects instead of shared source roots.")

      listener.onTaskOutput(id, WarningMessage(warning), false)
    }

    convert(root, data).toDataNode
  }

  private def convert(root: String, data: Structure): Node[ProjectData] = {
    val projects = data.projects

{'fraction_non_alphanumeric': 0.09851111564348358, 'fraction_numerical': 0.0, 'mean_word_length': 4.742388758782202, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6036378', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1328, 'n_words': 317}