New J2K: rearrange and optimize some post-processings

This commit is contained in:
Ilya Kirillov
2019-06-27 11:43:46 +03:00
parent 370f113b78
commit 934425e86a
23 changed files with 77 additions and 51 deletions
@@ -133,25 +133,44 @@ private val errorsFixingDiagnosticBasedPostProcessingGroup =
) )
private val addOrRemoveModifiersInspectionProcessing = private val addOrRemoveModifiersProcessingGroup =
InspectionLikeProcessingGroup( InspectionLikeProcessingGroup(
runSingleTime = true, runSingleTime = true,
processings = listOf( processings = listOf(
RemoveRedundantVisibilityModifierProcessing(), RemoveRedundantVisibilityModifierProcessing(),
RemoveRedundantModalityModifierProcessing(), 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 = private val inspectionLikePostProcessingGroup =
InspectionLikeProcessingGroup( InspectionLikeProcessingGroup(
RemoveRedundantConstructorKeywordProcessing(), RemoveRedundantConstructorKeywordProcessing(),
RemoveExplicitOpenInInterfaceProcessing(), RemoveExplicitOpenInInterfaceProcessing(),
generalInspectionBasedProcessing(ExplicitThisInspection()),
RemoveExplicitTypeArgumentsProcessing(),
RemoveRedundantOverrideVisibilityProcessing(), RemoveRedundantOverrideVisibilityProcessing(),
inspectionBasedProcessing(MoveLambdaOutsideParenthesesInspection()), inspectionBasedProcessing(MoveLambdaOutsideParenthesesInspection()),
generalInspectionBasedProcessing(RedundantCompanionReferenceInspection()),
ConvertToStringTemplateProcessing(), ConvertToStringTemplateProcessing(),
UsePropertyAccessSyntaxProcessing(), UsePropertyAccessSyntaxProcessing(),
UninitializedVariableReferenceFromInitializerToThisReferenceProcessing(), UninitializedVariableReferenceFromInitializerToThisReferenceProcessing(),
@@ -162,14 +181,11 @@ private val inspectionLikePostProcessingGroup =
UseExpressionBodyProcessing(), UseExpressionBodyProcessing(),
inspectionBasedProcessing(UnnecessaryVariableInspection()), inspectionBasedProcessing(UnnecessaryVariableInspection()),
RemoveExplicitPropertyTypeWithInspectionProcessing(), RemoveExplicitPropertyTypeWithInspectionProcessing(),
generalInspectionBasedProcessing(RedundantUnitReturnTypeInspection()),
JavaObjectEqualsToEqOperatorProcessing(), JavaObjectEqualsToEqOperatorProcessing(),
RemoveExplicitPropertyTypeProcessing(), RemoveExplicitPropertyTypeProcessing(),
RemoveRedundantNullabilityProcessing(), RemoveRedundantNullabilityProcessing(),
generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)), generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)),
inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()), inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()),
generalInspectionBasedProcessing(RedundantSemicolonInspection()),
intentionBasedProcessing(RemoveEmptyClassBodyIntention()),
intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()), intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()),
inspectionBasedProcessing(JavaMapForEachInspection()), inspectionBasedProcessing(JavaMapForEachInspection()),
intentionBasedProcessing(FoldIfToReturnIntention()) { it.then.isTrivialStatementBody() && it.`else`.isTrivialStatementBody() }, intentionBasedProcessing(FoldIfToReturnIntention()) { it.then.isTrivialStatementBody() && it.`else`.isTrivialStatementBody() },
@@ -233,8 +249,10 @@ private val processings: List<NamedPostProcessingGroup> = listOf(
"Cleaning up Kotlin code", "Cleaning up Kotlin code",
listOf( listOf(
errorsFixingDiagnosticBasedPostProcessingGroup, errorsFixingDiagnosticBasedPostProcessingGroup,
addOrRemoveModifiersInspectionProcessing, addOrRemoveModifiersProcessingGroup,
inspectionLikePostProcessingGroup, inspectionLikePostProcessingGroup,
removeRedundantSemicolonProcessing,
removeRedundantElementsProcessingGroup,
cleaningUpDiagnosticBasedPostProcessingGroup 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.idea.util.application.runWriteAction
import org.jetbrains.kotlin.j2k.ConverterSettings import org.jetbrains.kotlin.j2k.ConverterSettings
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile 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.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.utils.mapToIndex import org.jetbrains.kotlin.utils.mapToIndex
@@ -34,10 +36,15 @@ import kotlin.reflect.full.isSubclassOf
class InspectionLikeProcessingGroup( class InspectionLikeProcessingGroup(
private val runSingleTime: Boolean = false, private val runSingleTime: Boolean = false,
private val acceptNonKtElements: Boolean = false,
private val processings: List<InspectionLikeProcessing> private val processings: List<InspectionLikeProcessing>
) : ProcessingGroup { ) : 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() private val processingsToPriorityMap = processings.mapToIndex()
fun priority(processing: InspectionLikeProcessing): Int = processingsToPriorityMap.getValue(processing) fun priority(processing: InspectionLikeProcessing): Int = processingsToPriorityMap.getValue(processing)
@@ -88,22 +95,24 @@ class InspectionLikeProcessingGroup(
file.accept(object : PsiRecursiveElementVisitor() { file.accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) { override fun visitElement(element: PsiElement) {
val rangeResult = rangeFilter(element, rangeMarker) if (element is KtElement || acceptNonKtElements) {
if (rangeResult == RangeFilterResult.SKIP) return val rangeResult = rangeFilter(element, rangeMarker)
if (rangeResult == RangeFilterResult.SKIP) return
super.visitElement(element) super.visitElement(element)
if (rangeResult == RangeFilterResult.PROCESS) { if (rangeResult == RangeFilterResult.PROCESS) {
processings.forEach { processing -> processings.forEach { processing ->
val action = processing.createAction(element, context.converter.settings) val action = processing.createAction(element, context.converter.settings)
if (action != null) { if (action != null) {
availableActions.add( availableActions.add(
ActionData( ActionData(
element, action, element, action,
priority(processing), priority(processing),
processing.writeActionNeeded processing.writeActionNeeded
)
) )
) }
} }
} }
} }
@@ -199,12 +199,12 @@ class UninitializedVariableReferenceFromInitializerToThisReferenceProcessing :
override val writeActionNeeded = true override val writeActionNeeded = true
override fun createAction(element: PsiElement, settings: ConverterSettings?): (() -> Unit)? { 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 val resolved = element.mainReference.resolve() ?: return null
if (resolved.isAncestor(element, strict = true)) { if (resolved.isAncestor(element, strict = true)) {
if (resolved is KtVariableDeclaration && resolved.hasInitializer()) { if (resolved is KtVariableDeclaration && resolved.hasInitializer()) {
val anonymousObject = element.getParentOfType<KtClassOrObject>(true) ?: return null
if (resolved.initializer!!.getChildOfType<KtClassOrObject>() == anonymousObject) { if (resolved.initializer!!.getChildOfType<KtClassOrObject>() == anonymousObject) {
return { element.replaced(KtPsiFactory(element).createThisExpression()) } return { element.replaced(KtPsiFactory(element).createThisExpression()) }
} }
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
val formatCodeProcessing = val formatCodeProcessing =
postProcessing { file, rangeMarker, _ -> postProcessing { file, rangeMarker, _ ->
file.commitAndUnblockDocument()
val codeStyleManager = CodeStyleManager.getInstance(file.project) val codeStyleManager = CodeStyleManager.getInstance(file.project)
if (rangeMarker != null) { if (rangeMarker != null) {
if (rangeMarker.isValid) { 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
View File
@@ -1 +1 @@
val ss: Array<Array<String>> = Array(5) { arrayOfNulls(5) } val ss = Array<Array<String>>(5) { arrayOfNulls(5) }
+1 -1
View File
@@ -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
View File
@@ -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 -1
View File
@@ -1,7 +1,7 @@
import java.util.ArrayList import java.util.ArrayList
internal class A { internal class A {
private val list1: ArrayList<String> = ArrayList() private val list1 = ArrayList<String>()
private val list2: MutableList<String> = ArrayList() private val list2: MutableList<String> = ArrayList()
private val myList3: MutableList<String> = ArrayList() private val myList3: MutableList<String> = ArrayList()
fun getList1(): List<String> { fun getList1(): List<String> {
+3 -3
View File
@@ -122,10 +122,10 @@ internal class Java8Class {
} }
fun testGenericFunctions() { fun testGenericFunctions() {
val emptyList: JFunction1<List<String>> = JFunction1 { emptyList() } val emptyList = JFunction1<List<String>> { emptyList() }
emptyList.foo() emptyList.foo()
MethodReferenceHelperClass.staticFun1(JFunction1<List<String>> { emptyList() }) MethodReferenceHelperClass.staticFun1<List<String>> { emptyList() }
h.memberFun1(JFunction1<List<String>> { emptyList() }) h.memberFun1<List<String>> { emptyList() }
} }
fun memberFun(): Int { fun memberFun(): Int {
+1 -1
View File
@@ -1,6 +1,6 @@
class TestReturnsArray { class TestReturnsArray {
fun strings(n: Int): Array<String?> { fun strings(n: Int): Array<String?> {
val result: Array<String?> = arrayOfNulls(n) val result = arrayOfNulls<String>(n)
for (i in 0 until n) { for (i in 0 until n) {
result[i] = Integer.toString(i) result[i] = Integer.toString(i)
} }
+1 -1
View File
@@ -1,7 +1,7 @@
import java.util.HashMap import java.util.HashMap
class TestMethodReference { class TestMethodReference {
private val hashMap: HashMap<String, String> = HashMap() private val hashMap = HashMap<String, String>()
fun update(key: String, msg: String) { fun update(key: String, msg: String) {
hashMap.merge(key, msg) { obj: String, s: String -> obj + s } hashMap.merge(key, msg) { obj: String, s: String -> obj + s }
} }
+1 -1
View File
@@ -1,7 +1,7 @@
import java.util.HashMap import java.util.HashMap
class TestMethodReference { class TestMethodReference {
private val hashMap: HashMap<String, String> = HashMap() private val hashMap = HashMap<String, String>()
fun update(key: String, msg: String) { fun update(key: String, msg: String) {
hashMap.merge(key, msg) { obj: String, s: String -> obj + s } hashMap.merge(key, msg) { obj: String, s: String -> obj + s }
} }
+2 -2
View File
@@ -2,12 +2,12 @@ object ArrayNullable {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
val notNull = 0 val notNull = 0
val a1: Array<Int?> = arrayOfNulls(2) val a1 = arrayOfNulls<Int>(2)
a1[0] = null a1[0] = null
a1[1] = notNull a1[1] = notNull
println(a1[0]) println(a1[0])
println(a1[1]) println(a1[1])
val a2: Array<Int?> = arrayOfNulls(2) val a2 = arrayOfNulls<Int>(2)
a2[0] = nullable() a2[0] = nullable()
a2[1] = notNull a2[1] = notNull
println(a2[0]) println(a2[0])
+1 -1
View File
@@ -3,7 +3,7 @@ import java.util.ArrayList
class ForEach { class ForEach {
fun test() { fun test() {
val xs: ArrayList<Any> = ArrayList() val xs = ArrayList<Any>()
val ys: MutableList<Any> = LinkedList<Any>() val ys: MutableList<Any> = LinkedList<Any>()
for (x in xs) { for (x in xs) {
ys.add(x) ys.add(x)
+1 -1
View File
@@ -5,7 +5,7 @@ class Lists {
fun test() { fun test() {
val xs: MutableList<Any?> = ArrayList() val xs: MutableList<Any?> = ArrayList()
val ys: MutableList<Any?> = LinkedList<Any>() val ys: MutableList<Any?> = LinkedList<Any>()
val zs: ArrayList<Any?> = ArrayList() val zs = ArrayList<Any?>()
xs.add(null) xs.add(null)
ys.add(null) ys.add(null)
xs.clear() xs.clear()
@@ -1,8 +1,8 @@
internal class A { internal class A {
fun foo(): Map<String, String> { fun foo(): Map<String, String> {
val list1: List<String> = emptyList() val list1 = emptyList<String>()
val list2 = listOf(1) val list2 = listOf(1)
val set1: Set<String> = emptySet() val set1 = emptySet<String>()
val set2 = setOf("a") val set2 = setOf("a")
return emptyMap() return emptyMap()
} }
@@ -1,6 +1,6 @@
internal class A { internal class A {
fun foo() { fun foo() {
val list: List<String?> = listOf(null) val list = listOf<String?>(null)
val set: Set<String?> = setOf(null) val set = setOf<String?>(null)
} }
} }
+1 -1
View File
@@ -4,7 +4,7 @@ import java.util.HashMap
internal class Test { internal class Test {
fun main() { fun main() {
val commonMap: HashMap<String, Int> = HashMap() val commonMap = HashMap<String, Int>()
val rawMap: HashMap<*, *> = HashMap<String?, Int?>() val rawMap: HashMap<*, *> = HashMap<String?, Int?>()
val superRawMap: HashMap<*, *> = HashMap<Any?, Any?>() val superRawMap: HashMap<*, *> = HashMap<Any?, Any?>()
} }
+2 -2
View File
@@ -2,7 +2,7 @@ import java.util.HashMap
internal class A { internal class A {
fun foo() { fun foo() {
val map1: Map<String, Int> = getMap1() val map1 = getMap1<String, Int>()
val map2 = getMap2("a", 1) val map2 = getMap2("a", 1)
} }
@@ -11,7 +11,7 @@ internal class A {
} }
fun <K, V> getMap2(k: K, v: V): Map<K, V> { 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 map[k] = v
return map return map
} }