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());
}
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());
}
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);
}
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;
}
}
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();
}
Aggregations