Eclipe PDE: Jump to line X and highlight it
Asked Answered
D

2

6

A qustion about Eclipse PDE development: I write a small plugin for Eclipse and have the following * an org.eclipse.ui.texteditor.ITextEditor * a line number

How can I automatically jump to that line and mark it? It's a pity that the API seems only to support offsets (see: ITextEditor.selectAndReveal()) within the document but no line numbers.

The best would be - although this doesn't work:

ITextEditor editor = (ITextEditor)IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file, true );
editor.goto(line);
editor.markLine(line);

It this possible in some way? I did not find a solution

Dissonance answered 20/5, 2010 at 12:58 Comment(0)
I
5

on the class DetailsView I found the following method.

private static void goToLine(IEditorPart editorPart, int lineNumber) {
  if (!(editorPart instanceof ITextEditor) || lineNumber <= 0) {
    return;
  }
  ITextEditor editor = (ITextEditor) editorPart;
  IDocument document = editor.getDocumentProvider().getDocument(
    editor.getEditorInput());
  if (document != null) {
    IRegion lineInfo = null;
    try {
      // line count internaly starts with 0, and not with 1 like in
      // GUI
      lineInfo = document.getLineInformation(lineNumber - 1);
    } catch (BadLocationException e) {
      // ignored because line number may not really exist in document,
      // we guess this...
    }
    if (lineInfo != null) {
      editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
    }
  }
}
Infra answered 25/7, 2010 at 13:55 Comment(0)
C
1

Even though org.eclipse.ui.texteditor.ITextEditor deals wiith offset, it should be able to take your line number with the selectAndReveal() method.

See this thread and this thread.

Try something along the line of:

((ITextEditor)org.eclipse.jdt.ui.JavaUI.openInEditor(compilationUnit)).selectAndReveal(int, int);
Cohn answered 20/5, 2010 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.