Beispiel #1
1
 /**
  * Event.detail line start offset (input) Event.text line text (input) LineStyleEvent.styles
  * Enumeration of StyleRanges, need to be in order. (output) LineStyleEvent.background line
  * background color (output)
  */
 public void lineGetStyle(LineStyleEvent event) {
   Vector styles = new Vector();
   int token;
   StyleRange lastStyle;
   // If the line is part of a block comment, create one style for the entire line.
   if (inBlockComment(event.lineOffset, event.lineOffset + event.lineText.length())) {
     styles.addElement(
         new StyleRange(event.lineOffset, event.lineText.length(), getColor(COMMENT), null));
     event.styles = new StyleRange[styles.size()];
     styles.copyInto(event.styles);
     return;
   }
   Color defaultFgColor = ((Control) event.widget).getForeground();
   scanner.setRange(event.lineText);
   token = scanner.nextToken();
   while (token != EOF) {
     if (token == OTHER) {
       // do nothing for non-colored tokens
     } else if (token != WHITE) {
       Color color = getColor(token);
       // Only create a style if the token color is different than the
       // widget's default foreground color and the token's style is not
       // bold.  Keywords are bolded.
       if ((!color.equals(defaultFgColor)) || (token == KEY)) {
         StyleRange style =
             new StyleRange(
                 scanner.getStartOffset() + event.lineOffset, scanner.getLength(), color, null);
         if (token == KEY) {
           style.fontStyle = SWT.BOLD;
         }
         if (styles.isEmpty()) {
           styles.addElement(style);
         } else {
           // Merge similar styles.  Doing so will improve performance.
           lastStyle = (StyleRange) styles.lastElement();
           if (lastStyle.similarTo(style) && (lastStyle.start + lastStyle.length == style.start)) {
             lastStyle.length += style.length;
           } else {
             styles.addElement(style);
           }
         }
       }
     } else if ((!styles.isEmpty())
         && ((lastStyle = (StyleRange) styles.lastElement()).fontStyle == SWT.BOLD)) {
       int start = scanner.getStartOffset() + event.lineOffset;
       lastStyle = (StyleRange) styles.lastElement();
       // A font style of SWT.BOLD implies that the last style
       // represents a java keyword.
       if (lastStyle.start + lastStyle.length == start) {
         // Have the white space take on the style before it to
         // minimize the number of style ranges created and the
         // number of font style changes during rendering.
         lastStyle.length += scanner.getLength();
       }
     }
     token = scanner.nextToken();
   }
   event.styles = new StyleRange[styles.size()];
   styles.copyInto(event.styles);
 }
