Search in sources :

Example 6 with EditorActionManager

use of com.intellij.openapi.editor.actionSystem.EditorActionManager in project intellij-community by JetBrains.

the class LeaveCodeBlockEnterProcessor method doEnter.

@Override
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
    PsiElement parent = psiElement.getParent();
    if (!(parent instanceof PsiCodeBlock)) {
        return false;
    }
    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;
}
Also used : EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) PsiCodeBlock(com.intellij.psi.PsiCodeBlock) DataContext(com.intellij.openapi.actionSystem.DataContext) ASTNode(com.intellij.lang.ASTNode) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler) PsiElement(com.intellij.psi.PsiElement)

Example 7 with EditorActionManager

use of com.intellij.openapi.editor.actionSystem.EditorActionManager in project intellij-community by JetBrains.

the class ConsoleViewImplTest method testTypeInEmptyConsole.

public void testTypeInEmptyConsole() throws Exception {
    ConsoleViewImpl console = myConsole;
    console.clear();
    EditorActionManager actionManager = EditorActionManager.getInstance();
    DataContext dataContext = DataManager.getInstance().getDataContext(console.getComponent());
    TypedAction action = actionManager.getTypedAction();
    action.actionPerformed(console.getEditor(), 'h', dataContext);
    assertEquals(1, console.getContentSize());
}
Also used : EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) TypedAction(com.intellij.openapi.editor.actionSystem.TypedAction)

Example 8 with EditorActionManager

use of com.intellij.openapi.editor.actionSystem.EditorActionManager in project intellij-community by JetBrains.

the class PropertiesJoinLinesTest method performAction.

private static void performAction() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_JOIN_LINES);
    actionHandler.execute(getEditor(), DataManager.getInstance().getDataContext());
}
Also used : EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler)

Example 9 with EditorActionManager

use of com.intellij.openapi.editor.actionSystem.EditorActionManager in project intellij-community by JetBrains.

the class MvnDependencyPasteTest method performPaste.

private static void performPaste() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_PASTE);
    actionHandler.execute(getEditor(), null, DataManager.getInstance().getDataContextFromFocus().getResultSync());
}
Also used : EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler)

Example 10 with EditorActionManager

use of com.intellij.openapi.editor.actionSystem.EditorActionManager in project intellij-community by JetBrains.

the class PlainEnterProcessor method processExistingBlankLine.

/**
   * There is a possible case that target code block already starts with the empty line:
   * <pre>
   *   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} 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} otherwise
   */
private static boolean processExistingBlankLine(@NotNull Editor editor, @Nullable PsiCodeBlock codeBlock, @Nullable PsiElement element) {
    PsiWhiteSpace whiteSpace = null;
    if (codeBlock == null) {
        if (element != null && !(element instanceof PsiMember)) {
            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;
}
Also used : EditorActionManager(com.intellij.openapi.editor.actionSystem.EditorActionManager) DataContext(com.intellij.openapi.actionSystem.DataContext) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler) Document(com.intellij.openapi.editor.Document)

Aggregations

EditorActionManager (com.intellij.openapi.editor.actionSystem.EditorActionManager)11 EditorActionHandler (com.intellij.openapi.editor.actionSystem.EditorActionHandler)7 TypedAction (com.intellij.openapi.editor.actionSystem.TypedAction)4 DataContext (com.intellij.openapi.actionSystem.DataContext)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 Editor (com.intellij.openapi.editor.Editor)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiCodeBlock (com.intellij.psi.PsiCodeBlock)1 PsiElement (com.intellij.psi.PsiElement)1 HintHint (com.intellij.ui.HintHint)1 LightweightHint (com.intellij.ui.LightweightHint)1