Skip to content

Commit 5605a4e

Browse files
authored
Initial support for notebook CodeActions (microsoft#180740)
* support cmd codeActions on nb save * add setting * rm comment * add log + add await command * missed a space in setting description
1 parent f29dcfb commit 5605a4e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/vs/workbench/contrib/notebook/browser/contrib/saveParticipants/saveParticipants.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle
2222
import { IStoredFileWorkingCopy, IStoredFileWorkingCopyModel } from 'vs/workbench/services/workingCopy/common/storedFileWorkingCopy';
2323
import { IStoredFileWorkingCopySaveParticipant, IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/common/workingCopyFileService';
2424
import { NotebookSetting } from 'vs/workbench/contrib/notebook/common/notebookCommon';
25+
import { ICommandService } from 'vs/platform/commands/common/commands';
26+
import { ILogService } from 'vs/platform/log/common/log';
2527

2628
class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
2729
constructor(
@@ -85,6 +87,51 @@ class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
8587
}
8688
}
8789

90+
class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
91+
constructor(
92+
@IConfigurationService private readonly configurationService: IConfigurationService,
93+
@ICommandService private readonly commandService: ICommandService,
94+
@ILogService private readonly logService: ILogService,
95+
) {
96+
}
97+
98+
async participate(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, context: { reason: SaveReason }, progress: IProgress<IProgressStep>, _token: CancellationToken): Promise<void> {
99+
if (!workingCopy.model || !(workingCopy.model instanceof NotebookFileWorkingCopyModel)) {
100+
return;
101+
}
102+
103+
if (context.reason === SaveReason.AUTO) {
104+
return undefined;
105+
}
106+
107+
const setting = this.configurationService.getValue<{ [kind: string]: boolean } | string[]>('notebook.codeActionsOnSave');
108+
if (!setting) {
109+
return undefined;
110+
}
111+
112+
const settingItems: string[] = Array.isArray(setting)
113+
? setting
114+
: Object.keys(setting).filter(x => setting[x]);
115+
116+
if (!settingItems.length) {
117+
return undefined;
118+
}
119+
120+
progress.report({ message: 'CodeActionsOnSave running' });
121+
const disposable = new DisposableStore();
122+
try {
123+
for (const cmd of settingItems) {
124+
await this.commandService.executeCommand(cmd);
125+
}
126+
} catch {
127+
// Failure to apply a code action should not block other on save actions
128+
this.logService.warn('CodeActionsOnSave failed to apply a code action');
129+
} finally {
130+
progress.report({ increment: 100 });
131+
disposable.dispose();
132+
}
133+
}
134+
}
88135
export class SaveParticipantsContribution extends Disposable implements IWorkbenchContribution {
89136
constructor(
90137
@IInstantiationService private readonly instantiationService: IInstantiationService,
@@ -95,6 +142,7 @@ export class SaveParticipantsContribution extends Disposable implements IWorkben
95142
}
96143

97144
private registerSaveParticipants(): void {
145+
this._register(this.workingCopyFileService.addSaveParticipant(this.instantiationService.createInstance(CodeActionOnSaveParticipant)));
98146
this._register(this.workingCopyFileService.addSaveParticipant(this.instantiationService.createInstance(FormatOnSaveParticipant)));
99147
}
100148
}

src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,15 @@ configurationRegistry.registerConfiguration({
920920
tags: ['notebookLayout'],
921921
default: false
922922
},
923+
[NotebookSetting.codeActionsOnSave]: {
924+
markdownDescription: nls.localize('notebook.codeActionsOnSave', "Experimental. Run a series of CodeActions for a notebook on save. CodeActions must be specified, the file must not be saved after delay, and the editor must not be shutting down. Example: `notebook.format: true`"),
925+
type: 'object',
926+
additionalProperties: {
927+
type: 'boolean'
928+
},
929+
tags: ['notebookLayout'],
930+
default: {}
931+
},
923932
[NotebookSetting.confirmDeleteRunningCell]: {
924933
markdownDescription: nls.localize('notebook.confirmDeleteRunningCell', "Control whether a confirmation prompt is required to delete a running cell."),
925934
type: 'boolean',

src/vs/workbench/contrib/notebook/common/notebookCommon.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ export const NotebookSetting = {
937937
outputScrolling: 'notebook.output.scrolling',
938938
textOutputLineLimit: 'notebook.output.textLineLimit',
939939
formatOnSave: 'notebook.formatOnSave.enabled',
940+
codeActionsOnSave: 'notebook.codeActionsOnSave',
940941
outputWordWrap: 'notebook.output.wordWrap',
941942
outputLineHeightDeprecated: 'notebook.outputLineHeight',
942943
outputLineHeight: 'notebook.output.lineHeight',

0 commit comments

Comments
 (0)