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.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.addRemoveModifier.sortModifiers
|
import org.jetbrains.kotlin.psi.addRemoveModifier.sortModifiers
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
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()
|
val modifiers = modifierElements.mapNotNull { it.node.elementType as? KtModifierKeywordToken }.toList()
|
||||||
if (modifiers.isEmpty()) return
|
if (modifiers.isEmpty()) return
|
||||||
|
|
||||||
|
val startElement = modifierElements.firstOrNull { it.node.elementType is KtModifierKeywordToken } ?: return
|
||||||
|
|
||||||
val sortedModifiers = sortModifiers(modifiers)
|
val sortedModifiers = sortModifiers(modifiers)
|
||||||
if (modifiers == sortedModifiers && !modifiersBeforeAnnotations) return
|
if (modifiers == sortedModifiers && !modifiersBeforeAnnotations) return
|
||||||
|
|
||||||
@@ -41,12 +44,16 @@ class SortModifiersInspection : AbstractKotlinInspection(), CleanupLocalInspecti
|
|||||||
"Modifiers should follow annotations"
|
"Modifiers should follow annotations"
|
||||||
else
|
else
|
||||||
"Non-canonical modifiers order"
|
"Non-canonical modifiers order"
|
||||||
holder.registerProblem(
|
|
||||||
|
val descriptor = holder.manager.createProblemDescriptor(
|
||||||
|
startElement,
|
||||||
list,
|
list,
|
||||||
message,
|
message,
|
||||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||||
|
isOnTheFly,
|
||||||
SortModifiersFix(sortedModifiers)
|
SortModifiersFix(sortedModifiers)
|
||||||
)
|
)
|
||||||
|
holder.registerProblem(descriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +69,10 @@ private class SortModifiersFix(private val modifiers: List<KtModifierKeywordToke
|
|||||||
val owner = list.parent as? KtModifierListOwner ?: return
|
val owner = list.parent as? KtModifierListOwner ?: return
|
||||||
|
|
||||||
modifiers.forEach { owner.removeModifier(it) }
|
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) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<problems>
|
||||||
|
<problem>
|
||||||
|
<file>test.kt</file>
|
||||||
|
<line>4</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Non-canonical modifier order</problem_class>
|
||||||
|
<description>Non-canonical modifiers order</description>
|
||||||
|
</problem>
|
||||||
|
</problems>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.SortModifiersInspection
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
@Ann
|
||||||
|
abstract public class Test
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
open class Base {
|
||||||
|
open fun bar() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
open class Test : Base() {
|
||||||
|
@Ann
|
||||||
|
<caret>override final fun bar() {}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
open class Base {
|
||||||
|
open fun bar() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
open class Test : Base() {
|
||||||
|
@Ann
|
||||||
|
final override fun bar() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
open class Base {
|
||||||
|
open fun foo() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
open class Test : Base() {
|
||||||
|
<caret>override final public fun foo() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
open class Base {
|
||||||
|
open fun foo() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
open class Test : Base() {
|
||||||
|
public final override fun foo() {}
|
||||||
|
}
|
||||||
+6
@@ -372,6 +372,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sortModifiers/inspectionData/inspections.test")
|
||||||
|
public void testSortModifiers_inspectionData_Inspections_test() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/sortModifiers/inspectionData/inspections.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("spelling/inspectionData/inspections.test")
|
@TestMetadata("spelling/inspectionData/inspections.test")
|
||||||
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
||||||
|
|||||||
+12
@@ -4152,6 +4152,18 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("annotationOverrideFinal.kt")
|
||||||
|
public void testAnnotationOverrideFinal() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/annotationOverrideFinal.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("overrideFinal.kt")
|
||||||
|
public void testOverrideFinal() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/overrideFinal.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simple.kt")
|
@TestMetadata("simple.kt")
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/simple.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/simple.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user