skip redundant write actions

This commit is contained in:
Anna Kozlova
2016-03-10 16:54:35 +01:00
parent 4d11193365
commit 6f89598638
4 changed files with 45 additions and 77 deletions
@@ -17,9 +17,7 @@ package org.jetbrains.plugins.groovy.intentions.control;
import com.intellij.ide.util.DefaultPsiElementCellRenderer;
import com.intellij.ide.util.MethodCellRenderer;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
@@ -118,15 +116,9 @@ public class CreateParameterForFieldIntention extends Intention {
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
AccessToken accessToken = WriteAction.start();
try{
for (Object selectedValue : selectedValues) {
LOG.assertTrue(((GrMethod)selectedValue).isValid());
addParameter(field, ((GrMethod)selectedValue), project);
}
}
finally {
accessToken.finish();
for (Object selectedValue : selectedValues) {
LOG.assertTrue(((GrMethod)selectedValue).isValid());
addParameter(field, ((GrMethod)selectedValue), project);
}
}
}, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
@@ -155,15 +147,9 @@ public class CreateParameterForFieldIntention extends Intention {
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
AccessToken accessToken = WriteAction.start();
try {
for (Object selectedValue : selectedValues) {
LOG.assertTrue(((GrField)selectedValue).isValid());
addParameter(((GrField)selectedValue), constructor, project);
}
}
finally {
accessToken.finish();
for (Object selectedValue : selectedValues) {
LOG.assertTrue(((GrField)selectedValue).isValid());
addParameter(((GrField)selectedValue), constructor, project);
}
}
}, GroovyIntentionsBundle.message("create.parameter.for.field.intention.name"), null);
@@ -233,6 +219,11 @@ public class CreateParameterForFieldIntention extends Intention {
}
@Override
public boolean startInWriteAction() {
return false;
}
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
@@ -15,7 +15,6 @@
*/
package org.jetbrains.plugins.groovy.refactoring.introduceParameter
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.refactoring.IntroduceParameterRefactoring
import gnu.trove.TIntArrayList
import org.jetbrains.plugins.groovy.LightGroovyTestCase
@@ -27,7 +26,6 @@ import org.jetbrains.plugins.groovy.refactoring.introduce.parameter.GrIntroduceP
import org.jetbrains.plugins.groovy.refactoring.introduce.parameter.GrIntroduceParameterSettings
import org.jetbrains.plugins.groovy.refactoring.introduce.parameter.IntroduceParameterInfo
import org.jetbrains.plugins.groovy.util.TestUtils
/**
* @author Max Medvedev
*/
@@ -59,11 +57,9 @@ public abstract class ExtractClosureTest extends LightGroovyTestCase {
}
}
}
handler.invoke myFixture.project, myFixture.editor, myFixture.file, null
doPostponedFormatting(myFixture.project)
WriteCommandAction.runWriteCommandAction project, {
handler.invoke myFixture.project, myFixture.editor, myFixture.file, null
doPostponedFormatting(myFixture.project)
}
myFixture.checkResult after
}
@@ -15,8 +15,6 @@
*/
package org.jetbrains.plugins.groovy.refactoring.introduceParameter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
@@ -37,7 +35,6 @@ import org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase
import org.jetbrains.plugins.groovy.refactoring.introduce.parameter.*
import org.jetbrains.plugins.groovy.util.TestUtils
import org.junit.Assert
/**
* @author Maxim.Medvedev
*/
@@ -123,34 +120,30 @@ public class GrIntroduceParameterTest extends LightGroovyTestCase {
final Project project,
final Editor editor,
final PsiFile file) {
CommandProcessor.instance.executeCommand(project, {
ApplicationManager.application.runWriteAction {
try {
final GrIntroduceParameterHandler hackedHandler = new GrIntroduceParameterHandler() {
@Override
protected void showDialog(IntroduceParameterInfo info) {
final GrIntroduceParameterSettings hackedSettings =
getSettings(info, removeUnusedParameters, replaceFieldsWithGetters, declareFinal, generateDelegate);
if (info.getToReplaceIn() instanceof GrMethod) {
new GrIntroduceParameterProcessor(hackedSettings).run();
}
else {
new GrIntroduceClosureParameterProcessor(hackedSettings).run();
}
}
};
hackedHandler.invoke(project, editor, file, null);
if (conflicts != null) fail("Conflicts were expected");
}
catch (Exception e) {
if (conflicts == null) {
e.printStackTrace();
fail("Conflicts were not expected");
try {
final GrIntroduceParameterHandler hackedHandler = new GrIntroduceParameterHandler() {
@Override
protected void showDialog(IntroduceParameterInfo info) {
final GrIntroduceParameterSettings hackedSettings =
getSettings(info, removeUnusedParameters, replaceFieldsWithGetters, declareFinal, generateDelegate);
if (info.getToReplaceIn() instanceof GrMethod) {
new GrIntroduceParameterProcessor(hackedSettings).run();
}
else {
new GrIntroduceClosureParameterProcessor(hackedSettings).run();
}
Assert.assertEquals(conflicts, e.getMessage());
}
};
hackedHandler.invoke(project, editor, file, null);
if (conflicts != null) fail("Conflicts were expected");
}
catch (Exception e) {
if (conflicts == null) {
e.printStackTrace();
fail("Conflicts were not expected");
}
}, "introduce Parameter", null);
Assert.assertEquals(conflicts, e.getMessage());
}
}
private static GrIntroduceParameterSettings getSettings(final IntroduceParameterInfo context,
@@ -15,8 +15,7 @@
*/
package org.jetbrains.plugins.groovy.refactoring.introduceParameter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
@@ -103,28 +102,17 @@ public class IntroduceParameterTest extends LightCodeInsightFixtureTestCase {
replaceAllOccurrences, replaceFieldsWithGetters, declareFinal, generateDelegate, null,
parametersToRemove);
CommandProcessor.instance.executeCommand(project, new Runnable() {
@Override
public void run() {
ApplicationManager.application.runWriteAction(new Runnable() {
@Override
public void run() {
try {
processor.run();
if (conflicts != null) fail("Conflicts were expected");
}
catch (Exception e) {
if (conflicts == null){
e.printStackTrace();
fail("Conflicts were not expected");
}
assertEquals(conflicts, e.message);
}
}
});
try {
processor.run();
if (conflicts != null) fail("Conflicts were expected");
}
catch (Exception e) {
if (conflicts == null){
e.printStackTrace();
fail("Conflicts were not expected");
}
}, "introduce Parameter", null);
assertEquals(conflicts, e.message);
}
editor.selectionModel.removeSelection();
return true;