Search in sources :

Example 1 with NontrivialBraceMatcher

use of com.intellij.codeInsight.highlighting.NontrivialBraceMatcher in project intellij-community by JetBrains.

the class TypedHandler method handleRParen.

public static boolean handleRParen(@NotNull Editor editor, @NotNull FileType fileType, char charTyped) {
    if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET)
        return false;
    int offset = editor.getCaretModel().getOffset();
    if (offset == editor.getDocument().getTextLength())
        return false;
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    if (iterator.atEnd())
        return false;
    if (iterator.getEnd() - iterator.getStart() != 1 || editor.getDocument().getCharsSequence().charAt(iterator.getStart()) != charTyped) {
        return false;
    }
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    CharSequence text = editor.getDocument().getCharsSequence();
    if (!braceMatcher.isRBraceToken(iterator, text, fileType)) {
        return false;
    }
    IElementType tokenType = iterator.getTokenType();
    iterator.retreat();
    IElementType lparenTokenType = braceMatcher.getOppositeBraceTokenType(tokenType);
    int lparenthOffset = BraceMatchingUtil.findLeftmostLParen(iterator, lparenTokenType, text, fileType);
    if (lparenthOffset < 0) {
        if (braceMatcher instanceof NontrivialBraceMatcher) {
            for (IElementType t : ((NontrivialBraceMatcher) braceMatcher).getOppositeBraceTokenTypes(tokenType)) {
                if (t == lparenTokenType)
                    continue;
                lparenthOffset = BraceMatchingUtil.findLeftmostLParen(iterator, t, text, fileType);
                if (lparenthOffset >= 0)
                    break;
            }
        }
        if (lparenthOffset < 0)
            return false;
    }
    iterator = ((EditorEx) editor).getHighlighter().createIterator(lparenthOffset);
    boolean matched = BraceMatchingUtil.matchBrace(text, fileType, iterator, true, true);
    if (!matched)
        return false;
    EditorModificationUtil.moveCaretRelatively(editor, 1);
    return true;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) NontrivialBraceMatcher(com.intellij.codeInsight.highlighting.NontrivialBraceMatcher) NontrivialBraceMatcher(com.intellij.codeInsight.highlighting.NontrivialBraceMatcher) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)1 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)1 IElementType (com.intellij.psi.tree.IElementType)1