Вопрос

I am now writing a small Eclipse Plugin.
In the plugin, the program will get the .java and line number in this .java
My question is How to use the file name and line number to highlight the entire line and jump to it?

For example, the program get template.java and line 38, how can I highlight the line 38 in template.java and jump to it?

I am new to eclipse plugin.
Please help
Thanks a lots

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top