use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class BraceHighlightingHandler method updateBraces.
void updateBraces() {
ApplicationManager.getApplication().assertIsDispatchThread();
if (myPsiFile == null || !myPsiFile.isValid())
return;
clearBraceHighlighters();
if (!myCodeInsightSettings.HIGHLIGHT_BRACES)
return;
if (myEditor.getSelectionModel().hasSelection())
return;
if (myEditor.getSoftWrapModel().isInsideOrBeforeSoftWrap(myEditor.getCaretModel().getVisualPosition()))
return;
int offset = myEditor.getCaretModel().getOffset();
final CharSequence chars = myEditor.getDocument().getCharsSequence();
//if (myEditor.offsetToLogicalPosition(offset).column != myEditor.getCaretModel().getLogicalPosition().column) {
// // we are in virtual space
// final int caretLineNumber = myEditor.getCaretModel().getLogicalPosition().line;
// if (caretLineNumber >= myDocument.getLineCount()) return;
// offset = myDocument.getLineEndOffset(caretLineNumber) + myDocument.getLineSeparatorLength(caretLineNumber);
//}
final int originalOffset = offset;
EditorHighlighter highlighter = getEditorHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset);
FileType fileType = PsiUtilBase.getPsiFileAtOffset(myPsiFile, offset).getFileType();
if (iterator.atEnd()) {
offset--;
} else if (BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
offset--;
} else if (!BraceMatchingUtil.isLBraceToken(iterator, chars, fileType)) {
offset--;
if (offset >= 0) {
HighlighterIterator it = highlighter.createIterator(offset);
if (!BraceMatchingUtil.isRBraceToken(it, chars, getFileTypeByIterator(it)))
offset++;
}
}
if (offset < 0) {
removeLineMarkers();
return;
}
iterator = highlighter.createIterator(offset);
fileType = getFileTypeByIterator(iterator);
myAlarm.cancelAllRequests();
if (BraceMatchingUtil.isLBraceToken(iterator, chars, fileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, fileType)) {
doHighlight(offset, originalOffset, fileType);
} else if (offset > 0 && offset < chars.length()) {
// There is a possible case that there are paired braces nearby the caret position and the document contains only white
// space symbols between them. We want to highlight such braces as well.
// Example:
// public void test() { <caret>
// }
char c = chars.charAt(offset);
boolean searchForward = c != '\n';
// Try to find matched brace backwards.
if (offset >= originalOffset && (c == ' ' || c == '\t' || c == '\n')) {
int backwardNonWsOffset = CharArrayUtil.shiftBackward(chars, offset - 1, "\t ");
if (backwardNonWsOffset >= 0) {
iterator = highlighter.createIterator(backwardNonWsOffset);
FileType newFileType = getFileTypeByIterator(iterator);
if (BraceMatchingUtil.isLBraceToken(iterator, chars, newFileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, newFileType)) {
offset = backwardNonWsOffset;
searchForward = false;
doHighlight(backwardNonWsOffset, originalOffset, newFileType);
}
}
}
// Try to find matched brace forward.
if (searchForward) {
int forwardOffset = CharArrayUtil.shiftForward(chars, offset, "\t ");
if (forwardOffset > offset || c == ' ' || c == '\t') {
iterator = highlighter.createIterator(forwardOffset);
FileType newFileType = getFileTypeByIterator(iterator);
if (BraceMatchingUtil.isLBraceToken(iterator, chars, newFileType) || BraceMatchingUtil.isRBraceToken(iterator, chars, newFileType)) {
offset = forwardOffset;
doHighlight(forwardOffset, originalOffset, newFileType);
}
}
}
}
//highlight scope
if (!myCodeInsightSettings.HIGHLIGHT_SCOPE) {
removeLineMarkers();
return;
}
final int _offset = offset;
final FileType _fileType = fileType;
myAlarm.addRequest(() -> {
if (!myProject.isDisposed() && !myEditor.isDisposed()) {
highlightScope(_offset, _fileType);
}
}, 300);
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class EnterBetweenXmlTagsHandler method isBetweenXmlTags.
private static boolean isBetweenXmlTags(Project project, Editor editor, PsiFile file, 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() != XmlTokenType.XML_TAG_END)
return false;
if (isAtTheEndOfEmptyTag(project, editor, file, iterator)) {
return false;
}
iterator.retreat();
int retrieveCount = 1;
while (!iterator.atEnd()) {
final IElementType tokenType = iterator.getTokenType();
if (tokenType == XmlTokenType.XML_END_TAG_START)
return false;
if (tokenType == XmlTokenType.XML_START_TAG_START)
break;
++retrieveCount;
iterator.retreat();
}
for (int i = 0; i < retrieveCount; ++i) iterator.advance();
iterator.advance();
return !iterator.atEnd() && iterator.getTokenType() == XmlTokenType.XML_END_TAG_START;
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-plugins by JetBrains.
the class HbEnterHandler method isBetweenHbTags.
/**
* Checks to see if {@code Enter} has been typed while the caret is between an open and close tag pair.
*
* @return true if between open and close tags, false otherwise
*/
private static boolean isBetweenHbTags(Editor editor, PsiFile file, 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);
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
final PsiElement openerElement = file.findElementAt(iterator.getStart());
PsiElement openTag = HbPsiUtil.findParentOpenTagElement(openerElement);
if (openTag == null) {
return false;
}
iterator.advance();
if (iterator.atEnd()) {
// no more tokens, so certainly no close tag
return false;
}
final PsiElement closerElement = file.findElementAt(iterator.getStart());
PsiElement closeTag = HbPsiUtil.findParentCloseTagElement(closerElement);
// if we got this far, we're between open and close tags iff this is a close tag
return closeTag != null;
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-plugins by JetBrains.
the class MarkdownCssSettingsForm method setHighlighting.
private static void setHighlighting(EditorEx editor) {
final FileType cssFileType = FileTypeManager.getInstance().getFileTypeByExtension("css");
if (cssFileType == UnknownFileType.INSTANCE) {
return;
}
final EditorHighlighter editorHighlighter = HighlighterFactory.createHighlighter(cssFileType, EditorColorsManager.getInstance().getGlobalScheme(), null);
editor.setHighlighter(editorHighlighter);
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project Perl5-IDEA by Camelcade.
the class PerlTypedHandler method charTyped.
@NotNull
@Override
public Result charTyped(char typedChar, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final int offset = editor.getCaretModel().getOffset() - 1;
if (offset < 0) {
return Result.CONTINUE;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset);
IElementType elementTokenType = iterator.getTokenType();
Document document = editor.getDocument();
if (QUOTE_OPEN_ANY.contains(elementTokenType) && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
IElementType quotePrefixType = offset > 0 ? PerlEditorUtil.getPreviousTokenType(highlighter.createIterator(offset - 1)) : null;
CharSequence text = document.getCharsSequence();
if (offset > text.length() - 1 || text.charAt(offset) != typedChar) {
return Result.CONTINUE;
}
if (elementTokenType == QUOTE_DOUBLE_OPEN || elementTokenType == QUOTE_SINGLE_OPEN) {
AutoPopupController.getInstance(project).scheduleAutoPopup(editor);
}
char openChar = text.charAt(offset);
char closeChar = PerlBaseLexer.getQuoteCloseChar(openChar);
iterator.advance();
IElementType possibleCloseQuoteType = iterator.atEnd() ? null : iterator.getTokenType();
if (QUOTE_CLOSE_FIRST_ANY.contains(possibleCloseQuoteType) && closeChar == text.charAt(iterator.getStart())) {
if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType) && StringUtil.containsChar(HANDLED_BY_BRACE_MATCHER, openChar)) {
iterator.advance();
if (iterator.atEnd() || !QUOTE_OPEN_ANY.contains(iterator.getTokenType())) {
EditorModificationUtil.insertStringAtCaret(editor, Character.toString(closeChar) + openChar, false, false);
}
}
return Result.CONTINUE;
}
StringBuilder textToAppend = new StringBuilder();
textToAppend.append(closeChar);
if (DOUBLE_QUOTE_OPENERS.contains(quotePrefixType)) {
textToAppend.append(openChar);
if (openChar != closeChar) {
textToAppend.append(closeChar);
}
}
EditorModificationUtil.insertStringAtCaret(editor, textToAppend.toString(), false, false);
} else if (elementTokenType == LEFT_BRACE) {
AutoPopupController.getInstance(project).scheduleAutoPopup(editor, CompletionType.BASIC, psiFile -> {
PsiElement newElement = psiFile.findElementAt(offset);
return PsiUtilCore.getElementType(newElement) == elementTokenType && newElement.getParent() instanceof PsiPerlHashIndex;
});
}
return Result.CONTINUE;
}
Aggregations