Example usage for com.intellij.openapi.editor.actionSystem EditorActionManager getActionHandler

List of usage examples for com.intellij.openapi.editor.actionSystem EditorActionManager getActionHandler

Introduction

In this page you can find the example usage for com.intellij.openapi.editor.actionSystem EditorActionManager getActionHandler.

Prototype

public abstract EditorActionHandler getActionHandler(@NonNls @NotNull String actionId);

Source Link

Document

Returns the handler currently defined for the specified editor actions.

Usage

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected static void type(char c, Editor editor) {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    DataContext dataContext = DataManager.getInstance().getDataContext();
    if (c == '\n') {
        actionManager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER).execute(editor, dataContext);
        return;//  w w w  .j  av  a 2 s  .  co  m
    }
    TypedAction action = actionManager.getTypedAction();
    action.actionPerformed(editor, c, dataContext);
}

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected void caretRight() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT);
    action.execute(getEditor(), DataManager.getInstance().getDataContext());
}

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected void caretUp() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
    action.execute(getEditor(), DataManager.getInstance().getDataContext());
}

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected void deleteLine() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_DELETE_LINE);
    action.execute(getEditor(), DataManager.getInstance().getDataContext());
}

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected void backspace(final Editor editor) {
    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
        @Override/*w w w .j  a  v a  2 s. co  m*/
        public void run() {
            EditorActionManager actionManager = EditorActionManager.getInstance();
            EditorActionHandler actionHandler = actionManager
                    .getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE);

            actionHandler.execute(editor, DataManager.getInstance().getDataContext());
        }
    }, "backspace", editor.getDocument());
}

From source file:com.intellij.codeInsight.CodeInsightTestCase.java

License:Apache License

protected void delete(@NotNull final Editor editor) {
    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
        @Override/*w w w . jav  a 2  s.  c om*/
        public void run() {
            EditorActionManager actionManager = EditorActionManager.getInstance();
            EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_DELETE);

            actionHandler.execute(editor, DataManager.getInstance().getDataContext());
        }
    }, "delete", editor.getDocument());
}

From source file:com.intellij.codeInsight.editorActions.smartEnter.LeaveCodeBlockEnterProcessor.java

License:Apache License

@Override
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
    PsiElement parent = psiElement.getParent();
    if (!(parent instanceof PsiCodeBlock)) {
        return false;
    }// w  ww.  j ava2 s  .  c om

    final ASTNode node = psiElement.getNode();
    if (node != null && CONTROL_FLOW_ELEMENT_TYPES.contains(node.getElementType())) {
        return false;
    }

    boolean leaveCodeBlock = isControlFlowBreak(psiElement);
    if (!leaveCodeBlock) {
        return false;
    }

    final int offset = parent.getTextRange().getEndOffset();

    // Check if there is empty line after the code block. Just move caret there in the case of the positive answer.
    final CharSequence text = editor.getDocument().getCharsSequence();
    if (offset < text.length() - 1) {
        final int i = CharArrayUtil.shiftForward(text, offset + 1, " \t");
        if (i < text.length() && text.charAt(i) == '\n') {
            editor.getCaretModel().moveToOffset(offset + 1);
            EditorActionManager actionManager = EditorActionManager.getInstance();
            EditorActionHandler actionHandler = actionManager
                    .getActionHandler(IdeActions.ACTION_EDITOR_MOVE_LINE_END);
            final DataContext dataContext = DataManager.getInstance().getDataContext(editor.getComponent());
            if (dataContext != null) {
                actionHandler.execute(editor, dataContext);
                return true;
            }
        }
    }

    editor.getCaretModel().moveToOffset(offset);
    return false;
}

From source file:com.intellij.codeInsight.editorActions.smartEnter.PlainEnterProcessor.java

License:Apache License

/**
 * There is a possible case that target code block already starts with the empty line:
 * <pre>//from ww w.j a v a 2  s.c om
 *   void test(int i) {
 *     if (i > 1[caret]) {
 *       
 *     }
 *   }
 * </pre>
 * We want just move caret to correct position at that empty line without creating additional empty line then.
 *  
 * @param editor      target editor
 * @param codeBlock   target code block to which new empty line is going to be inserted
 * @param element     target element under caret
 * @return            <code>true</code> if it was found out that the given code block starts with the empty line and caret
 *                    is pointed to correct position there, i.e. no additional processing is required;
 *                    <code>false</code> otherwise
 */
private static boolean processExistingBlankLine(@NotNull Editor editor, @Nullable PsiCodeBlock codeBlock,
        @Nullable PsiElement element) {
    PsiWhiteSpace whiteSpace = null;
    if (codeBlock == null) {
        if (element != null) {
            final PsiElement next = PsiTreeUtil.nextLeaf(element);
            if (next instanceof PsiWhiteSpace) {
                whiteSpace = (PsiWhiteSpace) next;
            }
        }
    } else {
        whiteSpace = PsiTreeUtil.findChildOfType(codeBlock, PsiWhiteSpace.class);
        if (whiteSpace == null) {
            return false;
        }

        PsiElement lbraceCandidate = whiteSpace.getPrevSibling();
        if (lbraceCandidate == null) {
            return false;
        }

        ASTNode node = lbraceCandidate.getNode();
        if (node == null || node.getElementType() != JavaTokenType.LBRACE) {
            return false;
        }
    }

    if (whiteSpace == null) {
        return false;
    }

    final TextRange textRange = whiteSpace.getTextRange();
    final Document document = editor.getDocument();
    final CharSequence whiteSpaceText = document.getCharsSequence().subSequence(textRange.getStartOffset(),
            textRange.getEndOffset());
    if (StringUtil.countNewLines(whiteSpaceText) < 2) {
        return false;
    }

    int i = CharArrayUtil.shiftForward(whiteSpaceText, 0, " \t");
    if (i >= whiteSpaceText.length() - 1) {
        assert false : String.format("code block: %s, white space: %s",
                codeBlock == null ? "undefined" : codeBlock.getTextRange(), whiteSpace.getTextRange());
        return false;
    }

    editor.getCaretModel().moveToOffset(i + 1 + textRange.getStartOffset());
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_LINE_END);
    final DataContext dataContext = DataManager.getInstance().getDataContext(editor.getComponent());
    if (dataContext == null) {
        i = CharArrayUtil.shiftForwardUntil(whiteSpaceText, i, "\n");
        if (i >= whiteSpaceText.length()) {
            i = whiteSpaceText.length();
        }
        editor.getCaretModel().moveToOffset(i + textRange.getStartOffset());
    } else {
        actionHandler.execute(editor, dataContext);
    }
    return true;
}

