KT-31295: Add New Kotlin Worksheet action

- creates new kotlin file with `.ws.kts` extension
This commit is contained in:
Roman Golyshev
2019-07-15 11:25:44 +03:00
committed by Natalia Selezneva
parent f6b03dc02f
commit 232c7fdd0e
6 changed files with 74 additions and 8 deletions
@@ -51,6 +51,9 @@
<action id="Kotlin.NewScript" class="org.jetbrains.kotlin.idea.actions.NewKotlinScriptAction">
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewFromTemplate"/>
</action>
<action id="Kotlin.NewWorksheet" class="org.jetbrains.kotlin.idea.actions.NewKotlinWorksheetAction">
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewFromTemplate"/>
</action>
<group id = "ConvertJavaToKotlinGroup">
<separator/>
@@ -381,6 +384,7 @@
<internalFileTemplate name="Kotlin Class"/>
<internalFileTemplate name="Kotlin Enum"/>
<internalFileTemplate name="Kotlin Interface"/>
<internalFileTemplate name="Kotlin Worksheet"/>
<internalFileTemplate name="Kotlin Script"/>
<gotoSymbolContributor implementation="org.jetbrains.kotlin.idea.goto.KotlinGotoSymbolContributor"/>
@@ -0,0 +1,14 @@
<html>
<body>
<table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3">
<font face="verdana" size="-1">
This is a built-in template used by <b>IDEA</b> each time you create a
Kotlin Worksheet
</font>
</td>
</tr>
</table>
</body>
</html>
@@ -148,7 +148,7 @@ class NewKotlinFileAction : CreateFileFromTemplateAction(
get() = NameValidator
private fun findOrCreateTarget(dir: PsiDirectory, name: String, directorySeparators: CharArray): Pair<String, PsiDirectory> {
var className = name.substringBeforeLast(".kt")
var className = removeKotlinExtensionIfPresent(name)
var targetDir = dir
for (splitChar in directorySeparators) {
@@ -168,6 +168,13 @@ class NewKotlinFileAction : CreateFileFromTemplateAction(
return Pair(className, targetDir)
}
private fun removeKotlinExtensionIfPresent(name: String): String = when {
name.endsWith(".$KOTLIN_WORKSHEET_EXTENSION") -> name.removeSuffix(".$KOTLIN_WORKSHEET_EXTENSION")
name.endsWith(".$KOTLIN_SCRIPT_EXTENSION") -> name.removeSuffix(".$KOTLIN_SCRIPT_EXTENSION")
name.endsWith(".${KotlinFileType.EXTENSION}") -> name.removeSuffix(".${KotlinFileType.EXTENSION}")
else -> name
}
private fun createFromTemplate(dir: PsiDirectory, className: String, template: FileTemplate): PsiFile? {
val project = dir.project
val defaultProperties = FileTemplateManager.getInstance(project).defaultProperties
@@ -16,12 +16,24 @@ import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.KotlinIcons
import org.jetbrains.kotlin.psi.KtFile
class NewKotlinScriptAction : CreateFileFromTemplateAction(
"Kotlin Script",
"Creates new Kotlin script",
open class NewKotlinScriptAction(
val actionName: String,
val description: String,
val dialogTitle: String,
val templateName: String
) : CreateFileFromTemplateAction(
actionName,
description,
KotlinIcons.SCRIPT
), DumbAware {
constructor() : this(
actionName = "Kotlin Script",
description = "Creates new Kotlin script",
dialogTitle = "New Kotlin Script",
templateName = "Kotlin Script"
)
override fun postProcess(createdElement: PsiFile, templateName: String?, customProperties: Map<String, String>?) {
super.postProcess(createdElement, templateName, customProperties)
@@ -33,18 +45,18 @@ class NewKotlinScriptAction : CreateFileFromTemplateAction(
}
override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) {
builder.setTitle("New Kotlin Script")
.addKind("Kotlin Script", KotlinIcons.SCRIPT, "Kotlin Script")
builder.setTitle(dialogTitle)
.addKind(actionName, KotlinIcons.SCRIPT, templateName)
}
override fun createFileFromTemplate(name: String, template: FileTemplate, dir: PsiDirectory) =
NewKotlinFileAction.createFileFromTemplateWithStat(name, template, dir)
override fun getActionName(directory: PsiDirectory, newName: String, templateName: String) = "Kotlin Script"
override fun getActionName(directory: PsiDirectory, newName: String, templateName: String) = actionName
override fun startInWriteAction() = false
override fun hashCode(): Int = 0
override fun equals(other: Any?): Boolean = other is NewKotlinScriptAction
override fun equals(other: Any?): Boolean = this::class.isInstance(other)
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.actions
import com.intellij.ide.fileTemplates.FileTemplate
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
const val KOTLIN_WORKSHEET_EXTENSION: String = "ws.kts"
const val KOTLIN_SCRIPT_EXTENSION = "kts"
class NewKotlinWorksheetAction : NewKotlinScriptAction(
actionName = "Kotlin Worksheet",
description = "Creates new Kotlin Worksheet",
dialogTitle = "New Kotlin Worksheet",
templateName = "Kotlin Worksheet"
) {
override fun createFileFromTemplate(name: String, template: FileTemplate, dir: PsiDirectory): PsiFile? {
val kotlinWorksheetTemplate = object : FileTemplate by template {
override fun getExtension(): String = KOTLIN_WORKSHEET_EXTENSION
}
return super.createFileFromTemplate(name, kotlinWorksheetTemplate, dir)
}
}