write access for elements which probably differ from the current file

This commit is contained in:
Anna.Kozlova
2016-11-28 11:47:06 +01:00
parent a9d9485b9a
commit 01bf7bfecd
38 changed files with 241 additions and 105 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -26,9 +26,11 @@ import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
import com.intellij.openapi.roots.JavaProjectModelModificationService;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author cdr
@@ -73,9 +75,10 @@ public class IncreaseLanguageLevelFix implements IntentionAction {
JavaProjectModelModificationService.getInstance(project).changeLanguageLevel(module, myLevel);
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return false;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return null;
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
@@ -52,6 +53,12 @@ public class AddMethodBodyFix implements IntentionAction {
myMethod.getManager().isInProject(myMethod);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myMethod;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
PsiUtil.setModifierProperty(myMethod, PsiModifier.ABSTRACT, false);
@@ -56,6 +56,12 @@ public class AddNewArrayExpressionFix implements IntentionAction {
return getType() != null;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myInitializer;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiManager manager = file.getManager();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -55,6 +55,12 @@ public class AddReturnFix implements IntentionAction {
;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myMethod;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
String value = suggestReturnValue();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -65,6 +65,12 @@ public class AddVariableInitializerFix implements IntentionAction {
;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myVariable;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final LookupElement[] suggestedInitializers = suggestInitializer(myVariable);
@@ -71,6 +71,12 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
doProcessIntention(mySwitchExpression);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return mySwitchExpression;
}
@Override
public boolean startInWriteAction() {
return true;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -47,6 +47,12 @@ public class DeleteCatchFix implements IntentionAction {
return myCatchParameter.isValid() && PsiManager.getInstance(project).isInProject(myCatchParameter.getContainingFile());
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myCatchParameter;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
final PsiTryStatement tryStatement = ((PsiCatchSection)myCatchParameter.getDeclarationScope()).getTryStatement();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiCodeBlock;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.util.IncorrectOperationException;
@@ -52,6 +53,12 @@ public class DeleteMethodBodyFix implements IntentionAction {
return myMethod.isValid() && myMethod.getManager().isInProject(myMethod) && myMethod.getBody() != null;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myMethod;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiCodeBlock body = myMethod.getBody();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -52,6 +52,12 @@ public class DeleteMultiCatchFix implements IntentionAction {
return myTypeElement.isValid() && PsiManager.getInstance(project).isInProject(myTypeElement.getContainingFile());
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myTypeElement;
}
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final PsiElement parentType = myTypeElement.getParent();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -78,6 +78,12 @@ public class GeneralizeCatchFix implements IntentionAction {
return myCatchParameter != null;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myElement;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElementFactory factory = JavaPsiFacade.getInstance(myElement.getProject()).getElementFactory();
@@ -50,6 +50,12 @@ public class InsertNewFix implements IntentionAction {
return myMethodCall.isValid() && myMethodCall.getManager().isInProject(myMethodCall);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myMethodCall;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElementFactory factory = JavaPsiFacade.getInstance(myMethodCall.getProject()).getElementFactory();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -54,6 +54,12 @@ public class InsertSuperFix implements IntentionAction, HighPriorityAction {
;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myConstructor;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
PsiStatement superCall =
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.util.IncorrectOperationException;
@@ -51,6 +52,12 @@ public class MakeMethodConstructorFix implements IntentionAction {
return myMethod.isValid() && myMethod.getReturnTypeElement() != null && myMethod.getManager().isInProject(myMethod);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myMethod;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myMethod.getReturnTypeElement().delete();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiParameter;
import com.intellij.util.IncorrectOperationException;
@@ -51,6 +52,12 @@ public class MakeVarargParameterLastFix implements IntentionAction {
return myParameter.isValid() && myParameter.getManager().isInProject(myParameter);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myParameter;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myParameter.getParent().add(myParameter);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiCatchSection;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiTryStatement;
import com.intellij.psi.util.PsiUtil;
@@ -63,6 +64,12 @@ public class MoveCatchUpFix implements IntentionAction {
PsiUtil.resolveClassInType(myMoveBeforeSection.getCatchType()));
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myCatchSection;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
PsiTryStatement statement = myCatchSection.getTryStatement();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -22,10 +22,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.file.JavaDirectoryServiceImpl;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -67,6 +64,12 @@ public class MoveClassToSeparateFileFix implements IntentionAction {
return true;
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myClass;
}
@Override
public void invoke(@NotNull Project project, @Nullable Editor editor, @NotNull PsiFile file) {
PsiDirectory dir = file.getContainingDirectory();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -81,6 +81,12 @@ public class NegationBroadScopeFix implements IntentionAction {
return binaryExpression.getLOperand() == myPrefixExpression && TypeConversionUtil.isBooleanType(binaryExpression.getType());
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myPrefixExpression;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (!isAvailable(project, editor, file)) return;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -19,10 +19,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiNewExpression;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -60,6 +57,12 @@ public class RemoveNewQualifierFix implements IntentionAction {
expression.isValid() && (aClass == null || aClass.isValid()) && expression.getManager().isInProject(expression);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return expression;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiJavaCodeReferenceElement classReference = expression.getClassReference();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -62,6 +62,12 @@ public class ReuseVariableDeclarationFix implements IntentionAction {
myVariable.getManager().isInProject(myVariable);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myVariable;
}
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
final PsiVariable refVariable = findPreviousVariable();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -15,10 +15,12 @@
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -101,25 +103,28 @@ public class VariableAccessFromInnerClassFix implements IntentionAction {
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
try {
switch (myFixType) {
case MAKE_FINAL:
makeFinal();
break;
case MAKE_ARRAY:
makeArray();
break;
case COPY_TO_FINAL:
copyToFinal();
break;
if (!FileModificationService.getInstance().preparePsiElementsForWrite(myContext, myVariable)) return;
WriteAction.run(() -> {
try {
switch (myFixType) {
case MAKE_FINAL:
makeFinal();
break;
case MAKE_ARRAY:
makeArray();
break;
case COPY_TO_FINAL:
copyToFinal();
break;
}
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
finally {
getVariablesToFix().clear();
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
finally {
getVariablesToFix().clear();
}
});
}
private void makeArray() {
@@ -354,6 +359,6 @@ public class VariableAccessFromInnerClassFix implements IntentionAction {
@Override
public boolean startInWriteAction() {
return true;
return false;
}
}
@@ -207,9 +207,10 @@ public class MagicConstantInspection extends BaseJavaLocalInspectionTool {
return "Attach annotations";
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return false;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return null;
}
@Override
@@ -16,6 +16,10 @@
package com.intellij.codeInsight.intention;
import com.intellij.openapi.application.WriteActionAware;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* An interface that {@link IntentionAction} and {@link com.intellij.codeInspection.LocalQuickFix} share.
@@ -26,17 +30,17 @@ import com.intellij.openapi.application.WriteActionAware;
public interface IntentionLike extends WriteActionAware {
/**
* Controls whether this intention/fix is going to modify the current editor file.
* If {@code true}, and the file is read-only,
* it will be made writable (honoring version control integration) before the intention/fix is invoked.<p/>
* Controls whether this intention/fix is going to modify the file.
* If {@code @NotNull}, and the file is read-only,
* it will be made writable (honoring version control integration) before the intention/fix is invoked. <p/>
*
* By default, as a heuristic, returns the same as {@link #startInWriteAction()}.<p/>
*
* If the action is going to modify multiple files, or the set of the files is unknown in advance, please
* don't bother overriding this method, return {@code false} from {@link #startInWriteAction()}, and call {@link com.intellij.codeInsight.FileModificationService} methods in the implementation, and take write actions yourself as needed.
*/
default boolean shouldMakeCurrentFileWritable() {
return startInWriteAction();
@Nullable
default PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return startInWriteAction() ? file : null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -61,9 +61,10 @@ public abstract class PriorityActionWrapper extends LocalQuickFixAndIntentionAct
return fix.startInWriteAction();
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return fix.shouldMakeCurrentFileWritable();
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return fix.getElementToMakeWritable(file);
}
@NotNull
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -169,9 +169,10 @@ public class ExternalAnnotatorInspectionVisitor extends PsiElementVisitor {
myAction.invoke(project, null, getPsiFile(descriptor));
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return myAction.shouldMakeCurrentFileWritable();
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myAction.getElementToMakeWritable(file);
}
@Nullable
@@ -30,6 +30,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
/**
@@ -97,9 +98,10 @@ public class QuickFixWrapper implements IntentionAction {
return getFix().startInWriteAction();
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return getFix().shouldMakeCurrentFileWritable();
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return getFix().getElementToMakeWritable(file);
}
public LocalQuickFix getFix() {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -20,9 +20,11 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.LowPriorityAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Danila Ponomarenko
@@ -55,10 +57,10 @@ public abstract class PriorityIntentionActionWrapper implements IntentionAction
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
action.invoke(project, editor, file);
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return action.shouldMakeCurrentFileWritable();
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return action.getElementToMakeWritable(file);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -22,9 +22,11 @@ import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Created by IntelliJ IDEA.
@@ -68,10 +70,10 @@ public class IntentionWrapper implements LocalQuickFix, IntentionAction, ActionC
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myAction.invoke(project, editor, file);
}
@Nullable
@Override
public final boolean shouldMakeCurrentFileWritable() {
return myAction.shouldMakeCurrentFileWritable();
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myAction.getElementToMakeWritable(file);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -94,9 +94,10 @@ public class CreateFileFix extends LocalQuickFixAndIntentionActionOnPsiElement {
return CodeInsightBundle.message("create.file.family");
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return false;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return null;
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -188,8 +188,8 @@ public class ShowIntentionActionsHandler implements CodeInsightActionHandler {
}
private static void invokeIntention(@NotNull IntentionAction action, @NotNull Editor editor, @NotNull PsiFile file) {
if (action.shouldMakeCurrentFileWritable() &&
!FileModificationService.getInstance().preparePsiElementsForWrite(file)) {
PsiElement elementToMakeWritable = action.getElementToMakeWritable(file);
if (elementToMakeWritable != null && !FileModificationService.getInstance().preparePsiElementsForWrite(elementToMakeWritable)) {
return;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -18,13 +18,15 @@ package com.intellij.codeInsight.intention.impl.config;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.IntentionActionBean;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class IntentionActionWrapper implements IntentionAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.intention.impl.config.IntentionActionWrapper");
@@ -66,9 +68,10 @@ public class IntentionActionWrapper implements IntentionAction {
return getDelegate().startInWriteAction();
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return getDelegate().shouldMakeCurrentFileWritable();
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return getDelegate().getElementToMakeWritable(file);
}
@NotNull
@@ -18,11 +18,13 @@ package com.intellij.refactoring.changeSignature.inplace;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.refactoring.BaseRefactoringIntentionAction;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.changeSignature.ChangeInfo;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: anna
@@ -73,9 +75,10 @@ public class ApplyChangeSignatureAction extends BaseRefactoringIntentionAction {
detector.performChange(currentInfo, editor, initialSignature);
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return true;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return file;
}
@Override
@@ -58,7 +58,10 @@ import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionManagerEx;
import com.intellij.openapi.application.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.*;
@@ -1740,7 +1743,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
public static boolean invokeIntention(@NotNull IntentionAction action, PsiFile file, Editor editor, String actionText) {
// Test that action will automatically clear the read-only attribute if modification is necessary.
// If your test fails due to this, make sure that your quick-fix/intention
// overrides "shouldMakeCurrentFileWritable" or has the following line:
// overrides "getElementToMakeWritable" or has the following line:
// if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
ReadonlyStatusHandlerImpl handler = (ReadonlyStatusHandlerImpl)ReadonlyStatusHandler.getInstance(file.getProject());
@@ -89,9 +89,10 @@ public class ConvertConcatenationToGstringIntention extends Intention {
}
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return true;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return file;
}
@Override
@@ -25,6 +25,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pass;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import com.intellij.refactoring.IntroduceTargetChooser;
import com.intellij.util.Function;
@@ -252,9 +253,10 @@ public class ConvertStringToMultilineIntention extends Intention {
};
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return true;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return file;
}
@Override
@@ -24,6 +24,7 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.LocalSearchScope;
@@ -99,9 +100,10 @@ public class PyRenameElementQuickFix implements LocalQuickFix {
return false;
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return true;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return file;
}
private static void renameInUnitTestMode(@NotNull Project project, @NotNull PsiNameIdentifierOwner nameOwner,
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -53,11 +53,6 @@ public class RenameParameterQuickFix implements LocalQuickFix {
return PyBundle.message("QFIX.rename.parameter.to.$0", myNewName);
}
@Override
public boolean shouldMakeCurrentFileWritable() {
return true;
}
@Override
public boolean startInWriteAction() {
return false;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -114,11 +114,6 @@ public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix {
}
}
@Override
public boolean shouldMakeCurrentFileWritable() {
return true;
}
@Override
public boolean startInWriteAction() {
return false;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -23,10 +23,12 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xml.Html5SchemaProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Eugene.Kudelevsky
@@ -65,9 +67,10 @@ public class SwitchToHtml5Action implements LocalQuickFix, IntentionAction {
applyFix(project);
}
@Nullable
@Override
public boolean shouldMakeCurrentFileWritable() {
return false;
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return null;
}
private static void applyFix(Project project) {