<commit_before>package org.jetbrains.sbt
package project.module

import com.intellij.openapi.externalSystem.service.project.wizard.AbstractExternalModuleBuilder
import org.jetbrains.sbt.project.settings.SbtProjectSettings
import com.intellij.openapi.module.{Module, ModifiableModuleModel, ModuleType}
import com.intellij.ide.util.projectWizard.ModuleBuilder
import com.intellij.openapi.vfs.{LocalFileSystem, VirtualFile}
import java.io.File
import com.intellij.openapi.util.io.{FileUtilRt, FileUtil}
import javax.swing.Icon
import org.jetbrains.sbt.project.SbtProjectSystem
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.externalSystem.settings.{ExternalSystemSettingsListener, AbstractExternalSystemSettings}

/**
 * User: <NAME>
 * Date: 11/23/13
 */
class SbtModuleBuilder extends AbstractExternalModuleBuilder[SbtProjectSettings](SbtProjectSystem.Id, new SbtProjectSettings) {
  def getExternalProjectConfigFile(contentRootDir: VirtualFile): VirtualFile = null

  def getTemplateConfigName(settings: SbtProjectSettings): String = null

  def getModuleType: ModuleType[_ <: ModuleBuilder] = SbtModuleType.instance

  override def createModule(moduleModel: ModifiableModuleModel): Module = {
    createSbtStub()
    super.createModule(moduleModel)
  }
  
  private def createSbtStub() {
    val modulePath = getModuleFileDirectory
    val moduleName = getName

    val dir = new File(modulePath)

    if (!dir.exists() || !dir.isDirectory) return
    
    val buildFile = new File(dir, "build.sbt")
    val projectDir = new File(dir, "project")
    val pluginsFile = new File(projectDir, "plugins.sbt")

    if (!buildFile.createNewFile() || !FileUtil.createDirectory(projectDir) || !pluginsFile.createNewFile()) return

    FileUtil.writeToFile(buildFile, SbtModuleBuilder buildSbt moduleName)
    FileUtil.writeToFile(pluginsFile, SbtModuleBuilder.pluginsSbt)
  }

  override def getNodeIcon: Icon = Sbt.Icon

  override def setupRootModel(model: ModifiableRootModel) {
    val contentPath = getContentEntryPath
    if (StringUtil isEmpty contentPath) return

    val contentRootDir = new File(contentPath)
    FileUtilRt createDirectory contentRootDir

    val fileSystem = LocalFileSystem.getInstance
    val vContentRootDir = fileSystem refreshAndFindFileByIoFile contentRootDir
    if (vContentRootDir == null) return

    model addContentEntry vContentRootDir
    model.inheritSdk()
    val settings =
      ExternalSystemApiUtil.getSettings(model.getProject, SbtProjectSystem.Id).
        asInstanceOf[AbstractExternalSystemSettings[_ <: AbstractExternalSystemSettings[_, SbtProjectSettings, _],
        SbtProjectSettings, _ <: ExternalSystemSettingsListener[SbtProjectSettings]]]
    model.commit()

    getExternalProjectSettings setExternalProjectPath getContentEntryPath
    settings linkProject getExternalProjectSettings<commit_msg>#SCL-6694 {AE at com.intellij.openapi.roots.impl.RootModelImpl.dispose} fixed<commit_after>package org.jetbrains.sbt
package project.module

import com.intellij.openapi.externalSystem.service.project.wizard.AbstractExternalModuleBuilder
import org.jetbrains.sbt.project.settings.SbtProjectSettings
import com.intellij.openapi.module.{Module, ModifiableModuleModel, ModuleType}
import com.intellij.ide.util.projectWizard.ModuleBuilder
import com.intellij.openapi.vfs.{LocalFileSystem, VirtualFile}
import java.io.File
import com.intellij.openapi.util.io.{FileUtilRt, FileUtil}
import javax.swing.Icon
import org.jetbrains.sbt.project.SbtProjectSystem
import com.intellij.openapi.roots.ModifiableRootModel
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.externalSystem.settings.{ExternalSystemSettingsListener, AbstractExternalSystemSettings}

/**
 * User: <NAME>
 * Date: 11/23/13
 */
class SbtModuleBuilder extends AbstractExternalModuleBuilder[SbtProjectSettings](SbtProjectSystem.Id, new SbtProjectSettings) {
  def getExternalProjectConfigFile(contentRootDir: VirtualFile): VirtualFile = null

  def getTemplateConfigName(settings: SbtProjectSettings): String = null

  def getModuleType: ModuleType[_ <: ModuleBuilder] = SbtModuleType.instance

  override def createModule(moduleModel: ModifiableModuleModel): Module = {
    createSbtStub()
    super.createModule(moduleModel)
  }
  
  private def createSbtStub() {
    val modulePath = getModuleFileDirectory
    val moduleName = getName

    val dir = new File(modulePath)

    if (!dir.exists() || !dir.isDirectory) return
    
    val buildFile = new File(dir, "build.sbt")
    val projectDir = new File(dir, "project")
    val pluginsFile = new File(projectDir, "plugins.sbt")

    if (!buildFile.createNewFile() || !FileUtil.createDirectory(projectDir) || !pluginsFile.createNewFile()) return

    FileUtil.writeToFile(buildFile, SbtModuleBuilder buildSbt moduleName)
    FileUtil.writeToFile(pluginsFile, SbtModuleBuilder.pluginsSbt)
  }

  override def getNodeIcon: Icon = Sbt.Icon

  override def setupRootModel(model: ModifiableRootModel) {
    val contentPath = getContentEntryPath
    if (StringUtil isEmpty contentPath) return

    val contentRootDir = new File(contentPath)
    FileUtilRt createDirectory contentRootDir

    val fileSystem = LocalFileSystem.getInstance
    val vContentRootDir = fileSystem refreshAndFindFileByIoFile contentRootDir
    if (vContentRootDir == null) return

    model addContentEntry vContentRootDir
    model.inheritSdk()
    val settings =
      ExternalSystemApiUtil.getSettings(model.getProject, SbtProjectSystem.Id).
        asInstanceOf[AbstractExternalSystemSettings[_ <: AbstractExternalSystemSettings[_, SbtProjectSettings, _],
        SbtProjectSettings, _ <: ExternalSystemSettingsListener[SbtProjectSettings]]]
//    model.commit()

    getExternalProjectSettings setExternalProjectPath getContentEntryPath
    settings linkProject getExternalProjectSettings
{'fraction_non_alphanumeric': 0.0813570487483531, 'fraction_numerical': 0.002635046113306983, 'mean_word_length': 5.629912663755459, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '5565369', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1714, 'n_words': 335}