add quickfix for NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION
This commit is contained in:
@@ -90,6 +90,7 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
|
||||
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION,
|
||||
Errors.DEPRECATED_SYMBOL_WITH_MESSAGE,
|
||||
Errors.ACCESS_TO_PRIVATE_TOP_LEVEL_FROM_ANOTHER_FILE,
|
||||
Errors.NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION,
|
||||
Errors.BACKING_FIELD_SYNTAX_DEPRECATED
|
||||
)
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ import static org.jetbrains.kotlin.lexer.JetTokens.ABSTRACT_KEYWORD;
|
||||
public class AddModifierFix extends JetIntentionAction<JetModifierListOwner> {
|
||||
@NotNull private final JetModifierKeywordToken modifier;
|
||||
|
||||
private AddModifierFix(@NotNull JetModifierListOwner element, @NotNull JetModifierKeywordToken modifier) {
|
||||
public AddModifierFix(@NotNull JetModifierListOwner element, @NotNull JetModifierKeywordToken modifier) {
|
||||
super(element);
|
||||
this.modifier = modifier;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.JetDeclaration
|
||||
import org.jetbrains.kotlin.psi.JetReferenceExpression
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.ConstModifierChecker
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||
|
||||
public object ConstFixFactory : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val expr = diagnostic.psiElement as? JetReferenceExpression ?: return null
|
||||
val bindingContext = expr.analyze(BodyResolveMode.PARTIAL)
|
||||
val targetDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, expr) as? VariableDescriptor ?: return null
|
||||
val declaration = (targetDescriptor.source as? PsiSourceElement)?.psi as? JetDeclaration ?: return null
|
||||
if (ConstModifierChecker.checkCanBeConst(declaration, declaration, targetDescriptor) == null) {
|
||||
return object : AddModifierFix(declaration, JetTokens.CONST_KEYWORD), CleanupFix {}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -321,6 +321,8 @@ public class QuickFixRegistrar : QuickFixContributor {
|
||||
UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
|
||||
TYPE_INFERENCE_UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory)
|
||||
|
||||
NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION.registerFactory(ConstFixFactory)
|
||||
|
||||
BACKING_FIELD_SYNTAX_DEPRECATED.registerFactory(MigrateBackingFieldSyntaxFix)
|
||||
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(MigrateBackingFieldUsageFix)
|
||||
BACKING_FIELD_USAGE_DEPRECATED.registerFactory(IntroduceBackingPropertyFix)
|
||||
|
||||
@@ -36,6 +36,12 @@ fun unnecessaryElvis(x: String) = x ?: ""
|
||||
|
||||
@JavaAnn(1, "abc") class MyClass
|
||||
|
||||
val i = 1
|
||||
|
||||
annotation class Fancy(val param: Int)
|
||||
|
||||
@Fancy(<caret>i) class D
|
||||
|
||||
class Foo {
|
||||
var x: Int = 0
|
||||
get = $x
|
||||
|
||||
@@ -35,6 +35,12 @@ fun unnecessaryElvis(x: String) = x
|
||||
|
||||
@JavaAnn(1, arg1 = "abc") class MyClass
|
||||
|
||||
const val i = 1
|
||||
|
||||
annotation class Fancy(val param: Int)
|
||||
|
||||
@Fancy(<caret>i) class D
|
||||
|
||||
class Foo {
|
||||
var x: Int = 0
|
||||
get = field
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add 'const' modifier" "true"
|
||||
val i = 1
|
||||
|
||||
annotation class Fancy(val param: Int)
|
||||
|
||||
@Fancy(<caret>i) class D
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add 'const' modifier" "true"
|
||||
const val i = 1
|
||||
|
||||
annotation class Fancy(val param: Int)
|
||||
|
||||
@Fancy(<caret>i) class D
|
||||
@@ -4223,6 +4223,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("constVal.kt")
|
||||
public void testConstVal() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/constVal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("finalTrait.kt")
|
||||
public void testFinalTrait() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/finalTrait.kt");
|
||||
|
||||
Reference in New Issue
Block a user