Search in sources :

Example 6 with SyntaxHighlighter

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Language(com.intellij.lang.Language) PlainTextFileType(com.intellij.openapi.fileTypes.PlainTextFileType) FileType(com.intellij.openapi.fileTypes.FileType) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) EditorHighlighterFactory(com.intellij.openapi.editor.highlighter.EditorHighlighterFactory)

Example 7 with SyntaxHighlighter

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);
            }
        }
    }
}
Also used : Lexer(com.intellij.lexer.Lexer) ParserDefinition(com.intellij.lang.ParserDefinition) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) TokenSet(com.intellij.psi.tree.TokenSet) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) TIntArrayList(gnu.trove.TIntArrayList) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)

Example 8 with SyntaxHighlighter

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);
    }
}
Also used : SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) Language(com.intellij.lang.Language) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 9 with SyntaxHighlighter

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();
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIteratorWrapper(com.intellij.openapi.editor.ex.util.HighlighterIteratorWrapper) Language(com.intellij.lang.Language) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) TextRange(com.intellij.openapi.util.TextRange) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LightweightHint(com.intellij.ui.LightweightHint) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with SyntaxHighlighter

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();
}
Also used : EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) EditorHighlightingProvidingColorSettingsPage(com.intellij.openapi.options.colors.EditorHighlightingProvidingColorSettingsPage) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Aggregations

SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)16 Language (com.intellij.lang.Language)7 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)5 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)5 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)4 NotNull (org.jetbrains.annotations.NotNull)4 EditorWindow (com.intellij.injected.editor.EditorWindow)3 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)3 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)3 TextRange (com.intellij.openapi.util.TextRange)3 IElementType (com.intellij.psi.tree.IElementType)3 Lexer (com.intellij.lexer.Lexer)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 FileType (com.intellij.openapi.fileTypes.FileType)2 PlainSyntaxHighlighter (com.intellij.openapi.fileTypes.PlainSyntaxHighlighter)2 Project (com.intellij.openapi.project.Project)2 HighlightData (com.intellij.application.options.colors.highlighting.HighlightData)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 HintManager (com.intellij.codeInsight.hint.HintManager)1 TemplateContextType (com.intellij.codeInsight.template.TemplateContextType)1