Java Code Examples for com.intellij.openapi.fileTypes.SyntaxHighlighterFactory

The following examples show how to use com.intellij.openapi.fileTypes.SyntaxHighlighterFactory. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: bamboo-soy   Source File: SoyLayeredHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
public SoyLayeredHighlighter(
    @Nullable Project project,
    @Nullable VirtualFile virtualFile,
    @NotNull EditorColorsScheme colors) {
  // Creating main highlighter.
  super(new SoySyntaxHighlighter(), colors);

  // Highlighter for the outer language.
  FileType type = null;
  if (project == null || virtualFile == null) {
    type = StdFileTypes.PLAIN_TEXT;
  } else {
    Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(virtualFile);
    if (language != null) type = language.getAssociatedFileType();
    if (type == null) type = SoyLanguage.getDefaultTemplateLang();
  }

  SyntaxHighlighter outerHighlighter =
      SyntaxHighlighterFactory.getSyntaxHighlighter(type, project, virtualFile);

  registerLayer(OTHER, new LayerDescriptor(outerHighlighter, ""));
}
 
Example 2
Source Project: consulo   Source File: DiffUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
private static EditorHighlighter createEditorHighlighter(@Nullable Project project, @Nonnull 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) {
    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;
}
 
Example 3
Source Project: Custom-Syntax-Highlighter   Source File: BaseColorSettings.java    License: MIT License 5 votes vote down vote up
static SyntaxHighlighter getSyntaxHighlighterWithFallback(final Language lang) {
  final SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(lang, null, null);
  if (syntaxHighlighter == null) {
    return SyntaxHighlighterFactory.getSyntaxHighlighter(Language.ANY, null, null);
  }
  return syntaxHighlighter;
}
 
Example 4
Source Project: consulo   Source File: ChunkExtractor.java    License: Apache License 2.0 5 votes vote down vote up
private ChunkExtractor(@Nonnull PsiFile file) {
  myColorsScheme = UsageTreeColorsScheme.getInstance().getScheme();

  Project project = file.getProject();
  myDocument = PsiDocumentManager.getInstance(project).getDocument(file);
  LOG.assertTrue(myDocument != null);
  final FileType fileType = file.getFileType();
  SyntaxHighlighter highlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(fileType, project, file.getVirtualFile());
  highlighter = highlighter == null ? new PlainSyntaxHighlighter() : highlighter;
  myHighlighter = new SyntaxHighlighterOverEditorHighlighter(highlighter, file.getVirtualFile(), project);
  myDocumentStamp = -1;
}
 
Example 5
Source Project: consulo   Source File: ConsoleViewUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void printAsFileType(@Nonnull ConsoleView console, @Nonnull String text, @Nonnull FileType fileType) {
  SyntaxHighlighter highlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(fileType, null, null);
  if (highlighter != null) {
    printWithHighlighting(console, text, highlighter);
  }
  else {
    console.print(text, ConsoleViewContentType.NORMAL_OUTPUT);
  }
}
 
Example 6
Source Project: consulo   Source File: BraceHighlightingHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
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()) {
      @Nonnull
      @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();
}
 
Example 7
Source Project: consulo   Source File: EditorWindowImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public EditorHighlighter getHighlighter() {
  EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(myInjectedFile.getLanguage(), getProject(), myInjectedFile.getVirtualFile());
  EditorHighlighter highlighter = HighlighterFactory.createHighlighter(syntaxHighlighter, scheme);
  highlighter.setText(getDocument().getText());
  highlighter.setEditor(new LightHighlighterClient(getDocument(), getProject()));
  return highlighter;
}
 
Example 8
Source Project: BashSupport   Source File: BashLanguage.java    License: Apache License 2.0 4 votes vote down vote up
public BashLanguage() {
    super("Bash", "application/x-bsh", "application/x-sh", "text/x-script.sh");

    SyntaxHighlighterFactory.LANGUAGE_FACTORY.addExplicitExtension(this, new BashHighlighterFactory());
}
 
Example 9
Source Project: BashSupport   Source File: BashLanguageInjectionTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testSyntaxHighlighterFactory() throws Exception {
    PsiFile file = myFixture.configureByText(BashFileType.BASH_FILE_TYPE, "echo");
    SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(BashFileType.BASH_FILE_TYPE, getProject(), file.getVirtualFile());

    Assert.assertNotNull(syntaxHighlighter);
}
 
Example 10
Source Project: consulo   Source File: FileTypeBasedContextType.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SyntaxHighlighter createHighlighter() {
  return SyntaxHighlighterFactory.getSyntaxHighlighter(myFileType, null, null);
}