From source file:com.intellij.codeInsight.navigation.IncrementalSearchHandler.java

License:Apache License

public void invoke(Project project, final Editor editor) {
    if (!ourActionsRegistered) {
        ourActionsRegistered = true;/* ww w . java  2  s  .  co m*/

        EditorActionManager actionManager = EditorActionManager.getInstance();

        TypedAction typedAction = actionManager.getTypedAction();
        typedAction.setupHandler(new MyTypedHandler(typedAction.getHandler()));

        actionManager.setActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE,
                new BackSpaceHandler(actionManager.getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE)));
        actionManager.setActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP,
                new UpHandler(actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP)));
        actionManager.setActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN,
                new DownHandler(actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN)));
    }

    FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.incremental.search");

    String selection = editor.getSelectionModel().getSelectedText();
    JLabel label2 = new MyLabel(selection == null ? "" : selection);

    PerEditorSearchData data = editor.getUserData(SEARCH_DATA_IN_EDITOR_VIEW_KEY);
    if (data == null) {
        data = new PerEditorSearchData();
    } else {
        if (data.hint != null) {
            if (data.lastSearch != null) {
                PerHintSearchData hintData = data.hint.getUserData(SEARCH_DATA_IN_HINT_KEY);
                //The user has not started typing
                if ("".equals(hintData.label.getText())) {
                    label2 = new MyLabel(data.lastSearch);
                }
            }
            data.hint.hide();
        }
    }

    JLabel label1 = new MyLabel(" " + CodeInsightBundle.message("incremental.search.tooltip.prefix"));
    label1.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD));

    JPanel panel = new MyPanel(label1);
    panel.add(label1, BorderLayout.WEST);
    panel.add(label2, BorderLayout.CENTER);
    panel.setBorder(BorderFactory.createLineBorder(Color.black));

    final DocumentListener[] documentListener = new DocumentListener[1];
    final CaretListener[] caretListener = new CaretListener[1];
    final Document document = editor.getDocument();

    final LightweightHint hint = new LightweightHint(panel) {
        @Override
        public void hide() {
            PerHintSearchData data = getUserData(SEARCH_DATA_IN_HINT_KEY);
            LOG.assertTrue(data != null);
            String prefix = data.label.getText();

            super.hide();

            if (data.segmentHighlighter != null) {
                data.segmentHighlighter.dispose();
            }
            PerEditorSearchData editorData = editor.getUserData(SEARCH_DATA_IN_EDITOR_VIEW_KEY);
            editorData.hint = null;
            editorData.lastSearch = prefix;

            if (documentListener[0] != null) {
                document.removeDocumentListener(documentListener[0]);
            }

            if (caretListener[0] != null) {
                CaretListener listener = caretListener[0];
                editor.getCaretModel().removeCaretListener(listener);
            }
        }
    };

    documentListener[0] = new DocumentAdapter() {
        @Override
        public void documentChanged(DocumentEvent e) {
            if (!hint.isVisible())
                return;
            hint.hide();
        }
    };
    document.addDocumentListener(documentListener[0]);

    caretListener[0] = new CaretAdapter() {
        @Override
        public void caretPositionChanged(CaretEvent e) {
            PerHintSearchData data = hint.getUserData(SEARCH_DATA_IN_HINT_KEY);
            if (data != null && data.ignoreCaretMove)
                return;
            if (!hint.isVisible())
                return;
            hint.hide();
        }
    };
    CaretListener listener = caretListener[0];
    editor.getCaretModel().addCaretListener(listener);

    final JComponent component = editor.getComponent();
    int x = SwingUtilities.convertPoint(component, 0, 0, component).x;
    int y = -hint.getComponent().getPreferredSize().height;
    Point p = SwingUtilities.convertPoint(component, x, y, component.getRootPane().getLayeredPane());

    HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, p,
            HintManagerImpl.HIDE_BY_ESCAPE | HintManagerImpl.HIDE_BY_TEXT_CHANGE, 0, false,
            new HintHint(editor, p).setAwtTooltip(false));

    PerHintSearchData hintData = new PerHintSearchData(project, label2);
    hintData.searchStart = editor.getCaretModel().getOffset();
    hint.putUserData(SEARCH_DATA_IN_HINT_KEY, hintData);

    data.hint = hint;
    editor.putUserData(SEARCH_DATA_IN_EDITOR_VIEW_KEY, data);

    if (hintData.label.getText().length() > 0) {
        updatePosition(editor, hintData, true, false);
    }
}

From source file:com.intellij.lang.properties.PropertiesEnterTest.java

License:Apache License

private static void typeEnter() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
    actionHandler.execute(getEditor(), DataManager.getInstance().getDataContext());
}