Reformat: NewKotlinFileAction
This commit is contained in:
@@ -45,12 +45,11 @@ import org.jetbrains.kotlin.psi.KtFile
|
|||||||
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class NewKotlinFileAction
|
class NewKotlinFileAction : CreateFileFromTemplateAction(
|
||||||
: CreateFileFromTemplateAction("Kotlin File/Class",
|
"Kotlin File/Class",
|
||||||
"Creates new Kotlin file or class",
|
"Creates new Kotlin file or class",
|
||||||
KotlinFileType.INSTANCE.icon),
|
KotlinFileType.INSTANCE.icon
|
||||||
DumbAware
|
), DumbAware {
|
||||||
{
|
|
||||||
override fun postProcess(createdElement: PsiFile?, templateName: String?, customProperties: Map<String, String>?) {
|
override fun postProcess(createdElement: PsiFile?, templateName: String?, customProperties: Map<String, String>?) {
|
||||||
super.postProcess(createdElement, templateName, customProperties)
|
super.postProcess(createdElement, templateName, customProperties)
|
||||||
|
|
||||||
@@ -66,8 +65,7 @@ class NewKotlinFileAction
|
|||||||
val ktClass = createdElement.declarations.singleOrNull() as? KtNamedDeclaration
|
val ktClass = createdElement.declarations.singleOrNull() as? KtNamedDeclaration
|
||||||
if (ktClass != null) {
|
if (ktClass != null) {
|
||||||
CreateFromTemplateAction.moveCaretAfterNameIdentifier(ktClass)
|
CreateFromTemplateAction.moveCaretAfterNameIdentifier(ktClass)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
val editor = FileEditorManager.getInstance(createdElement.project).selectedTextEditor ?: return
|
val editor = FileEditorManager.getInstance(createdElement.project).selectedTextEditor ?: return
|
||||||
if (editor.document == createdElement.viewProvider.document) {
|
if (editor.document == createdElement.viewProvider.document) {
|
||||||
val lineCount = editor.document.lineCount
|
val lineCount = editor.document.lineCount
|
||||||
@@ -81,11 +79,11 @@ class NewKotlinFileAction
|
|||||||
|
|
||||||
override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) {
|
override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) {
|
||||||
builder.setTitle("New Kotlin File/Class")
|
builder.setTitle("New Kotlin File/Class")
|
||||||
.addKind("File", KotlinFileType.INSTANCE.icon, "Kotlin File")
|
.addKind("File", KotlinFileType.INSTANCE.icon, "Kotlin File")
|
||||||
.addKind("Class", KotlinIcons.CLASS, "Kotlin Class")
|
.addKind("Class", KotlinIcons.CLASS, "Kotlin Class")
|
||||||
.addKind("Interface", KotlinIcons.INTERFACE, "Kotlin Interface")
|
.addKind("Interface", KotlinIcons.INTERFACE, "Kotlin Interface")
|
||||||
.addKind("Enum class", KotlinIcons.ENUM, "Kotlin Enum")
|
.addKind("Enum class", KotlinIcons.ENUM, "Kotlin Enum")
|
||||||
.addKind("Object", KotlinIcons.OBJECT, "Kotlin Object")
|
.addKind("Object", KotlinIcons.OBJECT, "Kotlin Object")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getActionName(directory: PsiDirectory, newName: String, templateName: String) = "Kotlin File/Class"
|
override fun getActionName(directory: PsiDirectory, newName: String, templateName: String) = "Kotlin File/Class"
|
||||||
@@ -104,14 +102,14 @@ class NewKotlinFileAction
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun equals(obj: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
return obj is NewKotlinFileAction
|
return other is NewKotlinFileAction
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startInWriteAction() = false
|
override fun startInWriteAction() = false
|
||||||
|
|
||||||
override fun createFileFromTemplate(name: String, template: FileTemplate, dir: PsiDirectory) =
|
override fun createFileFromTemplate(name: String, template: FileTemplate, dir: PsiDirectory) =
|
||||||
Companion.createFileFromTemplate(name, template, dir)
|
Companion.createFileFromTemplate(name, template, dir)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private fun findOrCreateTarget(dir: PsiDirectory, name: String, directorySeparators: Array<Char>): Pair<String, PsiDirectory> {
|
private fun findOrCreateTarget(dir: PsiDirectory, name: String, directorySeparators: Array<Char>): Pair<String, PsiDirectory> {
|
||||||
@@ -142,14 +140,14 @@ class NewKotlinFileAction
|
|||||||
val properties = Properties(defaultProperties)
|
val properties = Properties(defaultProperties)
|
||||||
|
|
||||||
val element = try {
|
val element = try {
|
||||||
CreateFromTemplateDialog(project, dir, template,
|
CreateFromTemplateDialog(
|
||||||
AttributesDefaults(className).withFixedName(true),
|
project, dir, template,
|
||||||
properties).create()
|
AttributesDefaults(className).withFixedName(true),
|
||||||
}
|
properties
|
||||||
catch (e: IncorrectOperationException) {
|
).create()
|
||||||
|
} catch (e: IncorrectOperationException) {
|
||||||
throw e
|
throw e
|
||||||
}
|
} catch (e: Exception) {
|
||||||
catch (e: Exception) {
|
|
||||||
LOG.error(e)
|
LOG.error(e)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -165,8 +163,7 @@ class NewKotlinFileAction
|
|||||||
service.isAlternativeResolveEnabled = true
|
service.isAlternativeResolveEnabled = true
|
||||||
try {
|
try {
|
||||||
return createFromTemplate(targetDir, className, template)
|
return createFromTemplate(targetDir, className, template)
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
service.isAlternativeResolveEnabled = false
|
service.isAlternativeResolveEnabled = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,7 +173,7 @@ class NewKotlinFileAction
|
|||||||
abstract class NewKotlinFileHook {
|
abstract class NewKotlinFileHook {
|
||||||
companion object {
|
companion object {
|
||||||
val EP_NAME: ExtensionPointName<NewKotlinFileHook> =
|
val EP_NAME: ExtensionPointName<NewKotlinFileHook> =
|
||||||
ExtensionPointName.create<NewKotlinFileHook>("org.jetbrains.kotlin.newFileHook")
|
ExtensionPointName.create<NewKotlinFileHook>("org.jetbrains.kotlin.newFileHook")
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract fun postProcess(createdElement: KtFile, module: Module)
|
abstract fun postProcess(createdElement: KtFile, module: Module)
|
||||||
|
|||||||
Reference in New Issue
Block a user