我正在开发一个Eclipse插件,可以通过编程从工作区修改C++文件。我现在正试图保存所做的更改。我看了一下这个解决方案:
How can I call save method in eclipse plugin development programmatically
但这包括在编辑器中打开我想要保存的文件。因为我有
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)
因此,我的问题是:
编辑:
我努力实现的示例
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);
}
编辑:
虽然这不能回答问题(不使用编辑器),但我的行为是正确的:
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);
}
但是,这会为每个文件打开一个编辑器页面并立即关闭,因此对于大量文件来说,性能并不令人满意。