Java 类com.intellij.openapi.editor.ex.util.LexerEditorHighlighter 实例源码

项目:intellij-ce-playground    文件:EditorHighlighterCache.java   
@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  if (document == null) {
    return null;
  }
  final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  final EditorHighlighter someEditorHighlighter = SoftReference.dereference(editorHighlighterWeakReference);

  if (someEditorHighlighter instanceof LexerEditorHighlighter &&
      ((LexerEditorHighlighter)someEditorHighlighter).isValid()
    ) {
    return someEditorHighlighter;
  }
  document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  return null;
}
项目:intellij-ce-playground    文件:GuiEditor.java   
public void showFormSource() {
  EditorFactory editorFactory = EditorFactory.getInstance();

  Editor editor = editorFactory.createViewer(myDocument, myProject);

  try {
    ((EditorEx)editor).setHighlighter(
      new LexerEditorHighlighter(new XmlFileHighlighter(), EditorColorsManager.getInstance().getGlobalScheme()));

    JComponent component = editor.getComponent();
    component.setPreferredSize(new Dimension(640, 480));

    DialogBuilder dialog = new DialogBuilder(myProject);

    dialog.title("Form - " + myFile.getPresentableName()).dimensionKey("GuiDesigner.FormSource.Dialog");
    dialog.centerPanel(component).setPreferredFocusComponent(editor.getContentComponent());
    dialog.addOkAction();

    dialog.show();
  }
  finally {
    editorFactory.releaseEditor(editor);
  }
}
项目:tools-idea    文件:EditorHighlighterCache.java   
@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  if (document == null) {
    return null;
  }
  final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  final EditorHighlighter someEditorHighlighter = editorHighlighterWeakReference != null ? editorHighlighterWeakReference.get() : null;

  if (someEditorHighlighter instanceof LexerEditorHighlighter &&
      ((LexerEditorHighlighter)someEditorHighlighter).isValid()
    ) {
    return someEditorHighlighter;
  }
  document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  return null;
}
项目:tools-idea    文件:FileTemplateConfigurable.java   
private EditorHighlighter createHighlighter() {
  if (myTemplate != null && myProject != null && myVelocityFileType != FileTypes.UNKNOWN) {
    return EditorHighlighterFactory.getInstance().createEditorHighlighter(myProject, new LightVirtualFile("aaa." + myTemplate.getExtension() + ".ft"));
  }

  FileType fileType = null;
  if (myTemplate != null) {
    fileType = FileTypeManager.getInstance().getFileTypeByExtension(myTemplate.getExtension());
  }
  if (fileType == null) {
    fileType = FileTypes.PLAIN_TEXT;
  }
  SyntaxHighlighter originalHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(fileType, null, null);
  if (originalHighlighter == null) originalHighlighter = new PlainSyntaxHighlighter();
  return new LexerEditorHighlighter(new TemplateHighlighter(originalHighlighter), EditorColorsManager.getInstance().getGlobalScheme());
}
项目:tools-idea    文件:ResourceBundleEditor.java   
private static void reinitSettings(final EditorEx editor) {
  EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  editor.setColorsScheme(scheme);
  EditorSettings settings = editor.getSettings();
  settings.setLineNumbersShown(false);
  settings.setWhitespacesShown(false);
  settings.setLineMarkerAreaShown(false);
  settings.setIndentGuidesShown(false);
  settings.setFoldingOutlineShown(false);
  settings.setAdditionalColumnsCount(0);
  settings.setAdditionalLinesCount(0);
  settings.setRightMarginShown(true);
  settings.setRightMargin(60);

  editor.setHighlighter(new LexerEditorHighlighter(new PropertiesValueHighlighter(), scheme));
  editor.setVerticalScrollbarVisible(true);
}
项目:consulo    文件:EditorHighlighterCache.java   
@Nullable
public static EditorHighlighter getEditorHighlighterForCachesBuilding(Document document) {
  if (document == null) {
    return null;
  }
  final WeakReference<EditorHighlighter> editorHighlighterWeakReference = document.getUserData(ourSomeEditorSyntaxHighlighter);
  final EditorHighlighter someEditorHighlighter = SoftReference.dereference(editorHighlighterWeakReference);

  if (someEditorHighlighter instanceof LexerEditorHighlighter &&
      ((LexerEditorHighlighter)someEditorHighlighter).isValid()
          ) {
    return someEditorHighlighter;
  }
  document.putUserData(ourSomeEditorSyntaxHighlighter, null);
  return null;
}
项目:consulo    文件:LanguageConsoleImpl.java   
public static String printWithHighlighting(@Nonnull LanguageConsoleView console, @Nonnull Editor inputEditor, @Nonnull TextRange textRange) {
  String text;
  EditorHighlighter highlighter;
  if (inputEditor instanceof EditorWindow) {
    PsiFile file = ((EditorWindow)inputEditor).getInjectedFile();
    highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), console.getProject());
    String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
    highlighter.setText(fullText);
    text = textRange.substring(fullText);
  }
  else {
    text = inputEditor.getDocument().getText(textRange);
    highlighter = ((EditorEx)inputEditor).getHighlighter();
  }
  SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter)highlighter).getSyntaxHighlighter() : null;
  ((LanguageConsoleImpl)console).doAddPromptToHistory();
  if (syntax != null) {
    ConsoleViewUtil.printWithHighlighting(console, text, syntax);
  }
  else {
    console.print(text, ConsoleViewContentType.USER_INPUT);
  }
  console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
  return text;
}
项目:intellij-ce-playground    文件:ImmediatePainter.java   
private boolean canPaintImmediately(char c) {
  return getDocument() instanceof DocumentImpl &&
         getHighlighter() instanceof LexerEditorHighlighter &&
         !getSelectionModel().hasSelection() &&
         arePositionsWithinDocument(getCaretModel().getAllCarets()) &&
         areVisualLinesUnique(getCaretModel().getAllCarets()) &&
         !isInplaceRenamerActive() &&
         !KEY_CHARS_TO_SKIP.contains(c);
}
项目:intellij-ce-playground    文件:LanguageConsoleImpl.java   
public static String printWithHighlighting(@NotNull LanguageConsoleView console, @NotNull Editor inputEditor, @NotNull TextRange textRange) {
  String text;
  EditorHighlighter highlighter;
  if (inputEditor instanceof EditorWindow) {
    PsiFile file = ((EditorWindow)inputEditor).getInjectedFile();
    highlighter =
      HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), console.getProject());
    String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
    highlighter.setText(fullText);
    text = textRange.substring(fullText);
  }
  else {
    text = inputEditor.getDocument().getText(textRange);
    highlighter = ((EditorEx)inputEditor).getHighlighter();
  }
  SyntaxHighlighter syntax =
    highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter)highlighter).getSyntaxHighlighter() : null;
  ((LanguageConsoleImpl)console).doAddPromptToHistory();
  if (syntax != null) {
    ConsoleViewUtil.printWithHighlighting(console, text, syntax);
  }
  else {
    console.print(text, ConsoleViewContentType.USER_INPUT);
  }
  console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
  return text;
}
项目:intellij-ce-playground    文件:CustomFileTypeFoldingBuilder.java   
public static void buildBraceMatcherBasedFolding(List<FoldingDescriptor> descriptors,
                                                 PsiElement root,
                                                 Document document,
                                                 SyntaxHighlighter highlighter) {
  LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
  editorHighlighter.setText(document.getText());
  FileType fileType = root.getContainingFile().getFileType();
  BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
  TextRange totalRange = root.getTextRange();
  final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());

  final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<Trinity<Integer, Integer, IElementType>>();
  String editorText = document.getText();
  while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
    final Trinity<Integer, Integer, IElementType> last;
    if (braceMatcher.isLBraceToken(iterator, editorText, fileType) &&
        braceMatcher.isStructuralBrace(iterator, editorText, fileType)) {
      stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
    }
    else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) &&
             braceMatcher.isStructuralBrace(iterator, editorText, fileType)
             && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
      stack.removeLast();
      TextRange range = new TextRange(last.first, iterator.getEnd());
      if (StringUtil.countChars(document.getText(range), '\n') >= 3) {
        descriptors.add(new FoldingDescriptor(root, range));
      }
    }
    iterator.advance();
  }
}
项目:tools-idea    文件:EditorHighlighterFactoryImpl.java   
@Override
public EditorHighlighter createEditorHighlighter(@NotNull VirtualFile vFile, @NotNull EditorColorsScheme settings, @Nullable Project project) {
  FileType fileType = vFile.getFileType();
  if (fileType instanceof LanguageFileType) {
    LanguageFileType substFileType = substituteFileType(((LanguageFileType)fileType).getLanguage(), vFile, project);
    if (substFileType != null) {
      EditorHighlighterProvider provider = FileTypeEditorHighlighterProviders.INSTANCE.forFileType(substFileType);
      EditorHighlighter editorHighlighter = provider.getEditorHighlighter(project, fileType, vFile, settings);
      boolean isPlain = editorHighlighter.getClass() == LexerEditorHighlighter.class &&
                        ((LexerEditorHighlighter) editorHighlighter).isPlain();
      if (!isPlain) {
        return editorHighlighter;
      }
    }
    return FileTypeEditorHighlighterProviders.INSTANCE.forFileType(fileType).getEditorHighlighter(project, fileType, vFile, settings);
  }

  SyntaxHighlighter highlighter = null;
  for (ContentBasedFileSubstitutor processor : Extensions.getExtensions(ContentBasedFileSubstitutor.EP_NAME)) {
    boolean applicable;
    try {
      applicable = processor.isApplicable(project, vFile);
    }
    catch (Exception e) {
      LOG.error(e);
      continue;
    }
    if (applicable && processor instanceof ContentBasedClassFileProcessor) {
      highlighter = ((ContentBasedClassFileProcessor)processor).createHighlighter(project, vFile);
    }
  }
  if (highlighter == null) {
    highlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(fileType, project, vFile);
  }
  return createEditorHighlighter(highlighter, settings);
}
项目:nosql4idea    文件:QueryPanel.java   
private EditorHighlighter createHighlighter(EditorColorsScheme settings) {
    Language language = Language.findLanguageByID("JSON");
    if (language == null) {
        language = Language.ANY;
    }
    return new LexerEditorHighlighter(PlainTextSyntaxHighlighterFactory.getSyntaxHighlighter(language, null, null), settings);
}
项目:consulo-ui-designer    文件:GuiEditor.java   
public void showFormSource()
{
    EditorFactory editorFactory = EditorFactory.getInstance();

    Editor editor = editorFactory.createViewer(myDocument, myProject);

    try
    {
        ((EditorEx) editor).setHighlighter(new LexerEditorHighlighter(new XmlFileHighlighter(),
                EditorColorsManager.getInstance().getGlobalScheme()));

        JComponent component = editor.getComponent();
        component.setPreferredSize(new Dimension(640, 480));

        DialogBuilder dialog = new DialogBuilder(myProject);

        dialog.title("Form - " + myFile.getPresentableName()).dimensionKey("GuiDesigner.FormSource.Dialog");
        dialog.centerPanel(component).setPreferredFocusComponent(editor.getContentComponent());
        dialog.addOkAction();

        dialog.show();
    }
    finally
    {
        editorFactory.releaseEditor(editor);
    }
}
项目:consulo    文件:ImmediatePainter.java   
private static boolean canPaintImmediately(final EditorImpl editor) {
  final CaretModel caretModel = editor.getCaretModel();
  final Caret caret = caretModel.getPrimaryCaret();
  final Document document = editor.getDocument();

  return !(editor.getComponent().getParent() instanceof EditorTextField) &&
         document instanceof DocumentImpl &&
         editor.getHighlighter() instanceof LexerEditorHighlighter &&
         !editor.getSelectionModel().hasSelection() &&
         caretModel.getCaretCount() == 1 &&
         !isInVirtualSpace(editor, caret) &&
         !isInsertion(document, caret.getOffset()) &&
         !caret.isAtRtlLocation() &&
         !caret.isAtBidiRunBoundary();
}
项目:consulo    文件:BraceHighlightingHandler.java   
@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();
}
项目:intellij-ce-playground    文件:EditorHighlighterFactoryImpl.java   
@NotNull
@Override
public EditorHighlighter createEditorHighlighter(SyntaxHighlighter highlighter, @NotNull final EditorColorsScheme colors) {
  if (highlighter == null) highlighter = new PlainSyntaxHighlighter();
  return new LexerEditorHighlighter(highlighter, colors);
}
项目:intellij-ce-playground    文件:ImmediatePainter.java   
private void paintImmediately(Graphics g, int offset, char c, boolean insert) {
  if (g == null) return; // editor component is currently not displayable

  TextAttributes attributes = ((LexerEditorHighlighter)getHighlighter()).getAttributes((DocumentImpl)getDocument(), offset, c);

  int fontType = attributes.getFontType();
  FontInfo fontInfo = EditorUtil.fontForChar(c, attributes.getFontType(), myEditor);
  Font font = fontInfo.getFont();

  // it's more reliable to query actual font metrics
  FontMetrics fontMetrics = myEditor.getFontMetrics(fontType);

  int charWidth = fontMetrics.charWidth(c);

  int delta = charWidth;

  if (!insert && offset < getDocument().getTextLength()) {
    delta -= fontMetrics.charWidth(getDocument().getCharsSequence().charAt(offset));
  }

  Rectangle tailArea = lineRectangleBetween(offset, getDocument().getLineEndOffset(myEditor.offsetToLogicalLine(offset)));
  if (tailArea.isEmpty()) {
    tailArea.width += EditorUtil.getSpaceWidth(fontType, myEditor); // include caret
  }

  Color background = attributes.getBackgroundColor() == null ? getCaretRowBackground() : attributes.getBackgroundColor();

  Rectangle newArea = lineRectangleBetween(offset, offset);
  newArea.width += charWidth;

  String newText = Character.toString(c);
  Point point = newArea.getLocation();
  int ascent = myEditor.getAscent();
  Color foreground = attributes.getForegroundColor() == null ? myEditor.getForegroundColor() : attributes.getForegroundColor();

  EditorUIUtil.setupAntialiasing(g);

  // pre-compute all the arguments beforehand to minimize delays between the calls (as there's no double-buffering)
  if (delta != 0) {
    shift(g, tailArea, delta);
  }
  fill(g, newArea, background);
  print(g, newText, point, ascent, font, foreground);

  // flush changes (there can be batching / buffering in video driver)
  Toolkit.getDefaultToolkit().sync();

  if (isZeroLatencyTypingDebugEnabled()) {
    pause();
  }
}
项目:android-codegenerator-plugin-intellij    文件:TemplateConfigurable.java   
private LexerEditorHighlighter getEditorHighlighter() {
    return new LexerEditorHighlighter(new JavaFileHighlighter(), EditorColorsManager.getInstance().getGlobalScheme());
}
项目:tools-idea    文件:EditorHighlighterFactoryImpl.java   
@Override
public EditorHighlighter createEditorHighlighter(SyntaxHighlighter highlighter, final EditorColorsScheme colors) {
  if (highlighter == null) highlighter = new PlainSyntaxHighlighter();
  return new LexerEditorHighlighter(highlighter, colors);
}
项目:consulo    文件:EditorHighlighterFactoryImpl.java   
@Nonnull
@Override
public EditorHighlighter createEditorHighlighter(SyntaxHighlighter highlighter, @Nonnull final EditorColorsScheme colors) {
  if (highlighter == null) highlighter = new PlainSyntaxHighlighter();
  return new LexerEditorHighlighter(highlighter, colors);
}
项目:consulo    文件:ImmediatePainter.java   
private void paintImmediately(final Graphics g, final int offset, final char c2) {
  final EditorImpl editor = myEditor;
  final Document document = editor.getDocument();
  final LexerEditorHighlighter highlighter = (LexerEditorHighlighter)myEditor.getHighlighter();

  final EditorSettings settings = editor.getSettings();
  final boolean isBlockCursor = editor.isInsertMode() == settings.isBlockCursor();
  final int lineHeight = editor.getLineHeight();
  final int ascent = editor.getAscent();
  final int topOverhang = editor.myView.getTopOverhang();
  final int bottomOverhang = editor.myView.getBottomOverhang();

  final char c1 = offset == 0 ? ' ' : document.getCharsSequence().charAt(offset - 1);

  final List<TextAttributes> attributes = highlighter.getAttributesForPreviousAndTypedChars(document, offset, c2);
  updateAttributes(editor, offset, attributes);

  final TextAttributes attributes1 = attributes.get(0);
  final TextAttributes attributes2 = attributes.get(1);

  if (!(canRender(attributes1) && canRender(attributes2))) {
    return;
  }

  FontLayoutService fontLayoutService = FontLayoutService.getInstance();
  final float width1 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes1.getFontType()), c1);
  final float width2 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes2.getFontType()), c2);

  final Font font1 = EditorUtil.fontForChar(c1, attributes1.getFontType(), editor).getFont();
  final Font font2 = EditorUtil.fontForChar(c1, attributes2.getFontType(), editor).getFont();

  final Point2D p2 = editor.offsetToXY(offset, false);
  float p2x = (float)p2.getX();
  int p2y = (int)p2.getY();
  int width1i = (int)(p2x) - (int)(p2x - width1);
  int width2i = (int)(p2x + width2) - (int)p2x;

  Caret caret = editor.getCaretModel().getPrimaryCaret();
  //noinspection ConstantConditions
  final int caretWidth = isBlockCursor ? editor.getCaretLocations(false)[0].myWidth
                                       : JBUI.scale(caret.getVisualAttributes().getWidth(settings.getLineCursorWidth()));
  final float caretShift = isBlockCursor ? 0 : caretWidth == 1 ? 0 : 1 / JBUI.sysScale((Graphics2D)g);
  final Rectangle2D caretRectangle = new Rectangle2D.Float((int)(p2x + width2) - caretShift, p2y - topOverhang,
                                                           caretWidth, lineHeight + topOverhang + bottomOverhang + (isBlockCursor ? -1 : 0));

  final Rectangle rectangle1 = new Rectangle((int)(p2x - width1), p2y, width1i, lineHeight);
  final Rectangle rectangle2 = new Rectangle((int)p2x, p2y, (int)(width2i + caretWidth - caretShift), lineHeight);

  final Consumer<Graphics> painter = graphics -> {
    EditorUIUtil.setupAntialiasing(graphics);

    fillRect(graphics, rectangle2, attributes2.getBackgroundColor());
    drawChar(graphics, c2, p2x, p2y + ascent, font2, attributes2.getForegroundColor());

    fillRect(graphics, caretRectangle, getCaretColor(editor));

    fillRect(graphics, rectangle1, attributes1.getBackgroundColor());
    drawChar(graphics, c1, p2x - width1, p2y + ascent, font1, attributes1.getForegroundColor());
  };

  final Shape originalClip = g.getClip();

  g.setClip(new Rectangle2D.Float((int)p2x - caretShift, p2y, width2i + caretWidth, lineHeight));

  if (DOUBLE_BUFFERING.asBoolean()) {
    paintWithDoubleBuffering(g, painter);
  }
  else {
    painter.consume(g);
  }

  g.setClip(originalClip);

  if (PIPELINE_FLUSH.asBoolean()) {
    Toolkit.getDefaultToolkit().sync();
  }

  if (DEBUG.asBoolean()) {
    pause();
  }
}