user196036
user196036

Reputation:

Eclipse-plugin how to get current text editor cursor position

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?

Upvotes: 5

Views: 6828

Answers (2)

thSoft
thSoft

Reputation: 22650

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.

Upvotes: 2

Alexandre Pauzies
Alexandre Pauzies

Reputation: 801

You can use the getCursorPosition() method of AbstractTextEditor

Upvotes: -1

Related Questions