Java Code Examples for com.intellij.openapi.editor.highlighter.HighlighterIterator#getTextAttributes()

The following examples show how to use com.intellij.openapi.editor.highlighter.HighlighterIterator#getTextAttributes() . 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: HTMLTextPainter.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void writeStyles(@NonNls final Writer writer) throws IOException {
  writer.write("<style type=\"text/css\">\n");
  writer.write(".ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }\n");
  HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
  while(!hIterator.atEnd()) {
    TextAttributes textAttributes = hIterator.getTextAttributes();
    if (!myStyleMap.containsKey(textAttributes)) {
      @NonNls String styleName = "s" + myStyleMap.size();
      myStyleMap.put(textAttributes, styleName);
      writer.write("." + styleName + " { ");
      final Color foreColor = textAttributes.getForegroundColor();
      if (foreColor != null) {
        writer.write("color: rgb(" + foreColor.getRed() + "," + foreColor.getGreen() + "," + foreColor.getBlue() + "); ");
      }
      if ((textAttributes.getFontType() & Font.BOLD) != 0) {
        writer.write("font-weight: bold; ");
      }
      if ((textAttributes.getFontType() & Font.ITALIC) != 0) {
        writer.write("font-style: italic; ");
      }
      writer.write("}\n");
    }
    hIterator.advance();
  }
  writer.write("</style>\n");
}
 
Example 2
Source File: TodoItemNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void collectHighlights(@Nonnull List<? super HighlightedRegion> highlights, @Nonnull EditorHighlighter highlighter, int startOffset, int endOffset, int highlightOffsetShift) {
  HighlighterIterator iterator = highlighter.createIterator(startOffset);
  while (!iterator.atEnd()) {
    int start = Math.max(iterator.getStart(), startOffset);
    int end = Math.min(iterator.getEnd(), endOffset);
    if (start >= endOffset) break;

    TextAttributes attributes = iterator.getTextAttributes();
    int fontType = attributes.getFontType();
    if ((fontType & Font.BOLD) != 0) { // suppress bold attribute
      attributes = attributes.clone();
      attributes.setFontType(fontType & ~Font.BOLD);
    }
    HighlightedRegion region = new HighlightedRegion(highlightOffsetShift + start - startOffset, highlightOffsetShift + end - startOffset, attributes);
    highlights.add(region);
    iterator.advance();
  }
}
 
Example 3
Source File: AbstractBashSyntaxHighlighterTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
protected void doLexerHighlightingTest(String fileContent, IElementType targetElementType) {
    BashSyntaxHighlighter syntaxHighlighter = new BashSyntaxHighlighter();
    TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(targetElementType);
    Assert.assertEquals("Expected one key", 1, keys.length);

    TextAttributesKey attributesKey = keys[0];
    Assert.assertNotNull(attributesKey);

    EditorColorsManager manager = EditorColorsManager.getInstance();
    EditorColorsScheme scheme = (EditorColorsScheme) manager.getGlobalScheme().clone();
    manager.addColorsScheme(scheme);
    EditorColorsManager.getInstance().setGlobalScheme(scheme);

    TextAttributes targetAttributes = new TextAttributes(JBColor.RED, JBColor.BLUE, JBColor.GRAY, EffectType.BOXED, Font.BOLD);
    scheme.setAttributes(attributesKey, targetAttributes);

    myFixture.configureByText(BashFileType.BASH_FILE_TYPE, fileContent);

    TextAttributes actualAttributes = null;
    HighlighterIterator iterator = ((EditorImpl) myFixture.getEditor()).getHighlighter().createIterator(0);
    while (!iterator.atEnd()) {
        if (iterator.getTokenType() == targetElementType) {
            actualAttributes = iterator.getTextAttributes();
            break;
        }

        iterator.advance();
    }

    Assert.assertEquals("Expected text attributes for " + attributesKey, targetAttributes, actualAttributes);
}
 
Example 4
Source File: EditorPainter.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void paintBorderEffect(EditorHighlighter highlighter) {
  HighlighterIterator it = highlighter.createIterator(myStartOffset);
  while (!it.atEnd() && it.getStart() < myEndOffset) {
    TextAttributes attributes = it.getTextAttributes();
    EffectDescriptor borderDescriptor = getBorderDescriptor(attributes);
    if (borderDescriptor != null) {
      paintBorderEffect(it.getStart(), it.getEnd(), borderDescriptor);
    }
    it.advance();
  }
}