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

The following examples show how to use com.intellij.openapi.fileTypes.SyntaxHighlighter. 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: 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 2
public SyntaxHighlighterOverEditorHighlighter(SyntaxHighlighter _highlighter, VirtualFile file, Project project) {
  if (file.getFileType() == PlainTextFileType.INSTANCE) { // optimization for large files, PlainTextSyntaxHighlighterFactory is slow
    highlighter = new PlainSyntaxHighlighter();
    lexer = highlighter.getHighlightingLexer();
  } else {
    highlighter = _highlighter;
    LayeredLexer.ourDisableLayersFlag.set(Boolean.TRUE);
    EditorHighlighter editorHighlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(project, file);

    try {
      if (editorHighlighter instanceof LayeredLexerEditorHighlighter) {
        lexer = new LexerEditorHighlighterLexer(editorHighlighter, false);
      }
      else {
        lexer = highlighter.getHighlightingLexer();
      }
    }
    finally {
      LayeredLexer.ourDisableLayersFlag.set(null);
    }
  }
}
 
Example 3
Source Project: consulo   Source File: ConsoleViewUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void printWithHighlighting(@Nonnull ConsoleView console, @Nonnull String text, @Nonnull SyntaxHighlighter highlighter, Runnable doOnNewLine) {
  Lexer lexer = highlighter.getHighlightingLexer();
  lexer.start(text, 0, text.length(), 0);

  IElementType tokenType;
  while ((tokenType = lexer.getTokenType()) != null) {
    ConsoleViewContentType contentType = getContentTypeForToken(tokenType, highlighter);
    StringTokenizer eolTokenizer = new StringTokenizer(lexer.getTokenText(), "\n", true);
    while (eolTokenizer.hasMoreTokens()) {
      String tok = eolTokenizer.nextToken();
      console.print(tok, contentType);
      if (doOnNewLine != null && "\n".equals(tok)) {
        doOnNewLine.run();
      }
    }

    lexer.advance();
  }
}
 
Example 4
Source Project: intellij-latte   Source File: LatteEditorHighlighter.java    License: MIT License 6 votes vote down vote up
public LatteEditorHighlighter(@Nullable Project project, @Nullable VirtualFile virtualFile, @NotNull EditorColorsScheme colors) {
	super(new LatteSyntaxHighlighter() {
		@Override
		public @NotNull Lexer getHighlightingLexer() {
			return new LatteHtmlHighlightingLexer(super.getHighlightingLexer());
		}
	}, colors);

	final SyntaxHighlighter highlighter = getHighlighter(project, virtualFile);
	this.registerLayer(LatteTypes.T_TEXT, new LayerDescriptor(new SyntaxHighlighter() {
		@NotNull
		public Lexer getHighlightingLexer() {
			return highlighter.getHighlightingLexer();
		}

		@NotNull
		public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
			return highlighter.getTokenHighlights(tokenType);
		}
	}, ""));
}
 
Example 5
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 6
Source Project: consulo   Source File: ClickNavigator.java    License: Apache License 2.0 5 votes vote down vote up
public static String highlightingTypeFromTokenType(IElementType tokenType, SyntaxHighlighter highlighter) {
  TextAttributesKey[] highlights = highlighter.getTokenHighlights(tokenType);
  String s = null;
  for (int i = highlights.length - 1; i >= 0; i--) {
    if (highlights[i] != HighlighterColors.TEXT) {
      s = highlights[i].getExternalName();
      break;
    }
  }
  return s == null ? HighlighterColors.TEXT.getExternalName() : s;
}
 
Example 7
Source Project: consulo   Source File: ClickNavigator.java    License: Apache License 2.0 5 votes vote down vote up
public void addClickNavigator(final Editor view,
                              final SyntaxHighlighter highlighter,
                              final HighlightData[] data,
                              final boolean isBackgroundImportant) {
  addMouseMotionListener(view, highlighter, data, isBackgroundImportant);

  CaretListener listener = new CaretAdapter() {
    @Override
    public void caretPositionChanged(CaretEvent e) {
      navigate(view, true, e.getNewPosition(), highlighter, data, isBackgroundImportant);
    }
  };
  view.getCaretModel().addCaretListener(listener);
}
 
Example 8
Source Project: consulo   Source File: CustomFileTypeHighlighterProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public SyntaxHighlighter create(final FileType fileType, @Nullable final Project project, @Nullable final VirtualFile file) {
  if (fileType instanceof AbstractFileType) {
    return new CustomFileHighlighter(((CustomSyntaxTableFileType) fileType).getSyntaxTable());
  }
  return null;
}
 
Example 9
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 10
Source Project: consulo   Source File: ClickNavigator.java    License: Apache License 2.0 5 votes vote down vote up
private void addMouseMotionListener(final Editor view,
                                    final SyntaxHighlighter highlighter,
                                    final HighlightData[] data, final boolean isBackgroundImportant) {
  view.getContentComponent().addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseMoved(MouseEvent e) {
      LogicalPosition pos = view.xyToLogicalPosition(new Point(e.getX(), e.getY()));
      navigate(view, false, pos, highlighter, data, isBackgroundImportant);
    }
  });
}
 
Example 11
Source Project: consulo   Source File: SimpleEditorPreview.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private static String selectItem(HighlighterIterator itr, SyntaxHighlighter highlighter) {
  IElementType tokenType = itr.getTokenType();
  if (tokenType == null) return null;

  TextAttributesKey[] highlights = highlighter.getTokenHighlights(tokenType);
  String s = null;
  for (int i = highlights.length - 1; i >= 0; i--) {
    if (highlights[i] != HighlighterColors.TEXT) {
      s = highlights[i].getExternalName();
      break;
    }
  }
  return s == null ? HighlighterColors.TEXT.getExternalName() : s;
}
 
