Search in sources :

Example 36 with HighlighterIterator

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

the class JavaCompletionContributor method semicolonNeeded.

public static boolean semicolonNeeded(final Editor editor, PsiFile file, final int startOffset) {
    final PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(file, startOffset, PsiJavaCodeReferenceElement.class, false);
    if (ref != null && !(ref instanceof PsiReferenceExpression)) {
        if (ref.getParent() instanceof PsiTypeElement) {
            return true;
        }
    }
    if (psiElement(PsiIdentifier.class).withParent(psiParameter()).accepts(file.findElementAt(startOffset))) {
        return true;
    }
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(startOffset);
    if (iterator.atEnd())
        return false;
    if (iterator.getTokenType() == JavaTokenType.IDENTIFIER) {
        iterator.advance();
    }
    while (!iterator.atEnd() && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(iterator.getTokenType())) {
        iterator.advance();
    }
    if (!iterator.atEnd() && iterator.getTokenType() == JavaTokenType.LPARENTH && PsiTreeUtil.getParentOfType(ref, PsiExpression.class, PsiClass.class) == null) {
        // looks like a method declaration, e.g. StringBui<caret>methodName() inside a class
        return true;
    }
    if (!iterator.atEnd() && (iterator.getTokenType() == JavaTokenType.COLON) && null == PsiTreeUtil.findElementOfClassAtOffset(file, startOffset, PsiConditionalExpression.class, false)) {
        return true;
    }
    while (!iterator.atEnd() && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(iterator.getTokenType())) {
        iterator.advance();
    }
    if (iterator.atEnd() || iterator.getTokenType() != JavaTokenType.IDENTIFIER)
        return false;
    iterator.advance();
    while (!iterator.atEnd() && ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(iterator.getTokenType())) {
        iterator.advance();
    }
    if (iterator.atEnd())
        return false;
    // <caret> foo = something, we don't want the reference to be treated as a type
    return iterator.getTokenType() == JavaTokenType.EQ;
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 37 with HighlighterIterator

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

the class AnnotationInsertHandler method isAtTokenNeeded.

private static boolean isAtTokenNeeded(InsertionContext myContext) {
    HighlighterIterator iterator = ((EditorEx) myContext.getEditor()).getHighlighter().createIterator(myContext.getStartOffset());
    LOG.assertTrue(iterator.getTokenType() == JavaTokenType.IDENTIFIER);
    iterator.retreat();
    if (iterator.getTokenType() == TokenType.WHITE_SPACE)
        iterator.retreat();
    return iterator.getTokenType() != JavaTokenType.AT && iterator.getTokenType() != JavaTokenType.DOT;
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 38 with HighlighterIterator

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

the class JavaBraceMatcherTest method testBinaryStatement.

public void testBinaryStatement() {
    myFixture.configureByText("a.java", "import java.util.ArrayList;" + "class A {" + "  int i = 3 <caret>< 4 ? 5 > 6 : 1 : 1 : 1;" + "}");
    final Editor editor = myFixture.getEditor();
    final EditorHighlighter editorHighlighter = ((EditorEx) editor).getHighlighter();
    final HighlighterIterator iterator = editorHighlighter.createIterator(editor.getCaretModel().getOffset());
    boolean matched = BraceMatchingUtil.matchBrace(editor.getDocument().getCharsSequence(), myFixture.getFile().getFileType(), iterator, true);
    assertFalse(matched);
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) Editor(com.intellij.openapi.editor.Editor) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 39 with HighlighterIterator

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

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

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