use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project Perl5-IDEA by Camelcade.
the class PerlBackspaceHandler method beforeCharDeleted.
@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
CaretModel caretModel = editor.getCaretModel();
int currentOffset = caretModel.getOffset() - 1;
if (currentOffset < 0) {
return;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(currentOffset);
IElementType tokenToDelete = iterator.atEnd() ? null : iterator.getTokenType();
if (QUOTE_OPEN_ANY.contains(tokenToDelete)) {
PerlEditorUtil.moveToNextMeaningfulToken(iterator);
if (iterator.atEnd()) {
return;
}
IElementType nextTokenType = iterator.getTokenType();
if (QUOTE_CLOSE_PAIRED.contains(nextTokenType)) {
int startOffsetToDelete = currentOffset + 1;
if (currentOffset > 0 && QUOTE_MIDDLE.contains(tokenToDelete)) {
HighlighterIterator preQuoteIterator = PerlEditorUtil.moveToPreviousMeaningfulToken(highlighter.createIterator(currentOffset - 1));
if (!preQuoteIterator.atEnd() && QUOTE_OPEN_ANY.contains(preQuoteIterator.getTokenType())) {
startOffsetToDelete = preQuoteIterator.getEnd();
caretModel.moveToOffset(preQuoteIterator.getEnd());
}
}
editor.getDocument().deleteString(startOffsetToDelete, iterator.getEnd());
}
}
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-code-outline by sitano.
the class CodeOutlineImageEx method renderToImg.
/**
* Renders the given characters to the code outline image starting at the
* given position.
*
* @param chars a character array
* @param offset the offset into the given array to start rendering
* @param len the number of characters to render
* @param pos the position at which to start rendering
*/
@Override
protected void renderToImg(CharSequence chars, int offset, int len, LogicalPosition pos) {
if (img == null)
return;
final EditorEx ex = (EditorEx) editor;
final EditorHighlighter hl = ex.getHighlighter();
final HighlighterIterator hi = hl.createIterator(offset);
int line = pos.line, col = pos.column;
// Render characters
for (int i = offset; i < len; i++) {
final char ch = chars.charAt(i);
while (!hi.atEnd() && i > hi.getEnd()) {
hi.advance();
}
if (ch == '\n') {
line++;
if (getScaledLine(line, scale) >= visibleImgHeight)
break;
col = 0;
} else {
if (col >= visibleImgWidth)
continue;
// Whitespaces are skipped inside drawChar
drawChar(ch, col, line, getCharColor(editor, hi));
col++;
}
}
}
use of com.intellij.openapi.editor.highlighter.HighlighterIterator in project intellij-elixir by KronicDeth.
the class TypedHandler method charTyped.
/*
* Instance Methods
*/
/**
* Called after the specified character typed by the user has been inserted in the editor.
*
* @param charTyped the character that was typed
* @param project the project in which the {@code file} exists
* @param editor the editor that has the {@code file} open
* @param file the file into which the {@code charTyped} was typed
*/
@Override
public Result charTyped(char charTyped, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
Result result = Result.CONTINUE;
if (file instanceof ElixirFile) {
if (charTyped == ' ') {
int caret = editor.getCaretModel().getOffset();
if (caret > 2) {
// "(do|fn)<space><caret>"
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(caret - 2);
IElementType tokenType = iterator.getTokenType();
if (tokenType == ElixirTypes.DO || tokenType == ElixirTypes.FN) {
editor.getDocument().insertString(caret, " end");
result = Result.STOP;
}
}
} else if (charTyped == '<') {
int caret = editor.getCaretModel().getOffset();
if (caret > 2) {
// "~<sigil_name><"
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(caret - 1);
IElementType tokenType = iterator.getTokenType();
if (tokenType == LINE_PROMOTER) {
editor.getDocument().insertString(caret, ">");
result = Result.STOP;
}
}
if (result == Result.CONTINUE && caret > 1) {
// "<<"
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(caret - 2);
if (iterator.getTokenType() == ElixirTypes.OPENING_BIT) {
editor.getDocument().insertString(caret, ">>");
result = Result.STOP;
}
}
} else if (charTyped == '/' || charTyped == '|') {
int caret = editor.getCaretModel().getOffset();
if (caret > 2) {
// "~<sigil_name>(/|\|)"
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(caret - 1);
IElementType tokenType = iterator.getTokenType();
if (tokenType == LINE_PROMOTER) {
editor.getDocument().insertString(caret, String.valueOf(charTyped));
result = Result.STOP;
}
}
} else if (charTyped == '{') {
// #{
int caret = editor.getCaretModel().getOffset();
if (caret > 1) {
final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(caret - 1);
IElementType tokenType = iterator.getTokenType();
if (tokenType == ElixirTypes.INTERPOLATION_START) {
editor.getDocument().insertString(caret, "}");
result = Result.STOP;
}
}
}
}
return result;
}
Aggregations