This is code adapted from the standard Goto Line
action:
private void gotoLine(int line) {
ITextEditor editor = getTextEditor();
// TODO check for null editor
IDocumentProvider provider = editor.getDocumentProvider();
IDocument document = provider.getDocument(editor.getEditorInput());
try {
int start = document.getLineOffset(line);
int length = document.getLineLength(line);
editor.selectAndReveal(start, length);
// Ensure editor is active - you may not need this
IWorkbenchPage page = editor.getSite().getPage();
page.activate(editor);
} catch (BadLocationException x) {
// ignore
}
}
This needs a getTextEditor()
method, which could be something like
ITextEditor getTextEditor()
{
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = page.getActiveEditor();
if (editor instanceof ITextEditor) {
return (ITextEditor)editor;
}
return null;
}
depending on how your plugin runs there might be better ways of determining the text editor.