Search in sources :

Example 41 with HighlighterIterator

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

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

the class TemplateToolkitPsiUtil method getLastOpenMarker.

@Nullable
@Contract("null -> null")
public static IElementType getLastOpenMarker(@Nullable Editor editor) {
    if (editor == null) {
        return null;
    }
    int offset = editor.getCaretModel().getOffset();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    while (!iterator.atEnd()) {
        IElementType tokenType = iterator.getTokenType();
        if (TemplateToolkitSyntaxElements.OPEN_TAGS.contains(tokenType)) {
            return tokenType;
        }
        iterator.retreat();
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with HighlighterIterator

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

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

Example 45 with HighlighterIterator

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-elixir by KronicDeth.

the class Issue443 method isLBraceTokenBrace.

/*
     * Private Instance Methods
     */
private boolean isLBraceTokenBrace() {
    int offset = myFixture.getCaretOffset();
    Editor editor = myFixture.getEditor();
    CharSequence text = editor.getDocument().getCharsSequence();
    FileType fileType = ElixirFileType.INSTANCE;
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    return BraceMatchingUtil.isLBraceToken(iterator, text, fileType);
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) ElixirFileType(org.elixir_lang.ElixirFileType) Editor(com.intellij.openapi.editor.Editor) 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