获取eclipse插件中的行号

获取eclipse插件中的行号,eclipse,eclipse-plugin,eclipse-rcp,Eclipse,Eclipse Plugin,Eclipse Rcp,我正在为我的插件寻找正确的扩展点,以访问编辑器中的光标位置。目的是在插件视图中为当前代码行提供附加信息。 我确信这是可能的,因为Outline视图突出显示了我所在的当前函数。 thx以下代码符合我的要求 import org.eclipse.ui.IWorkbench; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IEditorPart; import

我正在为我的插件寻找正确的扩展点,以访问编辑器中的光标位置。目的是在插件视图中为当前代码行提供附加信息。 我确信这是可能的,因为Outline视图突出显示了我所在的当前函数。
thx

以下代码符合我的要求

import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.ISelection;        

IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
IEditorPart editor = page.getActiveEditor();
if(editor instanceof ITextEditor){
    ISelectionProvider selectionProvider = ((ITextEditor)editor).getSelectionProvider();
    ISelection selection = selectionProvider.getSelection();
    if (selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection)selection;
        System.out.println("startline:"+textSelection.getStartLine());
    }
}

您可以从找到的模式中使用
textSelection.getOffset()
,然后使用
org.eclipse.jface.text.IDocument
接口从文档中提取文本,以执行您想要的任何分析

ITextEditor textEditor = (ITextEditor)editor;
IDocumentProvider dp = editor.getDocumentProvider();
IDocument doc = dp.getDocument(editor.getEditorInput());

i文档
具有在字符偏移量和行之间来回转换的方法。

可能:getCursorPosition,字符串可能包含格式信息,如何获取当前编辑器的句柄