Search in sources :

Example 56 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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 57 with EditorHighlighter

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

the class EnterBetweenXmlTagsHandler method isBetweenXmlTags.

private static boolean isBetweenXmlTags(Project project, Editor editor, PsiFile file, int offset) {
    if (offset == 0)
        return false;
    CharSequence chars = editor.getDocument().getCharsSequence();
    if (chars.charAt(offset - 1) != '>')
        return false;
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    if (iterator.getTokenType() != XmlTokenType.XML_TAG_END)
        return false;
    if (isAtTheEndOfEmptyTag(project, editor, file, iterator)) {
        return false;
    }
    iterator.retreat();
    int retrieveCount = 1;
    while (!iterator.atEnd()) {
        final IElementType tokenType = iterator.getTokenType();
        if (tokenType == XmlTokenType.XML_END_TAG_START)
            return false;
        if (tokenType == XmlTokenType.XML_START_TAG_START)
            break;
        ++retrieveCount;
        iterator.retreat();
    }
    for (int i = 0; i < retrieveCount; ++i) iterator.advance();
    iterator.advance();
    return !iterator.atEnd() && iterator.getTokenType() == XmlTokenType.XML_END_TAG_START;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 58 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-plugins by JetBrains.

the class HbEnterHandler method isBetweenHbTags.

/**
   * Checks to see if {@code Enter} has been typed while the caret is between an open and close tag pair.
   *
   * @return true if between open and close tags, false otherwise
   */
private static boolean isBetweenHbTags(Editor editor, PsiFile file, int offset) {
    if (offset == 0)
        return false;
    CharSequence chars = editor.getDocument().getCharsSequence();
    if (chars.charAt(offset - 1) != '}')
        return false;
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
    final PsiElement openerElement = file.findElementAt(iterator.getStart());
    PsiElement openTag = HbPsiUtil.findParentOpenTagElement(openerElement);
    if (openTag == null) {
        return false;
    }
    iterator.advance();
    if (iterator.atEnd()) {
        // no more tokens, so certainly no close tag
        return false;
    }
    final PsiElement closerElement = file.findElementAt(iterator.getStart());
    PsiElement closeTag = HbPsiUtil.findParentCloseTagElement(closerElement);
    // if we got this far, we're between open and close tags iff this is a close tag
    return closeTag != null;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 59 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-plugins by JetBrains.

the class MarkdownCssSettingsForm method setHighlighting.

private static void setHighlighting(EditorEx editor) {
    final FileType cssFileType = FileTypeManager.getInstance().getFileTypeByExtension("css");
    if (cssFileType == UnknownFileType.INSTANCE) {
        return;
    }
    final EditorHighlighter editorHighlighter = HighlighterFactory.createHighlighter(cssFileType, EditorColorsManager.getInstance().getGlobalScheme(), null);
    editor.setHighlighter(editorHighlighter);
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 60 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project Perl5-IDEA by Camelcade.

the class PerlTypedHandler method charTyped.

@NotNull
@Override
public Result charTyped(char typedChar, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    final int offset = editor.getCaretModel().getOffset() - 1;
    if (offset < 0) {
        return Result.CONTINUE;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset);
    IElementType elementTokenType = iterator.getTokenType();
    Document document = editor.getDocument();
    if (QUOTE_OPEN_ANY.contains(elementTokenType) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
        IElementType quotePrefixType = offset > 0 ? PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(offset - 1)) : null;
        CharSequence text = document.getCharsSequence();
        if (offset > text.length() - 1 || text.charAt(offset) != typedChar) {
            return Result.CONTINUE;
        }
        if (elementTokenType == QUOTE_DOUBLE_OPEN || elementTokenType == QUOTE_SINGLE_OPEN) {
            AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
        }
        char openChar = text.charAt(offset);
        char closeChar = PerlBaseLexer.getQuoteCloseChar(openChar);
        iterator.advance();
        IElementType possibleCloseQuoteType = iterator.atEnd() ? null : iterator.getTokenType();
        if (QUOTE_CLOSE_FIRST_ANY.contains(possibleCloseQuoteType) && closeChar == text.charAt(iterator.getStart())) {
            if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType) && StringUtil.containsChar(HANDLED_BY_BRACE_MATCHER, openChar)) {
                iterator.advance();
                if (iterator.atEnd() || !QUOTE_OPEN_ANY.contains(iterator.getTokenType())) {
                    EditorModificationUtil.insertStringAtCaret(editor, Character.toString(closeChar) + openChar, false, false);
                }
            }
            return Result.CONTINUE;
        }
        StringBuilder textToAppend = new StringBuilder();
        textToAppend.append(closeChar);
        if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType)) {
            textToAppend.append(openChar);
            if (openChar != closeChar) {
                textToAppend.append(closeChar);
            }
        }
        EditorModificationUtil.insertStringAtCaret(editor, textToAppend.toString(), false, false);
    } else if (elementTokenType == LEFT_BRACE) {
        AutoPopupController.getInstance(project).scheduleAutoPopup(editor, CompletionType.BASIC, psiFile -> {
            PsiElement newElement = psiFile.findElementAt(offset);
            return PsiUtilCore.getElementType(newElement) == elementTokenType && newElement.getParent() instanceof PsiPerlHashIndex;
        });
    }
    return Result.CONTINUE;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) QUOTE_CLOSE_FIRST_ANY(com.perl5.lang.perl.lexer.PerlTokenSets.QUOTE_CLOSE_FIRST_ANY) IElementType(com.intellij.psi.tree.IElementType) Document(com.intellij.openapi.editor.Document) PerlPsiUtil(com.perl5.lang.perl.psi.utils.PerlPsiUtil) CaretModel(com.intellij.openapi.editor.CaretModel) AutoPopupController(com.intellij.codeInsight.AutoPopupController) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiPerlCommaSequenceExpr(com.perl5.lang.perl.psi.PsiPerlCommaSequenceExpr) QUOTE_OPEN_ANY(com.perl5.lang.perl.lexer.PerlTokenSets.QUOTE_OPEN_ANY) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) EditorModificationUtil(com.intellij.openapi.editor.EditorModificationUtil) PsiPerlHashIndex(com.perl5.lang.perl.psi.PsiPerlHashIndex) CompletionType(com.intellij.codeInsight.completion.CompletionType) TypedHandlerDelegate(com.intellij.codeInsight.editorActions.TypedHandlerDelegate) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) StringUtil(com.intellij.openapi.util.text.StringUtil) CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) FileType(com.intellij.openapi.fileTypes.FileType) Editor(com.intellij.openapi.editor.Editor) Perl5CodeInsightSettings(com.perl5.lang.perl.idea.codeInsight.Perl5CodeInsightSettings) TokenSet(com.intellij.psi.tree.TokenSet) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) PerlElementTypes(com.perl5.lang.perl.lexer.PerlElementTypes) NotNull(org.jetbrains.annotations.NotNull) PerlBaseLexer(com.perl5.lang.perl.lexer.PerlBaseLexer) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PsiPerlHashIndex(com.perl5.lang.perl.psi.PsiPerlHashIndex) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)63 EditorEx (com.intellij.openapi.editor.ex.EditorEx)36 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)36 Document (com.intellij.openapi.editor.Document)13 IElementType (com.intellij.psi.tree.IElementType)11 Editor (com.intellij.openapi.editor.Editor)8 FileType (com.intellij.openapi.fileTypes.FileType)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)8 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)7 PsiFile (com.intellij.psi.PsiFile)7 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)6 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)5 PsiElement (com.intellij.psi.PsiElement)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 Project (com.intellij.openapi.project.Project)4 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)3 EditorWindow (com.intellij.injected.editor.EditorWindow)3 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)3