SortModifiersInspection detects modifiers before annotations

#KT-22013 Fixed
This commit is contained in:
Dmitry Jemerov
2018-01-03 12:38:06 +01:00
parent 408c753837
commit 165ac97c4b
6 changed files with 59 additions and 35 deletions

View File

@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.inspections
@@ -20,9 +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.psi.KtModifierList
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.addRemoveModifier.sortModifiers
import org.jetbrains.kotlin.psi.psiUtil.allChildren
@@ -33,16 +20,32 @@ class SortModifiersInspection : AbstractKotlinInspection(), CleanupLocalInspecti
override fun visitModifierList(list: KtModifierList) {
super.visitModifierList(list)
val modifiers = list.allChildren.toList().mapNotNull { it.node.elementType as? KtModifierKeywordToken }.toList()
val modifierElements = list.allChildren.toList()
var modifiersBeforeAnnotations = false
var seenModifiers = false
for (modifierElement in modifierElements) {
if (modifierElement.node.elementType is KtModifierKeywordToken) {
seenModifiers = true
} else if (seenModifiers && (modifierElement is KtAnnotationEntry || modifierElement is KtAnnotation)) {
modifiersBeforeAnnotations = true
}
}
val modifiers = modifierElements.mapNotNull { it.node.elementType as? KtModifierKeywordToken }.toList()
if (modifiers.isEmpty()) return
val sortedModifiers = sortModifiers(modifiers)
if (modifiers == sortedModifiers) return
if (modifiers == sortedModifiers && !modifiersBeforeAnnotations) return
holder.registerProblem(list,
"Non-canonical modifiers order",
ProblemHighlightType.WEAK_WARNING,
SortModifiersFix(sortedModifiers)
val message = if (modifiersBeforeAnnotations)
"Modifiers should follow annotations"
else
"Non-canonical modifiers order"
holder.registerProblem(
list,
message,
ProblemHighlightType.WEAK_WARNING,
SortModifiersFix(sortedModifiers)
)
}
}

View File

@@ -0,0 +1,3 @@
annotation class Test
<caret>public @Test /* this is a test */ class MyTest

View File

@@ -0,0 +1,4 @@
annotation class Test
@Test
public /* this is a test */ class MyTest

View File

@@ -0,0 +1,6 @@
annotation class Inject
annotation class VisibleForTesting
class Example {
<caret>public @set:[Inject VisibleForTesting] var x: String = ""
}

View File

@@ -0,0 +1,7 @@
annotation class Inject
annotation class VisibleForTesting
class Example {
<caret>@set:[Inject VisibleForTesting]
public var x: String = ""
}

View File

@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.inspections;
@@ -3830,6 +3819,18 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/sortModifiers"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("annotation.kt")
public void testAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/annotation.kt");
doTest(fileName);
}
@TestMetadata("annotationGroup.kt")
public void testAnnotationGroup() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/annotationGroup.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/sortModifiers/simple.kt");