New J2K: suggest user to configure Kotlin in a project before nj2k conversion
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.j2k
|
package org.jetbrains.kotlin.idea.j2k
|
||||||
|
|
||||||
|
import com.intellij.openapi.module.Module
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.j2k.*
|
import org.jetbrains.kotlin.j2k.*
|
||||||
|
|
||||||
@@ -18,4 +19,7 @@ object JavaToKotlinConverterFactory {
|
|||||||
|
|
||||||
fun createPostProcessor(formatCode: Boolean): PostProcessor =
|
fun createPostProcessor(formatCode: Boolean): PostProcessor =
|
||||||
J2kConverterExtension.extension.createPostProcessor(formatCode)
|
J2kConverterExtension.extension.createPostProcessor(formatCode)
|
||||||
|
|
||||||
|
fun doCheckBeforeConversion(project: Project, module: Module): Boolean =
|
||||||
|
J2kConverterExtension.extension.doCheckBeforeConversion(project, module)
|
||||||
}
|
}
|
||||||
@@ -21,9 +21,7 @@ import com.intellij.ide.highlighter.ArchiveFileType
|
|||||||
import com.intellij.ide.highlighter.JavaFileType
|
import com.intellij.ide.highlighter.JavaFileType
|
||||||
import com.intellij.ide.scratch.ScratchFileService
|
import com.intellij.ide.scratch.ScratchFileService
|
||||||
import com.intellij.ide.scratch.ScratchRootType
|
import com.intellij.ide.scratch.ScratchRootType
|
||||||
import com.intellij.openapi.actionSystem.AnAction
|
import com.intellij.openapi.actionSystem.*
|
||||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
|
||||||
import com.intellij.openapi.actionSystem.CommonDataKeys
|
|
||||||
import com.intellij.openapi.command.CommandProcessor
|
import com.intellij.openapi.command.CommandProcessor
|
||||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||||
@@ -182,6 +180,10 @@ class JavaToKotlinAction : AnAction() {
|
|||||||
.showInCenterOf(statusBar.component)
|
.showInCenterOf(statusBar.component)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
val module = e.getData(LangDataKeys.MODULE)!!
|
||||||
|
if (!JavaToKotlinConverterFactory.doCheckBeforeConversion(project, module)) return
|
||||||
|
|
||||||
val firstSyntaxError = javaFiles.asSequence().map { PsiTreeUtil.findChildOfType(it, PsiErrorElement::class.java) }.firstOrNull()
|
val firstSyntaxError = javaFiles.asSequence().map { PsiTreeUtil.findChildOfType(it, PsiErrorElement::class.java) }.firstOrNull()
|
||||||
|
|
||||||
if (firstSyntaxError != null) {
|
if (firstSyntaxError != null) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.jetbrains.kotlin.j2k
|
|||||||
|
|
||||||
import com.intellij.openapi.extensions.AbstractExtensionPointBean
|
import com.intellij.openapi.extensions.AbstractExtensionPointBean
|
||||||
import com.intellij.openapi.extensions.ExtensionPointName
|
import com.intellij.openapi.extensions.ExtensionPointName
|
||||||
|
import com.intellij.openapi.module.Module
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.registry.Registry
|
import com.intellij.openapi.util.registry.Registry
|
||||||
|
|
||||||
@@ -16,6 +17,8 @@ abstract class J2kConverterExtension : AbstractExtensionPointBean() {
|
|||||||
|
|
||||||
abstract fun createPostProcessor(formatCode: Boolean): PostProcessor
|
abstract fun createPostProcessor(formatCode: Boolean): PostProcessor
|
||||||
|
|
||||||
|
open fun doCheckBeforeConversion(project: Project, module: Module): Boolean =
|
||||||
|
true
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private fun useNewJ2k() = Registry.`is`("kotlin.use.new.j2k", false)
|
private fun useNewJ2k() = Registry.`is`("kotlin.use.new.j2k", false)
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ dependencies {
|
|||||||
compile(project(":idea:fir-view"))
|
compile(project(":idea:fir-view"))
|
||||||
compile(project(":idea:idea-core"))
|
compile(project(":idea:idea-core"))
|
||||||
compile(project(":idea:ide-common"))
|
compile(project(":idea:ide-common"))
|
||||||
|
compile(project(":idea:idea-jvm"))
|
||||||
compile(project(":idea:idea-jps-common"))
|
compile(project(":idea:idea-jps-common"))
|
||||||
compile(project(":idea:kotlin-gradle-tooling"))
|
compile(project(":idea:kotlin-gradle-tooling"))
|
||||||
compile(project(":plugins:uast-kotlin"))
|
compile(project(":plugins:uast-kotlin"))
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package org.jetbrains.kotlin.nj2k
|
package org.jetbrains.kotlin.nj2k
|
||||||
|
|
||||||
|
import com.intellij.openapi.module.Module
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.openapi.ui.Messages
|
||||||
|
import org.jetbrains.kotlin.idea.configuration.getAbleToRunConfigurators
|
||||||
|
import org.jetbrains.kotlin.idea.configuration.hasAnyKotlinRuntimeInScope
|
||||||
|
import org.jetbrains.kotlin.idea.configuration.isModuleConfigured
|
||||||
|
import org.jetbrains.kotlin.idea.configuration.toModuleGroup
|
||||||
import org.jetbrains.kotlin.j2k.*
|
import org.jetbrains.kotlin.j2k.*
|
||||||
import org.jetbrains.kotlin.nj2k.postProcessing.NewJ2kPostProcessor
|
import org.jetbrains.kotlin.nj2k.postProcessing.NewJ2kPostProcessor
|
||||||
|
import org.jetbrains.kotlin.platform.jvm.isJvm
|
||||||
|
|
||||||
class NewJ2kConverterExtension : J2kConverterExtension() {
|
class NewJ2kConverterExtension : J2kConverterExtension() {
|
||||||
override val isNewJ2k = true
|
override val isNewJ2k = true
|
||||||
@@ -16,4 +23,43 @@ class NewJ2kConverterExtension : J2kConverterExtension() {
|
|||||||
|
|
||||||
override fun createPostProcessor(formatCode: Boolean): PostProcessor =
|
override fun createPostProcessor(formatCode: Boolean): PostProcessor =
|
||||||
NewJ2kPostProcessor()
|
NewJ2kPostProcessor()
|
||||||
|
|
||||||
|
override fun doCheckBeforeConversion(project: Project, module: Module): Boolean =
|
||||||
|
checkKotlinIsConfigured(project, module)
|
||||||
|
|
||||||
|
private fun checkKotlinIsConfigured(project: Project, module: Module): Boolean {
|
||||||
|
val kotlinIsConfigured =
|
||||||
|
hasAnyKotlinRuntimeInScope(module) || isModuleConfigured(module.toModuleGroup())
|
||||||
|
if (kotlinIsConfigured) return true
|
||||||
|
|
||||||
|
val title = "Kotlin is not configured in the project"
|
||||||
|
if (Messages.showOkCancelDialog(
|
||||||
|
project,
|
||||||
|
"You will have to configure Kotlin in project before performing a conversion.",
|
||||||
|
title,
|
||||||
|
"OK, configure Kotlin in the project",
|
||||||
|
"No, cancel conversion",
|
||||||
|
Messages.getWarningIcon()
|
||||||
|
) == Messages.OK
|
||||||
|
) {
|
||||||
|
val configurators = getAbleToRunConfigurators(module).filter { it.targetPlatform.isJvm() }
|
||||||
|
when {
|
||||||
|
configurators.isEmpty() -> Messages.showErrorDialog("There aren't configurators available", title)
|
||||||
|
configurators.size == 1 -> configurators.single().configure(project, emptyList())
|
||||||
|
else -> {
|
||||||
|
val resultIndex = Messages.showChooseDialog(//TODO a better dialog?
|
||||||
|
project,
|
||||||
|
"Choose Configurator",
|
||||||
|
title,
|
||||||
|
null,
|
||||||
|
configurators.map { it.presentableText }.toTypedArray(),
|
||||||
|
configurators.first().presentableText
|
||||||
|
)
|
||||||
|
configurators.getOrNull(resultIndex)?.configure(project, emptyList())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user