use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-plugins by JetBrains.
the class CfmlTypedHandlerTest method testEditing.
public void testEditing() throws Throwable {
@NonNls final String s1 = "<table bgcolor=\"#FFFFFF\"><cfoutput>\n" + " <div id=\"#bColumn2";
String s2 = "\" />\n" + "</cfoutput></table>";
String s = s1 + s2;
final Document doc = new DocumentImpl(s);
EditorEx editor = (EditorEx) EditorFactory.getInstance().createEditor(doc);
try {
EditorHighlighter highlighter = HighlighterFactory.createHighlighter(getProject(), CfmlFileType.INSTANCE);
editor.setHighlighter(highlighter);
CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> doc.insertString(s1.length(), "#")), "", null);
List tokensAfterUpdate = getAllTokens(highlighter);
highlighter = HighlighterFactory.createHighlighter(getProject(), CfmlFileType.INSTANCE);
editor.setHighlighter(highlighter);
List tokensWithoutUpdate = getAllTokens(highlighter);
TestCase.assertEquals(tokensWithoutUpdate, tokensAfterUpdate);
} finally {
EditorFactory.getInstance().releaseEditor(editor);
}
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class BraceMatchingUtil method getMatchedBraceOffset.
@TestOnly
public static int getMatchedBraceOffset(@NotNull Editor editor, boolean forward, @NotNull PsiFile file) {
Document document = editor.getDocument();
int offset = editor.getCaretModel().getOffset();
EditorHighlighter editorHighlighter = BraceHighlightingHandler.getLazyParsableHighlighterIfAny(file.getProject(), editor, file);
HighlighterIterator iterator = editorHighlighter.createIterator(offset);
boolean matched = matchBrace(document.getCharsSequence(), file.getFileType(), iterator, forward);
assert matched;
return iterator.getStart();
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class ImplementationViewComponent method update.
public void update(@NotNull final PsiElement[] elements, final int index) {
update(elements, (psiElements, fileDescriptors) -> {
if (myEditor.isDisposed())
return false;
if (psiElements.length == 0)
return false;
final Project project = psiElements[0].getProject();
myElements = psiElements;
myIndex = index < myElements.length ? index : 0;
PsiFile psiFile = getContainingFile(myElements[myIndex]);
VirtualFile virtualFile = psiFile.getVirtualFile();
EditorHighlighter highlighter;
if (virtualFile != null)
highlighter = HighlighterFactory.createHighlighter(project, virtualFile);
else {
// some artificial psi file, lets do best we can
String fileName = psiFile.getName();
highlighter = HighlighterFactory.createHighlighter(project, fileName);
}
((EditorEx) myEditor).setHighlighter(highlighter);
if (myElements.length > 1) {
myFileChooser.setVisible(true);
myCountLabel.setVisible(true);
myLabel.setVisible(false);
myFileChooser.setModel(new DefaultComboBoxModel(fileDescriptors.toArray(new FileDescriptor[fileDescriptors.size()])));
updateRenderer(project);
} else {
myFileChooser.setVisible(false);
myCountLabel.setVisible(false);
VirtualFile file = psiFile.getVirtualFile();
if (file != null) {
myLabel.setIcon(getIconForFile(psiFile));
myLabel.setForeground(FileStatusManager.getInstance(project).getStatus(file).getColor());
myLabel.setText(file.getPresentableName());
myLabel.setBorder(new CompoundBorder(IdeBorderFactory.createRoundedBorder(), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5)));
myLabel.setVisible(true);
}
}
updateControls();
revalidate();
repaint();
return true;
});
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class TodoItemNode method update.
@Override
public void update(PresentationData presentation) {
TodoItem todoItem = getValue().getTodoItem();
RangeMarker myRangeMarker = getValue().getRangeMarker();
if (!todoItem.getFile().isValid() || !myRangeMarker.isValid() || myRangeMarker.getStartOffset() == myRangeMarker.getEndOffset()) {
myRangeMarker.dispose();
setValue(null);
return;
}
myHighlightedRegions.clear();
// Update name
Document document = getValue().getDocument();
CharSequence chars = document.getCharsSequence();
int startOffset = myRangeMarker.getStartOffset();
int endOffset = myRangeMarker.getEndOffset();
LOG.assertTrue(startOffset > -1);
LOG.assertTrue(startOffset <= document.getTextLength());
LOG.assertTrue(endOffset > -1);
LOG.assertTrue(endOffset < document.getTextLength() + 1);
int lineNumber = document.getLineNumber(startOffset);
LOG.assertTrue(lineNumber > -1);
LOG.assertTrue(lineNumber < document.getLineCount());
int lineStartOffset = document.getLineStartOffset(lineNumber);
LOG.assertTrue(lineStartOffset > -1);
LOG.assertTrue(lineStartOffset <= startOffset);
LOG.assertTrue(lineStartOffset <= document.getTextLength());
int columnNumber = startOffset - lineStartOffset;
LOG.assertTrue(columnNumber > -1);
while (lineStartOffset < document.getTextLength() && (chars.charAt(lineStartOffset) == '\t' || chars.charAt(lineStartOffset) == ' ')) {
lineStartOffset++;
}
int lineEndOffset = document.getLineEndOffset(lineNumber);
LOG.assertTrue(lineEndOffset >= 0);
LOG.assertTrue(lineEndOffset <= document.getTextLength());
String lineColumnPrefix = "(" + (lineNumber + 1) + ", " + (columnNumber + 1) + ") ";
String highlightedText = chars.subSequence(lineStartOffset, Math.min(lineEndOffset, chars.length())).toString();
String newName = lineColumnPrefix + highlightedText;
// Update icon
Icon newIcon = todoItem.getPattern().getAttributes().getIcon();
// Update highlighted regions
myHighlightedRegions.clear();
EditorHighlighter highlighter = myBuilder.getHighlighter(todoItem.getFile(), document);
HighlighterIterator iterator = highlighter.createIterator(lineStartOffset);
while (!iterator.atEnd()) {
int start = Math.max(iterator.getStart(), lineStartOffset);
int end = Math.min(iterator.getEnd(), lineEndOffset);
if (lineEndOffset < start || lineEndOffset < end) {
break;
}
TextAttributes attributes = iterator.getTextAttributes();
int fontType = attributes.getFontType();
if ((fontType & Font.BOLD) != 0) {
// suppress bold attribute
attributes = attributes.clone();
attributes.setFontType(fontType & ~Font.BOLD);
}
HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + start - lineStartOffset, lineColumnPrefix.length() + end - lineStartOffset, attributes);
myHighlightedRegions.add(region);
iterator.advance();
}
TextAttributes attributes = todoItem.getPattern().getAttributes().getTextAttributes();
HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + startOffset - lineStartOffset, lineColumnPrefix.length() + endOffset - lineStartOffset, attributes);
myHighlightedRegions.add(region);
//
presentation.setPresentableText(newName);
presentation.setIcon(newIcon);
}
use of com.intellij.openapi.editor.highlighter.EditorHighlighter in project intellij-community by JetBrains.
the class EnterAfterUnmatchedBraceHandler method getUnmatchedLBracesNumberBefore.
/**
* Calculates number of unmatched left braces before the given offset.
*
* @param editor target editor
* @param offset target offset
* @param fileType target file type
* @return number of unmatched braces before the given offset;
* negative value if it's not possible to perform the calculation or if there are no unmatched left braces before
* the given offset
*/
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType) {
if (offset == 0) {
return -1;
}
CharSequence chars = editor.getDocument().getCharsSequence();
if (chars.charAt(offset - 1) != '{') {
return -1;
}
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
if (!braceMatcher.isLBraceToken(iterator, chars, fileType) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
return -1;
}
Language language = iterator.getTokenType().getLanguage();
iterator = highlighter.createIterator(0);
int lBracesBeforeOffset = 0;
int lBracesAfterOffset = 0;
int rBracesBeforeOffset = 0;
int rBracesAfterOffset = 0;
for (; !iterator.atEnd(); iterator.advance()) {
IElementType tokenType = iterator.getTokenType();
if (!tokenType.getLanguage().equals(language) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
continue;
}
boolean beforeOffset = iterator.getStart() < offset;
if (braceMatcher.isLBraceToken(iterator, chars, fileType)) {
if (beforeOffset) {
lBracesBeforeOffset++;
} else {
lBracesAfterOffset++;
}
} else if (braceMatcher.isRBraceToken(iterator, chars, fileType)) {
if (beforeOffset) {
rBracesBeforeOffset++;
} else {
rBracesAfterOffset++;
}
}
}
return lBracesBeforeOffset - rBracesBeforeOffset - (rBracesAfterOffset - lBracesAfterOffset);
}
Aggregations