use of com.intellij.openapi.fileTypes.SyntaxHighlighter in project intellij-community by JetBrains.
the class DiffUtil method createEditorHighlighter.
@Nullable
private static EditorHighlighter createEditorHighlighter(@Nullable Project project, @NotNull DocumentContent content) {
FileType type = content.getContentType();
VirtualFile file = content.getHighlightFile();
Language language = content.getUserData(DiffUserDataKeys.LANGUAGE);
EditorHighlighterFactory highlighterFactory = EditorHighlighterFactory.getInstance();
if (language != null) {
SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, file);
return highlighterFactory.createEditorHighlighter(syntaxHighlighter, EditorColorsManager.getInstance().getGlobalScheme());
}
if (file != null && file.isValid()) {
if ((type == null || type == PlainTextFileType.INSTANCE) || file.getFileType() == type || file instanceof LightVirtualFile) {
return highlighterFactory.createEditorHighlighter(project, file);
}
}
if (type != null) {
return highlighterFactory.createEditorHighlighter(project, type);
}
return null;
}
use of com.intellij.openapi.fileTypes.SyntaxHighlighter in project intellij-community by JetBrains.
the class IndexPatternSearcher method findCommentTokenRanges.
private static void findCommentTokenRanges(final PsiFile file, final CharSequence chars, final TextRange range, final TIntArrayList commentStarts, final TIntArrayList commentEnds) {
if (file instanceof PsiPlainTextFile) {
FileType fType = file.getFileType();
if (fType instanceof CustomSyntaxTableFileType) {
Lexer lexer = SyntaxHighlighterFactory.getSyntaxHighlighter(fType, file.getProject(), file.getVirtualFile()).getHighlightingLexer();
findComments(lexer, chars, range, COMMENT_TOKENS, commentStarts, commentEnds, null);
} else {
commentStarts.add(0);
commentEnds.add(file.getTextLength());
}
} else {
final FileViewProvider viewProvider = file.getViewProvider();
final Set<Language> relevantLanguages = viewProvider.getLanguages();
for (Language lang : relevantLanguages) {
final TIntArrayList commentStartsList = new TIntArrayList();
final TIntArrayList commentEndsList = new TIntArrayList();
final SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(lang, file.getProject(), file.getVirtualFile());
Lexer lexer = syntaxHighlighter.getHighlightingLexer();
TokenSet commentTokens = null;
IndexPatternBuilder builderForFile = null;
for (IndexPatternBuilder builder : Extensions.getExtensions(IndexPatternBuilder.EP_NAME)) {
Lexer lexerFromBuilder = builder.getIndexingLexer(file);
if (lexerFromBuilder != null) {
lexer = lexerFromBuilder;
commentTokens = builder.getCommentTokenSet(file);
builderForFile = builder;
}
}
if (builderForFile == null) {
final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
if (parserDefinition != null) {
commentTokens = parserDefinition.getCommentTokens();
}
}
if (commentTokens != null) {
findComments(lexer, chars, range, commentTokens, commentStartsList, commentEndsList, builderForFile);
mergeCommentLists(commentStarts, commentEnds, commentStartsList, commentEndsList);
}
}
}
}
use of com.intellij.openapi.fileTypes.SyntaxHighlighter in project intellij-community by JetBrains.
the class InjectedGeneralHighlightingPass method highlightInjectedSyntax.
private void highlightInjectedSyntax(@NotNull PsiFile injectedPsi, @NotNull HighlightInfoHolder holder) {
List<Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange>> tokens = InjectedLanguageUtil.getHighlightTokens(injectedPsi);
if (tokens == null)
return;
final Language injectedLanguage = injectedPsi.getLanguage();
Project project = injectedPsi.getProject();
SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(injectedLanguage, project, injectedPsi.getVirtualFile());
final TextAttributes defaultAttrs = myGlobalScheme.getAttributes(HighlighterColors.TEXT);
for (Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange> token : tokens) {
ProgressManager.checkCanceled();
IElementType tokenType = token.getFirst();
PsiLanguageInjectionHost injectionHost = token.getSecond().getElement();
if (injectionHost == null)
continue;
TextRange textRange = token.getThird();
TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(tokenType);
if (textRange.getLength() == 0)
continue;
TextRange annRange = textRange.shiftRight(injectionHost.getTextRange().getStartOffset());
// force attribute colors to override host' ones
TextAttributes attributes = null;
for (TextAttributesKey key : keys) {
TextAttributes attrs2 = myGlobalScheme.getAttributes(key);
if (attrs2 != null) {
attributes = attributes == null ? attrs2 : TextAttributes.merge(attributes, attrs2);
}
}
TextAttributes forcedAttributes;
if (attributes == null || attributes.isEmpty() || attributes.equals(defaultAttrs)) {
forcedAttributes = TextAttributes.ERASE_MARKER;
} else {
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT).range(annRange).textAttributes(TextAttributes.ERASE_MARKER).createUnconditionally();
holder.add(info);
forcedAttributes = new TextAttributes(attributes.getForegroundColor(), attributes.getBackgroundColor(), attributes.getEffectColor(), attributes.getEffectType(), attributes.getFontType());
}
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT).range(annRange).textAttributes(forcedAttributes).createUnconditionally();
holder.add(info);
}
}
use of com.intellij.openapi.fileTypes.SyntaxHighlighter in project intellij-community by JetBrains.
the class BraceHighlightingHandler method getLazyParsableHighlighterIfAny.
@NotNull
static EditorHighlighter getLazyParsableHighlighterIfAny(Project project, Editor editor, PsiFile psiFile) {
if (!PsiDocumentManager.getInstance(project).isCommitted(editor.getDocument())) {
return ((EditorEx) editor).getHighlighter();
}
PsiElement elementAt = psiFile.findElementAt(editor.getCaretModel().getOffset());
for (PsiElement e : SyntaxTraverser.psiApi().parents(elementAt).takeWhile(Conditions.notEqualTo(psiFile))) {
if (!(PsiUtilCore.getElementType(e) instanceof ILazyParseableElementType))
continue;
Language language = ILazyParseableElementType.LANGUAGE_KEY.get(e.getNode());
if (language == null)
continue;
TextRange range = e.getTextRange();
final int offset = range.getStartOffset();
SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, psiFile.getVirtualFile());
LexerEditorHighlighter highlighter = new LexerEditorHighlighter(syntaxHighlighter, editor.getColorsScheme()) {
@NotNull
@Override
public HighlighterIterator createIterator(int startOffset) {
return new HighlighterIteratorWrapper(super.createIterator(Math.max(startOffset - offset, 0))) {
@Override
public int getStart() {
return super.getStart() + offset;
}
@Override
public int getEnd() {
return super.getEnd() + offset;
}
};
}
};
highlighter.setText(editor.getDocument().getText(range));
return highlighter;
}
return ((EditorEx) editor).getHighlighter();
}
use of com.intellij.openapi.fileTypes.SyntaxHighlighter in project intellij-community by JetBrains.
the class SimpleEditorPreview method updateView.
@Override
public void updateView() {
EditorColorsScheme scheme = myOptions.getSelectedScheme();
myEditor.setColorsScheme(scheme);
EditorHighlighter highlighter = null;
if (myPage instanceof EditorHighlightingProvidingColorSettingsPage) {
highlighter = ((EditorHighlightingProvidingColorSettingsPage) myPage).createEditorHighlighter(scheme);
}
if (highlighter == null) {
final SyntaxHighlighter pageHighlighter = myPage.getHighlighter();
highlighter = HighlighterFactory.createHighlighter(pageHighlighter, scheme);
}
myEditor.setHighlighter(highlighter);
updateHighlighters();
myEditor.reinitSettings();
}
Aggregations