1
votes

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.

1
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
not answering your question using org.eclipse.ui.ide.IDE.saveAllEditors(IResource[], boolean) is probably the best way to save editors. - Jonah Graham
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
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 minimal reproducible example that would be helpful! - Jonah Graham
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

1 Answers

1
votes

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