Beispiel #2
1
  /**
   * Event.detail line start offset (input) Event.text line text (input) LineStyleEvent.styles
   * Enumeration of StyleRanges, need to be in order. (output) LineStyleEvent.background line
   * background color (output)
   */
  public void lineGetStyle(LineStyleEvent event) {
    Vector<StyleRange> styles = new Vector<StyleRange>();
    int token;
    StyleRange lastStyle;

    if (inBlockComment(event.lineOffset, event.lineOffset + event.lineText.length())) {
      styles.addElement(
          new StyleRange(event.lineOffset, event.lineText.length() + 4, colors[2], null));
      event.styles = new StyleRange[styles.size()];
      styles.copyInto(event.styles);
      return;
    }
    scanner.setRange(event.lineText);
    String xs = ((StyledText) event.widget).getText();
    if (xs != null) parseBlockComments(xs);
    token = scanner.nextToken();
    while (token != EOF) {
      if (token == OTHER) {
        // do nothing
      } else if ((token == WHITE) && (!styles.isEmpty())) {
        int start = scanner.getStartOffset() + event.lineOffset;
        lastStyle = (StyleRange) styles.lastElement();
        if (lastStyle.fontStyle != SWT.NORMAL) {
          if (lastStyle.start + lastStyle.length == start) {
            // have the white space take on the style before it to minimize font style
            // changes
            lastStyle.length += scanner.getLength();
          }
        }
      } else {
        Color color = getColor(token);
        if (color != colors[0]) { // hardcoded default foreground color, black
          StyleRange style =
              new StyleRange(
                  scanner.getStartOffset() + event.lineOffset, scanner.getLength(), color, null);
          if (token == KEY) {
            style.fontStyle = SWT.BOLD;
          }
          if (styles.isEmpty()) {
            styles.addElement(style);
          } else {
            lastStyle = (StyleRange) styles.lastElement();
            if (lastStyle.similarTo(style) && (lastStyle.start + lastStyle.length == style.start)) {
              lastStyle.length += style.length;
            } else {
              styles.addElement(style);
            }
          }
        }
      }
      token = scanner.nextToken();
    }
    event.styles = new StyleRange[styles.size()];
    styles.copyInto(event.styles);
  }
 public static StyleRange copy(final StyleRange range) {
   final StyleRange result = new StyleRange(range);
   result.start = range.start;
   result.length = range.length;
   result.fontStyle = range.fontStyle;
   return result;
 }
  private void updateStyle(ITextViewer viewer) {
    StyledText text = viewer.getTextWidget();
    int widgetOffset = getWidgetOffset(viewer, fRememberedStyleRange.start);
    StyleRange range = new StyleRange(fRememberedStyleRange);
    range.start = widgetOffset;
    range.length = fRememberedStyleRange.length;
    StyleRange currentRange = text.getStyleRangeAtOffset(widgetOffset);
    if (currentRange != null) {
      range.strikeout = currentRange.strikeout;
      range.underline = currentRange.underline;
      range.fontStyle = currentRange.fontStyle;
    }

    // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754
    try {
      text.setStyleRange(range);
    } catch (IllegalArgumentException x) {
      // catching exception as offset + length might be outside of the text widget
      fRememberedStyleRange = null;
    }
  }
  private boolean prepareTwigRegions(
      Collection<StyleRange> holdResults,
      ITwigScriptRegion region,
      int regionStart,
      int partitionStartOffset,
      int partitionLength) {

    assert (region.getType() == TwigRegionContext.TWIG_CONTENT
        || region.getType() == TwigRegionContext.TWIG_COMMENT);

    StyleRange styleRange = null;
    TextAttribute attr;
    TextAttribute previousAttr = null;

    ITextRegion[] twigTokens = null;
    try {

      int from;
      int length;
      if (partitionStartOffset < regionStart) {
        from = 0;
        length = partitionLength - (regionStart - partitionStartOffset);
      } else {
        from = partitionStartOffset - regionStart;
        length = partitionLength;
      }
      twigTokens = region.getTwigTokens(from, Math.min(length, region.getLength()));
      ITextRegion prevElement = null;
      for (int i = 0; i < twigTokens.length; i++) {

        ITextRegion element = twigTokens[i];
        attr = getAttributeFor(element);
        // Check that the elements are different - otherwise the
        // coloring is not valid
        if (prevElement == element || attr == null) {
          continue;
        }
        if ((styleRange != null)
            && (previousAttr != null)
            && (previousAttr.equals(attr))
            && prevElement != null
            && prevElement.getLength() == prevElement.getLength()) {
          // extends the prev styleRange with the current element
          // length
          styleRange.length += element.getLength();
          if (styleRange.start + styleRange.length > partitionStartOffset + partitionLength) {
            styleRange.length -=
                (styleRange.start + styleRange.length) - (partitionStartOffset + partitionLength);
          }
        } else {
          // create new styleRange
          int styleStart = regionStart + element.getStart();
          int styleLength = element.getLength();
          if (styleStart + styleLength < partitionStartOffset) { // if
            // the
            // range
            // ends
            // before
            // the
            // requested
            // starting
            // position
            // -
            // ignoring
            // it
            continue;
          }
          if (styleStart < partitionStartOffset) { // if the region
            // starts before
            // the requested
            // starting
            // position -
            // adjusting the
            // style start
            // position
            styleLength -= (partitionStartOffset - styleStart);
            styleStart = partitionStartOffset;
          }
          if (styleStart > partitionStartOffset + partitionLength) {
            // if the region ends after the requested end position -
            // making it shorter
            styleLength -= styleStart - (partitionStartOffset + partitionLength);
          }
          if (attr.getBackground() != null && element.getTextEnd() != element.getEnd()) { // in
            // case
            // of
            // background
            // color
            // make
            // sure
            // the
            // highlighting
            // will
            // not
            // paint
            // the
            // whitespaces
            // applying style to the region w/o the whitespace
            styleRange =
                new StyleRange(
                    styleStart,
                    styleLength - (element.getEnd() - element.getTextEnd()),
                    attr.getForeground(),
                    attr.getBackground(),
                    attr.getStyle());
            if ((attr.getStyle() & TextAttribute.UNDERLINE) != 0) {
              styleRange.underline = true;
              styleRange.fontStyle &= ~TextAttribute.UNDERLINE;
            }
            if ((attr.getStyle() & TextAttribute.STRIKETHROUGH) != 0) {
              styleRange.strikeout = true;
              styleRange.fontStyle &= ~TextAttribute.STRIKETHROUGH;
            }
            holdResults.add(styleRange);
            // applying style to the whitespace (important for the
            // refresh of the specific range
            styleRange =
                new StyleRange(
                    regionStart + element.getTextEnd(),
                    element.getEnd() - element.getTextEnd(),
                    attr.getForeground(),
                    null,
                    attr.getStyle());
            holdResults.add(styleRange);
            previousAttr = null;
          } else {
            styleRange =
                new StyleRange(
                    styleStart,
                    styleLength,
                    attr.getForeground(),
                    attr.getBackground(),
                    attr.getStyle());
            if ((attr.getStyle() & TextAttribute.UNDERLINE) != 0) {
              styleRange.underline = true;
              styleRange.fontStyle &= ~TextAttribute.UNDERLINE;
            }
            if ((attr.getStyle() & TextAttribute.STRIKETHROUGH) != 0) {
              styleRange.strikeout = true;
              styleRange.fontStyle &= ~TextAttribute.STRIKETHROUGH;
            }
            holdResults.add(styleRange);
            // technically speaking, we don't need to update
            // previousAttr
            // in the other case, because the other case is when
            // it hasn't changed
            previousAttr = attr;
          }
        }
        prevElement = element;
      }
      return true;
    } catch (BadLocationException e) {
      Logger.logException(e);
      return false;
    }
  }