[英]eclipse plugin: How to programmatically select text in editor?
我想以编程方式跳转到文本编辑器中的某个位置并突出显示代码。
我无法得到Andrew在Eclipse 3.7中的工作答案 。 编译器给出了这个错误:
The method getSourceViewer() from the type AbstractTextEditor is not visible.
但是,我能够使用selectAndReveal()
方法:
IFile myfile = ...
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ITextEditor editor = (ITextEditor) IDE.openEditor(page, myfile);
editor.selectAndReveal(offset, length);
如果您已经拥有当前编辑器的句柄,那么您可以执行以下操作:
editor.getSourceViewer().setSelectedRange(offset, length);
如果你没有当前编辑器的句柄,那么你需要做一些工作来实现(假设一个文本编辑器):
TextEditor editor = (TextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
虽然这会奏效,但我简化了一些事情。
TextEditor
,因此您将要执行instanceof测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.