java - 如何打开一个新的具有特定光标偏移位置的eclipse编辑器

标签 java eclipse-plugin editor

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

我看了How to get cursor position in an eclipse TextEditorEclipse-plugin how to get current text editor corsor position所以我有点知道如何从当前打开的编辑器中获取光标偏移量。但是,我正在尝试在我以编程方式打开的新编辑器中设置光标偏移量。

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

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

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

谢谢!

最佳答案

我使用了下面的,它比之前的答案更简单。假设您有 int offsetIWorkbenchPage pageIFile file(而且它们似乎都存在于 OP 的问题中):

ITextEditor editor = (ITextEditor) IDE.openEditor(page, file);
editor.selectAndReveal(offset, 0);

我从 this answer. 找到了如何做到这一点(但是通过将 selectAndReveal 的第二个参数设置为零,不会突出显示任何文本)

关于java - 如何打开一个新的具有特定光标偏移位置的eclipse编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257105/

相关文章:

java - 有没有办法捕获某种类型的所有功能接口(interface)?

java - JPA 得到分离的结果

java 邮件存储类型 "imap"与 "imaps"

mysql - 我应该将富文本编辑器编辑的哪种类型的数据存储在 MySql 中?

javascript - 使用脚本在窗口中打开链接(Google Sheet)

java - eclipse中JRE系统库的概念

eclipse - 如何在Eclipse中扩展源菜单? (或 : What is its locationURI? )

java - 自定义 Eclipse 内容辅助的键绑定(bind)

java - 访问 EclipsePreferences 的线程安全方式(项目)

python - 用于在 pycharm 中切换终端 session 的键盘快捷键