Search in sources :

Example 16 with CommandProcessor

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;
        }
    });
}
Also used : CommandAdapter(com.intellij.openapi.command.CommandAdapter) CommandEvent(com.intellij.openapi.command.CommandEvent) CommandProcessor(com.intellij.openapi.command.CommandProcessor)

Example 17 with CommandProcessor

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;
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) CommandProcessor(com.intellij.openapi.command.CommandProcessor)

Example 18 with CommandProcessor

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());
    }
}
Also used : CommandProcessor(com.intellij.openapi.command.CommandProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 19 with CommandProcessor

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);
}
Also used : CommandProcessor(com.intellij.openapi.command.CommandProcessor)

Example 20 with CommandProcessor

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);
}
Also used : Project(com.intellij.openapi.project.Project) FileEditor(com.intellij.openapi.fileEditor.FileEditor) FindManager(com.intellij.find.FindManager) CommandProcessor(com.intellij.openapi.command.CommandProcessor)

Aggregations

CommandProcessor (com.intellij.openapi.command.CommandProcessor)22 Project (com.intellij.openapi.project.Project)8 FileEditorManagerEx (com.intellij.openapi.fileEditor.ex.FileEditorManagerEx)3 FindManager (com.intellij.find.FindManager)2 Editor (com.intellij.openapi.editor.Editor)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 EditorWindow (com.intellij.openapi.fileEditor.impl.EditorWindow)2 Content (com.intellij.ui.content.Content)2 MessageView (com.intellij.ui.content.MessageView)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 EditorGotoLineNumberDialog (com.intellij.ide.util.EditorGotoLineNumberDialog)1 GotoLineNumberDialog (com.intellij.ide.util.GotoLineNumberDialog)1 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 ApplicationImpl (com.intellij.openapi.application.impl.ApplicationImpl)1 CommandAdapter (com.intellij.openapi.command.CommandAdapter)1 CommandEvent (com.intellij.openapi.command.CommandEvent)1 Document (com.intellij.openapi.editor.Document)1 DocumentImpl (com.intellij.openapi.editor.impl.DocumentImpl)1 EditorComposite (com.intellij.openapi.fileEditor.impl.EditorComposite)1