本文介绍了如何将IFile处理程序在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).

我们想以特定的行和具体的消息(我已经在一些ArrayList中有线和消息)来报告IMarker,现在我只需要把它们放在打开的文件上)。但是我无法获取打开的文件的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).

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

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 如果编辑器没有编辑工作区文件,则可能为null。

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

这篇关于如何将IFile处理程序在Eclipse编辑器中的活动文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:26