如何使用特定的光标偏移位置打开一个新的eclipse编辑器

aka*_*spi 7 java eclipse-plugin editor

我想以编程方式执行上述操作.

我看了如何在eclipse中获取光标位置TextEditorEclipse-plugin如何获取当前文本编辑器的corsor位置,所以我知道如何从当前的打开编辑器中获取光标偏移量.但是,我正在尝试在我以编程方式打开的新编辑器中设置光标偏移量.

我目前打开新编辑器的方式如下:

IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = win.getActivePage();
    if (page != null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
            IEditorInput input = editor.getEditorInput();
            if (input instanceof IFileEditorInput) {
                String fileLocation = ((IFileEditorInput) input).getFile().getLocation().toOSString();
                String newFileLocartion = generateNewFileLocation(fileLocation);
                File file = new File(newFileLocartion);
                IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI());
                try {
                    IDE.openEditorOnFileStore(page, fileStore);
                } catch (PartInitException e) {
                    // TODO error handling
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

有没有办法打开设置新编辑器以特定偏移量打开(假设我已经提前知道了偏移量)?

谢谢!

dbr*_*nk0 3

它使用此代码片段导航到文件中的指定行。

public static void navigateToLine(IFile file, Integer line)
{
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(IMarker.LINE_NUMBER, line);
    IMarker marker = null;
    try {
        marker = file.createMarker(IMarker.TEXT);
        marker.setAttributes(map);
        try {
            IDE.openEditor(getActivePage(), marker);
        } catch ( PartInitException e ) {
            //complain
        }
    } catch ( CoreException e1 ) {
        //complain
    } finally {
        try {
            if (marker != null)
                marker.delete();
        } catch ( CoreException e ) {
            //whatever
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

可能不完全是您所需要的,但可能很有用。(//complain 替换特定于使用此功能的产品的错误处理代码)