I am developing a plugin that recommends code using eclipse plugin development environment (PDE). For now I'm working on designing the interface. The thing is I want to get the cursor location in the eclipse editor and open a JFrame at that position. I've tried to get the location with the help of documentation and forums and only able to get the offset till now or you can say line and column offset. I want to get it in a point(x,y) that represents a location. So any ideas how to get the cursor position?
Asked
Active
Viewed 1,248 times
1
-
what did you try?. – Khalil M Nov 22 '16 at 01:26
-
What objects do you already have access to? Just the editor? The viewer? It's main text widget? – nitind Nov 22 '16 at 05:39
-
I tried the following code from the answer in the link but it only gets me offset. http://stackoverflow.com/questions/1619623/eclipse-plugin-how-to-get-current-text-editor-corsor-position – Salman Javed Nov 23 '16 at 16:17
1 Answers
4
Assuming you have the StyledText
control for the editor use getCaretOffset
to get the caret offset:
StyledText text = ... get editor styled text
int caret = text.getCaretOffset();
Then call getLocationAtOffset
to get the x, y coordinates of the offset relative to the control:
Point point = text.getLocationAtOffset(caret);
If necessary you can convert this to be relative to the display:
point = text.toDisplay(point);
Note that Eclipse plugins normally use SWT, not Swing. It will be a lot more difficult to open a JFrame than a SWT control.
You can get the StyledText
for an ITextEditor
using:
StyledText text = (StyledText)editor.getAdapter(Control.class);

greg-449
- 109,219
- 232
- 102
- 145
-
I want to get the caret position from eclipse's editor in Point like you did above, not from a styled text. – Salman Javed Nov 23 '16 at 16:14
-
-
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); if (editor instanceof ITextEditor) { ISelectionProvider selectionProvider = ((ITextEditor)editor).getSelectionProvider(); ISelection selection = selectionProvider.getSelection(); if (selection instanceof ITextSelection) { ITextSelection textSelection;= (ITextSelection)selection; offset = textSelection.getOffset(); } } – Salman Javed Nov 23 '16 at 16:24
-
This's what I am doing right now, it returns offset.sorry for bad indentation. – Salman Javed Nov 23 '16 at 16:24
-
The text selection is still just giving you offsets in the styled text. I have added how to get the styled text from an editor. If you want information about the pixel positions of a selection you must deal with the styled text. – greg-449 Nov 23 '16 at 16:33
-
I'm not getting the point.You say get styled text editor from editor.How to get eclipse's styled text? – Salman Javed Nov 23 '16 at 16:40
-
There is no such thing as 'eclipse's styled text'. Each text editor uses a StyledText control to hold the text being editor. I have shown you how to get that control, how to convert an offset to a control relative position and a display relative position. – greg-449 Nov 23 '16 at 16:43