Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » get ASTNode from codeposition.
get ASTNode from codeposition. [message #244761] Thu, 21 June 2007 12:21 Go to next message
Eclipse UserFriend
Originally posted by: martin.nowack.gmail.com

Hi,
im currently working on a plugin which extends jdt.
I have to know, which ASTNode correspond to a
given source code position.

My current way is to use the source code, create an AST from it and travel
over it with an ASTVisitor, which traverse the complete tree and checks
with getCodePosition() and getCodeLength() each node to narrow down to the
correct element.

It works but I would like to ask, whether there is a smarter way?
Especially because there have to be an AST created before each check.

Is it possible to get the current used AST depending on the IDocument?

Thanks for help.
Re: get ASTNode from codeposition. [message #244822 is a reply to message #244761] Fri, 22 June 2007 07:49 Go to previous message
Eclipse UserFriend
Originally posted by: jacek.pospychala.pl.ibm.com

Martin,
i don't think there is any AST up to date all the time, that you could
use, because maintaining such would be really expensive.
If you need the AST tree only when user requests it, eg. by clicking
some button, then it would be fine to create new one just for you.
If you needed AST that is just going to be compiled, you could register
your compilationParticipant (using
org.eclipse.jdt.core.compilationParticipant extension point), but then i
guess you wouldn't have any certain position to look at.

Looking at JDT code, extending ASTVisitor is correct way. I've just
found a class that does something similar to what your does -
org.eclipse.jdt.internal.corext.dom.NodeFinder. Given an ASTNode and
offset range, it finds the node best matching given range.

MartinN wrote:
> Hi,
> im currently working on a plugin which extends jdt.
> I have to know, which ASTNode correspond to a
> given source code position.
>
> My current way is to use the source code, create an AST from it and travel
> over it with an ASTVisitor, which traverse the complete tree and checks
> with getCodePosition() and getCodeLength() each node to narrow down to the
> correct element.
>
> It works but I would like to ask, whether there is a smarter way?
> Especially because there have to be an AST created before each check.
>
> Is it possible to get the current used AST depending on the IDocument?
>
> Thanks for help.
>
>
>
>
Previous Topic:XML editor ala plugin.xml/MANIFEST.MF editor
Next Topic:How to generate Type from ITypeBinding?
Goto Forum:
  


Current Time: Tue Sep 17 14:11:48 GMT 2024

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

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

Back to the top