use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class VariableInplaceRenamer method performRefactoringRename.
protected void performRefactoringRename(final String newName, final StartMarkAction markAction) {
final String refactoringId = getRefactoringId();
try {
PsiNamedElement elementToRename = getVariable();
if (refactoringId != null) {
final RefactoringEventData beforeData = new RefactoringEventData();
beforeData.addElement(elementToRename);
beforeData.addStringProperties(myOldName);
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
}
if (!isIdentifier(newName, myLanguage)) {
return;
}
if (elementToRename != null) {
new WriteCommandAction(myProject, getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
renameSynthetic(newName);
}
}.execute();
}
AutomaticRenamerFactory[] factories = Extensions.getExtensions(AutomaticRenamerFactory.EP_NAME);
for (AutomaticRenamerFactory renamerFactory : factories) {
if (elementToRename != null && renamerFactory.isApplicable(elementToRename)) {
final List<UsageInfo> usages = new ArrayList<>();
final AutomaticRenamer renamer = renamerFactory.createRenamer(elementToRename, newName, new ArrayList<>());
if (renamer.hasAnythingToRename()) {
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final AutomaticRenamingDialog renamingDialog = new AutomaticRenamingDialog(myProject, renamer);
if (!renamingDialog.showAndGet()) {
continue;
}
}
final Runnable runnable = () -> ApplicationManager.getApplication().runReadAction(() -> renamer.findUsages(usages, false, false));
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, RefactoringBundle.message("searching.for.variables"), true, myProject)) {
return;
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, PsiUtilCore.toPsiElementArray(renamer.getElements())))
return;
final Runnable performAutomaticRename = () -> {
CommandProcessor.getInstance().markCurrentCommandAsGlobal(myProject);
final UsageInfo[] usageInfos = usages.toArray(new UsageInfo[usages.size()]);
final MultiMap<PsiElement, UsageInfo> classified = RenameProcessor.classifyUsages(renamer.getElements(), usageInfos);
for (final PsiNamedElement element : renamer.getElements()) {
final String newElementName = renamer.getNewName(element);
if (newElementName != null) {
final Collection<UsageInfo> infos = classified.get(element);
RenameUtil.doRename(element, newElementName, infos.toArray(new UsageInfo[infos.size()]), myProject, RefactoringElementListener.DEAF);
}
}
};
final WriteCommandAction writeCommandAction = new WriteCommandAction(myProject, getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
performAutomaticRename.run();
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
writeCommandAction.execute();
} else {
ApplicationManager.getApplication().invokeLater(() -> writeCommandAction.execute());
}
}
}
}
} finally {
if (refactoringId != null) {
final RefactoringEventData afterData = new RefactoringEventData();
afterData.addElement(getVariable());
afterData.addStringProperties(newName);
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, afterData);
}
try {
((EditorImpl) InjectedLanguageUtil.getTopLevelEditor(myEditor)).stopDumbLater();
} finally {
FinishMarkAction.finish(myProject, myEditor, markAction);
}
}
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class InplaceRefactoring method buildTemplateAndStart.
protected boolean buildTemplateAndStart(final Collection<PsiReference> refs, final Collection<Pair<PsiElement, TextRange>> stringUsages, final PsiElement scope, final PsiFile containingFile) {
final PsiElement context = InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile);
myScope = context != null ? context.getContainingFile() : scope;
final TemplateBuilderImpl builder = new TemplateBuilderImpl(myScope);
PsiElement nameIdentifier = getNameIdentifier();
int offset = InjectedLanguageUtil.getTopLevelEditor(myEditor).getCaretModel().getOffset();
PsiElement selectedElement = getSelectedInEditorElement(nameIdentifier, refs, stringUsages, offset);
boolean subrefOnPrimaryElement = false;
boolean hasReferenceOnNameIdentifier = false;
for (PsiReference ref : refs) {
if (isReferenceAtCaret(selectedElement, ref)) {
builder.replaceElement(ref.getElement(), getRangeToRename(ref), PRIMARY_VARIABLE_NAME, createLookupExpression(selectedElement), true);
subrefOnPrimaryElement = true;
continue;
}
addVariable(ref, selectedElement, builder, offset);
hasReferenceOnNameIdentifier |= isReferenceAtCaret(nameIdentifier, ref);
}
if (nameIdentifier != null) {
hasReferenceOnNameIdentifier |= selectedElement.getTextRange().contains(nameIdentifier.getTextRange());
if (!subrefOnPrimaryElement || !hasReferenceOnNameIdentifier) {
addVariable(nameIdentifier, selectedElement, builder);
}
}
for (Pair<PsiElement, TextRange> usage : stringUsages) {
addVariable(usage.first, usage.second, selectedElement, builder);
}
addAdditionalVariables(builder);
int segmentsLimit = Registry.intValue("inplace.rename.segments.limit", -1);
if (segmentsLimit != -1 && builder.getElementsCount() > segmentsLimit) {
return false;
}
try {
myMarkAction = startRename();
} catch (final StartMarkAction.AlreadyStartedException e) {
final Document oldDocument = e.getDocument();
if (oldDocument != myEditor.getDocument()) {
final int exitCode = Messages.showYesNoCancelDialog(myProject, e.getMessage(), getCommandName(), "Navigate to Started", "Cancel Started", "Cancel", Messages.getErrorIcon());
if (exitCode == Messages.CANCEL)
return true;
navigateToAlreadyStarted(oldDocument, exitCode);
return true;
} else {
if (!ourRenamersStack.isEmpty() && ourRenamersStack.peek() == this) {
ourRenamersStack.pop();
if (!ourRenamersStack.empty()) {
myOldName = ourRenamersStack.peek().myOldName;
}
}
revertState();
final TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
if (templateState != null) {
templateState.gotoEnd(true);
}
}
return false;
}
beforeTemplateStart();
new WriteCommandAction(myProject, getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
startTemplate(builder);
}
}.execute();
if (myBalloon == null) {
showBalloon();
}
return true;
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class InplaceChangeSignature method updateMethodSignature.
private void updateMethodSignature(ChangeInfo changeInfo) {
ArrayList<TextRange> deleteRanges = new ArrayList<>();
ArrayList<TextRange> newRanges = new ArrayList<>();
String methodSignature = myDetector.getMethodSignaturePreview(changeInfo, deleteRanges, newRanges);
myPreview.getMarkupModel().removeAllHighlighters();
new WriteCommandAction(null) {
@Override
protected void run(@NotNull Result result) throws Throwable {
myPreview.getDocument().replaceString(0, myPreview.getDocument().getTextLength(), methodSignature);
}
}.execute();
TextAttributes deprecatedAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.DEPRECATED_ATTRIBUTES);
for (TextRange range : deleteRanges) {
myPreview.getMarkupModel().addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), HighlighterLayer.ADDITIONAL_SYNTAX, deprecatedAttributes, HighlighterTargetArea.EXACT_RANGE);
}
TextAttributes todoAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.TODO_DEFAULT_ATTRIBUTES);
for (TextRange range : newRanges) {
myPreview.getMarkupModel().addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), HighlighterLayer.ADDITIONAL_SYNTAX, todoAttributes, HighlighterTargetArea.EXACT_RANGE);
}
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class StringComboboxEditor method setItem.
@Override
public void setItem(Object anObject) {
if (anObject == null)
anObject = "";
if (anObject.equals(getItem()))
return;
final String s = (String) anObject;
new WriteCommandAction(myProject) {
@Override
protected void run(@NotNull Result result) throws Throwable {
getDocument().setText(s);
}
}.execute();
final Editor editor = getEditor();
if (editor != null)
editor.getCaretModel().moveToOffset(s.length());
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class ConstructorInsertHandler method startTemplate.
private static void startTemplate(final PsiAnonymousClass aClass, final Editor editor, final Runnable runnable, @NotNull final PsiTypeElement[] parameters) {
final Project project = aClass.getProject();
new WriteCommandAction(project, getCommandName(), getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
editor.getCaretModel().moveToOffset(aClass.getTextOffset());
final TemplateBuilderImpl templateBuilder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(aClass);
for (int i = 0; i < parameters.length; i++) {
PsiTypeElement parameter = parameters[i];
templateBuilder.replaceElement(parameter, "param" + i, new TypeExpression(project, new PsiType[] { parameter.getType() }), true);
}
Template template = templateBuilder.buildInlineTemplate();
TemplateManager.getInstance(project).startTemplate(editor, template, false, null, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (!brokenOff) {
runnable.run();
}
}
});
}
}.execute();
}
Aggregations