don't wrap all inplace introducers into a write action (EA-81338 - E: BaseRefactoringProcessor.run)

This commit is contained in:
peter
2016-04-05 19:07:35 +02:00
parent 4995fc5728
commit db690867c8
6 changed files with 30 additions and 36 deletions
@@ -530,7 +530,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
@Override
protected boolean performRefactoring() {
if (!ensureValid()) return false;
WriteCommandAction.runWriteCommandAction(myProject, getCommandName(), getCommandName(), new Runnable() {
CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
@Override
public void run() {
final String refactoringId = getRefactoringId();
@@ -539,7 +539,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
final V localVariable = getLocalVariable();
if (localVariable != null) {
beforeData.addElement(localVariable);
}
}
else {
final E beforeExpr = getBeforeExpr();
if (beforeExpr != null) {
@@ -551,7 +551,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
}
performIntroduce();
}
});
}, getCommandName(), getCommandName());
V variable = getVariable();
if (variable != null) {
@@ -15,7 +15,7 @@
*/
package org.jetbrains.plugins.groovy.refactoring.introduce;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.project.Project;
@@ -191,28 +191,23 @@ public abstract class GrAbstractInplaceIntroducer<Settings extends GrIntroduceSe
final Settings settings = getInitialSettingsForInplace(myContext, myReplaceChoice, names);
if (settings == null) return null;
SmartPsiElementPointer<GrVariable> pointer = ApplicationManager.getApplication().runWriteAction(new Computable<SmartPsiElementPointer<GrVariable>>() {
@Override
public SmartPsiElementPointer<GrVariable> compute() {
GrVariable var = runRefactoring(myContext, settings, false);
return var != null ? SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(var) : null;
}
});
if (pointer != null) {
GrVariable var = pointer.getElement();
if (var != null) {
myVarMarker = myContext.getEditor().getDocument().createRangeMarker(var.getTextRange());
}
return var;
}
else {
return null;
GrVariable var = runRefactoring(myContext, settings, false);
if (var != null) {
myVarMarker = myContext.getEditor().getDocument().createRangeMarker(var.getTextRange());
}
return var;
}
protected abstract GrVariable runRefactoring(GrIntroduceContext context, Settings settings, boolean processUsages);
protected final GrVariable refactorInWriteAction(Computable<GrVariable> computable) {
SmartPsiElementPointer<GrVariable> pointer = WriteAction.compute(() -> {
GrVariable var = computable.compute();
return var != null ? SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(var) : null;
});
return pointer != null ? pointer.getElement() : null;
}
@Nullable
protected abstract Settings getInitialSettingsForInplace(@NotNull GrIntroduceContext context,
@NotNull OccurrencesChooser.ReplaceChoice choice,
@@ -92,13 +92,15 @@ public class GrInplaceConstantIntroducer extends GrAbstractInplaceIntroducer<GrI
@Override
protected GrVariable runRefactoring(GrIntroduceContext context, GrIntroduceConstantSettings settings, boolean processUsages) {
if (processUsages) {
return new GrIntroduceConstantProcessor(context, settings).run();
}
else {
PsiElement scope = context.getScope();
return new GrIntroduceConstantProcessor(context, settings).addDeclaration(scope instanceof GroovyFileBase ? ((GroovyFileBase)scope).getScriptClass() : (PsiClass)scope).getVariables()[0];
}
return refactorInWriteAction(() -> {
if (processUsages) {
return new GrIntroduceConstantProcessor(context, settings).run();
}
else {
PsiElement scope = context.getScope();
return new GrIntroduceConstantProcessor(context, settings).addDeclaration(scope instanceof GroovyFileBase ? ((GroovyFileBase)scope).getScriptClass() : (PsiClass)scope).getVariables()[0];
}
});
}
@Nullable
@@ -97,9 +97,11 @@ public class GrInplaceFieldIntroducer extends GrAbstractInplaceIntroducer<GrIntr
@Override
protected GrVariable runRefactoring(GrIntroduceContext context, GrIntroduceFieldSettings settings, boolean processUsages) {
return refactorInWriteAction(() -> {
GrIntroduceFieldProcessor processor = new GrIntroduceFieldProcessor(context, settings);
return processUsages ? processor.run()
: processor.insertField((PsiClass)context.getScope()).getVariables()[0];
});
}
@Nullable
@@ -18,6 +18,7 @@ package org.jetbrains.plugins.groovy.refactoring.introduce.parameter;
import com.intellij.codeInsight.template.TextResult;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.impl.DocumentMarkupModel;
@@ -203,8 +204,7 @@ public class GrInplaceParameterIntroducer extends GrAbstractInplaceIntroducer<Gr
processor.run();
}
else {
GrIntroduceParameterProcessor processor = new GrIntroduceParameterProcessor(settings, wrapper);
processor.performRefactoring(UsageInfo.EMPTY_ARRAY);
WriteAction.run(() -> new GrIntroduceParameterProcessor(settings, wrapper).performRefactoring(UsageInfo.EMPTY_ARRAY));
}
GrParametersOwner owner = settings.getToReplaceIn();
return ArrayUtil.getLastElement(owner.getParameters());
@@ -158,12 +158,7 @@ public class GrIntroduceVariableHandler extends GrIntroduceHandlerBase<GroovyInt
return new GrInplaceVariableIntroducer(getRefactoringName(), choice, contextRef.get()) {
@Override
protected GrVariable runRefactoring(GrIntroduceContext context, GroovyIntroduceVariableSettings settings, boolean processUsages) {
if (processUsages) {
return processExpression(context, settings);
}
else {
return addVariable(context, settings);
}
return refactorInWriteAction(() -> processUsages ? processExpression(context, settings) : addVariable(context, settings));
}
@Override