ParserNotice.getLine() never called?

Questions on using RSyntaxTextArea should go here.

Moderator: robert

ParserNotice.getLine() never called?

Postby preditcon » Wed Jan 25, 2012 3:22 pm

I'm very impressed with RSytaxTextArea and it's flexibility and I've chosen it to implement a custom Relax NG schema aware XML editor.
So the first thing I tried to do was to append my own XML parser to the component the way it is described in http://fifesoft.com/forum/viewtopic.php?f=10&t=372&hilit=underline. I implemented ParseResult and ParserNotice and have got the actual parsing part working. But the thing is: error lines are not being properly reported. When an error is detected, the underline is always at the proper offset but at the first line of my XML document. When I put a breakpoint in my implementation of ParserNotice.getLine() the debugger never even stopped there. It hit the breakpoint at ParserNotice.getOffset() however, which makes sense because the underline is at the proper offset.
Is there a bug I should be aware of? I probably just missed something.. Here's my implementation of ParserNotice just in case (note that this is just a dummy class which I use to see how stuff works):
Code: Select all
public class XMLParserNotice implements ParserNotice {

        private int level;
        private int line;
        private String message;
        private Parser parser;
        private String tooltip;
        private int offset;

        public XMLParserNotice(
                     Parser parser,
                     SAXParseException ex,
                     RSyntaxDocument rsd,
                     int level) {
            this.level = level;
            this.line = ex.getLineNumber();
            this.message = "";
            switch(level) {
                case ParserNotice.ERROR:
                    this.message = "Error ";
                    break;
                case ParserNotice.WARNING:
                    this.message = "Warning ";
                    break;
                default:
                    this.message = "Info ";
                    break;
            }
            this.tooltip = ex.getMessage();
            this.message += " at (" + ex.getLineNumber() + ":" + ex.getColumnNumber() + "): " + ex.getMessage();
            this.parser = parser;
            this.offset = ex.getColumnNumber();
        }

        @Override
        public boolean containsPosition(int i) {
            return true;
        }

        @Override
        public Color getColor() {
            switch(getLevel()) {
                case ParserNotice.ERROR:
                    return Color.RED;
                case ParserNotice.WARNING:
                    return Color.ORANGE;
                default:
                    return Color.DARK_GRAY;
            }
        }

        @Override
        public int getLength() {
            return 1;
        }

        @Override
        public int getLevel() {
            return level;
        }

        @Override
        public int getLine() {
            return line;
        }

        @Override
        public String getMessage() {
            return message;
        }

        @Override
        public int getOffset() {
            return offset;
        }

        @Override
        public Parser getParser() {
            return parser;
        }

        @Override
        public boolean getShowInEditor() {
            return true;
        }

        @Override
        public String getToolTipText() {
            return tooltip;
        }

        @Override
        public int compareTo(Object o) {
            // just compare levels
            if (o instanceof XMLParserNotice) {
                XMLParserNotice other = (XMLParserNotice) o;
                if (this.level == other.level) {
                    return 0;
                } else {
                    return this.level > other.level ? 1 : -1;
                }
            }
            return -1;
        }

    }


I'd also like to know what ParserNotice.containsPosition(int i) is used for.
preditcon
 
Posts: 27
Joined: Wed Jan 25, 2012 10:09 am

Re: ParserNotice.getLine() never called?

Postby robert » Wed Jan 25, 2012 6:36 pm

Glad you're finding the project useful. For the first problem, the "offset" field of a ParserNotice is actually its absolute offset in the document, not just the offset in the line. Looking at your example, you should be able to calculate the offset expected easily enough (untested):

Code: Select all
private int getLineOfOffset(RSyntaxDocument doc, int line, int relativeOffs) {
   Element root = doc.getDefaultRootElement();
   return root.getElement(line-1).getStartOffset() + relativeOffs - 1;
}


I see that this isn't actually specified anywhere in the Javadoc, so I'll try to remember to update it to be more clear.

The only place I can see the line number of a ParserNotice being used is in the ErrorStrip component, to know where to paint the error markers. I believe it's mainly there for that, and for users who want to display a list of parser notices in a JTable elsewhere in their UI, with a "line" column, for example.

As for the containsPosition() method, I can't see that it's used anywhere. It's probably legacy code that was used to determine if a given offset in the document (determined e.g. from a mouseover event) was contained in a parser notice, and if so, a tool tip was displayed. It looks to be no longer used, and this behavior is instead implemented at a lower level. If it were used though, the implementation you would most likely want can be seen in the DefaultParserNotice class:

Code: Select all
public boolean containsPosition(int pos) {
    return offset<=pos && pos<(offset+length);
}


Hope this helps!
User avatar
robert
 
Posts: 774
Joined: Sat May 10, 2008 5:16 pm

Postby preditcon » Thu Jan 26, 2012 9:09 am

Thank you for your quick reply. The answer to my main question actually came to me the moment I woke up this morning. My editor currently works as expected - it is able to detect and display XML syntax errors. On to content validation and autocompletion which I assume is going to be more troublesome to implement since I'll have to convert your Tokens to a fully qualified XML path for any document offset (so I can query my schema).
preditcon
 
Posts: 27
Joined: Wed Jan 25, 2012 10:09 am


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron