Search in sources :

Example 1 with JavaSmartEnterProcessor

use of com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor in project intellij-community by JetBrains.

the class JavaTypedHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
    if (!(file instanceof PsiJavaFile))
        return Result.CONTINUE;
    if (c == '@') {
        autoPopupJavadocLookup(project, editor);
    } else if (c == '#' || c == '.') {
        autoPopupMemberLookup(project, editor);
    }
    int offsetBefore = editor.getCaretModel().getOffset();
    //important to calculate before inserting charTyped
    myJavaLTTyped = '<' == c && !(file instanceof JspFile) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && PsiUtil.isLanguageLevel5OrHigher(file) && isAfterClassLikeIdentifierOrDot(offsetBefore, editor);
    if ('>' == c) {
        if (!(file instanceof JspFile) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && PsiUtil.isLanguageLevel5OrHigher(file)) {
            if (handleJavaGT(editor, JavaTokenType.LT, JavaTokenType.GT, INVALID_INSIDE_REFERENCE))
                return Result.STOP;
        }
    }
    if (c == ';') {
        if (handleSemicolon(editor, fileType))
            return Result.STOP;
    }
    if (fileType == StdFileTypes.JAVA && c == '{') {
        int offset = editor.getCaretModel().getOffset();
        if (offset == 0) {
            return Result.CONTINUE;
        }
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 1);
        while (!iterator.atEnd() && iterator.getTokenType() == TokenType.WHITE_SPACE) {
            iterator.retreat();
        }
        if (iterator.atEnd() || iterator.getTokenType() == JavaTokenType.RBRACKET || iterator.getTokenType() == JavaTokenType.EQ) {
            return Result.CONTINUE;
        }
        Document doc = editor.getDocument();
        PsiDocumentManager.getInstance(project).commitDocument(doc);
        final PsiElement leaf = file.findElementAt(offset);
        if (PsiTreeUtil.getParentOfType(leaf, PsiArrayInitializerExpression.class, false, PsiCodeBlock.class, PsiMember.class) != null) {
            return Result.CONTINUE;
        }
        PsiElement st = leaf != null ? leaf.getParent() : null;
        PsiElement prev = offset > 1 ? file.findElementAt(offset - 1) : null;
        if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && isRparenth(leaf) && (st instanceof PsiWhileStatement || st instanceof PsiIfStatement) && shouldInsertStatementBody(st, doc, prev)) {
            CommandProcessor.getInstance().executeCommand(project, () -> new JavaSmartEnterProcessor().process(project, editor, file), "Insert block statement", null);
            return Result.STOP;
        }
        PsiElement prevLeaf = leaf == null ? null : PsiTreeUtil.prevVisibleLeaf(leaf);
        if (PsiUtil.isJavaToken(prevLeaf, JavaTokenType.ARROW) || PsiTreeUtil.getParentOfType(prevLeaf, PsiNewExpression.class, true, PsiCodeBlock.class, PsiMember.class) != null) {
            return Result.CONTINUE;
        }
        if (PsiTreeUtil.getParentOfType(leaf, PsiCodeBlock.class, false, PsiMember.class) != null) {
            EditorModificationUtil.insertStringAtCaret(editor, "{");
            TypedHandler.indentOpenedBrace(project, editor);
            // use case: manually wrapping part of method's code in 'if', 'while', etc
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
Also used : JavaSmartEnterProcessor(com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor) JspFile(com.intellij.psi.jsp.JspFile) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

JavaSmartEnterProcessor (com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor)1 Document (com.intellij.openapi.editor.Document)1 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)1 JspFile (com.intellij.psi.jsp.JspFile)1