Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Use of getSourceRange
Use of getSourceRange [message #635410] Tue, 26 October 2010 17:18 Go to next message
Chris  is currently offline Chris Friend
Messages: 5
Registered: October 2010
Junior Member
Hello,

I'm new to using Eclipse plugin development and JDT and so am making slow progress and naive mistakes.

What I want to do is allow an end user to select a variable in the Java source editor and then highlight that variable (e.g. x) and other variables that x is assigned to and then the variables they are assigned to (reaching definitions).

I have an existing tool that deals with the reaching definition idea and so I thought I could is extract the variable identifier (perhaps using JDT) then use my tool to find what it assigns to and then use JDT to find that identifier and its source code offset and length. I would then somehow get JDT ui to highlight these identifiers in the editor.

As a start I have some handler code to get the offset of the text selected, extract IJavaElement array and then see if we have an ISourceReference in order to get the variable's offset. I notice that the offset is the variable's declaration position rather that its use position. So clearly I'm taking the wrong approach.

Here's the code so far:

IJavaElement[] elements = unit.codeSelect(textSelected.getOffset(), 0);

// There should only be one element
if (elements.length > 0){
	if (elements[0] instanceof ISourceReference){
		ISourceReference variable = (ISourceReference)elements[0];
							
	System.out.println("Source range for selected local variable is: offset: " + 
									variable.getSourceRange().getOffset() +
									" length: " + variable.getSourceRange().getLength());
							
						}
					}


Any suggestions would be appreciated.

Chris
PS It would be great if the Eclipse Series had a book on JDT
Re: Use of getSourceRange [message #635507 is a reply to message #635410] Wed, 27 October 2010 07:20 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
You should probably use DOM/AST for this. Model is not good to parse method statements. Look at this article which could help you to get started on DOM/AST. http://www.eclipse.org/articles/article.php?file=Article-Jav aCodeManipulation_AST/index.html
Re: Use of getSourceRange [message #635703 is a reply to message #635507] Wed, 27 October 2010 18:47 Go to previous message
Chris  is currently offline Chris Friend
Messages: 5
Registered: October 2010
Junior Member
Many thanks that's helpful
Chris
Previous Topic:Eclipse SDK for "hpux.motif.ia64" 64 bit
Next Topic:Dynamic Website Problem
Goto Forum:
  


Current Time: Thu Apr 25 11:55:56 GMT 2024

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

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

Back to the top