com.intellij.openapi.editor.ex.util.LexerEditorHighlighter Java Examples

The following examples show how to use com.intellij.openapi.editor.ex.util.LexerEditorHighlighter. 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: ImmediatePainter.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean canPaintImmediately(final DesktopEditorImpl editor) {
  final CaretModel caretModel = editor.getCaretModel();
  final Caret caret = caretModel.getPrimaryCaret();
  final Document document = editor.getDocument();

  return document instanceof DocumentImpl &&
         editor.getHighlighter() instanceof LexerEditorHighlighter &&
         !(editor.getComponent().getParent() instanceof EditorTextField) &&
         editor.myView.getTopOverhang() <= 0 && editor.myView.getBottomOverhang() <= 0 &&
         !editor.getSelectionModel().hasSelection() &&
         caretModel.getCaretCount() == 1 &&
         !isInVirtualSpace(editor, caret) &&
         !isInsertion(document, caret.getOffset()) &&
         !caret.isAtRtlLocation() &&
         !caret.isAtBidiRunBoundary();
}
 
Example #2
Source File: EditorHighlighterCache.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: QueryPanel.java    From nosql4idea with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #4
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 #5
Source File: LanguageConsoleImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
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 consoleImpl = (LanguageConsoleImpl)console;
  consoleImpl.doAddPromptToHistory();
  if (syntax != null) {
    ConsoleViewUtil.printWithHighlighting(console, text, syntax, () -> {
      String identPrompt = consoleImpl.myConsoleExecutionEditor.getConsolePromptDecorator().getIndentPrompt();
      if (StringUtil.isNotEmpty(identPrompt)) {
        consoleImpl.addPromptToHistoryImpl(identPrompt);
      }
    });
  }
  else {
    console.print(text, ConsoleViewContentType.USER_INPUT);
  }
  console.print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
  return text;
}
 
Example #6
Source File: TemplateConfigurable.java    From android-codegenerator-plugin-intellij with Apache License 2.0 4 votes vote down vote up
private LexerEditorHighlighter getEditorHighlighter() {
    return new LexerEditorHighlighter(new JavaFileHighlighter(), EditorColorsManager.getInstance().getGlobalScheme());
}
 
Example #7
Source File: EditorHighlighterFactoryImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public EditorHighlighter createEditorHighlighter(SyntaxHighlighter highlighter, @Nonnull final EditorColorsScheme colors) {
  if (highlighter == null) highlighter = new PlainSyntaxHighlighter();
  return new LexerEditorHighlighter(highlighter, colors);
}
 
Example #8
Source File: ImmediatePainter.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void paintImmediately(final Graphics2D g, final int offset, final char c2) {
  final DesktopEditorImpl 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.offsetToPoint2D(offset);
  float p2x = (float)p2.getX();
  int p2y = (int)p2.getY();

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

  final Rectangle2D rectangle1 = new Rectangle2D.Float(p2x - width1, p2y, width1, lineHeight);
  final Rectangle2D rectangle2 = new Rectangle2D.Float(p2x, p2y, width2 + caretWidth - caretShift, lineHeight);

  final Consumer<Graphics2D> painter = graphics -> {
    EditorUIUtil.setupAntialiasing(graphics);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, editor.myFractionalMetricsHintValue);

    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();

  float clipStartX = (float)PaintUtil.alignToInt(p2x > editor.getContentComponent().getInsets().left ? p2x - caretShift : p2x, g, PaintUtil.RoundingMode.FLOOR);
  float clipEndX = (float)PaintUtil.alignToInt(p2x + width2 - caretShift + caretWidth, g, PaintUtil.RoundingMode.CEIL);
  g.setClip(new Rectangle2D.Float(clipStartX, p2y, clipEndX - clipStartX, lineHeight));
  // at the moment, lines in editor are not aligned to dev pixel grid along Y axis, when fractional scale is used,
  // so double buffering is disabled (as it might not produce the same result as direct painting, and will case text jitter)
  if (DOUBLE_BUFFERING.asBoolean() && !PaintUtil.isFractionalScale(g.getTransform())) {
    paintWithDoubleBuffering(g, painter);
  }
  else {
    painter.consume(g);
  }

  g.setClip(originalClip);

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

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