TrailingCommaInspection: introduce enum TrailingCommaAction
#KT-34744
This commit is contained in:
@@ -34,20 +34,22 @@ class TrailingCommaInspection(
|
|||||||
override val recursively: Boolean = false
|
override val recursively: Boolean = false
|
||||||
|
|
||||||
override fun process(commaOwner: KtElement) {
|
override fun process(commaOwner: KtElement) {
|
||||||
checkCommaPosition(commaOwner)
|
val action = TrailingCommaAction.create(commaOwner)
|
||||||
checkTrailingComma(commaOwner)
|
if (action != TrailingCommaAction.REMOVE) {
|
||||||
|
checkCommaPosition(commaOwner)
|
||||||
|
}
|
||||||
|
checkTrailingComma(commaOwner, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkCommaPosition(commaOwner: KtElement) {
|
private fun checkCommaPosition(commaOwner: KtElement) {
|
||||||
if (!needComma(commaOwner, checkExistingTrailingComma = true)) return
|
|
||||||
for (invalidComma in findInvalidCommas(commaOwner)) {
|
for (invalidComma in findInvalidCommas(commaOwner)) {
|
||||||
reportProblem(invalidComma, "Comma loses the advantages in this position", "Fix comma position")
|
reportProblem(invalidComma, "Comma loses the advantages in this position", "Fix comma position")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkTrailingComma(commaOwner: KtElement) {
|
private fun checkTrailingComma(commaOwner: KtElement, action: TrailingCommaAction) {
|
||||||
val trailingCommaOrLastElement = trailingCommaOrLastElement(commaOwner) ?: return
|
val trailingCommaOrLastElement = trailingCommaOrLastElement(commaOwner) ?: return
|
||||||
if (needComma(commaOwner, checkExistingTrailingComma = false)) {
|
if (action == TrailingCommaAction.ADD) {
|
||||||
if (!trailingCommaAllowedInModule(commaOwner) || trailingCommaOrLastElement.isComma) return
|
if (!trailingCommaAllowedInModule(commaOwner) || trailingCommaOrLastElement.isComma) return
|
||||||
reportProblem(
|
reportProblem(
|
||||||
trailingCommaOrLastElement,
|
trailingCommaOrLastElement,
|
||||||
@@ -58,7 +60,7 @@ class TrailingCommaInspection(
|
|||||||
else
|
else
|
||||||
ProblemHighlightType.INFORMATION,
|
ProblemHighlightType.INFORMATION,
|
||||||
)
|
)
|
||||||
} else if (!needComma(commaOwner, checkExistingTrailingComma = true)) {
|
} else if (action == TrailingCommaAction.REMOVE) {
|
||||||
if (!trailingCommaOrLastElement.isComma) return
|
if (!trailingCommaOrLastElement.isComma) return
|
||||||
reportProblem(trailingCommaOrLastElement, "Useless trailing comma", "Remove trailing comma")
|
reportProblem(trailingCommaOrLastElement, "Useless trailing comma", "Remove trailing comma")
|
||||||
}
|
}
|
||||||
@@ -97,3 +99,15 @@ class TrailingCommaInspection(
|
|||||||
return panel
|
return panel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum class TrailingCommaAction {
|
||||||
|
ADD, REFORMAT, REMOVE;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(commaOwner: KtElement): TrailingCommaAction = when {
|
||||||
|
needComma(commaOwner, checkExistingTrailingComma = false) -> ADD
|
||||||
|
needComma(commaOwner, checkExistingTrailingComma = true) -> REFORMAT
|
||||||
|
else -> REMOVE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user