get ASTNode from codeposition. [message #244761] |
Thu, 21 June 2007 08:21  |
Eclipse User |
|
|
|
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 03:49  |
Eclipse User |
|
|
|
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.
>
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.32272 seconds