Search in sources :

Example 76 with HighlighterIterator

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

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

use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project oxy-template-support-plugin by mutant-industries.

the class MatchingTagsAndJsBlock method buildBraceMatcherBasedFolding.

private static void buildBraceMatcherBasedFolding(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, @NotNull SyntaxHighlighter highlighter) {
    LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
    editorHighlighter.setText(document.getText());
    FileType fileType = root.getContainingFile().getFileType();
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
    TextRange totalRange = root.getTextRange();
    final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    String editorText = document.getText();
    TextRange range;
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (OxyTemplateParserDefinition.PARAMETER_QUOTES.contains(iterator.getTokenType())) {
            iterator.advance();
            continue;
        }
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
            if (iterator.getTokenType() == OxyTemplateTypes.T_XML_TAG_START) {
                IElementType tokenType = iterator.getTokenType();
                do {
                    iterator.advance();
                } while (!iterator.atEnd() && iterator.getTokenType() != OxyTemplateTypes.T_XML_OPEN_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_CLOSE_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_EMPTY_TAG_END);
                if (iterator.atEnd()) {
                    return;
                }
                stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), tokenType));
                iterator.retreat();
            } else {
                stack.addLast(Trinity.create(iterator.getStart() + 2, iterator.getEnd(), iterator.getTokenType()));
            }
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            range = new TextRange(last.first, iterator.getEnd() - (iterator.getTokenType() == OxyTemplateTypes.T_XML_CLOSE_TAG_END ? 1 : 2));
            if (last.third != OxyTemplateTypes.T_XML_EMPTY_TAG_END && StringUtil.countChars(document.getText(range), '\n') >= 2) {
                descriptors.add(new FoldingDescriptor(root, range));
            }
        }
        iterator.advance();
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) OxyTemplateFileType(ool.intellij.plugin.file.type.OxyTemplateFileType) FileType(com.intellij.openapi.fileTypes.FileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 79 with HighlighterIterator

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

the class PerlEnterInCommentHandler 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)) {
        return Result.Continue;
    }
    int currentOffset = caretOffset.get();
    assert editor instanceof EditorEx;
    HighlighterIterator highlighterIterator = ((EditorEx) editor).getHighlighter().createIterator(currentOffset);
    if (highlighterIterator.atEnd()) {
        return Result.Continue;
    }
    IElementType currentTokenType = highlighterIterator.getTokenType();
    int currentTokenStart = highlighterIterator.getStart();
    // noinspection ConstantConditions
    if (currentTokenType == COMMENT_LINE && currentOffset > currentTokenStart) {
        Document document = editor.getDocument();
        int lineNumber = document.getLineNumber(currentOffset);
        int lineEnd = document.getLineEndOffset(lineNumber);
        if (lineEnd > currentOffset) {
            document.insertString(currentOffset, "# ");
        }
    }
    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)

Example 80 with HighlighterIterator

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

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