2010-05-20 26 views
6

關於Eclipse的PDE發展的一個qustion:我寫一個Eclipse小插件,並具有以下 *的org.eclipse.ui.texteditor.ITextEditor *行號的Eclipe PDE:跳轉到行X和高亮

我怎麼能自動跳轉到那行和標記呢?可惜的是API似乎只支持文檔中的偏移量(請參閱:ITextEditor.selectAndReveal()),但沒有行號。

最好的是 - 雖然這並不工作:

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

這本可能以某種方式?我沒有找到解決方案

回答

5

的類DetailsView我發現下面的方法。

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()); 
    } 
    } 
}