Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to find equivalent node in a reparsed AST (or, better idea for inserting comments)
How to find equivalent node in a reparsed AST (or, better idea for inserting comments) [message #258633] Wed, 11 February 2009 15:48 Go to next message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

I'm generating code using the AST, and need to attach (non-Javadoc)
comments in various places in the generated code.

As far as I've been able to determine, there is no way to do this using
the AST directly. However, I believe I should be able to do it using a
series of TextEdits applied to the IDocument after the initial
generation pass is complete.

When I generate, I save off a set of ASTNodes that need comments, and
the comments (just simple Strings).

Problem #1 is that at generation time, these ASTNodes have no source
position information attached to them. So after I complete generation,
I reparse the file to get an AST with source information needed to
position the text edits. Now I have a list of ASTNodes that came from
the 'old' AST, and need to find the corresponding nodes in the new
(reparsed) AST.

(If I've already gone completely off track in my approach here, feel
free to redirect me in a more productive direction).

Now, I need to figure out how to locate the nodes in the new AST. I
thought perhaps that the StructuralPropertyDescriptors would allow me to
create a 'path' that I could navigate. So I've done this:

Stack<StructuralPropertyDescriptor> pathToNode =
new Stack<StructuralPropertyDescriptor>();
ASTNode originalASTNode = node;
while (originalASTNode.getParent() != null) {
pathToNode.push(originalASTNode.getLocationInParent());
originalASTNode = originalASTNode.getParent();
}

So, this gives me an 'upward' path from the ASTNode to the root.
I then get the AST root node of the new AST, and start popping
structural property descriptors off the stack so I can traverse downward
in the new AST until I locate the 'same' (equivalent) node.

This only works when there the property descriptor is a
ChildPropertyDescriptor. When the property descriptor is a
ChildListPropertyDescriptor (like types, or bodydeclarations, or
statements), then I don't have any data that tells me which element from
the list is the one I want to navigate into.

Am I taking a reasonable approach here? Is there an easy way to capture
a position-in-list value in addition to the structural property
descriptor that would allow me to implement the traversal I'm trying to
do?

Thanks,
Mike
Re: How to find equivalent node in a reparsed AST (or, better idea for inserting comments) [message #258654 is a reply to message #258633] Thu, 12 February 2009 09:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alamothe.ptt.yu

Mike Yawn wrote:
> I'm generating code using the AST, and need to attach (non-Javadoc)
> comments in various places in the generated code.

If you're rewritting the AST using ASTRewrite, you can insert comments using
createStringPlaceholder.

> As far as I've been able to determine, there is no way to do this
> using the AST directly. However, I believe I should be able to do
> it using a series of TextEdits applied to the IDocument after the
> initial generation pass is complete.
>
> When I generate, I save off a set of ASTNodes that need comments, and
> the comments (just simple Strings).
>
> Problem #1 is that at generation time, these ASTNodes have no source
> position information attached to them. So after I complete
> generation, I reparse the file to get an AST with source information
> needed to position the text edits. Now I have a list of ASTNodes
> that came from the 'old' AST, and need to find the corresponding
> nodes in the new (reparsed) AST.
>
> (If I've already gone completely off track in my approach here, feel
> free to redirect me in a more productive direction).
>
> Now, I need to figure out how to locate the nodes in the new AST. I
> thought perhaps that the StructuralPropertyDescriptors would allow me
> to create a 'path' that I could navigate. So I've done this:
>
> Stack<StructuralPropertyDescriptor> pathToNode =
> new Stack<StructuralPropertyDescriptor>();
> ASTNode originalASTNode = node;
> while (originalASTNode.getParent() != null) {
> pathToNode.push(originalASTNode.getLocationInParent());
> originalASTNode = originalASTNode.getParent();
> }
>
> So, this gives me an 'upward' path from the ASTNode to the root.
> I then get the AST root node of the new AST, and start popping
> structural property descriptors off the stack so I can traverse
> downward in the new AST until I locate the 'same' (equivalent) node.
>
> This only works when there the property descriptor is a
> ChildPropertyDescriptor. When the property descriptor is a
> ChildListPropertyDescriptor (like types, or bodydeclarations, or
> statements), then I don't have any data that tells me which element
> from the list is the one I want to navigate into.

I think the only way to capture a position is this:

StructuralPropertyDescriptor location = node.getLocationInParent();
if (location.isChildListProperty()) {
List list = (List) node.getParent().getStructuralProperty(location);
int index = list.indexOf(node);
// now you have a location and an index
}

> Am I taking a reasonable approach here? Is there an easy way to
> capture a position-in-list value in addition to the structural
> property descriptor that would allow me to implement the traversal
> I'm trying to do?
>
> Thanks,
> Mike
Re: How to find equivalent node in a reparsed AST (or, better idea for inserting comments) [message #258673 is a reply to message #258654] Thu, 12 February 2009 12:02 Go to previous message
Eclipse UserFriend
Originally posted by: myawn.ebay.com

Nikola Mihajlovic wrote:
>
> I think the only way to capture a position is this:
>
> StructuralPropertyDescriptor location = node.getLocationInParent();
> if (location.isChildListProperty()) {
> List list = (List) node.getParent().getStructuralProperty(location);
> int index = list.indexOf(node);
> // now you have a location and an index
> }
>

Thanks -- that was the part I was missing. It's working now.

Mike
Previous Topic:Using the JDT CodeFormatter without jface
Next Topic:JDT JavaDoc hover over
Goto Forum:
  


Current Time: Wed May 07 08:34:59 EDT 2025

Powered by FUDForum. Page generated in 0.04438 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top