python intentions: make files writable outside write action

This commit is contained in:
peter
2016-11-25 09:48:18 +01:00
parent 59a2b56dc1
commit 4dc8ffadcd
8 changed files with 5 additions and 37 deletions
@@ -17,7 +17,6 @@
package com.jetbrains.python.codeInsight.intentions;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -29,7 +28,6 @@ public abstract class PyBaseIntentionAction extends BaseIntentionAction {
@Override
public final void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
doInvoke(project, editor, file);
}
@@ -15,13 +15,10 @@
*/
package com.jetbrains.python.inspections;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.controlflow.ControlFlowUtil;
import com.intellij.codeInsight.controlflow.Instruction;
import com.intellij.codeInspection.*;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
@@ -455,22 +452,14 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor {
}
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.getPsiElement())) {
return;
}
replace(descriptor.getPsiElement());
}
private void replace(final PsiElement psiElement) {
PsiElement psiElement = descriptor.getPsiElement();
final PyFile pyFile = (PyFile) PyElementGenerator.getInstance(psiElement.getProject()).createDummyFile(LanguageLevel.getDefault(),
"for _ in tuples:\n pass"
);
final PyExpression target = ((PyForStatement)pyFile.getStatements().get(0)).getForPart().getTarget();
CommandProcessor.getInstance().executeCommand(psiElement.getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
if (target != null) {
psiElement.replace(target);
}
}), getName(), null);
if (target != null) {
psiElement.replace(target);
}
}
}
}
@@ -15,7 +15,6 @@
*/
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
@@ -54,9 +53,6 @@ public class AddSelfQuickFix implements LocalQuickFix {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PyParameterList) {
final PyParameterList parameterList = (PyParameterList)element;
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) {
return;
}
PyNamedParameter newParameter = PyElementGenerator.getInstance(project).createParameter(myParamName);
parameterList.addParameter(newParameter);
}
@@ -15,7 +15,6 @@
*/
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
@@ -55,9 +54,6 @@ public class ReformatFix implements IntentionAction, LocalQuickFix, HighPriority
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) {
return;
}
CodeStyleManager.getInstance(project).reformat(file);
}
@@ -15,7 +15,6 @@
*/
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
@@ -37,11 +36,7 @@ public class RemoveTrailingSemicolonQuickFix implements LocalQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement problemElement = descriptor.getPsiElement();
if ((problemElement != null) && (";".equals(problemElement.getText()))) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(problemElement)) {
return;
}
if (problemElement != null && ";".equals(problemElement.getText())) {
problemElement.delete();
}
}
@@ -15,7 +15,6 @@
*/
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
@@ -41,7 +40,6 @@ public class UnresolvedRefAddFutureImportQuickFix implements LocalQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
PyFile file = (PyFile)element.getContainingFile();
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyFromImportStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(element), PyFromImportStatement.class,
"from __future__ import with_statement");
@@ -16,7 +16,6 @@
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.template.TemplateBuilder;
import com.intellij.codeInsight.template.TemplateBuilderFactory;
import com.intellij.codeInspection.LocalQuickFix;
@@ -53,7 +52,6 @@ public class UnresolvedRefCreateFunctionQuickFix implements LocalQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PyPsiUtils.assertValid(myElement);
if (!myElement.isValid() || !FileModificationService.getInstance().preparePsiElementForWrite(myElement)) return;
PyFunctionBuilder functionBuilder = new PyFunctionBuilder(myReference.getText(), myElement);
@@ -15,7 +15,6 @@
*/
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
@@ -52,7 +51,6 @@ public class UnresolvedReferenceAddSelfQuickFix implements LocalQuickFix, HighPr
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(myElement)) return;
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyExpression expression = elementGenerator.createExpressionFromText(LanguageLevel.forElement(myElement),
myQualifier + "." + myElement.getText());