Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » [NEWBIE] How to determine the line-number of a code-fragment?
[NEWBIE] How to determine the line-number of a code-fragment? [message #3832] Thu, 24 April 2003 13:32 Go to next message
Philipp Bouillon is currently offline Philipp BouillonFriend
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 #3845 is a reply to message #3832] Thu, 24 April 2003 14:14 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: adam_kiezun.ch.ibm.spam.protection.com

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
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 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: [NEWBIE] How to determine the line-number of a code-fragment? [message #4424 is a reply to message #4213] Thu, 24 April 2003 16:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: adam_kiezun.ch.ibm.spam.protection.com

i have no idea - the best way to find out (as always with performance) is to measure it yourself :-)

a.
--
eclipse.org
Re: [NEWBIE] How to determine the line-number of a code-fragment? [message #6475 is a reply to message #4213] Sat, 26 April 2003 15:32 Go to previous message
Martin Aeschlimann is currently offline Martin AeschlimannFriend
Messages: 71
Registered: July 2009
Member
If your file is open in the editor, the best way is to get the editor's
underlying document.

Assuming you have a ICompilationUnit cu in hand:

IFile correspondingFile;
if (cu.isWorkingCopy() {
correspondingFile= (IFile) cu.getOriginalElement().getResource();
} else {
correspondingFile= (IFile) cu.getResource();
}

IDocument doc=
JavaUI.getDocumentProvider().getDocument(
new FileEditorInput(correspondingFile));


....doc.getLineOfOffset(..)...

Martin

Jesper Kamstrup Linnet wrote:
> 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
>
Previous Topic:Multiple Consoles
Next Topic:AST.parseCompilationUnit -> Unhandled exception caught in event loop
Goto Forum:
  


Current Time: Wed Feb 05 13:02:21 GMT 2025

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

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

Back to the top