Eclipse-plugin how to get current text editor cursor position
Asked Answered
C

2

5

I try to show popup dialog at text cursor position of an editor. How can I get text cursor position in pixels of the active editor (Point) and a show popup dialog at this point?

Corona answered 25/10, 2009 at 0:46 Comment(0)
S
2

I'm not exactly sure what do you mean under "show popup dialog at this point", but do something like this:

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;
    int offset = textSelection.getOffset(); // etc.
  }
}

Of course, in production code do null checks etc.

Sacramentarian answered 11/2, 2010 at 0:19 Comment(1)
I need (X,Y) point (in pixels) to show a popup dialog and I want to show it under the text cursor like the code completion popup. In your code you get text offset in symbols, how to convert this offset to point in pixels. Or may be exists another way to show popup under the text cursor?Corona
C
-1

You can use the getCursorPosition() method of AbstractTextEditor

Columbine answered 12/11, 2009 at 15:55 Comment(2)
But that method is protected. How can one invoke it?Woodbine
Not only is it protected, it also returns a string "description of the cursor position", which is not what the OP was asking for at all.Anthropomorphize

© 2022 - 2024 — McMap. All rights reserved.