[NEWBIE] How to determine the line-number of a code-fragment? [message #3832] |
Thu, 24 April 2003 13:32 |
Philipp Bouillon Messages: 46 Registered: July 2009 |
Member |
|
|
Hi,
could anybody please provide me with information on how to find a line
number of a code fragment in a corresponding Java-resource-file?
In my program, I have a IJavaElement in an ICompilationUnit. This
IJavaElement has a Source-Range, but I would like to set a
JavaLineBreakpoint (programatically) at the line of the statement.
The only way I found of determining the line is -- well -- funny
(offset is the offset of the source fragment, window is an
IWorkbenchWindow):
// First, create a new TextSelection
TextSelection select = new TextSelection(offset, 1);
// Now, set this TextSelection in the active editor
window.getActivePage().getActiveEditor().getEditorSite().
getSelectionProvider().setSelection(select);
// And retrieve the selection again.
ISelection sel = window.getActivePage().getActiveEditor().getEditorSite().
getSelectionProvider().getSelection();
// Now, the lineNumber can be retrieved.
int lineNumber = ((TextSelection) sel).getStartLine();
// Clear the selection again...
window.getActivePage().getActiveEditor().getEditorSite().
getSelectionProvider().setSelection(TextSelection.emptySelec tion());
Apart from being ugly, this code only works, if the file that contains my
source fragment is currently open in the active editor...
Now, there has to be a better way of retrieving the line number, right?
Thanks for your help.
Philipp Bouillon
|
|
|
|
Re: [NEWBIE] How to determine the line-number of a code-fragment? [message #4213 is a reply to message #3845] |
Thu, 24 April 2003 15:50 |
Eclipse User |
|
|
|
Originally posted by: linnet.nospam.users.sourceforge.net
Adam Kiezun wrote:
> i see 2 easy ways:
> - parse the file and call CompilationUnit.lineNumber(position), or
> - create an IDocument for the file and call IDocument.getLineOfOffset
>
> a.
> --
> eclipse.org
>
>
Hi Adam,
Which of these ways is the best performancewise if you only want to
obtain the line number?
Best regards,
Jesper
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03131 seconds