我正在开发一个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中实现这一目标?
提前致谢。
答案 0 :(得分: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()); } }