add missing prepareForWrite() calls

This commit is contained in:
Dmitry Jemerov
2016-10-18 19:32:14 +02:00
parent 33f01ca5f3
commit 32290ec1d3
21 changed files with 50 additions and 1 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.intention.HighPriorityAction
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInsight.intention.LowPriorityAction
@@ -167,6 +168,7 @@ abstract class IntentionBasedInspection<TElement : PsiElement>(
override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {
assert(startElement == endElement)
if (!isAvailable(project, file, startElement, endElement)) return
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return
val editor = startElement.findExistingEditor()
editor?.caretModel?.moveToOffset(startElement.textOffset)
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.daemon.HighlightDisplayKey
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInspection.LocalInspectionEP
@@ -104,6 +105,7 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
final override fun invoke(project: Project, editor: Editor, file: PsiFile): Unit {
PsiDocumentManager.getInstance(project).commitAllDocuments()
val target = getTarget(editor, file) ?: return
if (!FileModificationService.getInstance().preparePsiElementForWrite(target)) return
applyTo(target, editor)
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.kdoc
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemsHolder
@@ -69,6 +70,7 @@ class KDocMissingDocumentationInspection(): AbstractKotlinInspection() {
override fun getFamilyName(): String = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
val declaration = descriptor.psiElement.getParentOfType<KtNamedDeclaration>(true)
?: throw IllegalStateException("Can't find declaration")
declaration.addBefore(KDocElementFactory(project).createKDocFromText("/** */"), declaration.firstChild)
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.SuppressIntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
@@ -47,6 +48,8 @@ class KotlinSuppressIntentionAction private constructor(
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement) = element.isValid
override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return
val id = "\"$suppressKey\""
when (suppressAt) {
is KtModifierListOwner ->
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.core.overrideImplement
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.hint.HintManager
import com.intellij.ide.util.MemberChooser
import com.intellij.lang.LanguageCodeInsightActionHandler
@@ -66,6 +67,8 @@ abstract class OverrideImplementMembersHandler : LanguageCodeInsightActionHandle
val elementAtCaret = file.findElementAt(editor.caretModel.offset)
val classOrObject = elementAtCaret?.getNonStrictParentOfType<KtClassOrObject>() ?: return
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return
val members = collectMembersToGenerate(classOrObject)
if (members.isEmpty() && !implementAll) {
HintManager.getInstance().showErrorHint(editor, getNoMembersFoundHint())
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
@@ -32,6 +33,7 @@ open class AddModifierFix(val modifierListOwner: KtModifierListOwner,
override fun getFamilyName() = text
final override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
addModifier(modifierListOwner, modifier)
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
@@ -77,6 +78,7 @@ class AddVarianceModifierInspection : AbstractKotlinInspection() {
override fun getFamilyName() = "Add variance"
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
val typeParameter = descriptor.psiElement as? KtTypeParameter
?: throw AssertionError("Add variance fix is used on ${descriptor.psiElement.text}")
addModifier(typeParameter, if (variance == Variance.IN_VARIANCE) KtTokens.IN_KEYWORD else KtTokens.OUT_KEYWORD)
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
@@ -124,6 +125,7 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
override fun getFamilyName() = fix.familyName
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
parameter.valOrVarKeyword?.delete()
// Delete visibility / open / final / lateinit, if any
// Retain annotations / vararg
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
@@ -78,6 +79,8 @@ class CanBePrimaryConstructorPropertyInspection : AbstractKotlinInspection() {
override fun getFamilyName() = "Move to constructor"
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
val commentSaver = CommentSaver(original)
val isVar = original.isVar
val modifiers = original.modifierList?.text
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.codeInspection.*
import com.intellij.openapi.diagnostic.Logger
@@ -204,6 +205,7 @@ class ConflictingExtensionPropertyInspection : AbstractKotlinInspection(), Clean
UIUtil.invokeLaterIfNeeded {
project.executeWriteCommand(text) {
importsToDelete.forEach { import ->
if (!FileModificationService.getInstance().preparePsiElementForWrite(import)) return@forEach
try {
import.delete()
}
@@ -16,6 +16,8 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.CodeInsightUtilBase
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.InspectionsBundle
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
@@ -42,6 +44,7 @@ object DeleteEqualsAndHashCodeFix : LocalQuickFix {
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
val objectDeclaration = descriptor.psiElement.getStrictParentOfType<KtObjectDeclaration>() ?: return
val classDescriptor = objectDeclaration.resolveToDescriptorIfAny() as? ClassDescriptor ?: return
classDescriptor.findDeclaredEquals(false)?.source?.getPsi()?.delete()
@@ -61,6 +64,7 @@ sealed class GenerateEqualsOrHashCodeFix : LocalQuickFix {
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
KotlinGenerateEqualsAndHashcodeAction().doInvoke(project, null, descriptor.psiElement.parent as KtClass)
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
@@ -56,6 +57,7 @@ class ProtectedInFinalInspection : AbstractKotlinInspection() {
override fun getFamilyName(): String = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
val modifierListOwner = descriptor.psiElement.getParentOfType<KtModifierListOwner>(true)
?: throw IllegalStateException("Can't find modifier list owner for modifier")
addModifier(modifierListOwner, KtTokens.PRIVATE_KEYWORD)
@@ -68,6 +70,7 @@ class ProtectedInFinalInspection : AbstractKotlinInspection() {
override fun getFamilyName(): String = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
val modifierListOwner = descriptor.psiElement.getParentOfType<KtModifierListOwner>(true)
?: throw IllegalStateException("Can't find modifier list owner for modifier")
val parentClass = modifierListOwner.getParentOfType<KtClass>(true) ?: return
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiComment
@@ -63,6 +64,7 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
descriptor.psiElement.delete()
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
@@ -33,6 +34,7 @@ class RemoveModifierFix(val text: String) : LocalQuickFix {
val modifierKeyword = descriptor.psiElement.node.elementType as KtModifierKeywordToken
val modifierListOwner = descriptor.psiElement.getParentOfType<KtModifierListOwner>(true)
?: throw IllegalStateException("Can't find modifier list owner for modifier")
if (!FileModificationService.getInstance().preparePsiElementForWrite(modifierListOwner)) return
removeModifier(modifierListOwner, modifierKeyword)
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.CallableDescriptor
@@ -56,6 +57,8 @@ class RemoveToStringFix: LocalQuickFix {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement.parent as? KtDotQualifiedExpression ?: return
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return
val receiverExpression = element.receiverExpression
if (receiverExpression is KtNameReferenceExpression) {
val templateEntry = receiverExpression.parent.parent
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
@@ -116,6 +117,8 @@ class UnusedReceiverParameterInspection : AbstractKotlinInspection() {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return
val function = element.parent as? KtCallableDeclaration ?: return
val callableDescriptor = function.analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, function] as? CallableDescriptor ?: return
runChangeSignature(project, callableDescriptor, configureChangeSignature(), element, name)
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.CodeInsightUtil
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.daemon.impl.quickfix.CreateClassKind
import com.intellij.codeInsight.intention.impl.CreateClassDialog
import com.intellij.openapi.application.ApplicationManager
@@ -79,6 +80,7 @@ class CreateKotlinSubClassIntention : SelfTargetingRangeIntention<KtClass>(KtCla
override fun applyTo(element: KtClass, editor: Editor?) {
if (editor == null) throw IllegalArgumentException("This intention requires an editor")
val name = element.name ?: throw IllegalStateException("This intention should not be applied to anonymous classes")
val baseClass = element
if (baseClass.isSealed()) {
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.intention.IntentionAction
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.diagnostics.Diagnostic
@@ -44,6 +45,7 @@ object AddOverrideToEqualsHashCodeToStringActionFactory : KotlinSingleIntentionA
}
private fun KtNamedFunction.doInvoke() {
if (!FileModificationService.getInstance().preparePsiElementForWrite(this)) return
addModifier(OVERRIDE_KEYWORD)
removeModifier(PUBLIC_KEYWORD)
}
@@ -83,6 +83,8 @@ class AddExclExclCallFix(val psiElement: PsiElement) : ExclExclCallFix() {
getExpressionForIntroduceCall() != null
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return
val modifiedExpression = getExpressionForIntroduceCall() ?: return
val exclExclExpression = KtPsiFactory(project).createExpressionByPattern("$0!!", modifiedExpression)
modifiedExpression.replace(exclExclExpression)
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
@@ -30,6 +31,8 @@ class SpecifyTypeExplicitlyFix : PsiElementBaseIntentionAction() {
override fun getFamilyName() = "Specify type explicitly"
override fun invoke(project: Project, editor: Editor, element: PsiElement) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return
val declaration = declarationByElement(element)!!
val type = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(declaration)
SpecifyTypeExplicitlyIntention.addTypeAnnotation(editor, declaration, type)
@@ -455,7 +455,7 @@ fun PsiElement.canRefactor(): Boolean {
this is KtElement ||
this is PsiMember && language == JavaLanguage.INSTANCE ||
this is PsiDirectory ->
isWritable && ProjectRootsUtil.isInProjectSource(this)
ProjectRootsUtil.isInProjectSource(this)
else ->
false
}