2013-05-31 80 views
1

我正在開發一個eclipse插件,其中我需要在eclipse編輯器中打開一個文本文件並以編程方式突出顯示文件內部的一行。以編程方式突出顯示在eclipse編輯器區域中的文本

要打開文件,並強調在eclipse編輯區文本/線,我用下面的代碼,

fileStore = EFS.getLocalFileSystem().getStore(file.toURI()); 
page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 
ITextEditor editor = (ITextEditor) IDE.openEditorOnFileStore(page, fileStore); 
editor.selectAndReveal(startOffset, endOffset); 

現在說我有一個文件,下面的內容

Line:1   xxxxxxxxxxxxxxxxxxx 
Line:2   yyyyyyyyyyyyyyyyyyyy 
: 
: 
Line: 20  aaaaaaaaaaaaaaaaaaaaa 
Line: 21  bbbbbbbbbbbbbbbbbbbbb 

現在我需要在上面的文件中突出顯示Line:20。爲此,我需要開始偏移和該行的結束偏移量。我如何在Java中實現這一點?

在此先感謝。

回答

1

得到它與selectAndReveal()方法工作:

IFile myfile = ... 
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 
ITextEditor editor = (ITextEditor) IDE.openEditor(page, myfile); 
editor.selectAndReveal(offset, length); 

編輯

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

如何GE偏移?我在這裏上吊。 – AJJ

+0

太好了。您編輯的解決方案工作! – AJJ

相關問題