mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-21 00:21:29 +00:00
SortModifiersInspection detects modifiers before annotations
#KT-22013 Fixed
This commit is contained in:
@@ -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)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
3
idea/testData/inspectionsLocal/sortModifiers/annotation.kt
vendored
Normal file
3
idea/testData/inspectionsLocal/sortModifiers/annotation.kt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
annotation class Test
|
||||
|
||||
<caret>public @Test /* this is a test */ class MyTest
|
||||
4
idea/testData/inspectionsLocal/sortModifiers/annotation.kt.after
vendored
Normal file
4
idea/testData/inspectionsLocal/sortModifiers/annotation.kt.after
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
annotation class Test
|
||||
|
||||
@Test
|
||||
public /* this is a test */ class MyTest
|
||||
6
idea/testData/inspectionsLocal/sortModifiers/annotationGroup.kt
vendored
Normal file
6
idea/testData/inspectionsLocal/sortModifiers/annotationGroup.kt
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
annotation class Inject
|
||||
annotation class VisibleForTesting
|
||||
|
||||
class Example {
|
||||
<caret>public @set:[Inject VisibleForTesting] var x: String = ""
|
||||
}
|
||||
7
idea/testData/inspectionsLocal/sortModifiers/annotationGroup.kt.after
vendored
Normal file
7
idea/testData/inspectionsLocal/sortModifiers/annotationGroup.kt.after
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
annotation class Inject
|
||||
annotation class VisibleForTesting
|
||||
|
||||
class Example {
|
||||
<caret>@set:[Inject VisibleForTesting]
|
||||
public var x: String = ""
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user