Add inspection to sort modifiers #KT-21560 Fixed
This commit is contained in:
committed by
Dmitry Jemerov
parent
2be7116b0b
commit
0e2bdf8995
@@ -121,6 +121,13 @@ fun removeModifier(owner: KtModifierListOwner, modifier: KtModifierKeywordToken)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun sortModifiers(modifiers: List<KtModifierKeywordToken>): List<KtModifierKeywordToken> {
|
||||||
|
return modifiers.sortedBy {
|
||||||
|
val index = MODIFIERS_ORDER.indexOf(it)
|
||||||
|
if (index == -1) Int.MAX_VALUE else index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val MODIFIERS_TO_REPLACE = mapOf(
|
private val MODIFIERS_TO_REPLACE = mapOf(
|
||||||
OVERRIDE_KEYWORD to listOf(OPEN_KEYWORD),
|
OVERRIDE_KEYWORD to listOf(OPEN_KEYWORD),
|
||||||
ABSTRACT_KEYWORD to listOf(OPEN_KEYWORD, FINAL_KEYWORD),
|
ABSTRACT_KEYWORD to listOf(OPEN_KEYWORD, FINAL_KEYWORD),
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This inspection reports to sort the modifiers in canonical order
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -2701,6 +2701,16 @@
|
|||||||
language="kotlin"
|
language="kotlin"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.SortModifiersInspection"
|
||||||
|
displayName="Sort modifiers"
|
||||||
|
groupPath="Kotlin"
|
||||||
|
groupName="Style issues"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="WEAK WARNING"
|
||||||
|
language="kotlin"
|
||||||
|
/>
|
||||||
|
|
||||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||||
|
|
||||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
|
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.psi.KtModifierList
|
||||||
|
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||||
|
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||||
|
import org.jetbrains.kotlin.psi.addRemoveModifier.sortModifiers
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||||
|
|
||||||
|
class SortModifiersInspection : AbstractKotlinInspection() {
|
||||||
|
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||||
|
return object : KtVisitorVoid() {
|
||||||
|
override fun visitModifierList(list: KtModifierList) {
|
||||||
|
super.visitModifierList(list)
|
||||||
|
|
||||||
|
val modifiers = list.allChildren.toList().mapNotNull { it.node.elementType as? KtModifierKeywordToken }.toList()
|
||||||
|
if (modifiers.isEmpty()) return
|
||||||
|
|
||||||
|
val sortedModifiers = sortModifiers(modifiers)
|
||||||
|
if (modifiers == sortedModifiers) return
|
||||||
|
|
||||||
|
holder.registerProblem(list,
|
||||||
|
"Sort modifiers",
|
||||||
|
ProblemHighlightType.WEAK_WARNING,
|
||||||
|
SortModifiersFix(sortedModifiers)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SortModifiersFix(private val modifiers: List<KtModifierKeywordToken>) : LocalQuickFix {
|
||||||
|
override fun getName() = "Sort modifiers"
|
||||||
|
|
||||||
|
override fun getFamilyName() = name
|
||||||
|
|
||||||
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
|
val list = descriptor.psiElement as? KtModifierList ?: return
|
||||||
|
val owner = list.parent as? KtModifierListOwner ?: return
|
||||||
|
|
||||||
|
modifiers.forEach { owner.removeModifier(it) }
|
||||||
|
modifiers.forEach { owner.addModifier(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.inspections.SortModifiersInspection
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<caret>abstract public class Test
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
public abstract class Test
|
||||||
@@ -2562,6 +2562,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/inspectionsLocal/sortModifiers")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class SortModifiers extends AbstractLocalInspectionTest {
|
||||||
|
public void testAllFilesPresentInSortModifiers() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/sortModifiers"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/simple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/inspectionsLocal/unnecessaryVariable")
|
@TestMetadata("idea/testData/inspectionsLocal/unnecessaryVariable")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user