简体   繁体   English

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

[英]How to get IFile handler to an active file in Eclipse editor

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). 我正在准备一个eclipse插件,用于检查测试套件中的代码质量(编译器错误/警告/语法检查由默认编译器完成)。 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). 如果测试代码中有错误,我们想通知测试套件开发人员,例如GOTO跳转到标签上方,这可能会导致无限循环(测试套件很旧,它们不是使用Java或任何正常语言编写的) 。

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). 我们想用特定行和特定消息报告IMarker警告(我在某些ArrayList中同时包含行和消息,现在我只需要将它们放在打开的文件中)。 But I'm unable to get the IFile handler for opened file (not in any project, just an active tab in editor pane). 但是我无法获取打开文件的IFile处理程序(不在任何项目中,只是编辑器窗格中的活动选项卡)。

How do i get an IFile handler for active file in editor pane in Eclipse? 如何在Eclipse的编辑器窗格中为活动文件获取IFile处理程序?

Following code results in exception: 以下代码导致异常:

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

A console output (exception): 控制台输出(例外):

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)

Not all editors are actually editing IFile objects, some can be editing files which are not in the workspace. 并非所有的编辑器实际上都在编辑IFile对象,有些可以编辑不在工作区中的文件。 These editors use an editor input which is not based on IFileEditorEditor . 这些编辑器使用的编辑器输入不基于IFileEditorEditor

In the case you have the input is FileStoreEditorInput which implements IURIEditorInput which just gives you the URI of the file being edited. 如果您有输入,则为FileStoreEditorInput ,它实现IURIEditorInput ,该IURIEditorInput仅提供您要编辑的文件的URI。

You can use code something like the following to try and get the IFile from the editor input: 您可以使用类似以下的代码来尝试从编辑器输入中获取IFile:

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;
}

The returned IFile may be null if the editor is not editing a workspace file. 如果编辑器未编辑工作空间文件,则返回的IFile可能为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM