我正在编写一个Eclipse插件。它对Java代码进行代码分析,并使用ListViewer报告违反某些标准的代码行。当有人在listviewer中选择其中一行时,我希望打开Eclipse Java文件编辑器并跳转到该行。代码分析的方式是,每个违规都有一个字段,保存违规发生的文件作为标准java.io.File。因此,我需要将其转换为IFile,以便在Eclipse Java编辑器中打开它。
以下是用户单击listviewer中的某行时运行的大部分代码。Violation是我创建的类,用于表示违反某些标准的特定Java代码行:
运行此代码时,我在调用IDE.openEditor这一行(对应CalisthenicsView.java的第75行)时遇到了IllegalArgumentException异常。
我已经查看了FileEditorInput.java的代码,并定位到抛出此错误的源代码行号为218。显然,该代码行执行以下操作(其中“file”与第一个代码块中的“toOpen”相同):
对我来说,似乎.getFile(path)返回的是一个没有locationURI的IFile。我不知道为什么会这样。
有没有办法解决这个问题?是否有其他方法可以实现这一目标(打开Java编辑器到特定文件的特定行)?请注意,由于此分析可以在工作区中的任何项目甚至整个工作区上运行,因此我无法访问IProject。
编辑:我不理解的另一件事是试图调用FileEditorInput.getPaht()的代码。查看WorkbenchPage.busyOpenEditor的第3163行:
这是调用FileEditorInput.getPath()的代码。显然它正在尝试打开称为“外部编辑器”的东西,这听起来不像我想要做的事情。我只想打开默认的Java源代码编辑器。这听起来像是它正在尝试打开其他东西。
另外,我想补充一下,toOpen(我要打开的IFile)的.toString()值是: L/Programming/runtime-New_configuration/slackbot-resistance/src/main/java/com/chairbender/slackbot/resistance/ResistanceBot.java
所以它绝对不是null或无效的,并且我会认为Eclipse知道它是一个Java文件。
以下是用户单击listviewer中的某行时运行的大部分代码。Violation是我创建的类,用于表示违反某些标准的特定Java代码行:
Violation selectedViolation = <I get this from the ListViewer>
IPath path = new Path(violation.getSourceFile().getAbsolutePath());
IFile toOpen = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart == null) {
IWorkbenchPage curPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
editorPart = IDE.openEditor(curPage, toOpen, true);
}
运行此代码时,我在调用IDE.openEditor这一行(对应CalisthenicsView.java的第75行)时遇到了IllegalArgumentException异常。
at org.eclipse.ui.part.FileEditorInput.getPath(FileEditorInput.java:218)
at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:3163)
at org.eclipse.ui.internal.WorkbenchPage.access$25(WorkbenchPage.java:3149)
at org.eclipse.ui.internal.WorkbenchPage$10.run(WorkbenchPage.java:3131)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:3126)
at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:3090)
at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:3080)
at org.eclipse.ui.ide.IDE.openEditor(IDE.java:541)
at org.eclipse.ui.ide.IDE.openEditor(IDE.java:500)
at com.chairbender.eclipse.object_calisthenics_analyzer.views.CalisthenicsView$1.selectionChanged(CalisthenicsView.java:75)
我已经查看了FileEditorInput.java的代码,并定位到抛出此错误的源代码行号为218。显然,该代码行执行以下操作(其中“file”与第一个代码块中的“toOpen”相同):
final URI locationURI = file.getLocationURI();
if (locationURI == null)
throw new IllegalArgumentException();
对我来说,似乎.getFile(path)返回的是一个没有locationURI的IFile。我不知道为什么会这样。
有没有办法解决这个问题?是否有其他方法可以实现这一目标(打开Java编辑器到特定文件的特定行)?请注意,由于此分析可以在工作区中的任何项目甚至整个工作区上运行,因此我无法访问IProject。
编辑:我不理解的另一件事是试图调用FileEditorInput.getPaht()的代码。查看WorkbenchPage.busyOpenEditor的第3163行:
// Special handling for external editors (they have no tabs...)
if ("org.eclipse.ui.systemExternalEditor".equals(editorId)) { //$NON-NLS-1$
IPathEditorInput fileInput = getPathEditorInput(input);
if (fileInput == null) {
throw new PartInitException(WorkbenchMessages.EditorManager_systemEditorError);
}
String fullPath = fileInput.getPath().toOSString();
Program.launch(fullPath);
return null;
}
这是调用FileEditorInput.getPath()的代码。显然它正在尝试打开称为“外部编辑器”的东西,这听起来不像我想要做的事情。我只想打开默认的Java源代码编辑器。这听起来像是它正在尝试打开其他东西。
另外,我想补充一下,toOpen(我要打开的IFile)的.toString()值是: L/Programming/runtime-New_configuration/slackbot-resistance/src/main/java/com/chairbender/slackbot/resistance/ResistanceBot.java
所以它绝对不是null或无效的,并且我会认为Eclipse知道它是一个Java文件。
getFileForLocation
方法,通过绝对路径在工作空间中查找文件的功能。 - greg-449getFileForLocation
绝对可行。在核心 Eclipse 代码中至少有 10 处使用它。 - greg-449