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

import java.lang.String
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTypeDefinition
import com.intellij.openapi.project.{Project}
import com.intellij.openapi.project.DumbAware
import com.intellij.ide.actions.{CreateFileFromTemplateDialog, CreateTemplateInPackageAction}
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.plugins.scala.icons.Icons
import com.intellij.CommonBundle
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import com.intellij.psi._
import org.jetbrains.plugins.scala.util.ScalaUtils
import com.intellij.openapi.roots.{ProjectRootManager, ProjectFileIndex}
import com.intellij.ide.IdeView
import org.jetbrains.annotations.NonNls
import com.intellij.openapi.module.Module
import com.intellij.openapi.actionSystem._

/**
 * User: <NAME>
 * Date: 15.09.2009
 */

class NewScalaTypeDefinitionAction extends CreateTemplateInPackageAction[ScTypeDefinition](
  ScalaBundle.message("newclass.menu.action.text"), ScalaBundle.message("newclass.menu.action.description"), Icons.CLASS, true) with DumbAware {
  protected def buildDialog(project: Project, directory: PsiDirectory): CreateFileFromTemplateDialog.Builder = {
    val builder = CreateFileFromTemplateDialog.
      createDialog(project, ScalaBundle.message("newclass.dlg.title"));
    builder.addKind("Class", Icons.CLASS, "ScalaClass.scala");
    builder.addKind("Object", Icons.OBJECT, "ScalaObject.scala");
    builder.addKind("Trait", Icons.TRAIT, "ScalaTrait.scala");
    return builder;
  }

  def getActionName(directory: PsiDirectory, newName: String, templateName: String): String = {
    ScalaBundle.message("newclass.menu.action.text")
  }

  def getErrorTitle: String = CommonBundle.getErrorTitle

  def getNavigationElement(createdElement: ScTypeDefinition): PsiElement = createdElement.extendsBlock

  def doCreate(directory: PsiDirectory, newName: String, templateName: String): ScTypeDefinition = {
    val file: PsiFile = createClassFromTemplate(directory, newName, templateName);
    if (file.isInstanceOf[ScalaFile]) {
      val scalaFile = file.asInstanceOf[ScalaFile]
      val classes = scalaFile.getClasses
      if (classes.length == 1 && classes(0).isInstanceOf[ScTypeDefinition]) {
        val definition = classes(0).asInstanceOf[ScTypeDefinition]
        return definition
      }
    }
    return null
  }

  override def isAvailable(dataContext: DataContext): Boolean = {
    return super.isAvailable(dataContext) && isUnderSourceRoots(dataContext)
  }

  private def isUnderSourceRoots(dataContext: DataContext): Boolean = {
    val module: Module = dataContext.getData(LangDataKeys.MODULE.getName).asInstanceOf[Module]
    if (!ScalaUtils.isSuitableModule(module)) {
      return false
    }
    val view = dataContext.getData(LangDataKeys.IDE_VIEW.getName).asInstanceOf[IdeView]
    val project = dataContext.getData(PlatformDataKeys.PROJECT.getName).asInstanceOf[Project]
    if (view != null && project != null) {
      val projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex
      val dirs = view.getDirectories
      for (dir <- dirs) {
        val aPackage = JavaDirectoryService.getInstance.getPackage(dir)
        if (projectFileIndex.isInSourceContent(dir.getVirtualFile) && aPackage != null) {
          return true
        }
      }
    }
    return false
  }

  private def createClassFromTemplate(directory: PsiDirectory, className: String, templateName: String,
                                                   parameters: String*): PsiFile = {
    return ScalaTemplatesFactory.createFromTemplate(directory, className, className + SCALA_EXTENSIOIN, templateName, parameters: _*)
  }

  private val SCALA_EXTENSIOIN = ".scala";
}<commit_msg>SCL-1651: Cannot create class or objects ending with _? new Class creation now possible for all Scala valid identifiers. (also fixed bug with creation to folder with same name as Java file)<commit_after>package org.jetbrains.plugins.scala.actions

import java.lang.String
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScTypeDefinition
import com.intellij.openapi.project.{Project}
import com.intellij.openapi.project.DumbAware
import com.intellij.ide.actions.{CreateFileFromTemplateDialog, CreateTemplateInPackageAction}
import org.jetbrains.plugins.scala.icons.Icons
import com.intellij.CommonBundle
import org.jetbrains.plugins.scala.lang.psi.api.ScalaFile
import com.intellij.psi._
import org.jetbrains.plugins.scala.util.ScalaUtils
import com.intellij.openapi.roots.{ProjectRootManager, ProjectFileIndex}
import com.intellij.ide.IdeView
import org.jetbrains.annotations.NonNls
import com.intellij.openapi.module.Module
import com.intellij.openapi.actionSystem._
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.fileTypes.StdFileTypes
import org.jetbrains.plugins.scala.lang.refactoring.util.ScalaNamesUtil
import com.intellij.util.IncorrectOperationException
import org.jetbrains.plugins.scala.{ScalaFileType, ScalaBundle}

/**
 * User: <NAME>
 * Date: 15.09.2009
 */

class NewScalaTypeDefinitionAction extends CreateTemplateInPackageAction[ScTypeDefinition](
  ScalaBundle.message("newclass.menu.action.text"), ScalaBundle.message("newclass.menu.action.description"), Icons.CLASS, true) with DumbAware {
  protected def buildDialog(project: Project, directory: PsiDirectory): CreateFileFromTemplateDialog.Builder = {
    val builder = CreateFileFromTemplateDialog.
      createDialog(project, ScalaBundle.message("newclass.dlg.title"));
    builder.addKind("Class", Icons.CLASS, "ScalaClass.scala");
    builder.addKind("Object", Icons.OBJECT, "ScalaObject.scala");
    builder.addKind("Trait", Icons.TRAIT, "ScalaTrait.scala");
    return builder;
  }

  def getActionName(directory: PsiDirectory, newName: String, templateName: String): String = {
    ScalaBundle.message("newclass.menu.action.text")
  }

  def getErrorTitle: String = CommonBundle.getErrorTitle

  def getNavigationElement(createdElement: ScTypeDefinition): PsiElement = createdElement.extendsBlock

  def doCreate(directory: PsiDirectory, newName: String, templateName: String): ScTypeDefinition = {
    val file: PsiFile = createClassFromTemplate(directory, newName, templateName);
    if (file.isInstanceOf[ScalaFile]) {
      val scalaFile = file.asInstanceOf[ScalaFile]
      val classes = scalaFile.getClasses
      if (classes.length == 1 && classes(0).isInstanceOf[ScTypeDefinition]) {
        val definition = classes(0).asInstanceOf[ScTypeDefinition]
        return definition
      }
    }
    return null
  }

  override def isAvailable(dataContext: DataContext): Boolean = {
    return super.isAvailable(dataContext) && isUnderSourceRoots(dataContext)
  }

  private def isUnderSourceRoots(dataContext: DataContext): Boolean = {
    val module: Module = dataContext.getData(LangDataKeys.MODULE.getName).asInstanceOf[Module]
    if (!ScalaUtils.isSuitableModule(module)) {
      return false
    }
    val view = dataContext.getData(LangDataKeys.IDE_VIEW.getName).asInstanceOf[IdeView]
    val project = dataContext.getData(PlatformDataKeys.PROJECT.getName).asInstanceOf[Project]
    if (view != null && project != null) {
      val projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex
      val dirs = view.getDirectories
      for (dir <- dirs) {
        val aPackage = JavaDirectoryService.getInstance.getPackage(dir)
        if (projectFileIndex.isInSourceContent(dir.getVirtualFile) && aPackage != null) {
          return true
        }
      }
    }
    return false
  }

  private def createClassFromTemplate(directory: PsiDirectory, className: String, templateName: String,
                                                   parameters: String*): PsiFile = {
    return ScalaTemplatesFactory.createFromTemplate(directory, className, className + SCALA_EXTENSIOIN, templateName, parameters: _*)
  }

  private val SCALA_EXTENSIOIN = ".scala";

  override def doCheckCreate(dir: PsiDirectory, className: String, templateName: String): Unit = {
    if (!ScalaNamesUtil.isIdentifier(className)) {
      throw new IncorrectOperationException(PsiBundle.message("0.is.not.an.identifier", className))
    }
    var fileName: String = className + "." + ScalaFileType.DEFAULT_EXTENSION
    dir.checkCreateFile(fileName)
    var helper: PsiNameHelper = JavaPsiFacade.getInstance(dir.getProject).getNameHelper
    var aPackage: PsiPackage = JavaDirectoryService.getInstance.getPackage(dir)
    var qualifiedName: String = if (aPackage == null) null else aPackage.getQualifiedName
    if (!StringUtil.isEmpty(qualifiedName) && !helper.isQualifiedName(qualifiedName)) {
      throw new IncorrectOperationException("Cannot create class in invalid package: '" + qualifiedName + "'")
    }
  }
}
{'fraction_non_alphanumeric': 0.10048714172425513, 'fraction_numerical': 0.003058796873229863, 'mean_word_length': 4.658974358974359, 'pattern_counts': {'":': 0, '<': 7, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6891536', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2529, 'n_words': 486}