在Eclipse插件中打开文件到某一行 [英] Open file to a certain line in Eclipse plugin
本文介绍了在Eclipse插件中打开文件到某一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个插件,当按下按钮时必须在某一行打开文件。
我有以下代码在某一行打开文件。
I am writting a plugin that has to open a file at a certain line when a button is pressed. I have the following code to open the file at a certain line.
String filePath = "file path" ;
final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath));
if (inputFile != null) {
IWorkbenchPage page1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart openEditor11 = IDE.openEditor(page1, inputFile);
}
int Line = 20;
if (openEditor11 instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) openEditor ;
IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
textEditor.selectAndReveal(document.getLineOffset(Line - 1), document.getLineLength(Line-1));
}
我的问题是if语句中的变量openEditor11给出错误:openEditor11无法解析为变量。可能是什么问题?
My problem is that the variable openEditor11 in the if statement gives the error: openEditor11 cannot be resolved to a variable. What can be the problem ?
推荐答案
根据 https://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_on_a_file_in_the_workspace%3F
int lineNumber = ...
IPath path = ...
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
IMarker marker = file.createMarker(IMarker.TEXT);
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IDE.openEditor(page, marker);
marker.delete();
这篇关于在Eclipse插件中打开文件到某一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文