-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracting away common part for plugin and build file #SCL-21374
- Loading branch information
Showing
4 changed files
with
42 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
scala/scala-impl/src/org/jetbrains/plugins/scala/actions/NewPredefinedSbtFileAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
31 changes: 5 additions & 26 deletions
31
scala/scala-impl/src/org/jetbrains/plugins/scala/actions/NewSbtBuildFileAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
31 changes: 5 additions & 26 deletions
31
scala/scala-impl/src/org/jetbrains/plugins/scala/actions/NewSbtPluginFileAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |