use of com.intellij.openapi.command.CommandProcessor in project intellij-community by JetBrains.
the class ActionTracker method ignoreCurrentDocumentChange.
void ignoreCurrentDocumentChange() {
final CommandProcessor commandProcessor = CommandProcessor.getInstance();
if (commandProcessor.getCurrentCommand() == null)
return;
myIgnoreDocumentChanges = true;
commandProcessor.addCommandListener(new CommandAdapter() {
@Override
public void commandFinished(CommandEvent event) {
commandProcessor.removeCommandListener(this);
myIgnoreDocumentChanges = false;
}
});
}
use of com.intellij.openapi.command.CommandProcessor in project intellij-community by JetBrains.
the class CodeFormatterFacade method emulateEnter.
/**
* Emulates pressing {@code Enter} at current caret position.
*
* @param editor target editor
* @param project target project
* @param shifts two-elements array which is expected to be filled with the following info:
* 1. The first element holds added lines number;
* 2. The second element holds added symbols number;
*/
private static void emulateEnter(@NotNull final Editor editor, @NotNull Project project, int[] shifts) {
final DataContext dataContext = prepareContext(editor.getComponent(), project);
int caretOffset = editor.getCaretModel().getOffset();
Document document = editor.getDocument();
SelectionModel selectionModel = editor.getSelectionModel();
int startSelectionOffset = 0;
int endSelectionOffset = 0;
boolean restoreSelection = selectionModel.hasSelection();
if (restoreSelection) {
startSelectionOffset = selectionModel.getSelectionStart();
endSelectionOffset = selectionModel.getSelectionEnd();
selectionModel.removeSelection();
}
int textLengthBeforeWrap = document.getTextLength();
int lineCountBeforeWrap = document.getLineCount();
DataManager.getInstance().saveInDataContext(dataContext, WRAP_LONG_LINE_DURING_FORMATTING_IN_PROGRESS_KEY, true);
CommandProcessor commandProcessor = CommandProcessor.getInstance();
try {
Runnable command = () -> EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER).execute(editor, dataContext);
if (commandProcessor.getCurrentCommand() == null) {
commandProcessor.executeCommand(editor.getProject(), command, WRAP_LINE_COMMAND_NAME, null);
} else {
command.run();
}
} finally {
DataManager.getInstance().saveInDataContext(dataContext, WRAP_LONG_LINE_DURING_FORMATTING_IN_PROGRESS_KEY, null);
}
int symbolsDiff = document.getTextLength() - textLengthBeforeWrap;
if (restoreSelection) {
int newSelectionStart = startSelectionOffset;
int newSelectionEnd = endSelectionOffset;
if (startSelectionOffset >= caretOffset) {
newSelectionStart += symbolsDiff;
}
if (endSelectionOffset >= caretOffset) {
newSelectionEnd += symbolsDiff;
}
selectionModel.setSelection(newSelectionStart, newSelectionEnd);
}
shifts[0] = document.getLineCount() - lineCountBeforeWrap;
shifts[1] = symbolsDiff;
}
use of com.intellij.openapi.command.CommandProcessor in project intellij-community by JetBrains.
the class CopyClassesHandler method copyClassesImpl.
private static void copyClassesImpl(final String copyClassName, final Project project, final Map<PsiFile, PsiClass[]> classes, final HashMap<PsiFile, String> map, final Object targetDirectory, final PsiDirectory defaultTargetDirectory, final String commandName, final boolean selectInActivePanel, final boolean openInEditor) {
final boolean[] result = new boolean[] { false };
Runnable command = () -> {
PsiDirectory target;
if (targetDirectory instanceof PsiDirectory) {
target = (PsiDirectory) targetDirectory;
} else {
target = WriteAction.compute(() -> ((MoveDestination) targetDirectory).getTargetDirectory(defaultTargetDirectory));
}
try {
Collection<PsiFile> files = doCopyClasses(classes, map, copyClassName, target, project);
if (files != null) {
if (openInEditor) {
for (PsiFile file : files) {
CopyHandler.updateSelectionInActiveProjectView(file, project, selectInActivePanel);
}
EditorHelper.openFilesInEditor(files.toArray(new PsiFile[files.size()]));
}
}
} catch (IncorrectOperationException ex) {
Messages.showMessageDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon());
}
};
CommandProcessor processor = CommandProcessor.getInstance();
processor.executeCommand(project, command, commandName, null);
if (result[0]) {
ToolWindowManager.getInstance(project).invokeLater(() -> ToolWindowManager.getInstance(project).activateEditorComponent());
}
}
use of com.intellij.openapi.command.CommandProcessor in project intellij-community by JetBrains.
the class TemplateManagerImpl method startTemplateWithPrefix.
public void startTemplateWithPrefix(final Editor editor, final TemplateImpl template, final int templateStart, @Nullable final PairProcessor<String, String> processor, @Nullable final String argument) {
final int caretOffset = editor.getCaretModel().getOffset();
final TemplateState templateState = initTemplateState(editor);
CommandProcessor commandProcessor = CommandProcessor.getInstance();
commandProcessor.executeCommand(myProject, () -> {
editor.getDocument().deleteString(templateStart, caretOffset);
editor.getCaretModel().moveToOffset(templateStart);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
Map<String, String> predefinedVarValues = null;
if (argument != null) {
predefinedVarValues = new HashMap<>();
predefinedVarValues.put(TemplateImpl.ARG, argument);
}
templateState.start(template, processor, predefinedVarValues);
}, CodeInsightBundle.message("insert.code.template.command"), null);
}
use of com.intellij.openapi.command.CommandProcessor in project intellij-community by JetBrains.
the class SearchBackAction method actionPerformed.
@Override
public void actionPerformed(final AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
final FileEditor editor = e.getData(PlatformDataKeys.FILE_EDITOR);
CommandProcessor commandProcessor = CommandProcessor.getInstance();
commandProcessor.executeCommand(project, () -> {
PsiDocumentManager.getInstance(project).commitAllDocuments();
FindManager findManager = FindManager.getInstance(project);
if (!findManager.selectNextOccurrenceWasPerformed() && findManager.findPreviousUsageInEditor(editor)) {
return;
}
FindUtil.searchBack(project, editor, e.getDataContext());
}, IdeBundle.message("command.find.previous"), null);
}
Aggregations