Search in sources :

Example 31 with EditorHighlighter

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

the class ActionScriptHighlightingTest method testScriptHighlightingInE4X.

public void testScriptHighlightingInE4X() throws Exception {
    //                       10        20         30           40         50
    //             01234567890123456789012 34567890123456 7 89012 34567890123456789
    String text = "var a = <xml>{ c }</xml>";
    configureByFile(BASE_PATH + getTestName(false) + ".js2");
    EditorHighlighter highlighter = HighlighterFactory.createHighlighter(myProject, getFile().getVirtualFile());
    highlighter.setText(text);
    HighlighterIterator iterator = highlighter.createIterator(15);
    assertEquals(JSTokenTypes.IDENTIFIER, iterator.getTokenType());
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 32 with EditorHighlighter

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

the class PerlEnterInBracketsHandler method preprocessEnter.

@Override
public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) {
    if (!file.getLanguage().is(PerlLanguage.INSTANCE) || !CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) {
        return Result.Continue;
    }
    Integer currentOffset = caretOffset.get();
    if (currentOffset < 1) {
        return Result.Continue;
    }
    assert editor instanceof EditorEx;
    EditorHighlighter editorHighlighter = ((EditorEx) editor).getHighlighter();
    Document document = editor.getDocument();
    CharSequence documentChars = document.getCharsSequence();
    HighlighterIterator highlighterIterator = editorHighlighter.createIterator(currentOffset - 1);
    IElementType previousTokenType = PerlEditorUtil.getPreviousTokenType(highlighterIterator);
    if (highlighterIterator.atEnd() || StringUtil.containsLineBreak(documentChars.subSequence(highlighterIterator.getStart(), currentOffset))) {
        return Result.Continue;
    }
    int openTokenStart = highlighterIterator.getStart();
    IElementType nextTokenType = PerlEditorUtil.getNextTokenType(highlighterIterator);
    if (highlighterIterator.atEnd() || StringUtil.containsLineBreak(documentChars.subSequence(currentOffset, highlighterIterator.getEnd()))) {
        return Result.Continue;
    }
    if (previousTokenType == LEFT_BRACKET && nextTokenType == RIGHT_BRACKET) {
        doIndent(file, editor, dataContext, originalHandler, document);
    } else if (previousTokenType == QUOTE_SINGLE_OPEN && nextTokenType == QUOTE_SINGLE_CLOSE && openTokenStart > 0) {
        IElementType qwTokenType = PerlEditorUtil.getPreviousTokenType(editorHighlighter.createIterator(openTokenStart - 1));
        if (qwTokenType == RESERVED_QW) {
            doIndent(file, editor, dataContext, originalHandler, document);
        }
    }
    return Result.Continue;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 33 with EditorHighlighter

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

the class PerlTypedHandler method beforeCharTyped.

@NotNull
@Override
public Result beforeCharTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file, @NotNull FileType fileType) {
    CaretModel caretModel = editor.getCaretModel();
    int currentOffset = caretModel.getOffset();
    Document document = editor.getDocument();
    CharSequence documentSequence = document.getCharsSequence();
    if (currentOffset > documentSequence.length()) {
        return Result.CONTINUE;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(currentOffset);
    IElementType nextTokenType = iterator.atEnd() ? null : iterator.getTokenType();
    char nextChar = currentOffset == documentSequence.length() ? 0 : documentSequence.charAt(currentOffset);
    if (c == '<' && nextTokenType == QUOTE_DOUBLE_CLOSE && nextChar == '>' && currentOffset > 0 && documentSequence.charAt(currentOffset - 1) == '<' && (currentOffset < 3 || PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(currentOffset - 2)) != RESERVED_QQ)) {
        document.replaceString(currentOffset, currentOffset + 1, "<");
        caretModel.moveToOffset(currentOffset + 1);
        return Result.STOP;
    }
    if (QUOTE_CLOSE_FIRST_ANY.contains(nextTokenType) && c == nextChar) {
        caretModel.moveToOffset(currentOffset + 1);
        return Result.STOP;
    }
    if (c == ':' && nextTokenType == PACKAGE && Perl5CodeInsightSettings.getInstance().AUTO_INSERT_COLON) {
        document.insertString(currentOffset, "::");
        caretModel.moveToOffset(currentOffset + 2);
        AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
        return Result.STOP;
    }
    if (c == ' ') {
        Result result = tryToAddFatComma(editor, file, currentOffset);
        if (result != null) {
            return result;
        }
    }
    return Result.CONTINUE;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CaretModel(com.intellij.openapi.editor.CaretModel) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-code-outline by sitano.

the class CodeOutlineImageEx method renderRestOfLineToImg.

/**
 * Renders the characters at the given document offset to the code outline
 * image until the first newline character is reached or until the right
 * edge of the image is reached and no more characters can be rendered.
 *
 * @param startOff the offset into the document at which to start rendering
 * @return how many characters were rendered, not including the newline
 */
@Override
protected int renderRestOfLineToImg(int startOff) {
    final CharSequence chars = document.getCharsSequence();
    final int endOff = document.getTextLength();
    if (startOff >= endOff)
        return 0;
    final LogicalPosition startPos = editor.offsetToLogicalPosition(startOff);
    final int line = startPos.line;
    final EditorEx ex = (EditorEx) editor;
    final EditorHighlighter hl = ex.getHighlighter();
    final HighlighterIterator hi = hl.createIterator(startOff);
    int col = startPos.column;
    int painted = 0;
    for (int i = startOff; i < endOff; i++) {
        final char ch = chars.charAt(i);
        while (!hi.atEnd() && i > hi.getEnd()) {
            hi.advance();
        }
        if (ch == '\n')
            break;
        if (col >= visibleImgWidth)
            break;
        drawChar(ch, col, line, getCharColor(editor, hi));
        painted++;
        col++;
    }
    return painted;
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 35 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project idea-handlebars by dmarcotte.

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);
    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)

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