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 DuplicatesImpl method replaceMatch.
private static boolean replaceMatch(final Project project, final MatchProvider provider, final Match match, @NotNull final Editor editor, final int idx, final int size, Ref<Boolean> showAll, final String confirmDuplicatePrompt, boolean skipPromptWhenOne) {
final ArrayList<RangeHighlighter> highlighters = previewMatch(project, match, editor);
try {
if (!ApplicationManager.getApplication().isUnitTestMode()) {
if ((!skipPromptWhenOne || size > 1) && (showAll.get() == null || !showAll.get())) {
final String prompt = provider.getConfirmDuplicatePrompt(match);
final ReplacePromptDialog promptDialog = new ReplacePromptDialog(false, provider.getReplaceDuplicatesTitle(idx, size), project) {
@Override
protected String getMessage() {
final String message = super.getMessage();
return prompt != null ? message + " " + prompt : message;
}
};
promptDialog.show();
final boolean allChosen = promptDialog.getExitCode() == FindManager.PromptResult.ALL;
showAll.set(allChosen);
if (allChosen && confirmDuplicatePrompt != null && prompt == null) {
if (Messages.showOkCancelDialog(project, "In order to replace all occurrences method signature will be changed. Proceed?", CommonBundle.getWarningTitle(), Messages.getWarningIcon()) != Messages.OK)
return true;
}
if (promptDialog.getExitCode() == FindManager.PromptResult.SKIP)
return false;
if (promptDialog.getExitCode() == FindManager.PromptResult.CANCEL)
return true;
}
}
} finally {
HighlightManager.getInstance(project).removeSegmentHighlighter(editor, highlighters.get(0));
}
// call change signature when needed
provider.prepareSignature(match);
new WriteCommandAction(project, MethodDuplicatesHandler.REFACTORING_NAME, MethodDuplicatesHandler.REFACTORING_NAME) {
@Override
protected void run(@NotNull Result result) throws Throwable {
try {
provider.processMatch(match);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}.execute();
return false;
}
use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.
the class CreateFieldOrPropertyFix method applyFixInner.
private void applyFixInner(final Project project) {
final PsiFile file = myClass.getContainingFile();
final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, myClass.getContainingFile(), myClass);
if (editor != null) {
new WriteCommandAction(project, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
generateMembers(project, editor, file);
}
@Override
protected boolean isGlobalUndoAction() {
// todo check
return true;
}
}.execute();
}
}
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());
}
Aggregations