Search in sources :

Example 66 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class RParenthTailType method calcParensBalance.

private static int calcParensBalance(Document document, EditorHighlighter highlighter, int rangeStart, int rangeEnd) {
    LOG.assertTrue(0 <= rangeStart);
    LOG.assertTrue(rangeStart <= rangeEnd);
    LOG.assertTrue(rangeEnd <= document.getTextLength());
    HighlighterIterator iterator = highlighter.createIterator(rangeStart);
    int balance = 0;
    while (!iterator.atEnd() && iterator.getStart() < rangeEnd) {
        IElementType tokenType = iterator.getTokenType();
        if (tokenType == JavaTokenType.LPARENTH) {
            balance++;
        } else if (tokenType == JavaTokenType.RPARENTH) {
            balance--;
        }
        iterator.advance();
    }
    return balance;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 67 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class RParenthTailType method getExistingRParenthOffset.

private static int getExistingRParenthOffset(final Editor editor, final int tailOffset) {
    final Document document = editor.getDocument();
    if (tailOffset >= document.getTextLength())
        return -1;
    final CharSequence charsSequence = document.getCharsSequence();
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    int existingRParenthOffset = -1;
    for (HighlighterIterator iterator = highlighter.createIterator(tailOffset); !iterator.atEnd(); iterator.advance()) {
        final IElementType tokenType = iterator.getTokenType();
        if ((!(tokenType instanceof IJavaElementType) || !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(tokenType)) && tokenType != TokenType.WHITE_SPACE) {
            final int start = iterator.getStart();
            if (iterator.getEnd() == start + 1 && ')' == charsSequence.charAt(start)) {
                existingRParenthOffset = start;
            }
            break;
        }
    }
    if (existingRParenthOffset >= 0) {
        final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(editor.getProject());
        psiDocumentManager.commitDocument(document);
        TextRange range = getRangeToCheckParensBalance(psiDocumentManager.getPsiFile(document), document, editor.getCaretModel().getOffset());
        int balance = calcParensBalance(document, highlighter, range.getStartOffset(), range.getEndOffset());
        if (balance > 0) {
            return -1;
        }
    }
    return existingRParenthOffset;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) IJavaElementType(com.intellij.psi.tree.java.IJavaElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 68 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class JavaTypedHandler method isAfterClassLikeIdentifierOrDot.

public static boolean isAfterClassLikeIdentifierOrDot(final int offset, final Editor editor) {
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    if (iterator.atEnd())
        return false;
    if (iterator.getStart() > 0)
        iterator.retreat();
    final IElementType tokenType = iterator.getTokenType();
    if (tokenType == JavaTokenType.DOT)
        return true;
    return isClassLikeIdentifier(offset, editor, iterator, JavaTokenType.IDENTIFIER);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 69 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class JavaTypedHandler method handleAfterJavaLT.

//need custom handler, since brace matcher cannot be used
public static void handleAfterJavaLT(final Editor editor, final IElementType lt, final IElementType gt, final TokenSet invalidInsideReference) {
    if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET)
        return;
    int offset = editor.getCaretModel().getOffset();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    while (iterator.getStart() > 0 && !invalidInsideReference.contains(iterator.getTokenType())) {
        iterator.retreat();
    }
    if (invalidInsideReference.contains(iterator.getTokenType()))
        iterator.advance();
    int balance = 0;
    while (!iterator.atEnd() && balance >= 0) {
        final IElementType tokenType = iterator.getTokenType();
        if (tokenType == lt) {
            balance++;
        } else if (tokenType == gt) {
            balance--;
        } else if (invalidInsideReference.contains(tokenType)) {
            break;
        }
        iterator.advance();
    }
    if (balance == 1) {
        editor.getDocument().insertString(offset, ">");
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 70 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-community by JetBrains.

the class JavaTypedHandler method handleJavaGT.

//need custom handler, since brace matcher cannot be used
public static boolean handleJavaGT(final Editor editor, final IElementType lt, final IElementType gt, final TokenSet invalidInsideReference) {
    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.getTokenType() != gt)
        return false;
    while (!iterator.atEnd() && !invalidInsideReference.contains(iterator.getTokenType())) {
        iterator.advance();
    }
    if (!iterator.atEnd() && invalidInsideReference.contains(iterator.getTokenType()))
        iterator.retreat();
    int balance = 0;
    while (!iterator.atEnd() && balance >= 0) {
        final IElementType tokenType = iterator.getTokenType();
        if (tokenType == lt) {
            balance--;
        } else if (tokenType == gt) {
            balance++;
        } else if (invalidInsideReference.contains(tokenType)) {
            break;
        }
        iterator.retreat();
    }
    if (balance == 0) {
        EditorModificationUtil.moveCaretRelatively(editor, 1);
        return true;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)83 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)33 IElementType (com.intellij.psi.tree.IElementType)33 Document (com.intellij.openapi.editor.Document)12 FileType (com.intellij.openapi.fileTypes.FileType)12 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)9 TextRange (com.intellij.openapi.util.TextRange)8 Editor (com.intellij.openapi.editor.Editor)7 PsiElement (com.intellij.psi.PsiElement)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 PsiFile (com.intellij.psi.PsiFile)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)4 Project (com.intellij.openapi.project.Project)4 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 Trinity (com.intellij.openapi.util.Trinity)3 ArrayList (java.util.ArrayList)3 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)2