Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Is there any utility which can return the line number where an IJavaElement occurs inside its source
Is there any utility which can return the line number where an IJavaElement occurs inside its source [message #296603] Tue, 27 December 2005 21:42 Go to next message
Eclipse UserFriend
Originally posted by: varavamu.yahoo.com

hi,
I am writing a plugin and would like to find the line number in the
underlying source where a given IJavaElement is present. How can I infer
this? - either using a utility class (if there's one) or from the
IJavaElement itself?

thanks in advance,
Vijay
Re: Is there any utility which can return the line number where an IJavaElement occurs inside its [message #296607 is a reply to message #296603] Tue, 27 December 2005 23:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: varavamu.yahoo.com

I came up with this, but it has a dependency on a 3.2 API:
private int getLineNumberInSource(IMember member) throws
JavaModelException {
if (member == null || !member.exists()) {
return -1;
}
ICompilationUnit compilationUnit = member.getCompilationUnit();
if (compilationUnit == null) {
return -1;
}
String fullSource = compilationUnit.getBuffer().getContents();
if (fullSource == null) {
return -1;
}
ISourceRange nameRange = member.getNameRange();
if (nameRange == null) {
return -1;
}
String string2 = fullSource.substring(0, nameRange.getOffset());
return
string2.split(compilationUnit.findRecommendedLineSeparator() ).length;
}

if there's some other way, or if this code has bugs (null checks
needed, etc.), I would really appreciate the heads-up.

thanks,
Vijay


Vijay Aravamudhan wrote:

> hi,
> I am writing a plugin and would like to find the line number in the
> underlying source where a given IJavaElement is present. How can I
> infer this? - either using a utility class (if there's one) or from
> the IJavaElement itself?
>
> thanks in advance,
> Vijay
Re: Is there any utility which can return the line number where an IJavaElement occurs inside its [message #296609 is a reply to message #296607] Tue, 27 December 2005 23:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

Try the following ..

public static int getLineNumber(IMember member) throws JavaModelException, BadLocationException
{
	ICompilationUnit compUnit = member.getCompilationUnit();
	Document document = new Document(compUnit.getBuffer().getContents());
		
	return document.getLineOfOffset(member.getSourceRange().getOffset());
}


I think it may be expensive to create Document, but do a trial run. I dont know whether it works or not.

Thanks
~Venkat
Re: Is there any utility which can return the line number where an IJavaElement occurs inside its [message #296626 is a reply to message #296609] Wed, 28 December 2005 08:39 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: varavamu.yahoo.com

hi Venkat,
Thanks for the tip - but I am not sure which Document class you are
referring to - the one in javax.text, or org.w3c.dom or something else?
Anyways - the solution that I found works for me - so I will probably
stick with that. but its nice to know...
thanks,
Vijay

Venkataramana M wrote:

>Try the following ..
>
>
>public static int getLineNumber(IMember member) throws JavaModelException, BadLocationException
>{
>	ICompilationUnit compUnit = member.getCompilationUnit();
>	Document document = new Document(compUnit.getBuffer().getContents());
>		
>	return document.getLineOfOffset(member.getSourceRange().getOffset());
>}
>

>
>I think it may be expensive to create Document, but do a trial run. I dont know whether it works or not.
>
>Thanks
>~Venkat
>
>
Re: Is there any utility which can return the line number where an [message #296627 is a reply to message #296626] Wed, 28 December 2005 09:49 Go to previous message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

I was referring to org.eclipse.jface.text.Document which I think is extensively used in the implementation of JDT.
Previous Topic:Where's IPropertySource?
Next Topic:TableViewer for list display
Goto Forum:
  


Current Time: Mon May 12 13:27:41 EDT 2025

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

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

Back to the top