如何在特定的光标偏移位置打开新的Eclipse编辑器

8
我希望能够通过编程实现上述操作。
我查看了如何获取Eclipse TextEditor中的光标位置Eclipse插件如何获取当前文本编辑器的光标位置,因此我知道如何从当前打开的编辑器中获取光标偏移量。然而,我正在尝试在由我编程打开的新编辑器中设置光标偏移量。
我目前打开新编辑器的方式如下:
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
                }
            }
        }
    }

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

谢谢!
2个回答

4
我使用了以下方法,比之前的答案更加简单。假设您有int offsetIWorkbenchPage pageIFile file(似乎它们在OP的问题中都存在):
ITextEditor editor = (ITextEditor) IDE.openEditor(page, file);
editor.selectAndReveal(offset, 0);

我从这个答案中找到了如何实现这个功能。(但是通过将selectAndReveal的第二个参数设置为零,没有文本被高亮显示)


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
        }
    }
}

可能不完全符合您的需求,但这可能会很有用。(//complain 取代了针对特定产品使用的错误处理代码)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接