New J2K: add conversion progress bar
Relates to #KT-31812
This commit is contained in:
@@ -18,20 +18,25 @@ package org.jetbrains.kotlin.j2k
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
class AfterConversionPass(val project: Project, val postProcessor: PostProcessor) {
|
||||
fun run(kotlinFile: KtFile, converterContext: ConverterContext?, range: TextRange?) {
|
||||
@JvmOverloads
|
||||
fun run(
|
||||
kotlinFile: KtFile,
|
||||
converterContext: ConverterContext?,
|
||||
range: TextRange?,
|
||||
onPhaseChanged: ((Int, String) -> Unit)? = null
|
||||
) {
|
||||
val rangeMarker = if (range != null) {
|
||||
val document = kotlinFile.viewProvider.document!!
|
||||
val marker = document.createRangeMarker(range.startOffset, range.endOffset)
|
||||
val marker = runReadAction { document.createRangeMarker(range.startOffset, range.endOffset) }
|
||||
marker.isGreedyToLeft = true
|
||||
marker.isGreedyToRight = true
|
||||
marker
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} else null
|
||||
|
||||
postProcessor.doAdditionalProcessing(kotlinFile, converterContext, rangeMarker)
|
||||
postProcessor.doAdditionalProcessing(kotlinFile, converterContext, rangeMarker, onPhaseChanged)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package org.jetbrains.kotlin.j2k
|
||||
import com.intellij.openapi.extensions.AbstractExtensionPointBean
|
||||
import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.progress.ProgressIndicator
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.registry.Registry
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
|
||||
abstract class J2kConverterExtension : AbstractExtensionPointBean() {
|
||||
abstract val isNewJ2k: Boolean
|
||||
@@ -20,6 +22,12 @@ abstract class J2kConverterExtension : AbstractExtensionPointBean() {
|
||||
open fun doCheckBeforeConversion(project: Project, module: Module): Boolean =
|
||||
true
|
||||
|
||||
abstract fun createWithProgressProcessor(
|
||||
progress: ProgressIndicator?,
|
||||
files: List<PsiJavaFile>?,
|
||||
phasesCount: Int
|
||||
): WithProgressProcessor
|
||||
|
||||
companion object {
|
||||
private fun useNewJ2k() = Registry.`is`("kotlin.use.new.j2k", false)
|
||||
|
||||
|
||||
@@ -40,7 +40,14 @@ import java.util.*
|
||||
interface PostProcessor {
|
||||
fun insertImport(file: KtFile, fqName: FqName)
|
||||
|
||||
fun doAdditionalProcessing(file: KtFile, converterContext: ConverterContext?, rangeMarker: RangeMarker?)
|
||||
val phasesCount: Int
|
||||
|
||||
fun doAdditionalProcessing(
|
||||
file: KtFile,
|
||||
converterContext: ConverterContext?,
|
||||
rangeMarker: RangeMarker?,
|
||||
onPhaseChanged: ((Int, String) -> Unit)?
|
||||
)
|
||||
}
|
||||
|
||||
enum class ParseContext {
|
||||
@@ -73,9 +80,7 @@ abstract class JavaToKotlinConverter {
|
||||
progress: ProgressIndicator = EmptyProgressIndicator()
|
||||
): FilesResult
|
||||
|
||||
fun elementsToKotlin(inputElements: List<PsiElement>): Result {
|
||||
return elementsToKotlin(inputElements, WithProgressProcessor.DEFAULT)
|
||||
}
|
||||
abstract fun elementsToKotlin(inputElements: List<PsiElement>): Result
|
||||
}
|
||||
|
||||
class OldJavaToKotlinConverter(
|
||||
@@ -91,7 +96,7 @@ class OldJavaToKotlinConverter(
|
||||
postProcessor: PostProcessor,
|
||||
progress: ProgressIndicator
|
||||
): FilesResult {
|
||||
val withProgressProcessor = WithProgressProcessor(progress, files)
|
||||
val withProgressProcessor = OldWithProgressProcessor(progress, files)
|
||||
val (results, externalCodeProcessing) = ApplicationManager.getApplication().runReadAction(Computable {
|
||||
elementsToKotlin(files, withProgressProcessor)
|
||||
})
|
||||
@@ -106,7 +111,7 @@ class OldJavaToKotlinConverter(
|
||||
|
||||
result!!.importsToAdd.forEach { postProcessor.insertImport(kotlinFile, it) }
|
||||
|
||||
AfterConversionPass(project, postProcessor).run(kotlinFile, converterContext = null, range = null)
|
||||
AfterConversionPass(project, postProcessor).run(kotlinFile, converterContext = null, range = null, onPhaseChanged = null)
|
||||
|
||||
kotlinFile.text
|
||||
} catch (e: ProcessCanceledException) {
|
||||
@@ -157,6 +162,11 @@ class OldJavaToKotlinConverter(
|
||||
}
|
||||
}
|
||||
|
||||
override fun elementsToKotlin(inputElements: List<PsiElement>): Result {
|
||||
return elementsToKotlin(inputElements, OldWithProgressProcessor.DEFAULT)
|
||||
}
|
||||
|
||||
|
||||
private data class ReferenceInfo(
|
||||
val reference: PsiReference,
|
||||
val target: PsiElement,
|
||||
@@ -252,10 +262,21 @@ class OldJavaToKotlinConverter(
|
||||
}
|
||||
}
|
||||
|
||||
interface WithProgressProcessor {
|
||||
fun <TInputItem, TOutputItem> processItems(
|
||||
fractionPortion: Double,
|
||||
inputItems: Iterable<TInputItem>,
|
||||
processItem: (TInputItem) -> TOutputItem
|
||||
): List<TOutputItem>
|
||||
|
||||
class WithProgressProcessor(private val progress: ProgressIndicator?, private val files: List<PsiJavaFile>?) {
|
||||
fun updateState(fileIndex: Int, phase: Int, description: String)
|
||||
fun <T> process(action: () -> T): T
|
||||
}
|
||||
|
||||
|
||||
class OldWithProgressProcessor(private val progress: ProgressIndicator?, private val files: List<PsiJavaFile>?) : WithProgressProcessor {
|
||||
companion object {
|
||||
val DEFAULT = WithProgressProcessor(null, null)
|
||||
val DEFAULT = OldWithProgressProcessor(null, null)
|
||||
}
|
||||
|
||||
private val progressText = "Converting Java to Kotlin"
|
||||
@@ -264,7 +285,7 @@ class WithProgressProcessor(private val progress: ProgressIndicator?, private va
|
||||
private var fraction = 0.0
|
||||
private var pass = 1
|
||||
|
||||
fun <TInputItem, TOutputItem> processItems(
|
||||
override fun <TInputItem, TOutputItem> processItems(
|
||||
fractionPortion: Double,
|
||||
inputItems: Iterable<TInputItem>,
|
||||
processItem: (TInputItem) -> TOutputItem
|
||||
@@ -291,6 +312,14 @@ class WithProgressProcessor(private val progress: ProgressIndicator?, private va
|
||||
)
|
||||
return outputItems
|
||||
}
|
||||
|
||||
override fun <T> process(action: () -> T): T {
|
||||
throw AbstractMethodError("Should not be called for old J2K")
|
||||
}
|
||||
|
||||
override fun updateState(fileIndex: Int, phase: Int, description: String) {
|
||||
throw AbstractMethodError("Should not be called for old J2K")
|
||||
}
|
||||
}
|
||||
|
||||
private class ProgressPortionReporter(
|
||||
|
||||
Reference in New Issue
Block a user