Search in sources :

Example 71 with HighlighterIterator

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

Example 72 with HighlighterIterator

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

the class BraceHighlightingHandler method updateBraces.

void updateBraces() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (myPsiFile == null || !myPsiFile.isValid())
        return;
    clearBraceHighlighters();
    if (!myCodeInsightSettings.HIGHLIGHT_BRACES)
        return;
    if (myEditor.getSelectionModel().hasSelection())
        return;
    if (myEditor.getSoftWrapModel().isInsideOrBeforeSoftWrap(myEditor.getCaretModel().getVisualPosition()))
        return;
    int offset = myEditor.getCaretModel().getOffset();
    final CharSequence chars = myEditor.getDocument().getCharsSequence();
    //if (myEditor.offsetToLogicalPosition(offset).column != myEditor.getCaretModel().getLogicalPosition().column) {
    //  // we are in virtual space
    //  final int caretLineNumber = myEditor.getCaretModel().getLogicalPosition().line;
    //  if (caretLineNumber >= myDocument.getLineCount()) return;
    //  offset = myDocument.getLineEndOffset(caretLineNumber) + myDocument.getLineSeparatorLength(caretLineNumber);
    //}
    final int originalOffset = offset;
    EditorHighlighter highlighter = getEditorHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset);
    FileType fileType = PsiUtilBase.getPsiFileAtOffset(myPsiFile, offset).getFileType();
    if (iterator.atEnd()) {
        offset--;
    } else if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
        offset--;
    } else if (!BraceMatchingUtil.isLBraceToken(iterator, chars, fileType)) {
        offset--;
        if (offset >= 0) {
            HighlighterIterator it = highlighter.createIterator(offset);
            if (!BraceMatchingUtil.isRBraceToken(it, chars, getFileTypeByIterator(it)))
                offset++;
        }
    }
    if (offset < 0) {
        removeLineMarkers();
        return;
    }
    iterator = highlighter.createIterator(offset);
    fileType = getFileTypeByIterator(iterator);
    myAlarm.cancelAllRequests();
    if (BraceMatchingUtil.isLBraceToken(iterator, chars, fileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
        doHighlight(offset, originalOffset, fileType);
    } else if (offset > 0 && offset < chars.length()) {
        // There is a possible case that there are paired braces nearby the caret position and the document contains only white
        // space symbols between them. We want to highlight such braces as well.
        // Example: 
        //     public void test() { <caret>
        //     }
        char c = chars.charAt(offset);
        boolean searchForward = c != '\n';
        // Try to find matched brace backwards.
        if (offset >= originalOffset && (c == ' ' || c == '\t' || c == '\n')) {
            int backwardNonWsOffset = CharArrayUtil.shiftBackward(chars, offset - 1, "\t ");
            if (backwardNonWsOffset >= 0) {
                iterator = highlighter.createIterator(backwardNonWsOffset);
                FileType newFileType = getFileTypeByIterator(iterator);
                if (BraceMatchingUtil.isLBraceToken(iterator, chars, newFileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, newFileType)) {
                    offset = backwardNonWsOffset;
                    searchForward = false;
                    doHighlight(backwardNonWsOffset, originalOffset, newFileType);
                }
            }
        }
        // Try to find matched brace forward.
        if (searchForward) {
            int forwardOffset = CharArrayUtil.shiftForward(chars, offset, "\t ");
            if (forwardOffset > offset || c == ' ' || c == '\t') {
                iterator = highlighter.createIterator(forwardOffset);
                FileType newFileType = getFileTypeByIterator(iterator);
                if (BraceMatchingUtil.isLBraceToken(iterator, chars, newFileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, newFileType)) {
                    offset = forwardOffset;
                    doHighlight(forwardOffset, originalOffset, newFileType);
                }
            }
        }
    }
    //highlight scope
    if (!myCodeInsightSettings.HIGHLIGHT_SCOPE) {
        removeLineMarkers();
        return;
    }
    final int _offset = offset;
    final FileType _fileType = fileType;
    myAlarm.addRequest(() -> {
        if (!myProject.isDisposed() && !myEditor.isDisposed()) {
            highlightScope(_offset, _fileType);
        }
    }, 300);
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) LightweightHint(com.intellij.ui.LightweightHint) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 73 with HighlighterIterator

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

