<commit_before>/*
 * Copyright 2014-2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package intellij.haskell.action

import java.io.File
import java.text.ParseException

import com.intellij.ide.actions.CreateFileFromTemplateDialog.Builder
import com.intellij.ide.actions.{CreateFileAction, CreateFileFromTemplateAction}
import com.intellij.ide.fileTemplates.{FileTemplate, FileTemplateManager, FileTemplateUtil}
import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.{DumbAware, Project}
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.ui.{InputValidatorEx, Messages}
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.{PsiDirectory, PsiFile}
import intellij.haskell.HaskellIcons
import intellij.haskell.util.HaskellFileUtil
import intellij.haskell.util.index.HaskellModuleNameIndex

object CreateHaskellFileAction {
  private final val NEW_HASKELL_FILE = "New Haskell File"
}

class CreateHaskellFileAction extends CreateFileFromTemplateAction(CreateHaskellFileAction.NEW_HASKELL_FILE, "", HaskellIcons.HaskellFileLogo) with DumbAware {

  override def buildDialog(project: Project, directory: PsiDirectory, builder: Builder): Unit = {
    builder.setTitle(CreateHaskellFileAction.NEW_HASKELL_FILE).addKind("Empty module", HaskellIcons.HaskellFileLogo, "Haskell Module").setValidator(new InputValidatorEx {

      def checkInput(inputString: String): Boolean = {
        true
      }

      def canClose(inputString: String): Boolean = {
        !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null
      }

      def getErrorText(inputString: String): String = {
        val checkString = if (inputString.contains(".")) {
          inputString.trim.split("\\.").last
        } else {
          inputString
        }

        if (!StringUtil.isCapitalized(checkString)) {
          s"'$inputString' is not a valid Haskell module name"
        } else {
          null
        }
      }
    })
  }

  override def createFileFromTemplate(fileName: String, template: FileTemplate, fileDir: PsiDirectory): PsiFile = {
    val path = HaskellFileUtil.getAbsolutePath(fileDir.getVirtualFile)
    val pathItems = ProjectRootManager.getInstance(fileDir.getProject)
      .getContentSourceRoots
      .map(HaskellFileUtil.getAbsolutePath)
      .find(path.startsWith)
      .map(s => if (s != path) {
        path.replace(s + File.separator, "").split(File.separator).toList
      } else {
        List()
      })

    val createFileResult = if (fileName.contains(".")) {
      var targetDir = fileDir
      val names = fileName.trim().split("\\.").toList
      val moduleName = names.last
      val prefixes = names.dropRight(1)

      prefixes.foreach(dirName => {
        targetDir = Option(targetDir.findSubdirectory(dirName)).getOrElse(targetDir.createSubdirectory(dirName))
      })

      createFileFromTemplate(pathItems.map(_ ++ prefixes), moduleName, template, targetDir)
    } else {
      createFileFromTemplate(pathItems, fileName, template, fileDir)
    }<commit_msg>#339 test<commit_after>/*
 * Copyright 2014-2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package intellij.haskell.action

import java.io.File
import java.text.ParseException

import com.intellij.ide.actions.CreateFileFromTemplateDialog.Builder
import com.intellij.ide.actions.{CreateFileAction, CreateFileFromTemplateAction}
import com.intellij.ide.fileTemplates.{FileTemplate, FileTemplateManager, FileTemplateUtil}
import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.{DumbAware, Project}
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.ui.{InputValidatorEx, Messages}
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.{PsiDirectory, PsiFile}
import intellij.haskell.HaskellIcons
import intellij.haskell.util.HaskellFileUtil
import intellij.haskell.util.index.HaskellModuleNameIndex

object CreateHaskellFileAction {
  private final val NEW_HASKELL_FILE = "New Haskell File"
}

class CreateHaskellFileAction extends CreateFileFromTemplateAction(CreateHaskellFileAction.NEW_HASKELL_FILE, "", HaskellIcons.HaskellFileLogo) with DumbAware {

  override def buildDialog(project: Project, directory: PsiDirectory, builder: Builder): Unit = {
    builder.setTitle(CreateHaskellFileAction.NEW_HASKELL_FILE).addKind("Empty module", HaskellIcons.HaskellFileLogo, "Haskell Module").setValidator(new InputValidatorEx {

      def checkInput(inputString: String): Boolean = {
        true
      }

      def canClose(inputString: String): Boolean = {
        !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null
      }

      def getErrorText(inputString: String): String = {
        val checkString = if (inputString.contains(".")) {
          inputString.trim.split("\\.").last
        } else {
          inputString
        }

        if (!StringUtil.isCapitalized(checkString)) {
          s"'$inputString' is not a valid Haskell module name"
        } else {
          null
        }
      }
    })
  }

  override def createFileFromTemplate(fileName: String, template: FileTemplate, fileDir: PsiDirectory): PsiFile = {
    val path = HaskellFileUtil.getAbsolutePath(fileDir.getVirtualFile)
    val pathItems = ProjectRootManager.getInstance(fileDir.getProject)
      .getContentSourceRoots
      .map(HaskellFileUtil.getAbsolutePath)
      .find(path.startsWith)
      .map(s => if (s != path) {
        // See https://github.com/rikvdkleij/intellij-haskell/issues/339
        path.replace(s + "/", "").split("/").toList
      } else {
        List()
      })

    val createFileResult = if (fileName.contains(".")) {
      var targetDir = fileDir
      val names = fileName.trim().split("\\.").toList
      val moduleName = names.last
      val prefixes = names.dropRight(1)

      prefixes.foreach(dirName => {
        targetDir = Option(targetDir.findSubdirectory(dirName)).getOrElse(targetDir.createSubdirectory(dirName))
      })

      createFileFromTemplate(pathItems.map(_ ++ prefixes), moduleName, template, targetDir)
    } else {
      createFileFromTemplate(pathItems, fileName, template, fileDir)
    }
{'fraction_non_alphanumeric': 0.098792237752748, 'fraction_numerical': 0.004342515945175736, 'mean_word_length': 3.9035262807717896, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '3141058', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2104, 'n_words': 486}