[BLUEJ-182] Node positions are strange, especially FieldNode
Here is a Java class that I have in BlueJ:
{noformat}
/**
- Write a description of class Short here.
/
public class Short
{
/*
- Constructor for objects of class Short */ public Short() { // initialise instance variables x = 0; } private int x;
} {noformat}
I am looking at the NodeAndPosition representation of that code to understand how the tree of parse nodes is built. I'm traversing the NodeTree, effectively, rather than using the findAtOrAfter methods. Here is the tree of NodeAndPosition items, with position and end values (from getPosition and getEnd):
{noformat} bluej.parser.nodes.ParsedCUNode@19299f5(0,245) bluej.parser.nodes.ParsedTypeNode@842d9a(1,244) bluej.parser.nodes.CommentNode@186ed7a(0,51) bluej.parser.nodes.TypeInnerNode@1ce00b4(21,191) bluej.parser.nodes.MethodNode@1e3cc77(5,149) bluej.parser.nodes.CommentNode@150f0a7(0,57) bluej.parser.nodes.ParentParsedNode@19067e5(25,86) bluej.parser.nodes.CommentNode@e771f3(9,41) bluej.parser.nodes.ExpressionNode@ea3932(9,14) bluej.parser.nodes.FieldNode@15118a7(12,19) {noformat}
Here, for reference, is the file marked up with element offsets, in the format start-of-line -> first-non-whitespace-character, end-of-line:
{noformat} // 0->0,1: /** // 1->1,5:
- Write a description of class Short here. // 5->6,49: / // 49->50,53: public class Short // 53->53,72: { // 72->72,74: /* // 74->78,82:
- Constructor for objects of class Short // 82->87,128: */ // 128->133,136: public Short() // 136->140,155: { // 155->159,161: // initialise instance variables // 161->169,202: x = 0; // 202->210,217: } // 217->221,223: private int x; // 223->227,242: // 242->242,243: } // 243->243,245: // 245->245,246: {noformat}
If we look at the first TypeInnerNode, its start position claims to be at 21. That isn't right; 21 characters is midway through the first comment. To correct the indent, I believe I need to add on the end position (or possibly size) of its prior sibling node (the CommentNode), giving 21+51=72, but it's bit confusing to add on offsets from siblings (not just from parents). Judging by the MethodNode, it seems you need to add the end position/size of siblings, and the start position of parent nodes to get to the offset from the beginning of the whole file. Is that right?
The FieldNode would appear to back up this idea of size from sibling nodes and offset from parent, 51+21+144+12=228 (shouldn't it be 227, though? And why is the field node only 7 characters long?). But, now see what happens if I add a method after that field, giving the Java source:
{noformat}
/**
- Write a description of class Short here.
/
public class Short
{
/*
- Constructor for objects of class Short */ public Short() { // initialise instance variables x = 0; } private int x; public void doNothing() { }
} {noformat}
And the output:
{noformat} bluej.parser.nodes.ParsedCUNode@19299f5(0,285) bluej.parser.nodes.ParsedTypeNode@94aa42(1,284) bluej.parser.nodes.CommentNode@1a4eb56(0,51) bluej.parser.nodes.TypeInnerNode@35dc95(21,231) bluej.parser.nodes.MethodNode@a39de(5,149) bluej.parser.nodes.CommentNode@566633(0,57) bluej.parser.nodes.ParentParsedNode@24ea85(25,86) bluej.parser.nodes.CommentNode@4fdf11(9,41) bluej.parser.nodes.ExpressionNode@1be0369(9,14) bluej.parser.nodes.FieldNode@1d1fc02(161,168) bluej.parser.nodes.MethodNode@15d45d9(5,40) bluej.parser.nodes.ParentParsedNode@f800db(29,34) // 0->0,1: /** // 1->1,5:
- Write a description of class Short here. // 5->6,49: / // 49->50,53: public class Short // 53->53,72: { // 72->72,74: /* // 74->78,82:
- Constructor for objects of class Short // 82->87,128: */ // 128->133,136: public Short() // 136->140,155: { // 155->159,161: // initialise instance variables // 161->169,202: x = 0; // 202->210,217: } // 217->221,223: private int x; // 223->227,242: public void doNothing() // 242->246,270: { // 270->274,276: } // 276->280,282: // 282->282,283: } // 283->283,285: // 285->285,286: {noformat}
Note how the FieldNode offset has jumped by about 150 by adding something after it in the class. Regardless of any other misunderstandings, that looks like a bug, so hence this ticket. (All this node position stuff is in aid of the auto-indentation feature, where I need to traverse the node tree to work out what my indent should be on the current line, as it depends on the parent nodes.)
Issue metadata
- Issue type: Bug
- Priority: Medium
- Fix versions: 3.0.6