Eclipse-plugin how to get current text editor cursor position

前端 未结 2 1851
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 19:24

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 po

相关标签:
2条回答
  • 2020-12-06 19:56

    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.

    0 讨论(0)
  • 2020-12-06 19:58

    You can use the getCursorPosition() method of AbstractTextEditor

    0 讨论(0)
提交回复
热议问题