如何将 IFile 处理程序获取到 Eclipse 编辑器中的活动文件

2022-01-16 00:00:00 eclipse-plugin java eclipse

我正在准备一个 Eclipse 插件,用于检查测试套件中的代码质量(编译器错误/警告/语法检查由默认编译器完成).如果测试代码有问题,我们想通知测试套件的开发人员,例如 GOTO 跳转越过标签,可能导致无限循环(测试套件非常旧,它们不是 Java 或任何普通语言).

i'm preparing an eclipse plugin which checks quality of code in out test suites (compiler errors/warnings/ syntax checks are done by default compiler). We'd like to inform a developer of test suite if something is wrong in test code, like GOTO jumps goes over the label and it may result in infinite loop (test suites are very old, they are not in java or any normal language).

我们想在特定行和特定消息中报告带有警告的 IMarker(我在某些 ArrayList 中同时获得了行和消息,现在我只需要将它们放在打开的文件中).但我无法获取打开文件的 IFile 处理程序(不在任何项目中,只是编辑器窗格中的一个活动选项卡).

We'd like to report IMarker with warnings in specific lines and specific message (I've got both line & message in some ArrayList, now i need just to put them on opened file). But I'm unable to get the IFile handler for opened file (not in any project, just an active tab in editor pane).

如何在 Eclipse 的编辑器窗格中获取活动文件的 IFile 处理程序?

How do i get an IFile handler for active file in editor pane in Eclipse?

以下代码导致异常:

IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
IWorkbenchPage page = win.getActivePage();
IEditorPart editor = page.getActiveEditor();
IFileEditorInput iFileInput = (IFileEditorInput) editor.getEditorInput(); //exception is here

控制台输出(异常):

java.lang.ClassCastException: org.eclipse.ui.ide.FileStoreEditorInput cannot be cast to org.eclipse.ui.IFileEditorInput
at se.ericsson.ttcnplugin.handlers.AltHandler.execute(AltHandler.java:45)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:820)
at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:806)
at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:796)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3540)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3161)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
at org.eclipse.equinox.launcher.Main.main(Main.java:1384)

推荐答案

并非所有编辑器都在实际编辑 IFile 对象,有些可能正在编辑不在工作区中的文件.这些编辑器使用不基于 IFileEditorEditor 的编辑器输入.

Not all editors are actually editing IFile objects, some can be editing files which are not in the workspace. These editors use an editor input which is not based on IFileEditorEditor.

如果您输入的是 FileStoreEditorInput,它实现了 IURIEditorInput,它只为您提供正在编辑的文件的 URI.

In the case you have the input is FileStoreEditorInput which implements IURIEditorInput which just gives you the URI of the file being edited.

您可以使用类似以下的代码来尝试从编辑器输入中获取 IFile:

You can use code something like the following to try and get the IFile from the editor input:

public static IFile getFileFromEditorInput(IEditorInput input)
{
  if (input == null)
    return null;

  if (input instanceof IFileEditorInput)
    return ((IFileEditorInput)input).getFile();

  IPath path = getPathFromEditorInput(input);
  if (path == null)
    return null;

  return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
}


public static IPath getPathFromEditorInput(IEditorInput input)
{
  if (input instanceof ILocationProvider)
    return ((ILocationProvider)input).getPath(input);

  if (input instanceof IURIEditorInput)
   {
     URI uri = ((IURIEditorInput)input).getURI();
     if (uri != null)
      {
        IPath path = URIUtil.toPath(uri);
        if (path != null)
          return path;
      }
   }

  return null;
}

如果编辑器没有编辑工作区文件,则返回的 IFile 可能为空.

The returned IFile may be null if the editor is not editing a workspace file.

相关文章