AGenevray
AGenevray

Reputation: 13

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

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.

Upvotes: 1

Views: 1574

Answers (1)

HighCommander4
HighCommander4

Reputation: 52739

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

Upvotes: 1

Related Questions