mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-26 15:52:48 +00:00
New J2K: rearrange and optimize some post-processings
This commit is contained in:
@@ -133,25 +133,44 @@ private val errorsFixingDiagnosticBasedPostProcessingGroup =
|
||||
)
|
||||
|
||||
|
||||
private val addOrRemoveModifiersInspectionProcessing =
|
||||
private val addOrRemoveModifiersProcessingGroup =
|
||||
InspectionLikeProcessingGroup(
|
||||
runSingleTime = true,
|
||||
processings = listOf(
|
||||
RemoveRedundantVisibilityModifierProcessing(),
|
||||
RemoveRedundantModalityModifierProcessing(),
|
||||
inspectionBasedProcessing(AddOperatorModifierInspection())
|
||||
inspectionBasedProcessing(AddOperatorModifierInspection()),
|
||||
generalInspectionBasedProcessing(RedundantUnitReturnTypeInspection())
|
||||
)
|
||||
)
|
||||
|
||||
private val removeRedundantElementsProcessingGroup =
|
||||
InspectionLikeProcessingGroup(
|
||||
runSingleTime = true,
|
||||
processings = listOf(
|
||||
RemoveExplicitTypeArgumentsProcessing(),
|
||||
generalInspectionBasedProcessing(RedundantCompanionReferenceInspection()),
|
||||
generalInspectionBasedProcessing(ExplicitThisInspection()),
|
||||
intentionBasedProcessing(RemoveEmptyClassBodyIntention())
|
||||
)
|
||||
)
|
||||
|
||||
private val removeRedundantSemicolonProcessing =
|
||||
InspectionLikeProcessingGroup(
|
||||
runSingleTime = true,
|
||||
acceptNonKtElements = true,
|
||||
processings = listOf(
|
||||
generalInspectionBasedProcessing(RedundantSemicolonInspection())
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
private val inspectionLikePostProcessingGroup =
|
||||
InspectionLikeProcessingGroup(
|
||||
RemoveRedundantConstructorKeywordProcessing(),
|
||||
RemoveExplicitOpenInInterfaceProcessing(),
|
||||
generalInspectionBasedProcessing(ExplicitThisInspection()),
|
||||
RemoveExplicitTypeArgumentsProcessing(),
|
||||
RemoveRedundantOverrideVisibilityProcessing(),
|
||||
inspectionBasedProcessing(MoveLambdaOutsideParenthesesInspection()),
|
||||
generalInspectionBasedProcessing(RedundantCompanionReferenceInspection()),
|
||||
ConvertToStringTemplateProcessing(),
|
||||
UsePropertyAccessSyntaxProcessing(),
|
||||
UninitializedVariableReferenceFromInitializerToThisReferenceProcessing(),
|
||||
@@ -162,14 +181,11 @@ private val inspectionLikePostProcessingGroup =
|
||||
UseExpressionBodyProcessing(),
|
||||
inspectionBasedProcessing(UnnecessaryVariableInspection()),
|
||||
RemoveExplicitPropertyTypeWithInspectionProcessing(),
|
||||
generalInspectionBasedProcessing(RedundantUnitReturnTypeInspection()),
|
||||
JavaObjectEqualsToEqOperatorProcessing(),
|
||||
RemoveExplicitPropertyTypeProcessing(),
|
||||
RemoveRedundantNullabilityProcessing(),
|
||||
generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)),
|
||||
inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()),
|
||||
generalInspectionBasedProcessing(RedundantSemicolonInspection()),
|
||||
intentionBasedProcessing(RemoveEmptyClassBodyIntention()),
|
||||
intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()),
|
||||
inspectionBasedProcessing(JavaMapForEachInspection()),
|
||||
intentionBasedProcessing(FoldIfToReturnIntention()) { it.then.isTrivialStatementBody() && it.`else`.isTrivialStatementBody() },
|
||||
@@ -233,8 +249,10 @@ private val processings: List<NamedPostProcessingGroup> = listOf(
|
||||
"Cleaning up Kotlin code",
|
||||
listOf(
|
||||
errorsFixingDiagnosticBasedPostProcessingGroup,
|
||||
addOrRemoveModifiersInspectionProcessing,
|
||||
addOrRemoveModifiersProcessingGroup,
|
||||
inspectionLikePostProcessingGroup,
|
||||
removeRedundantSemicolonProcessing,
|
||||
removeRedundantElementsProcessingGroup,
|
||||
cleaningUpDiagnosticBasedPostProcessingGroup
|
||||
)
|
||||
),
|
||||
|
||||
@@ -23,7 +23,9 @@ import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import org.jetbrains.kotlin.j2k.ConverterSettings
|
||||
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.utils.mapToIndex
|
||||
@@ -34,10 +36,15 @@ import kotlin.reflect.full.isSubclassOf
|
||||
|
||||
class InspectionLikeProcessingGroup(
|
||||
private val runSingleTime: Boolean = false,
|
||||
private val acceptNonKtElements: Boolean = false,
|
||||
private val processings: List<InspectionLikeProcessing>
|
||||
) : ProcessingGroup {
|
||||
|
||||
constructor(vararg processings: InspectionLikeProcessing) : this(false, processings.toList())
|
||||
constructor(vararg processings: InspectionLikeProcessing) : this(
|
||||
runSingleTime = false,
|
||||
acceptNonKtElements = false,
|
||||
processings = processings.toList()
|
||||
)
|
||||
|
||||
private val processingsToPriorityMap = processings.mapToIndex()
|
||||
fun priority(processing: InspectionLikeProcessing): Int = processingsToPriorityMap.getValue(processing)
|
||||
@@ -88,22 +95,24 @@ class InspectionLikeProcessingGroup(
|
||||
|
||||
file.accept(object : PsiRecursiveElementVisitor() {
|
||||
override fun visitElement(element: PsiElement) {
|
||||
val rangeResult = rangeFilter(element, rangeMarker)
|
||||
if (rangeResult == RangeFilterResult.SKIP) return
|
||||
if (element is KtElement || acceptNonKtElements) {
|
||||
val rangeResult = rangeFilter(element, rangeMarker)
|
||||
if (rangeResult == RangeFilterResult.SKIP) return
|
||||
|
||||
super.visitElement(element)
|
||||
super.visitElement(element)
|
||||
|
||||
if (rangeResult == RangeFilterResult.PROCESS) {
|
||||
processings.forEach { processing ->
|
||||
val action = processing.createAction(element, context.converter.settings)
|
||||
if (action != null) {
|
||||
availableActions.add(
|
||||
ActionData(
|
||||
element, action,
|
||||
priority(processing),
|
||||
processing.writeActionNeeded
|
||||
if (rangeResult == RangeFilterResult.PROCESS) {
|
||||
processings.forEach { processing ->
|
||||
val action = processing.createAction(element, context.converter.settings)
|
||||
if (action != null) {
|
||||
availableActions.add(
|
||||
ActionData(
|
||||
element, action,
|
||||
priority(processing),
|
||||
processing.writeActionNeeded
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,12 +199,12 @@ class UninitializedVariableReferenceFromInitializerToThisReferenceProcessing :
|
||||
override val writeActionNeeded = true
|
||||
|
||||
override fun createAction(element: PsiElement, settings: ConverterSettings?): (() -> Unit)? {
|
||||
if (element !is KtSimpleNameExpression || element.mainReference.resolve() == null) return null
|
||||
if (element !is KtSimpleNameExpression) return null
|
||||
val anonymousObject = element.getParentOfType<KtClassOrObject>(true)?.takeIf { it.name == null } ?: return null
|
||||
|
||||
val resolved = element.mainReference.resolve() ?: return null
|
||||
if (resolved.isAncestor(element, strict = true)) {
|
||||
if (resolved is KtVariableDeclaration && resolved.hasInitializer()) {
|
||||
val anonymousObject = element.getParentOfType<KtClassOrObject>(true) ?: return null
|
||||
if (resolved.initializer!!.getChildOfType<KtClassOrObject>() == anonymousObject) {
|
||||
return { element.replaced(KtPsiFactory(element).createThisExpression()) }
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
|
||||
|
||||
val formatCodeProcessing =
|
||||
postProcessing { file, rangeMarker, _ ->
|
||||
file.commitAndUnblockDocument()
|
||||
val codeStyleManager = CodeStyleManager.getInstance(file.project)
|
||||
if (rangeMarker != null) {
|
||||
if (rangeMarker.isValid) {
|
||||
|
||||
@@ -1 +1 @@
|
||||
val a: Array<Any?> = arrayOfNulls(10)
|
||||
val a = arrayOfNulls<Any>(10)
|
||||
@@ -1 +1 @@
|
||||
val d2: Array<IntArray> = arrayOf()
|
||||
val d2 = arrayOf<IntArray>()
|
||||
@@ -1 +1 @@
|
||||
val d2: Array<IntArray?> = arrayOfNulls(5)
|
||||
val d2 = arrayOfNulls<IntArray>(5)
|
||||
@@ -1 +1 @@
|
||||
val d3: Array<Array<IntArray>> = Array(5) { arrayOfNulls(5) }
|
||||
val d3 = Array<Array<IntArray>>(5) { arrayOfNulls(5) }
|
||||
@@ -1 +1 @@
|
||||
val ss: Array<Array<String>> = Array(5) { arrayOfNulls(5) }
|
||||
val ss = Array<Array<String>>(5) { arrayOfNulls(5) }
|
||||
@@ -1 +1 @@
|
||||
val sss: Array<Array<Array<String>>> = Array(5) { Array(5) { arrayOfNulls(5) } }
|
||||
val sss = Array(5) { Array<Array<String>>(5) { arrayOfNulls(5) } }
|
||||
@@ -1 +1 @@
|
||||
val constrArgTypes: Array<Class<*>> = arrayOf(Array<String>::class.java, String::class.java, Int::class.java, Double::class.java)
|
||||
val constrArgTypes = arrayOf<Class<*>>(Array<String>::class.java, String::class.java, Int::class.java, Double::class.java)
|
||||
@@ -1,7 +1,7 @@
|
||||
import java.util.ArrayList
|
||||
|
||||
internal class A {
|
||||
private val list1: ArrayList<String> = ArrayList()
|
||||
private val list1 = ArrayList<String>()
|
||||
private val list2: MutableList<String> = ArrayList()
|
||||
private val myList3: MutableList<String> = ArrayList()
|
||||
fun getList1(): List<String> {
|
||||
|
||||
@@ -122,10 +122,10 @@ internal class Java8Class {
|
||||
}
|
||||
|
||||
fun testGenericFunctions() {
|
||||
val emptyList: JFunction1<List<String>> = JFunction1 { emptyList() }
|
||||
val emptyList = JFunction1<List<String>> { emptyList() }
|
||||
emptyList.foo()
|
||||
MethodReferenceHelperClass.staticFun1(JFunction1<List<String>> { emptyList() })
|
||||
h.memberFun1(JFunction1<List<String>> { emptyList() })
|
||||
MethodReferenceHelperClass.staticFun1<List<String>> { emptyList() }
|
||||
h.memberFun1<List<String>> { emptyList() }
|
||||
}
|
||||
|
||||
fun memberFun(): Int {
|
||||
|
||||
2
nj2k/testData/newJ2k/issues/kt-19336.kt
vendored
2
nj2k/testData/newJ2k/issues/kt-19336.kt
vendored
@@ -1,6 +1,6 @@
|
||||
class TestReturnsArray {
|
||||
fun strings(n: Int): Array<String?> {
|
||||
val result: Array<String?> = arrayOfNulls(n)
|
||||
val result = arrayOfNulls<String>(n)
|
||||
for (i in 0 until n) {
|
||||
result[i] = Integer.toString(i)
|
||||
}
|
||||
|
||||
2
nj2k/testData/newJ2k/issues/kt-19604.kt
vendored
2
nj2k/testData/newJ2k/issues/kt-19604.kt
vendored
@@ -1,7 +1,7 @@
|
||||
import java.util.HashMap
|
||||
|
||||
class TestMethodReference {
|
||||
private val hashMap: HashMap<String, String> = HashMap()
|
||||
private val hashMap = HashMap<String, String>()
|
||||
fun update(key: String, msg: String) {
|
||||
hashMap.merge(key, msg) { obj: String, s: String -> obj + s }
|
||||
}
|
||||
|
||||
2
nj2k/testData/newJ2k/issues/kt-19606.kt
vendored
2
nj2k/testData/newJ2k/issues/kt-19606.kt
vendored
@@ -1,7 +1,7 @@
|
||||
import java.util.HashMap
|
||||
|
||||
class TestMethodReference {
|
||||
private val hashMap: HashMap<String, String> = HashMap()
|
||||
private val hashMap = HashMap<String, String>()
|
||||
fun update(key: String, msg: String) {
|
||||
hashMap.merge(key, msg) { obj: String, s: String -> obj + s }
|
||||
}
|
||||
|
||||
4
nj2k/testData/newJ2k/issues/kt-8170.kt
vendored
4
nj2k/testData/newJ2k/issues/kt-8170.kt
vendored
@@ -2,12 +2,12 @@ object ArrayNullable {
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val notNull = 0
|
||||
val a1: Array<Int?> = arrayOfNulls(2)
|
||||
val a1 = arrayOfNulls<Int>(2)
|
||||
a1[0] = null
|
||||
a1[1] = notNull
|
||||
println(a1[0])
|
||||
println(a1[1])
|
||||
val a2: Array<Int?> = arrayOfNulls(2)
|
||||
val a2 = arrayOfNulls<Int>(2)
|
||||
a2[0] = nullable()
|
||||
a2[1] = notNull
|
||||
println(a2[0])
|
||||
|
||||
2
nj2k/testData/newJ2k/list/ForEach.kt
vendored
2
nj2k/testData/newJ2k/list/ForEach.kt
vendored
@@ -3,7 +3,7 @@ import java.util.ArrayList
|
||||
|
||||
class ForEach {
|
||||
fun test() {
|
||||
val xs: ArrayList<Any> = ArrayList()
|
||||
val xs = ArrayList<Any>()
|
||||
val ys: MutableList<Any> = LinkedList<Any>()
|
||||
for (x in xs) {
|
||||
ys.add(x)
|
||||
|
||||
2
nj2k/testData/newJ2k/list/Lists.kt
vendored
2
nj2k/testData/newJ2k/list/Lists.kt
vendored
@@ -5,7 +5,7 @@ class Lists {
|
||||
fun test() {
|
||||
val xs: MutableList<Any?> = ArrayList()
|
||||
val ys: MutableList<Any?> = LinkedList<Any>()
|
||||
val zs: ArrayList<Any?> = ArrayList()
|
||||
val zs = ArrayList<Any?>()
|
||||
xs.add(null)
|
||||
ys.add(null)
|
||||
xs.clear()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
internal class A {
|
||||
fun foo(): Map<String, String> {
|
||||
val list1: List<String> = emptyList()
|
||||
val list1 = emptyList<String>()
|
||||
val list2 = listOf(1)
|
||||
val set1: Set<String> = emptySet()
|
||||
val set1 = emptySet<String>()
|
||||
val set2 = setOf("a")
|
||||
return emptyMap()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
internal class A {
|
||||
fun foo() {
|
||||
val list: List<String?> = listOf(null)
|
||||
val set: Set<String?> = setOf(null)
|
||||
val list = listOf<String?>(null)
|
||||
val set = setOf<String?>(null)
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import java.util.HashMap
|
||||
|
||||
internal class Test {
|
||||
fun main() {
|
||||
val commonMap: HashMap<String, Int> = HashMap()
|
||||
val commonMap = HashMap<String, Int>()
|
||||
val rawMap: HashMap<*, *> = HashMap<String?, Int?>()
|
||||
val superRawMap: HashMap<*, *> = HashMap<Any?, Any?>()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import java.util.HashMap
|
||||
|
||||
internal class A {
|
||||
fun foo() {
|
||||
val map1: Map<String, Int> = getMap1()
|
||||
val map1 = getMap1<String, Int>()
|
||||
val map2 = getMap2("a", 1)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ internal class A {
|
||||
}
|
||||
|
||||
fun <K, V> getMap2(k: K, v: V): Map<K, V> {
|
||||
val map: HashMap<K, V> = HashMap()
|
||||
val map = HashMap<K, V>()
|
||||
map[k] = v
|
||||
return map
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user