If you already have a handle to the current editor, you can do:
editor.getSourceViewer().setSelectedRange(offset, length);
If you do not have a handle to the current editor, you need to do some work to get there (subject to a text editor):
TextEditor editor = (TextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow() .getActivePage().getActiveEditor();
Although this will work, I have simplified a few things.
- You need to make sure that the active editor is really a
TextEditor , so you want to make an instance of test - Sometimes the different parts of the long phrase above can be null (for example, at startup or shutdown). I tend to just wrap the expression in a try-catch block (NPE) and assume that if the NPE is thrown, then the editor is not available.
source share