【问题标题】:How to save files programmatically in Eclipse without using an editor?如何在不使用编辑器的情况下在 Eclipse 中以编程方式保存文件?
【发布时间】:2017-12-30 03:35:13
【问题描述】:

我正在开发一个 Eclipse 插件,它以编程方式从工作区修改 C++ 文件。我现在正在尝试保存所做的更改。我看过这个解决方案:How can I call save method in eclipse plugin development programmatically,但这包括在编辑器中打开我要保存的文件。既然我有 5k+ 文件要修改和保存,我不能在保存之前在编辑器中打开它们。

在寻找解决方案时,我在 Eclipse 官方论坛上找到了这个主题:https://www.eclipse.org/forums/index.php/t/1070377/。不幸的是,没有提供任何答案。

我尝试过使用:

PlatformUI.getWorkbench().getService(IHandlerService.class).executeCommand("org.eclipse.ui.file.saveAll", event)

IWorkspace.save(boolean, IProgressMonitor)

ITranslationUnit.save(IProgressMonitor, boolean)

ICProject.save(IProgressMonitor, boolean)

但是这些解决方案都没有奏效。 因此,我的问题是:

如何在不使用编辑器的情况下在 Eclipse 中以编程方式保存文件?

提前非常感谢

编辑:

我正在努力实现的示例

deleteAndSave(IFunction functionToDelete) {
    boolean forceDeletion = true;
    IProgressMonitor progressMonitor = new NullProgressMonitor();
    //delete the portion of code
    functionToDelete.delete(forceDeletion, progressMonitor);
    //we get the file containing the function
    IFile file = (IFile) functionToDelete.getUnderlyingResource();
    //save the file to the disk (ideally 'file.save()' ?)
    file.getWorkspace().save(true, progressMonitor);
}

在此执行结束时,我希望新版本的文件保存在磁盘上。如果我在编辑器中打开文件,该函数被正确删除并且修改出现在 IDE 中,但该文件被 Eclipse 标记为未保存(在文件名前显示一个星号)。

编辑:

虽然这不能回答问题(不使用编辑器),但我得到了正确的行为:

deleteAndSave(IFunction functionToDelete) {
    IProgressMonitor progressMonitor = new NullProgressMonitor();
    IWorkbenchPage activePage = PlatformUI
        .getWorkbench()
        .getActiveWorkbenchWindow()
        .getActivePage();

    functionToDelete.delete(true, progressMonitor);

    IFile file = (IFile) functionToDelete.getUnderlyingResource();
    IEditorPart editorPart = IDE.openEditor(activePage, file);
    editorPart.doSave(progressMonitor);
    activePage.closeEditor(editorPart, false);
}

但是,这会为每个文件打开一个编辑器页面并立即将其关闭,因此对于大量文件,性能并不令人满意。

【问题讨论】:

  • 请提供更多信息。如果您没有在编辑器中打开文件,您要保存什么以及如何加载文件?即您尝试保存的编辑存储在哪里。
  • 不回答您的问题 使用 org.eclipse.ui.ide.IDE.saveAllEditors(IResource[], boolean) 可能是保存编辑器的最佳方式。
  • 插件正在通过ISourceManipulation.delete删除一些特定的代码部分。该插件通过 Eclipse 索引 (IIndex) 解析给定项目中的所有文件,并删除相关代码,所有这些都在后台发生。所以一些代码被删除的文件与磁盘不同步,但它们并没有在编辑器中打开。
  • 文件是否在磁盘上更新,但在 IDE 中没有刷新?如果是这样 org.eclipse.core.resources.IResource.refreshLocal(int, IProgressMonitor) 可能是你想要的。但我怀疑你没有提交你的改变,这是问题吗?如果你能提供一个minimal reproducible example 那会很有帮助!
  • 顺便回答“如何在不使用编辑器的情况下在 Eclipse 中以编程方式保存文件?”这取决于,这就是为什么我要求这些后续行动得到真正的答案。

标签: java eclipse eclipse-plugin eclipse-rcp eclipse-cdt


【解决方案1】:

听起来问题可能是您的操作正在修改文件的工作副本,而不是基础文件本身。

可以通过调用IWorkingCopy.commit() 将工作副本的更改同步到基础文件。

以下有帮助吗?

...
functionToDelete.delete(forceDeletion, progressMonitor);

ITranslationUnit tu = functionToDelete.getTranslationUnit();
if (tu.isWorkingCopy()) {
    boolean forceCommit = true;
    ((IWorkingCopy) tu).commit(forceCommit, progressMonitor);
}

【讨论】:

  • 成功了!我知道我正在修改一个工作副本,但我认为它是由 Eclipse 框架在删除后的某个时间点提交的。我的错,非常感谢!
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2013-11-18
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多