Eclipse-plugin如何获取当前文本编辑器的光标位置

时间:2009-10-25 00:46:38

标签: eclipse-plugin text-editor cursor-position

我尝试在编辑器的文本光标位置显示弹出对话框。如何在此时获取活动编辑器(Point)和show popup对话框的文本光标位置?

2 个答案:

答案 0 :(得分:2)

我不确定你在“show popup dialog at 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.
  }
}

当然,在生产代码中进行空检查等。

答案 1 :(得分:-1)

您可以使用AbstractTextEditor

getCursorPosition()方法
相关问题