我想以编程方式执行上述操作。
我看了How to get cursor position in an eclipse TextEditor和 Eclipse-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 offset
、IWorkbenchPage page
和 IFile 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/