1

I am developing an Eclipse plug-in that programmatically modifies C++ files from the workspace. I am now trying to save the changes made. I have taken a look at this solution : How can I call save method in eclipse plugin development programmatically but this includes having the files I want to save open in the editor. Since I have 5k+ files to modify and save, I cannot afford to open them all in an editor before saving.

While looking for a solution I found this topic on the official Eclipse forum : https://www.eclipse.org/forums/index.php/t/1070377/ . Unfortunately, no answer has been provided.

I have tried using :

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

or

IWorkspace.save(boolean, IProgressMonitor)

or

ITranslationUnit.save(IProgressMonitor, boolean)

or

ICProject.save(IProgressMonitor, boolean)

but none of these solution worked. My question is therefore :

How to save files programmatically in Eclipse without using an editor ?

Thanks a lot in advance

EDIT :

example of what I'm trying to achieve

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

At the end of this execution, I would expect the new version of the file to be saved on the disk. The function is correctly deleted and the modifications appear in the IDE if I open the file in the editor, but the file is marked as unsaved by Eclipse (showing a star before the file name).

EDIT :

While this doesn't answer the question (without using an editor), I got the correct behaviour with :

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

However this opens an editor page for each file and closes it immediately, so the performance are not satisfying for a large volume of files.

AGenevray
  • 13
  • 5
  • More info needed please. If you don't have the file open in an editor, what are you trying to save and how have you loaded the files? i.e. where are the edits you are trying to save stored. – Jonah Graham Jul 24 '17 at 14:01
  • *not answering your question* using org.eclipse.ui.ide.IDE.saveAllEditors(IResource[], boolean) is probably the best way to save editors. – Jonah Graham Jul 24 '17 at 14:01
  • The plugin is deleting some specific portions of code via `ISourceManipulation.delete`. The plugin parses all the files in a given project via the Eclipse index (`IIndex`), and deletes the relevant code, all of this happening in the background. So the files where some code has been deleted are not in sync with the disk, but they are not open in an editor. – AGenevray Jul 24 '17 at 14:07
  • Are the files updated on disk, but not refreshed in the IDE? If so org.eclipse.core.resources.IResource.refreshLocal(int, IProgressMonitor) may be what you want. But I suspect that you are not committing your change, is that the issue? If you can provide an [MCVE] that would be helpful! – Jonah Graham Jul 24 '17 at 14:16
  • 1
    BTW to answer "How to save files programmatically in Eclipse without using an editor ?" is that it depends, that is why I am asking these follow-ups to get to a real answer. – Jonah Graham Jul 24 '17 at 14:19
  • That's actually the opposite, the modifications are showing in the IDE, but the files are not updated on disk. I added a minimal example, I hope it is complete enough. – AGenevray Jul 24 '17 at 14:38
  • How are you *opening* those files for modification? – nitind Jul 24 '17 at 18:51

1 Answers1

1

It sounds like the problem might be that your operation is modifying the working copy of a file, and not the underlying file itself.

Changes to the working copy can be synced to the underlying file by calling IWorkingCopy.commit().

Does the following help?

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

ITranslationUnit tu = functionToDelete.getTranslationUnit();
if (tu.isWorkingCopy()) {
    boolean forceCommit = true;
    ((IWorkingCopy) tu).commit(forceCommit, progressMonitor);
}
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • It worked ! I knew I was modifying a working copy, but I thought it was commited by the eclipse framework at some point after the delete. My bad, thanks a lot ! – AGenevray Jul 25 '17 at 07:50