the class KotlinTypedHandler method autoPopupParameterInfo.

private static void autoPopupParameterInfo(Project project, Editor editor) {
    int offset = editor.getCaretModel().getOffset();
    if (offset == 0)
        return;
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 1);
    IElementType tokenType = iterator.getTokenType();
    if (KtTokens.COMMENTS.contains(tokenType) || tokenType == KtTokens.REGULAR_STRING_PART || tokenType == KtTokens.OPEN_QUOTE || tokenType == KtTokens.CHARACTER_LITERAL)
        return;
    AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, null);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 74 with HighlighterIterator

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

the class KotlinTypedHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
    if (!(file instanceof KtFile)) {
        return Result.CONTINUE;
    }
    switch(c) {
        case '<':
            kotlinLTTyped = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && LtGtTypingUtils.shouldAutoCloseAngleBracket(editor.getCaretModel().getOffset(), editor);
            autoPopupParameterInfo(project, editor);
            return Result.CONTINUE;
        case '>':
            if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
                if (LtGtTypingUtils.handleKotlinGTInsert(editor)) {
                    return Result.STOP;
                }
            }
            return Result.CONTINUE;
        case '{':
            // Returning Result.CONTINUE will cause inserting "{}" for unmatched '{'
            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() || !(SUPPRESS_AUTO_INSERT_CLOSE_BRACE_AFTER.contains(iterator.getTokenType()))) {
                return Result.CONTINUE;
            }
            int tokenBeforeBraceOffset = iterator.getStart();
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            PsiElement leaf = file.findElementAt(offset);
            if (leaf != null) {
                PsiElement parent = leaf.getParent();
                if (parent != null && CONTROL_FLOW_EXPRESSIONS.contains(parent.getNode().getElementType())) {
                    ASTNode nonWhitespaceSibling = FormatterUtil.getPreviousNonWhitespaceSibling(leaf.getNode());
                    if (nonWhitespaceSibling != null && nonWhitespaceSibling.getStartOffset() == tokenBeforeBraceOffset) {
                        EditorModificationUtil.insertStringAtCaret(editor, "{", false, true);
                        indentBrace(project, editor, '{');
                        return Result.STOP;
                    }
                }
            }
            return Result.CONTINUE;
        case '.':
            autoPopupMemberLookup(project, editor);
            return Result.CONTINUE;
        case '@':
            autoPopupLabelLookup(project, editor);
            return Result.CONTINUE;
        case ':':
            autoPopupCallableReferenceLookup(project, editor);
            return Result.CONTINUE;
        case '[':
            autoPopupParameterInfo(project, editor);
            return Result.CONTINUE;
    }
    return Result.CONTINUE;
}
Also used : ASTNode(com.intellij.lang.ASTNode) KtFile(org.jetbrains.kotlin.psi.KtFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 75 with HighlighterIterator

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

the class PyTripleQuoteBackspaceDelegate method beforeCharDeleted.

@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
    isTripleQuote = false;
    if (c == '"' || c == '\'' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
        final QuoteHandler quoteHandler = TypedHandler.getQuoteHandler(file, editor);
        if (quoteHandler == null || !(quoteHandler instanceof BaseQuoteHandler))
            return;
        final int offset = editor.getCaretModel().getCurrentCaret().getOffset();
        String text = editor.getDocument().getText();
        boolean mayBeTripleQuote = offset >= 3 && offset + 2 < text.length();
        if (mayBeTripleQuote) {
            HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
            boolean hasTripleQuoteAfter = offset + 2 < text.length() && text.charAt(offset) == c && text.charAt(offset + 1) == c && text.charAt(offset + 2) == c;
            isTripleQuote = quoteHandler.isOpeningQuote(iterator, offset - 1) && hasTripleQuoteAfter;
        }
    }
}
Also used : BaseQuoteHandler(com.jetbrains.python.editor.BaseQuoteHandler) QuoteHandler(com.intellij.codeInsight.editorActions.QuoteHandler) BaseQuoteHandler(com.jetbrains.python.editor.BaseQuoteHandler) 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