Skip to content

Commit f029d57

Browse files
committed
add generic files.refactoring.autoSave and let users of the bulk edit service say if the setting should be honored
1 parent 79e49b2 commit f029d57

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

src/vs/editor/browser/services/bulkEditService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export interface IBulkEditOptions {
7575
undoRedoSource?: UndoRedoSource;
7676
undoRedoGroupId?: number;
7777
confirmBeforeUndo?: boolean;
78-
saveWhenDone?: boolean;
78+
respectAutoSaveConfig?: boolean;
7979
}
8080

8181
export interface IBulkEditResult {

src/vs/editor/contrib/codeAction/browser/codeActionCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export async function applyCodeAction(
173173
label: item.action.title,
174174
quotableLabel: item.action.title,
175175
code: 'undoredo.codeAction',
176-
saveWhenDone: true // TODO@jrieken make this configurable
176+
respectAutoSaveConfig: true
177177
});
178178
}
179179

src/vs/editor/contrib/rename/browser/rename.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class RenameController implements IEditorContribution {
239239
label: nls.localize('label', "Renaming '{0}' to '{1}'", loc?.text, inputFieldResult.newName),
240240
code: 'undoredo.rename',
241241
quotableLabel: nls.localize('quotableLabel', "Renaming {0} to {1}", loc?.text, inputFieldResult.newName),
242-
saveWhenDone: true // TODO@jrieken make this configurable
242+
respectAutoSaveConfig: true
243243
}).then(result => {
244244
if (result.ariaSummary) {
245245
alert(nls.localize('aria', "Successfully renamed '{0}' to '{1}'. Summary: {2}", loc!.text, inputFieldResult.newName, result.ariaSummary));

src/vs/workbench/contrib/bulkEdit/browser/bulkEditService.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
2424
import { ResourceMap, ResourceSet } from 'vs/base/common/map';
2525
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
2626
import { URI } from 'vs/base/common/uri';
27+
import { Registry } from 'vs/platform/registry/common/platform';
28+
import { Extensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
29+
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2730

2831
class BulkEdit {
2932

@@ -145,6 +148,7 @@ export class BulkEditService implements IBulkEditService {
145148
@ILifecycleService private readonly _lifecycleService: ILifecycleService,
146149
@IDialogService private readonly _dialogService: IDialogService,
147150
@IWorkingCopyService private readonly _workingCopyService: IWorkingCopyService,
151+
@IConfigurationService private readonly _configService: IConfigurationService,
148152
) { }
149153

150154
setPreviewHandler(handler: IBulkEditPreviewHandler): IDisposable {
@@ -218,18 +222,13 @@ export class BulkEditService implements IBulkEditService {
218222

219223
let listener: IDisposable | undefined;
220224
try {
221-
listener = this._lifecycleService.onBeforeShutdown(e => e.veto(this.shouldVeto(label, e.reason), 'veto.blukEditService'));
222-
const resources = new ResourceSet(await bulkEdit.perform());
223-
224-
if (options?.saveWhenDone) {
225-
// with `saveWhenDone` enabled loop over all dirty working copies and trigger save
226-
// for those that were involved in this bulk edit operation.
227-
const saves = this._workingCopyService.dirtyWorkingCopies.map(async copy => {
228-
if (resources.has(copy.resource)) {
229-
await copy.save();
230-
}
231-
});
232-
await Promise.allSettled(saves);
225+
listener = this._lifecycleService.onBeforeShutdown(e => e.veto(this._shouldVeto(label, e.reason), 'veto.blukEditService'));
226+
const resources = await bulkEdit.perform();
227+
228+
// when enabled (option AND setting) loop over all dirty working copies and trigger save
229+
// for those that were involved in this bulk edit operation.
230+
if (options?.respectAutoSaveConfig && this._configService.getValue(autoSaveSetting) === true && resources.length > 1) {
231+
await this._saveAll(resources);
233232
}
234233

235234
return { ariaSummary: bulkEdit.ariaMessage() };
@@ -244,7 +243,23 @@ export class BulkEditService implements IBulkEditService {
244243
}
245244
}
246245

247-
private async shouldVeto(label: string | undefined, reason: ShutdownReason): Promise<boolean> {
246+
private async _saveAll(resources: readonly URI[]) {
247+
const set = new ResourceSet(resources);
248+
const saves = this._workingCopyService.dirtyWorkingCopies.map(async (copy) => {
249+
if (set.has(copy.resource)) {
250+
await copy.save();
251+
}
252+
});
253+
254+
const result = await Promise.allSettled(saves);
255+
for (const item of result) {
256+
if (item.status === 'rejected') {
257+
this._logService.warn(item.reason);
258+
}
259+
}
260+
}
261+
262+
private async _shouldVeto(label: string | undefined, reason: ShutdownReason): Promise<boolean> {
248263
label = label || localize('fileOperation', "File operation");
249264
const reasonLabel = reason === ShutdownReason.CLOSE ? localize('closeTheWindow', "Close Window") : reason === ShutdownReason.LOAD ? localize('changeWorkspace', "Change Workspace") :
250265
reason === ShutdownReason.RELOAD ? localize('reloadTheWindow', "Reload Window") : localize('quit', "Quit");
@@ -258,3 +273,16 @@ export class BulkEditService implements IBulkEditService {
258273
}
259274

260275
registerSingleton(IBulkEditService, BulkEditService, true);
276+
277+
const autoSaveSetting = 'files.refactoring.autoSave';
278+
279+
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
280+
id: 'files',
281+
properties: {
282+
[autoSaveSetting]: {
283+
description: localize('refactoring.autoSave', "Controls if files that were part of a refactoring are saved automatically"),
284+
default: true,
285+
type: 'boolean'
286+
}
287+
}
288+
});

0 commit comments

Comments
 (0)