Eclipse-plugin how to get current text editor cursor position

北城余情 提交于 2019-12-28 06:49:08

问题


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?


回答1:


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.




回答2:


You can use the getCursorPosition() method of AbstractTextEditor



来源:https://stackoverflow.com/questions/1619623/eclipse-plugin-how-to-get-current-text-editor-cursor-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!