Odd bug when parsing the code

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Odd bug when parsing the code

Postby Debels » Sat Mar 15, 2014 2:36 am

I have this save method that looks for specific token types and based upon those I get an output, the problem is that the first result is perfect, but lets say it loaded into the RSyntaxTextArea:

"TOKEN_HERE{
Hello
}

TOKEN_2{
Something
}" and I edited it, now being:
"TOKEN_HERE{
Hello123
}

TOKEN_2{
Something
}".

when parsing it the output would be:
content: "Hello123" and token: "TOKEN_HERE", but when parsing the second one it outputs 3 characters less (or the amount of the characters added, in this case "123"), being:

content: "ething" and token: "EN_2", I been trying to debug it and no luck.

My guess would be that It somehow didn't update, but I even waited 5 minutes to see if something happened and nothing.

The code relevant to the problem:

Code: Select all
List<Fold> folds = Editor.getFoldParser().getFolds(editor);
for (Fold f : folds){
            Token t = editor.getTokenListForLine(f.getStartLine());
            if (t.getType() == Token.DATA_TYPE){
               System.out.println("|"+String.valueOf(t.getTextArray()).substring(f.getStartOffset()+2, f.getEndOffset()-1)+"|");
               String str = String.valueOf(t.getTextArray()).substring(t.getOffset(), t.getEndOffset());
               System.out.println("|"+str+"|");
            }
         }
Debels
 
Posts: 9
Joined: Fri Feb 14, 2014 4:02 pm

Re: Odd bug when parsing the code

Postby robert » Wed Mar 19, 2014 10:18 pm

The first System.out.println(),

java code:

System.out.println("|"+String.valueOf(t.getTextArray()).substring(f.getStartOffset()+2, f.getEndOffset()-1)+"|");


Isn't safe. It's not guaranteed that t.getTextArray() represents the entire document as a char array (and in fact it won't if you make edits in the document beforehand), so the Fold.getStartOffset()/getEndOffset() methods, which return offsets into the Document, won't point to the right offsets and may even cause IndexOutOfBoundErrors. I assume this line was for debugging though?

The second System.out.println(),

java code:

String str = String.valueOf(t.getTextArray()).substring(t.getOffset(), t.getEndOffset());


can be simplified to just:

java code:

String str = t.getLexeme();
User avatar
robert
 
Posts: 798
Joined: Sat May 10, 2008 5:16 pm

Re: Odd bug when parsing the code

Postby Debels » Wed Mar 19, 2014 11:49 pm

Yes this is for debugging, then how would I do what I'm trying to achieve?

I need to get the start and end of the fold or the text inside the fold, how would I achieve this if the fold start/end Offset isn't reliable?, trying to look for a solution in the javadocs, but no luck so far :/

edit: also is there a class that I can look at for error checking?, All I can find is XmlParser, but that uses the SaxParser to identify errors.
Debels
 
Posts: 9
Joined: Fri Feb 14, 2014 4:02 pm

Re: Odd bug when parsing the code

Postby robert » Fri Mar 21, 2014 3:38 am

Sorry, since I saw you doing stuff with the Token's offsets, I thought you were trying to get the Token's text. The fold's text you can get from the RSyntaxTextArea (or RSyntaxDocument) via textArea.getText(int start, int len) or document.getText(int start, int len). The fold's reported start and end offsets should be valid.
User avatar
robert
 
Posts: 798
Joined: Sat May 10, 2008 5:16 pm

Re: Odd bug when parsing the code

Postby Debels » Fri Mar 21, 2014 5:01 am

robert wrote:Sorry, since I saw you doing stuff with the Token's offsets, I thought you were trying to get the Token's text. The fold's text you can get from the RSyntaxTextArea (or RSyntaxDocument) via textArea.getText(int start, int len) or document.getText(int start, int len). The fold's reported start and end offsets should be valid.


I get a bad location exception when using it.

Code: Select all
List<Fold> folds = Editor.getFoldParser().getFolds(editor);
for (Fold f : folds){
            Token t = editor.getTokenListForLine(f.getStartLine());
            if (t.getType() == Token.DATA_TYPE){
               System.out.println("===============START2===============");
               try {
                  System.out.println("Content: "+editor.getText(f.getStartOffset(), f.getEndOffset()));
               } catch (BadLocationException e) {
                  e.printStackTrace();
               }
               System.out.println("Type: "+t.getLexeme());
               System.out.println("================END2================");
            }
         }


the fold parser I'm using is like curly fold parser except for a few changes I made, but nothing that could break it (I think).
http://pastie.org/private/mpcqd48l6zg1dtmdhbumw

Thanks
Debels
 
Posts: 9
Joined: Fri Feb 14, 2014 4:02 pm

Re: Odd bug when parsing the code

Postby Debels » Fri Mar 21, 2014 5:39 am

robert wrote:Sorry, since I saw you doing stuff with the Token's offsets, I thought you were trying to get the Token's text. The fold's text you can get from the RSyntaxTextArea (or RSyntaxDocument) via textArea.getText(int start, int len) or document.getText(int start, int len). The fold's reported start and end offsets should be valid.


Ok that worked, but I have to point out that doing textArea.getText(start, end) throws a BadLocation exception when getting the last one. I had to do getText().substring(start, end).
Debels
 
Posts: 9
Joined: Fri Feb 14, 2014 4:02 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 4 guests

cron