Skip to content

Commit

Permalink
Abstracting away common part for plugin and build file #SCL-21374
Browse files Browse the repository at this point in the history
  • Loading branch information
Sa1to committed May 13, 2024
1 parent af09862 commit c633f55
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 52 deletions.
1 change: 1 addition & 0 deletions scala/scala-impl/resources/messages/ScalaBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ newclassorfile.menu.action.sbt.description=Create new Sbt File

error.package.already.contains.build.sbt=''build.sbt'' already exists for package ''{0}''
error.package.already.contains.plugin.sbt=''plugin.sbt'' already exists for package ''{0}''
error.title.package.already.contains.file=Cannot Create File

### org/jetbrains/plugins/scala/actions/ShowTypeInfoAction.scala
type.info.text=Type Info
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jetbrains.plugins.scala.actions

import com.intellij.ide.IdeView
import com.intellij.ide.fileTemplates.actions.CreateFromTemplateActionBase
import com.intellij.ide.fileTemplates.{FileTemplate, FileTemplateManager}
import com.intellij.openapi.actionSystem.{CommonDataKeys, DataContext}
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.NlsActions
import com.intellij.psi.PsiDirectory
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.plugins.scala.extensions.PsiNamedElementExt

import javax.swing.Icon

abstract class NewPredefinedSbtFileAction(@NlsActions.ActionText title: String, @NlsActions.ActionDescription description: String, icon: Icon, fileName: String) extends CreateFromTemplateActionBase(title, description, icon) {

override def getTemplate(project: Project, dir: PsiDirectory): FileTemplate = FileTemplateManager.getDefaultInstance.getInternalTemplate(ScalaBundle.message("newclassorfile.menu.action.sbt.text"))

override def getTargetDirectory(dataContext: DataContext, view: IdeView): PsiDirectory = {
val directories: Array[PsiDirectory] = view.getDirectories
directories.find(directory => directory.findFile(fileName + ".sbt") != null).exists(dir => {
Messages.showErrorDialog(CommonDataKeys.PROJECT.getData(dataContext),
ScalaBundle.message("error.package.already.contains.build.sbt", dir.name),
ScalaBundle.message("error.title.package.already.contains.file", dir.name))
return null
}
)
super.getTargetDirectory(dataContext, view)
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
package org.jetbrains.plugins.scala.actions

import com.intellij.ide.IdeView
import com.intellij.ide.fileTemplates.actions.{AttributesDefaults, CreateFromTemplateActionBase}
import com.intellij.ide.fileTemplates.{FileTemplate, FileTemplateManager}
import com.intellij.openapi.actionSystem.{CommonDataKeys, DataContext}
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.psi.PsiDirectory
import org.jetbrains.plugins.scala.extensions.PsiNamedElementExt
import com.intellij.ide.fileTemplates.actions.AttributesDefaults
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.sbt.icons.Icons

final class NewSbtBuildFileAction extends CreateFromTemplateActionBase(
final class NewSbtBuildFileAction extends NewPredefinedSbtFileAction(
ScalaBundle.message("newclassorfile.menu.action.build.sbt.text"),
ScalaBundle.message("newclassorfile.menu.action.sbt.description"),
Icons.SBT_FILE
Icons.SBT_FILE,
ScalaBundle.message("newclassorfile.menu.action.build.sbt.defaultName")
) {

override def getTemplate(project: Project, dir: PsiDirectory): FileTemplate = FileTemplateManager.getDefaultInstance.getInternalTemplate(ScalaBundle.message("newclassorfile.menu.action.sbt.text"))

protected override def getAttributesDefaults(dataContext: DataContext): AttributesDefaults = new AttributesDefaults(ScalaBundle.message("newclassorfile.menu.action.build.sbt.defaultName")).withFixedName(true)

override def getTargetDirectory(dataContext: DataContext, view: IdeView): PsiDirectory = {
val directories: Array[PsiDirectory] = view.getDirectories
for (directory <- directories) {
if (directory.findFile(ScalaBundle.message("newclassorfile.menu.action.build.sbt.defaultName") + ".sbt") != null) {
Messages.showErrorDialog(CommonDataKeys.PROJECT.getData(dataContext),
ScalaBundle.message("error.package.already.contains.build.sbt", directory.name),
"Cannot Create File")
return null
}
}
super.getTargetDirectory(dataContext, view)
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
package org.jetbrains.plugins.scala.actions

import com.intellij.ide.IdeView
import com.intellij.ide.fileTemplates.actions.{AttributesDefaults, CreateFromTemplateActionBase}
import com.intellij.ide.fileTemplates.{FileTemplate, FileTemplateManager}
import com.intellij.openapi.actionSystem.{CommonDataKeys, DataContext}
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.psi.PsiDirectory
import com.intellij.ide.fileTemplates.actions.AttributesDefaults
import com.intellij.openapi.actionSystem.DataContext
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.plugins.scala.extensions.PsiNamedElementExt
import org.jetbrains.sbt.icons.Icons

final class NewSbtPluginFileAction extends CreateFromTemplateActionBase(
final class NewSbtPluginFileAction extends NewPredefinedSbtFileAction(
ScalaBundle.message("newclassorfile.menu.action.plugin.sbt.text"),
ScalaBundle.message("newclassorfile.menu.action.sbt.description"),
Icons.SBT_FILE
Icons.SBT_FILE,
ScalaBundle.message("newclassorfile.menu.action.plugin.sbt.defaultName")
) {

override def getTemplate(project: Project, dir: PsiDirectory): FileTemplate = FileTemplateManager.getDefaultInstance.getInternalTemplate(ScalaBundle.message("newclassorfile.menu.action.sbt.text"))

protected override def getAttributesDefaults(dataContext: DataContext): AttributesDefaults = new AttributesDefaults(ScalaBundle.message("newclassorfile.menu.action.plugin.sbt.defaultName")).withFixedName(true)

override def getTargetDirectory(dataContext: DataContext, view: IdeView): PsiDirectory = {
val directories: Array[PsiDirectory] = view.getDirectories
for (directory <- directories) {
if (directory.findFile(ScalaBundle.message("newclassorfile.menu.action.plugin.sbt.defaultName") + ".sbt") != null) {
Messages.showErrorDialog(CommonDataKeys.PROJECT.getData(dataContext),
ScalaBundle.message("error.package.already.contains.plugin.sbt", directory.name),
"Cannot Create File")
return null
}
}
super.getTargetDirectory(dataContext, view)
}
}

0 comments on commit c633f55

Please sign in to comment.