Sort modifiers: process final modifier correctly #KT-22954 fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
e88d5b2a76
commit
b9e7e8fca3
@@ -9,6 +9,7 @@ import com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.addRemoveModifier.sortModifiers
|
||||
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||
@@ -34,6 +35,8 @@ class SortModifiersInspection : AbstractKotlinInspection(), CleanupLocalInspecti
|
||||
val modifiers = modifierElements.mapNotNull { it.node.elementType as? KtModifierKeywordToken }.toList()
|
||||
if (modifiers.isEmpty()) return
|
||||
|
||||
val startElement = modifierElements.firstOrNull { it.node.elementType is KtModifierKeywordToken } ?: return
|
||||
|
||||
val sortedModifiers = sortModifiers(modifiers)
|
||||
if (modifiers == sortedModifiers && !modifiersBeforeAnnotations) return
|
||||
|
||||
@@ -41,12 +44,16 @@ class SortModifiersInspection : AbstractKotlinInspection(), CleanupLocalInspecti
|
||||
"Modifiers should follow annotations"
|
||||
else
|
||||
"Non-canonical modifiers order"
|
||||
holder.registerProblem(
|
||||
|
||||
val descriptor = holder.manager.createProblemDescriptor(
|
||||
startElement,
|
||||
list,
|
||||
message,
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
isOnTheFly,
|
||||
SortModifiersFix(sortedModifiers)
|
||||
)
|
||||
holder.registerProblem(descriptor)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,7 +69,10 @@ private class SortModifiersFix(private val modifiers: List<KtModifierKeywordToke
|
||||
val owner = list.parent as? KtModifierListOwner ?: return
|
||||
|
||||
modifiers.forEach { owner.removeModifier(it) }
|
||||
modifiers.forEach { owner.addModifier(it) }
|
||||
modifiers
|
||||
.partition { it == KtTokens.FINAL_KEYWORD }
|
||||
.let { it.second + it.first }
|
||||
.forEach { owner.addModifier(it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user