use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class CreateClassFromNewFix method invokeImpl.
@Override
protected void invokeImpl(PsiClass targetClass) {
assert ApplicationManager.getApplication().isWriteAccessAllowed();
final Project project = targetClass.getProject();
TransactionGuard.getInstance().submitTransactionLater(project, () -> {
PsiDocumentManager.getInstance(project).commitAllDocuments();
final PsiNewExpression newExpression = getNewExpression();
if (newExpression == null) {
return;
}
final PsiJavaCodeReferenceElement referenceElement = getReferenceElement(newExpression);
final PsiClass[] psiClass = new PsiClass[1];
CommandProcessor.getInstance().executeCommand(newExpression.getProject(), () -> psiClass[0] = CreateFromUsageUtils.createClass(referenceElement, CreateClassKind.CLASS, null), getText(), getText());
new WriteCommandAction(project, getText(), getText()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
setupClassFromNewExpression(psiClass[0], newExpression);
}
}.execute();
});
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class ImplementAbstractClassMethodsFix method invoke.
@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull final PsiElement startElement, @NotNull PsiElement endElement) {
if (editor == null)
return;
PsiJavaCodeReferenceElement classReference = ((PsiNewExpression) startElement).getClassReference();
if (classReference == null)
return;
final PsiClass psiClass = (PsiClass) classReference.resolve();
if (psiClass == null)
return;
final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, psiClass, false);
if (chooser == null)
return;
final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
if (selectedElements == null || selectedElements.isEmpty())
return;
new WriteCommandAction(project, file) {
@Override
protected void run(@NotNull final Result result) throws Throwable {
PsiNewExpression newExpression = (PsiNewExpression) JavaPsiFacade.getElementFactory(project).createExpressionFromText(startElement.getText() + "{}", startElement);
newExpression = (PsiNewExpression) startElement.replace(newExpression);
final PsiClass psiClass = newExpression.getAnonymousClass();
if (psiClass == null)
return;
Map<PsiClass, PsiSubstitutor> subst = new HashMap<>();
for (PsiMethodMember selectedElement : selectedElements) {
final PsiClass baseClass = selectedElement.getElement().getContainingClass();
if (baseClass != null) {
PsiSubstitutor substitutor = subst.get(baseClass);
if (substitutor == null) {
substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, psiClass, PsiSubstitutor.EMPTY);
subst.put(baseClass, substitutor);
}
selectedElement.setSubstitutor(substitutor);
}
}
OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
}
}.execute();
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class RangeMarkerTest method testRandomEdit_NoCommand.
public void testRandomEdit_NoCommand() {
final int N = 100;
final Random gen = new Random();
int N_TRIES = Timings.adjustAccordingToMySpeed(7000, false);
System.out.println("N_TRIES = " + N_TRIES);
DocumentEx document = null;
for (int tryn = 0; tryn < N_TRIES; tryn++) {
((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger();
((UndoManagerImpl) UndoManager.getGlobalInstance()).flushCurrentCommandMerger();
if (document != null) {
((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(document);
((UndoManagerImpl) UndoManager.getGlobalInstance()).clearUndoRedoQueueInTests(document);
}
if (tryn % 10000 == 0) {
System.out.println(tryn);
}
document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', N));
final DocumentEx finalDocument = document;
new WriteCommandAction(getProject()) {
@Override
protected void run(@NotNull Result result) throws Exception {
List<Pair<RangeMarker, TextRange>> adds = new ArrayList<>();
List<Pair<RangeMarker, TextRange>> dels = new ArrayList<>();
List<Trinity<Integer, Integer, Integer>> edits = new ArrayList<>();
try {
for (int i = 0; i < 30; i++) {
int x = gen.nextInt(N);
int y = x + gen.nextInt(N - x);
RangeMarkerEx r = (RangeMarkerEx) finalDocument.createRangeMarker(x, y);
adds.add(Pair.create(r, TextRange.create(r)));
}
for (int i = 0; i < 10; i++) {
int offset = gen.nextInt(finalDocument.getTextLength());
if (gen.nextBoolean()) {
int length = gen.nextInt(5);
edits.add(Trinity.create(offset, 0, length));
finalDocument.insertString(offset, StringUtil.repeatSymbol(' ', length));
} else {
int length = gen.nextInt(finalDocument.getTextLength() - offset);
edits.add(Trinity.create(offset, length, 0));
finalDocument.deleteString(offset, offset + length);
}
}
List<Pair<RangeMarker, TextRange>> candidates = new ArrayList<>(adds);
while (!candidates.isEmpty()) {
int size = candidates.size();
int x = gen.nextInt(size);
Pair<RangeMarker, TextRange> c = candidates.remove(x);
RangeMarkerEx r = (RangeMarkerEx) c.first;
assertEquals(size - 1, candidates.size());
dels.add(c);
r.dispose();
}
} catch (AssertionError e) {
String s = "adds: ";
for (Pair<RangeMarker, TextRange> c : adds) {
TextRange t = c.second;
s += t.getStartOffset() + "," + t.getEndOffset() + ", ";
}
s += "\nedits: ";
for (Trinity<Integer, Integer, Integer> edit : edits) {
s += edit.first + "," + edit.second + "," + edit.third + ", ";
}
s += "\ndels: ";
for (Pair<RangeMarker, TextRange> c : dels) {
int index = adds.indexOf(c);
assertSame(c, adds.get(index));
s += index + ", ";
}
System.err.println(s);
throw e;
}
}
}.execute();
}
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class ConstraintUtilities method saveModelToXML.
/**
* Utility function to commit to the NlModel the current state of all widgets
*
* @param nlModel
* @param commit if false, the changes are only reflected in memory not saved to the XML file.
*/
static void saveModelToXML(@NotNull NlModel nlModel, boolean commit) {
ConstraintModel model = ConstraintModel.getConstraintModel(nlModel);
if (commit) {
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
String label = "Constraint";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
saveXmlWidgets(model, commit);
}
};
action.execute();
} else {
saveXmlWidgets(model, commit);
}
}
use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.
the class AnchorTarget method mouseRelease.
/**
* On mouseRelease, we can either disconnect the current anchor (if the mouse release is on ourselve)
* or connect the anchor to a given target. Modifications are applied first in memory then commited
* to the XML model.
*
* @param x
* @param y
* @param closestTarget
*/
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
myLastX = -1;
myLastY = -1;
if (myComponent.getParent() != null) {
myComponent.getParent().setExpandTargetArea(false);
}
if (closestTarget != null && closestTarget instanceof AnchorTarget && !(((AnchorTarget) closestTarget).isConnected(this))) {
NlComponent component = myComponent.getNlComponent();
if (closestTarget == this) {
disconnectMe(component);
} else {
String attribute = getAttribute(closestTarget);
if (attribute != null) {
AnchorTarget targetAnchor = (AnchorTarget) closestTarget;
NlComponent targetComponent = targetAnchor.myComponent.getNlComponent();
AttributesTransaction attributes = connectMe(component, attribute, targetComponent);
NlModel nlModel = component.getModel();
Project project = nlModel.getProject();
XmlFile file = nlModel.getFile();
String label = "Constraint Connected";
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
attributes.commit();
}
};
action.execute();
myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
}
}
}
}
Aggregations