Search in sources :

Example 56 with HighlighterIterator

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

the class CustomFileTypeEditorTest method testCpp.

public void testCpp() throws Exception {
    EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), "A.cpp");
    //                   0123456789012345678 9 0123 45 6 7
    highlighter.setText("#include try enum \"\\xff\\z\\\"xxx\"");
    HighlighterIterator iterator = highlighter.createIterator(2);
    assertEquals(CustomHighlighterTokenType.KEYWORD_1, iterator.getTokenType());
    iterator = highlighter.createIterator(9);
    assertEquals(CustomHighlighterTokenType.KEYWORD_2, iterator.getTokenType());
    iterator = highlighter.createIterator(15);
    assertEquals(CustomHighlighterTokenType.KEYWORD_1, iterator.getTokenType());
    iterator = highlighter.createIterator(19);
    assertEquals(StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN, iterator.getTokenType());
    iterator = highlighter.createIterator(23);
    assertEquals(StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN, iterator.getTokenType());
    iterator = highlighter.createIterator(25);
    assertEquals(StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN, iterator.getTokenType());
    iterator = highlighter.createIterator(27);
    assertEquals(CustomHighlighterTokenType.STRING, iterator.getTokenType());
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 57 with HighlighterIterator

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

the class CustomFileTypeEditorTest method testHaskel.

public void testHaskel() throws Exception {
    EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), "A.hs");
    //                   0123456789012345678 9 0123 45 6 7
    highlighter.setText("{-# #-} module");
    HighlighterIterator iterator = highlighter.createIterator(2);
    assertEquals(CustomHighlighterTokenType.MULTI_LINE_COMMENT, iterator.getTokenType());
    iterator = highlighter.createIterator(12);
    assertEquals(CustomHighlighterTokenType.KEYWORD_1, iterator.getTokenType());
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 58 with HighlighterIterator

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

the class JavaBraceMatcherTest method testBrokenText.

public void testBrokenText() {
    myFixture.configureByText("a.java", "import java.util.ArrayList;" + "class A {" + "  ArrayList<caret><String");
    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 59 with HighlighterIterator

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

the class BaseIndentEnterHandler method preprocessEnter.

@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffset, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
    Result res = shouldSkipWithResult(file, editor, dataContext);
    if (res != null) {
        return res;
    }
    final Document document = editor.getDocument();
    int caret = editor.getCaretModel().getOffset();
    final int lineNumber = document.getLineNumber(caret);
    final int lineStartOffset = document.getLineStartOffset(lineNumber);
    final int previousLineStartOffset = lineNumber > 0 ? document.getLineStartOffset(lineNumber - 1) : lineStartOffset;
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    final HighlighterIterator iterator = highlighter.createIterator(caret - 1);
    final IElementType type = getNonWhitespaceElementType(iterator, lineStartOffset, previousLineStartOffset);
    final CharSequence editorCharSequence = document.getCharsSequence();
    final CharSequence lineIndent = editorCharSequence.subSequence(lineStartOffset, EditorActionUtil.findFirstNonSpaceOffsetOnTheLine(document, lineNumber));
    // Enter in line comment
    if (type == myLineCommentType) {
        final String restString = editorCharSequence.subSequence(caret, document.getLineEndOffset(lineNumber)).toString();
        if (!StringUtil.isEmptyOrSpaces(restString)) {
            final String linePrefix = lineIndent + myLineCommentPrefix;
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + linePrefix);
            editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, linePrefix.length()));
            return Result.Stop;
        } else if (iterator.getStart() < lineStartOffset) {
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
            return Result.Stop;
        }
    }
    if (!myWorksWithFormatter && LanguageFormatting.INSTANCE.forLanguage(myLanguage) != null) {
        return Result.Continue;
    } else {
        if (myIndentTokens.contains(type)) {
            final String newIndent = getNewIndent(file, document, lineIndent);
            EditorModificationUtil.insertStringAtCaret(editor, "\n" + newIndent);
            return Result.Stop;
        }
        EditorModificationUtil.insertStringAtCaret(editor, "\n" + lineIndent);
        editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(lineNumber + 1, calcLogicalLength(editor, lineIndent)));
        return Result.Stop;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) 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 60 with HighlighterIterator

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

the class CodeBlockUtil method calcBlockEndOffset.

private static int calcBlockEndOffset(Editor editor, PsiFile file) {
    Document document = editor.getDocument();
    int offset = editor.getCaretModel().getOffset();
    final FileType fileType = file.getFileType();
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    if (iterator.atEnd())
        return -1;
    int depth = 0;
    Language braceType;
    boolean isBeforeLBrace = false;
    if (isLStructuralBrace(fileType, iterator, document.getCharsSequence())) {
        isBeforeLBrace = true;
        depth = -1;
        braceType = getBraceType(iterator);
    } else {
        braceType = null;
    }
    boolean moved = false;
    while (true) {
        if (iterator.atEnd())
            return -1;
        if (isRStructuralBrace(fileType, iterator, document.getCharsSequence()) && (braceType == getBraceType(iterator) || braceType == null)) {
            if (moved) {
                if (depth == 0)
                    break;
                depth--;
            }
            if (braceType == null) {
                braceType = getBraceType(iterator);
            }
        } else if (isLStructuralBrace(fileType, iterator, document.getCharsSequence()) && (braceType == getBraceType(iterator) || braceType == null)) {
            if (braceType == null) {
                braceType = getBraceType(iterator);
            }
            depth++;
        }
        moved = true;
        iterator.advance();
    }
    return isBeforeLBrace ? iterator.getEnd() : iterator.getStart();
}
Also used : Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) 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