Search in sources :

Example 46 with HighlighterIterator

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

the class CfmlEnterHandler method isBetweenCfmlTags.

private static boolean isBetweenCfmlTags(PsiFile file, Editor editor, 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() != CfmlTokenTypes.R_ANGLEBRACKET)
        return false;
    iterator.retreat();
    int retrieveCount = 1;
    while (!iterator.atEnd()) {
        final IElementType tokenType = iterator.getTokenType();
        if (tokenType == CfmlTokenTypes.LSLASH_ANGLEBRACKET)
            return false;
        if (tokenType == CfmlTokenTypes.OPENER)
            break;
        ++retrieveCount;
        iterator.retreat();
    }
    for (int i = 0; i < retrieveCount; ++i) iterator.advance();
    iterator.advance();
    return !iterator.atEnd() && iterator.getTokenType() == CfmlTokenTypes.LSLASH_ANGLEBRACKET;
}
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 47 with HighlighterIterator

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

the class CfmlTypedHandler method insertCloseTagIfNeeded.

public static boolean insertCloseTagIfNeeded(Editor editor, PsiFile file, Project project) {
    final Document document = editor.getDocument();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    int offset = editor.getCaretModel().getOffset();
    documentManager.commitDocument(document);
    char charAtOffset = DocumentUtils.getCharAt(document, offset);
    if (charAtOffset != '>') {
        EditorModificationUtil.insertStringAtCaret(editor, ">", true, 0);
    }
    EditorModificationUtil.moveCaretRelatively(editor, 1);
    ++offset;
    if (DocumentUtils.getCharAt(document, offset - 2) == '/') {
        return false;
    }
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 2);
    while (!iterator.atEnd() && !iterator.getTokenType().equals(CfmlTokenTypes.CF_TAG_NAME)) {
        if (CfmlUtil.isControlToken(iterator.getTokenType())) {
            return false;
        }
        iterator.retreat();
    }
    if (!iterator.atEnd()) {
        iterator.retreat();
        if (!iterator.atEnd() && iterator.getTokenType().equals(CfmlTokenTypes.LSLASH_ANGLEBRACKET)) {
            return false;
        }
        iterator.advance();
    }
    if (iterator.atEnd()) {
        return false;
    }
    String tagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
    if (CfmlUtil.isSingleCfmlTag(tagName, project) || CfmlUtil.isUserDefined(tagName)) {
        return false;
    }
    PsiElement tagElement = file.findElementAt(iterator.getStart());
    while (tagElement != null && !(tagElement instanceof CfmlTag)) {
        tagElement = tagElement.getParent();
    }
    if (tagElement == null) {
        return false;
    }
    boolean doInsertion = false;
    if (tagElement.getLastChild() instanceof PsiErrorElement) {
        doInsertion = true;
    } else {
        iterator = ((EditorEx) editor).getHighlighter().createIterator(0);
        while (!iterator.atEnd() && iterator.getStart() < offset) {
            if (iterator.getTokenType() == CfmlTokenTypes.CF_TAG_NAME) {
                String currentTagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
                if (tagName.equals(currentTagName)) {
                    PsiElement currentTagElement = file.findElementAt(iterator.getStart());
                    currentTagElement = PsiTreeUtil.getParentOfType(currentTagElement, CfmlTag.class);
                    if (currentTagElement.getLastChild() instanceof PsiErrorElement) {
                        doInsertion = true;
                        break;
                    }
                }
            }
            iterator.advance();
        }
    }
    // tag name in lowercase
    String tagNameFromPsi = ((CfmlTag) tagElement).getTagName();
    if (doInsertion && CfmlUtil.isEndTagRequired(tagNameFromPsi, project)) {
        if (!Comparing.equal(tagNameFromPsi, tagName, false)) {
            // use tagName because it has proper case
            tagName = tagNameFromPsi;
        }
        EditorModificationUtil.insertStringAtCaret(editor, "</" + tagName + ">", true, 0);
        return true;
    }
    return false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 48 with HighlighterIterator

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

the class CfmlEditorUtil method countSharpsBalance.

public static int countSharpsBalance(Editor editor) {
    int sharpsCounter = 0;
    // count balance
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(0);
    while (!iterator.atEnd()) {
        if (iterator.getTokenType() == CfscriptTokenTypes.OPENSHARP || iterator.getTokenType() == CfmlTokenTypes.START_EXPRESSION) {
            sharpsCounter++;
        } else if (iterator.getTokenType() == CfscriptTokenTypes.CLOSESHARP || iterator.getTokenType() == CfmlTokenTypes.END_EXPRESSION) {
            sharpsCounter--;
        }
        iterator.advance();
    }
    return sharpsCounter;
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 49 with HighlighterIterator

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

the class CfmlMatcherTest method doTest.

private void doTest() throws Throwable {
    final int pairOffset = configureByTestFile(getTestName(false));
    int offset = myFixture.getEditor().getCaretModel().getOffset();
    EditorHighlighter editorHighlighter = ((EditorEx) myFixture.getEditor()).getHighlighter();
    HighlighterIterator iterator = editorHighlighter.createIterator(offset);
    boolean forward = offset < pairOffset;
    boolean matched = BraceMatchingUtil.matchBrace(myFixture.getEditor().getDocument().getCharsSequence(), myFixture.getFile().getFileType(), iterator, forward);
    assertTrue(matched);
    assertEquals(pairOffset, iterator.getStart());
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 50 with HighlighterIterator

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

the class ConsoleHighlighterTest method testIteratorEnd.

public void testIteratorEnd() {
    HighlighterIterator iterator = myHighlighter.createIterator(0);
    assertFalse(iterator.atEnd());
    iterator.retreat();
    assertTrue(iterator.atEnd());
    iterator.advance();
    assertFalse(iterator.atEnd());
    iterator.advance();
    assertFalse(iterator.atEnd());
    iterator.advance();
    assertFalse(iterator.atEnd());
    iterator.advance();
    assertTrue(iterator.atEnd());
    iterator.advance();
    assertTrue(iterator.atEnd());
}
Also used : 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