【问题标题】:How can I retrieve the contents a multi page editor plugin when selecting "Run as" in Eclipse Plugin Development?在 Eclipse 插件开发中选择“运行方式”时,如何检索多页编辑器插件的内容?
【发布时间】:2018-06-25 02:43:00
【问题描述】:

需要一个 Eclipse 插件来允许用户输入脚本并通过 IDE 运行该脚本。为此,我使用多页编辑器模板实现了一个 Eclipse 插件。我添加了扩展org.eclipse.debug.ui.launchShortcuts 并提供了对我的接口org.eclipse.debug.ui.ILaunchShortcut 的实现的引用。该接口需要提供这些方法的实现:

public void launch(final ISelection selection, final String mode);
public void launch(final IEditorPart editor, final String mode);

当用户 - 例如我 - 右键单击​​项目资源管理器中的文件,选择“运行方式”并选择我提供的启动器,然后执行此启动方法。第一种方法 - 即ISelection 版本 - 在从项目资源管理器中选择文件时被命中。第二种方法 - 即IEditorPart 版本 - 在右键单击编辑器区域并通过上下文菜单中的“运行方式”选项选择启动器时被命中。

我现在需要了解如何检索该编辑器页面的内容。在第一种方法中,我相信我可以使用以下代码检索文件名:

IFile file = (IFile) ((IStructuredSelection) selection).getFirstElement();
String path = file.getFullPath().makeAbsolute().toString();

但是,该路径与项目的根目录相关。我不知道如何检索项目根目录的路径。

由于我不知道如何根据IEditorPart找到路径,所以第二种方法的实现问题更大。

希望你能帮上忙。非常感谢

【问题讨论】:

    标签: eclipse-plugin eclipse-pde


    【解决方案1】:

    如果您询问如何获取编辑器当前正在编辑的文件,您可以使用以下内容:

    IPath filepath = null;
    
    IEditorInput input = editor.getEditorInput();
    
    IFile file = input.getAdapter(IFile.class);
    if (file != null) {
       filepath = file.getFullPath();
    }
    
    if (filepath == null) {
        ILocationProvider locationProvider = input.getAdapter(ILocationProvider.class);
        if (locationProvider != null) {
            filepath = locationProvider.getPath(input);
        }
    }
    

    尝试直接获取IFile frpm 编辑器,如果失败则尝试获取位置提供程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多