fix dialog from write action

This commit is contained in:
Anna.Kozlova
2016-11-23 14:55:52 +01:00
parent a3a1e0ec7a
commit 1ce98c3d83
3 changed files with 37 additions and 20 deletions
@@ -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.
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.editorActions.DeclarationJoinLinesHandler;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.codeInspection.RemoveInitializerFix;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
@@ -100,6 +101,11 @@ public class JoinDeclarationAndAssignmentAction extends PsiElementBaseIntentionA
return null;
}
@Override
public boolean startInWriteAction() {
return false;
}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
@@ -112,8 +118,10 @@ public class JoinDeclarationAndAssignmentAction extends PsiElementBaseIntentionA
if (initializer != null && assignmentExpression.getOperationTokenType() == JavaTokenType.EQ) {
RemoveInitializerFix.sideEffectAwareRemove(project, initializer, initializer, variable);
}
final PsiExpression initializerExpression = DeclarationJoinLinesHandler.getInitializerExpression(variable, assignmentExpression);
variable.setInitializer(initializerExpression);
assignmentExpression.delete();
WriteAction.run(() -> {
final PsiExpression initializerExpression = DeclarationJoinLinesHandler.getInitializerExpression(variable, assignmentExpression);
variable.setInitializer(initializerExpression);
assignmentExpression.delete();
});
}
}
@@ -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.
@@ -17,6 +17,7 @@ package com.intellij.codeInspection;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.editorActions.DeclarationJoinLinesHandler;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
@@ -49,11 +50,14 @@ public class RemoveAssignmentFix extends RemoveInitializerFix {
final PsiElement gParent = parent.getParent();
if ((gParent instanceof PsiExpression || gParent instanceof PsiExpressionList || gParent instanceof PsiReturnStatement) && rExpression != null) {
if (!FileModificationService.getInstance().prepareFileForWrite(gParent.getContainingFile())) return;
if (gParent instanceof PsiParenthesizedExpression) {
gParent.replace(rExpression);
} else {
parent.replace(rExpression);
}
PsiExpression finalRExpr = rExpression;
WriteAction.run(() -> {
if (gParent instanceof PsiParenthesizedExpression) {
gParent.replace(finalRExpr);
} else {
parent.replace(finalRExpr);
}
});
return;
}
@@ -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.
@@ -18,14 +18,13 @@ package com.intellij.codeInspection;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.impl.quickfix.RemoveUnusedVariableFix;
import com.intellij.codeInsight.daemon.impl.quickfix.RemoveUnusedVariableUtil;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiExpressionTrimRenderer;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
@@ -50,6 +49,11 @@ public class RemoveInitializerFix implements LocalQuickFix {
sideEffectAwareRemove(project, (PsiExpression)psiInitializer, psiInitializer, variable);
}
@Override
public boolean startInWriteAction() {
return false;
}
public static void sideEffectAwareRemove(Project project,
PsiExpression psiInitializer,
PsiElement elementToDelete,
@@ -59,7 +63,7 @@ public class RemoveInitializerFix implements LocalQuickFix {
final PsiElement declaration = variable.getParent();
final List<PsiElement> sideEffects = new ArrayList<>();
boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects);
RemoveUnusedVariableUtil.RemoveMode res = RemoveUnusedVariableUtil.RemoveMode.DELETE_ALL;
RemoveUnusedVariableUtil.RemoveMode res;
if (hasSideEffects) {
hasSideEffects = PsiUtil.isStatement(psiInitializer);
PsiTypeElement typeElement = variable.getTypeElement();
@@ -71,7 +75,10 @@ public class RemoveInitializerFix implements LocalQuickFix {
PsiExpressionTrimRenderer.render(psiInitializer)
);
}
try {
else {
res = RemoveUnusedVariableUtil.RemoveMode.DELETE_ALL;
}
WriteAction.run(() -> {
if (res == RemoveUnusedVariableUtil.RemoveMode.DELETE_ALL) {
elementToDelete.delete();
}
@@ -81,14 +88,12 @@ public class RemoveInitializerFix implements LocalQuickFix {
final PsiElement parent = elementToDelete.getParent();
if (parent instanceof PsiExpressionStatement) {
parent.replace(statementFromText);
} else {
}
else {
declaration.getParent().addBefore(statementFromText, declaration);
elementToDelete.delete();
}
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
});
}
}