Example 12
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 13
Source Project: consulo   Source File: LayeredLexerEditorHighlighter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public SyntaxHighlighter getActiveSyntaxHighlighter() {
  if (myCurrentMapper != null) {
    return myCurrentMapper.mySyntaxHighlighter;
  }

  return getSyntaxHighlighter();
}
 
Example 14
Source Project: consulo   Source File: LexerEditorHighlighter.java    License: Apache License 2.0 5 votes vote down vote up
public LexerEditorHighlighter(@Nonnull SyntaxHighlighter highlighter, @Nonnull EditorColorsScheme scheme) {
  myScheme = scheme;
  myLexer = highlighter.getHighlightingLexer();
  myLexer.start(ArrayUtil.EMPTY_CHAR_SEQUENCE);
  myInitialState = myLexer.getState();
  myHighlighter = highlighter;
  mySegments = createSegments();
}
 
Example 15
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 16
@NotNull
@Override
public SyntaxHighlighter getSyntaxHighlighter(Project project, VirtualFile virtualFile) {
	MapKey key = new MapKey(project, virtualFile != null ? virtualFile.getPath() + "/" + virtualFile.getName() : "");
	DslSyntaxHighlighter highlighter = highlighters.get(key);
	if (highlighter == null || highlighter.virtualFile == null || !highlighter.virtualFile.isValid()
			|| virtualFile != null && !virtualFile.equals(highlighter.virtualFile)
			|| highlighter.isDisposed()) {
		if (highlighter != null) highlighter.stop();
		highlighter = new DslSyntaxHighlighter(project, virtualFile);
		highlighters.put(key, highlighter);
	}
	return highlighter;
}
 
Example 17
Source Project: consulo   Source File: ClickNavigator.java    License: Apache License 2.0 5 votes vote down vote up
private boolean selectItem(boolean select, HighlighterIterator itr, SyntaxHighlighter highlighter) {

    IElementType tokenType = itr.getTokenType();
    if (tokenType == null) return false;
    String type = highlightingTypeFromTokenType(tokenType, highlighter);
    return setSelectedItem(type, select);
  }
 
Example 18
@Nonnull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
  final SyntaxHighlighter activeSyntaxHighlighter =
          layeredHighlighterIterator != null ? layeredHighlighterIterator.getActiveSyntaxHighlighter() : highlighter;
  return activeSyntaxHighlighter.getTokenHighlights(tokenType);
}
 
Example 19
Source Project: Intellij-Dust   Source File: DustColorSettingsPage.java    License: MIT License 4 votes vote down vote up
@NotNull
@Override
public SyntaxHighlighter getHighlighter() {
  return new DustSyntaxHighlighter();
}
 
Example 20
Source Project: consulo-csharp   Source File: CSharpColorSettingsPage.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public SyntaxHighlighter getHighlighter()
{
	return new CSharpSyntaxHighlighter();
}
 
Example 21
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);
}
 
Example 22
Source Project: Custom-Syntax-Highlighter   Source File: JavaColorSettings.java    License: MIT License 4 votes vote down vote up
@NotNull
@Override
public SyntaxHighlighter getHighlighter() {
  final Language lang = ObjectUtils.notNull(Language.findLanguageByID("JAVA"), Language.ANY);
  return getSyntaxHighlighterWithFallback(lang);
}
 
Example 23
Source Project: CppTools   Source File: MakefileLanguage.java    License: Apache License 2.0 4 votes vote down vote up
@NotNull
public SyntaxHighlighter getSyntaxHighlighter(Project project, VirtualFile virtualFile) {
  return new MakefileSyntaxHighlighter();
}
 
Example 24
Source Project: idea-php-typo3-plugin   Source File: FluidHighlighterFactory.java    License: MIT License 4 votes vote down vote up
@NotNull
@Override
public SyntaxHighlighter getSyntaxHighlighter(@Nullable Project project, @Nullable VirtualFile virtualFile) {

    return new FluidFileHighlighter();
}
 
Example 25
@NotNull
@Override
public SyntaxHighlighter getSyntaxHighlighter(Project project, VirtualFile virtualFile) {
    return new TypoScriptSyntaxHighlighter();
}
 
Example 26
Source Project: buck   Source File: BuckCodeContexts.java    License: Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public SyntaxHighlighter createHighlighter() {
  return new BuckSyntaxHighlighter();
}
 
Example 27
Source Project: CppTools   Source File: CppColorsAndFontsPage.java    License: Apache License 2.0 4 votes vote down vote up
@NotNull
public SyntaxHighlighter getHighlighter() {
  return SyntaxHighlighter.PROVIDER.create(CppSupportLoader.CPP_FILETYPE, null, new LightVirtualFile("A.cpp"));
}
 
Example 28
Source Project: consulo   Source File: EditorHighlighterFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
public abstract EditorHighlighter createEditorHighlighter(final SyntaxHighlighter syntaxHighlighter, @Nonnull EditorColorsScheme colors);
 
Example 29
Source Project: intellij-csv-validator   Source File: CsvColorSettings.java    License: Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public SyntaxHighlighter getHighlighter() {
    return new CsvSyntaxHighlighter(null, null);
}
 
Example 30
@NotNull
@Override
public SyntaxHighlighter getHighlighter() {
  return new LombokConfigSyntaxHighlighter();
}