Allow using add modifier quick-fixes in batch mode #KT-21950 Fixed
This commit is contained in:
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.diagnostics.Severity
|
||||
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
|
||||
import org.jetbrains.kotlin.idea.actions.internal.KotlinInternalMode
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
||||
import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.QuickFixes
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -285,7 +286,12 @@ private class ElementAnnotator(private val element: PsiElement,
|
||||
val annotation = data.create(diagnostic, range, holder)
|
||||
val fixes = fixesMap[diagnostic]
|
||||
|
||||
fixes.forEach { annotation.registerFix(it) }
|
||||
fixes.forEach {
|
||||
when (it) {
|
||||
is KotlinUniversalQuickFix -> annotation.registerUniversalFix(it, null, null)
|
||||
is IntentionAction -> annotation.registerFix(it)
|
||||
}
|
||||
}
|
||||
|
||||
if (diagnostic.severity == Severity.WARNING) {
|
||||
annotation.problemGroup = KotlinSuppressableWarningProblemGroup(diagnostic.factory)
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.codeInsight.intention.IntentionAction
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.openapi.project.Project
|
||||
|
||||
interface KotlinUniversalQuickFix : IntentionAction, LocalQuickFix {
|
||||
override fun getName() = text
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
invoke(project, null, descriptor.psiElement?.containingFile)
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiNameIdentifierOwner
|
||||
@@ -27,6 +28,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
|
||||
import org.jetbrains.kotlin.idea.inspections.KotlinUniversalQuickFix
|
||||
import org.jetbrains.kotlin.idea.refactoring.canRefactor
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -42,8 +44,7 @@ import org.jetbrains.kotlin.types.TypeUtils
|
||||
open class AddModifierFix(
|
||||
element: KtModifierListOwner,
|
||||
protected val modifier: KtModifierKeywordToken
|
||||
) : KotlinQuickFixAction<KtModifierListOwner>(element) {
|
||||
|
||||
) : KotlinQuickFixAction<KtModifierListOwner>(element), KotlinUniversalQuickFix {
|
||||
override fun getText(): String {
|
||||
val element = element ?: return ""
|
||||
if (modifier in modalityModifiers || modifier in KtTokens.VISIBILITY_MODIFIERS || modifier == KtTokens.CONST_KEYWORD) {
|
||||
|
||||
@@ -28,12 +28,15 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class AddSuspendModifierFix(element: KtModifierListOwner, private val name: String?): AddModifierFix(element, KtTokens.SUSPEND_KEYWORD) {
|
||||
class AddSuspendModifierFix(
|
||||
element: KtModifierListOwner,
|
||||
private val declarationName: String?
|
||||
): AddModifierFix(element, KtTokens.SUSPEND_KEYWORD) {
|
||||
|
||||
override fun getText() =
|
||||
when (element) {
|
||||
is KtNamedFunction -> "Make ${name ?: "containing function"} suspend"
|
||||
is KtTypeReference -> "Make ${name ?: "receiver"} type suspend"
|
||||
is KtNamedFunction -> "Make ${declarationName ?: "containing function"} suspend"
|
||||
is KtTypeReference -> "Make ${declarationName ?: "receiver"} type suspend"
|
||||
else -> super.getText()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user