Search in sources :

Example 6 with EditorActionHandler

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

the class CaretImpl method selectWordAtCaret.

@Override
public void selectWordAtCaret(final boolean honorCamelWordsSettings) {
    validateContext(true);
    myEditor.getCaretModel().doWithCaretMerging(() -> {
        removeSelection();
        final EditorSettings settings = myEditor.getSettings();
        boolean camelTemp = settings.isCamelWords();
        final boolean needOverrideSetting = camelTemp && !honorCamelWordsSettings;
        if (needOverrideSetting) {
            settings.setCamelWords(false);
        }
        try {
            EditorActionHandler handler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_SELECT_WORD_AT_CARET);
            handler.execute(myEditor, this, myEditor.getDataContext());
        } finally {
            if (needOverrideSetting) {
                settings.resetCamelWords();
            }
        }
    });
}
Also used : EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler)

Example 7 with EditorActionHandler

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

the class MvnDependencyPasteTest method performCut.

private static void performCut() {
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CUT);
    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 8 with EditorActionHandler

use of com.intellij.openapi.editor.actionSystem.EditorActionHandler in project go-lang-idea-plugin by go-lang-plugin-org.

the class BracesInsertHandler method handleInsert.

@Override
public void handleInsert(@NotNull InsertionContext context, LookupElement item) {
    Editor editor = context.getEditor();
    CharSequence documentText = context.getDocument().getImmutableCharSequence();
    int offset = skipWhiteSpaces(editor.getCaretModel().getOffset(), documentText);
    if (documentText.charAt(offset) != '{') {
        Project project = context.getProject();
        Template template = TemplateManager.getInstance(project).createTemplate("braces", "go", myOneLine ? "{$END$}" : " {\n$END$\n}");
        template.setToReformat(true);
        TemplateManager.getInstance(project).startTemplate(editor, template);
    } else {
        editor.getCaretModel().moveToOffset(offset);
        ApplicationManager.getApplication().runWriteAction(() -> {
            EditorActionHandler enterAction = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE);
            enterAction.execute(editor, editor.getCaretModel().getCurrentCaret(), ((EditorEx) editor).getDataContext());
        });
    }
}
Also used : Project(com.intellij.openapi.project.Project) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler) Editor(com.intellij.openapi.editor.Editor) Template(com.intellij.codeInsight.template.Template)

Example 9 with EditorActionHandler

use of com.intellij.openapi.editor.actionSystem.EditorActionHandler 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)

Example 10 with EditorActionHandler

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

the class PlainEnterProcessor method expandCodeBlock.

static boolean expandCodeBlock(Editor editor, PsiElement psiElement) {
    PsiCodeBlock block = getControlStatementBlock(editor.getCaretModel().getOffset(), psiElement);
    if (processExistingBlankLine(editor, block, psiElement)) {
        return true;
    }
    if (block == null) {
        return false;
    }
    EditorActionHandler enterHandler = getEnterHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE);
    PsiElement firstElement = block.getFirstBodyElement();
    if (firstElement == null) {
        firstElement = block.getRBrace();
        // Plain enter processor inserts enter after the end of line, hence, we don't want to use it here because the line ends with
        // the empty braces block. So, we get the following in case of default handler usage:
        //     Before:
        //         if (condition[caret]) {}
        //     After:
        //         if (condition) {}
        //             [caret]
        enterHandler = getEnterHandler(IdeActions.ACTION_EDITOR_ENTER);
    }
    editor.getCaretModel().moveToOffset(firstElement != null ? firstElement.getTextRange().getStartOffset() : block.getTextRange().getEndOffset());
    enterHandler.execute(editor, ((EditorEx) editor).getDataContext());
    return true;
}
Also used : EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler)

Aggregations

EditorActionHandler (com.intellij.openapi.editor.actionSystem.EditorActionHandler)16 EditorActionManager (com.intellij.openapi.editor.actionSystem.EditorActionManager)7 ASTNode (com.intellij.lang.ASTNode)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 CodeBlockEndAction (com.intellij.codeInsight.editorActions.CodeBlockEndAction)1 CodeBlockStartAction (com.intellij.codeInsight.editorActions.CodeBlockStartAction)1 Template (com.intellij.codeInsight.template.Template)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiCodeBlock (com.intellij.psi.PsiCodeBlock)1 PsiElement (com.intellij.psi.PsiElement)1 NonNls (org.jetbrains.annotations.NonNls)1