com.intellij.openapi.fileTypes.SyntaxHighlighterFactory Java Examples

The following examples show how to use com.intellij.openapi.fileTypes.SyntaxHighlighterFactory. 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 File: SoyLayeredHighlighter.java    From bamboo-soy with 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 File: DiffUtil.java    From consulo with 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 File: BaseColorSettings.java    From Custom-Syntax-Highlighter with 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 File: ChunkExtractor.java    From consulo with 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 File: ConsoleViewUtil.java    From consulo with 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 File: BraceHighlightingHandler.java    From consulo with 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 File: EditorWindowImpl.java    From consulo with 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 File: BashLanguage.java    From BashSupport with 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 File: BashLanguageInjectionTest.java    From BashSupport with 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 File: FileTypeBasedContextType.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public SyntaxHighlighter createHighlighter() {
  return SyntaxHighlighterFactory.getSyntaxHighlighter(myFileType, null, null);
}