bobbylight / RSyntaxTextArea

A syntax highlighting, code folding text editor for Java Swing applications.
BSD 3-Clause "New" or "Revised" License
1.1k stars 256 forks source link

Better highlighter for syntax errors #86

Open ricardojlrufino opened 9 years ago

ricardojlrufino commented 9 years ago

It would be interesting to put a better highlighter for errors: case Token.ERROR_CHAR: case Token.ERROR_IDENTIFIER: case Token.ERROR_NUMBER_FORMAT: case Token.ERROR_STRING_DOUBLE:

Ex: selecao_428

I did this by implementing a parser, but is relatively slow and also he needs to do the parser of the entire document (performace problem)

/**
 * Displays Syntax errors at the level of tokens, these errors are generated by {@link TokenMaker} like {@link CPlusPlusTokenMaker}
 * @author Ricardo JL Rufino (ricardo@criativasoft.com.br)
 */
public class TokenErrorMarker extends AbstractParser {

  private RSyntaxTextArea textarea;
  private DefaultParseResult result;

  public TokenErrorMarker(RSyntaxTextArea textarea) {
    setEnabled(true);
    this.textarea = textarea;
    result = new DefaultParseResult(this);
  }

  @Override
  public ParseResult parse(RSyntaxDocument doc, String style) {

    Element root = doc.getDefaultRootElement();
    int lineCount = root.getElementCount();

    result.clearNotices();
    result.setParsedLines(0, lineCount-1);

    for (int line=0; line<lineCount; line++) {
      parseTokensErrors(doc, line);
    }
    My Class:
    return result;
  }

  private boolean parseTokensErrors(RSyntaxDocument doc,int line){

    boolean found = false;

    Token t = doc.getTokenListForLine(line);
    int start = -1;
    int end = -1;

    while (t!=null) {
      if (isError(t)) {
        if(start == -1) start = t.getOffset();
      }else{
        if(start > 0 && end == -1) end =  t.getOffset();
      }
      t = t.getNextToken();
    }

    if (start>-1) {
      DefaultParserNotice pn = new DefaultParserNotice(this, "syntax error", line, start, (end-start));
      pn.setLevel(ParserNotice.Level.ERROR);
      pn.setColor(Color.RED);
      result.addNotice(pn);
      found = true;
    }

    return found;
  }

  private boolean isError(Token token){

    switch (token.getType()) {
      case Token.ERROR_CHAR:
      case Token.ERROR_IDENTIFIER:
      case Token.ERROR_NUMBER_FORMAT:
      case Token.ERROR_STRING_DOUBLE:
        return true;
    }

    return false;
  }

}
bobbylight commented 8 years ago

This is an ask to make error tokens paint a squiggle underline underneath themselves, like the Parser API does. This should be easy to do, but I consider it low priority since it is uncommon for TokenMakers to use these error tokens (at least, the out-of-the-box ones).