Exemplo n.º 1
0
  /**
   * Determine the area of the document whose syntax highlighting is impacted by the change of
   * source content
   *
   * @param offset The initial offset of the change
   * @param length The length of the change
   * @throws BadLocationException If the change processing fails
   */
  public void processChangedLines(int offset, int length) throws BadLocationException {

    Element line;
    int startOffset = offset;
    int changeOffset = offset;
    int changeLength = length;
    MutableAttributeSet highlight = getStyle(InformSyntax.Normal.getName());
    MutableAttributeSet tokenCheck = getStyle(InformSyntax.Normal.getName());

    // Locate start of first highlight token at insert/remove offset

    if (changeOffset > 0) {

      // Check for the element before the insertion/removal point (offset-1) so if the
      // insertion/removal point is at the boundary of two elements we get the previous
      // highlight token not the following highlight token

      Element token = getCharacterElement(offset - 1);
      startOffset = token.getStartOffset();
      highlight = (MutableAttributeSet) token.getAttributes();

      tokenCheck = getStyle((String) highlight.getAttribute(AttributeSet.NameAttribute));

      while (highlight.containsAttributes(tokenCheck) && changeOffset > 0) {
        changeOffset = startOffset;
        token = getCharacterElement(changeOffset - 1);
        startOffset = token.getStartOffset();
        highlight = (MutableAttributeSet) token.getAttributes();
      }
    }

    // Find the length of the text in the document impacted by the insert/remove.
    // The length of the text between the start of the first highlight token at the insert point
    // to the end of the current line plus the length of the text being inserted into the document.

    if (length > 0) {
      line = getParagraphElement(offset + length);
    } else {
      line = getParagraphElement(offset);
    }
    changeLength = line.getEndOffset() - changeOffset;

    applyHighlighting(changeOffset, changeLength);
  }
Exemplo n.º 2
0
  /**
   * Apply inform syntax highlighting to the specified portion of the document
   *
   * @param offset The initial offset at which to apply syntax highlighting
   * @param length The length of the document text to be highlighted
   * @throws BadLocationException If applying highlighting fails
   */
  public void applyHighlighting(int offset, int length) throws BadLocationException {

    int startOffset;
    int endOffset;
    int tokenLength;
    MutableAttributeSet syntax;

    String source = getText(offset, length);
    ResourceLexer lexer = new ResourceLexer(source, offset);
    ResourceToken token = lexer.nextElement();

    while (token.getType() != ResourceToken.EOS) {

      startOffset = token.getStartPosition();
      endOffset = token.getEndPosition();
      tokenLength = endOffset - startOffset;

      if (token.getType() == ResourceToken.COMMENT) {
        syntax = getStyle(InformSyntax.Comment.getName());
      } else if (token.getType() == ResourceToken.STRING) {
        syntax = getStyle(InformSyntax.String.getName());
      } else if (token.getType() == ResourceToken.SYMBOL) {
        if (Resource.isKeyword(token.getContent())) {
          syntax = getStyle(InformSyntax.Keyword.getName());
        } else {
          syntax = getStyle(InformSyntax.Normal.getName());
        }
      } else if (token.getType() == ResourceToken.WHITESPACE) {
        syntax = getStyle(InformSyntax.White.getName());
      } else {
        syntax = getStyle(InformSyntax.Normal.getName());
      }

      setCharacterAttributes(startOffset, tokenLength, syntax, true);
      token = lexer.nextElement();
    }
  }
Exemplo n.º 3
0
  /**
   * Insert a string with syntax highlighting
   *
   * @param offset The offset to insert the string
   * @param str The String to be inserted in the document
   * @param a The AttributeSet for the String
   * @throws BadLocationException If the insert action fails
   */
  @Override
  public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {

    super.insertString(offset, str, getStyle(InformSyntax.Normal.getName()));
    processChangedLines(offset, str.length());
  }