Inspection to replace Java Collections methods: fix formatting

This commit is contained in:
Mikhail Glukhikh
2018-01-29 14:41:00 +03:00
parent be4739e65e
commit 068f520c01
@@ -40,10 +40,12 @@ class JavaCollectionsStaticMethodInspection : AbstractKotlinInspection() {
if (!canReplaceWithStdLib(expression, fqName, args)) return if (!canReplaceWithStdLib(expression, fqName, args)) return
val methodName = fqName.split(".").last() val methodName = fqName.split(".").last()
holder.registerProblem(expression, holder.registerProblem(
"Java Collections static method call should be replaced with Kotlin stdlib", expression,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, "Java Collections static method call should be replaced with Kotlin stdlib",
ReplaceWithStdLibFix(methodName, firstArg.text)) ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
ReplaceWithStdLibFix(methodName, firstArg.text)
)
}) })
} }
@@ -91,13 +93,14 @@ private class ReplaceWithStdLibFix(private val methodName: String, private val r
val secondArg = valueArguments.getOrNull(1)?.getArgumentExpression() val secondArg = valueArguments.getOrNull(1)?.getArgumentExpression()
val factory = KtPsiFactory(project) val factory = KtPsiFactory(project)
val newExpression = if (secondArg != null) { val newExpression = if (secondArg != null) {
if (methodName == "sort") if (methodName == "sort") {
factory.createExpressionByPattern("$0.sortWith(Comparator $1)", firstArg, secondArg.text) factory.createExpressionByPattern("$0.sortWith(Comparator $1)", firstArg, secondArg.text)
else } else {
factory.createExpressionByPattern("$0.$methodName($1)", firstArg, secondArg) factory.createExpressionByPattern("$0.$methodName($1)", firstArg, secondArg)
} }
else } else {
factory.createExpressionByPattern("$0.$methodName()", firstArg) factory.createExpressionByPattern("$0.$methodName()", firstArg)
}
expression.replace(newExpression) expression.replace(newExpression)
} }
} }