[英]Highlight a text displayed in eclipse editor area programatically
我正在開發一個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中實現這一目標?
提前致謝。
使它與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()); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.