Skip to content

Commit 5d83e25

Browse files
Yoyokrazyrebornix
andauthored
Support for Notebook CodeAction Kind (microsoft#183457)
* nb kind support -- wip * allow notebook codeactions around single cell edit check * move notebook code action type out of editor --------- Co-authored-by: rebornix <[email protected]>
1 parent 60bfd19 commit 5d83e25

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,8 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
15251525
InteractiveSessionCopyKind: extHostTypes.InteractiveSessionCopyKind,
15261526
InteractiveEditorResponseFeedbackKind: extHostTypes.InteractiveEditorResponseFeedbackKind,
15271527
StackFrameFocus: extHostTypes.StackFrameFocus,
1528-
ThreadFocus: extHostTypes.ThreadFocus
1528+
ThreadFocus: extHostTypes.ThreadFocus,
1529+
NotebookCodeActionKind: extHostTypes.NotebookCodeActionKind
15291530
};
15301531
};
15311532
}

src/vs/workbench/api/common/extHostTypes.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,6 @@ export class CodeAction {
13581358
}
13591359
}
13601360

1361-
13621361
@es5ClassCompat
13631362
export class CodeActionKind {
13641363
private static readonly sep = '.';
@@ -1373,6 +1372,7 @@ export class CodeActionKind {
13731372
public static Source: CodeActionKind;
13741373
public static SourceOrganizeImports: CodeActionKind;
13751374
public static SourceFixAll: CodeActionKind;
1375+
public static Notebook: CodeActionKind;
13761376

13771377
constructor(
13781378
public readonly value: string
@@ -1390,6 +1390,17 @@ export class CodeActionKind {
13901390
return this.value === other.value || other.value.startsWith(this.value + CodeActionKind.sep);
13911391
}
13921392
}
1393+
1394+
export class NotebookCodeActionKind extends CodeActionKind {
1395+
public static override Notebook: CodeActionKind;
1396+
1397+
constructor(
1398+
public override readonly value: string
1399+
) {
1400+
super(value);
1401+
}
1402+
}
1403+
13931404
CodeActionKind.Empty = new CodeActionKind('');
13941405
CodeActionKind.QuickFix = CodeActionKind.Empty.append('quickfix');
13951406
CodeActionKind.Refactor = CodeActionKind.Empty.append('refactor');
@@ -1400,6 +1411,7 @@ CodeActionKind.RefactorRewrite = CodeActionKind.Refactor.append('rewrite');
14001411
CodeActionKind.Source = CodeActionKind.Empty.append('source');
14011412
CodeActionKind.SourceOrganizeImports = CodeActionKind.Source.append('organizeImports');
14021413
CodeActionKind.SourceFixAll = CodeActionKind.Source.append('fixAll');
1414+
CodeActionKind.Notebook = CodeActionKind.Empty.append('notebook');
14031415

14041416
@es5ClassCompat
14051417
export class SelectionRange {

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { CodeActionTriggerType, CodeActionProvider, IWorkspaceTextEdit } from 'v
3030
import { applyCodeAction, ApplyCodeActionReason, getCodeActions } from 'vs/editor/contrib/codeAction/browser/codeAction';
3131
import { isEqual } from 'vs/base/common/resources';
3232

33+
const NotebookCodeAction = new CodeActionKind('notebook');
34+
3335

3436
class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
3537
constructor(
@@ -133,9 +135,9 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
133135
return undefined;
134136
}
135137

136-
const codeActionsOnSave = this.createCodeActionsOnSave(settingItems);
138+
const codeActionsOnSave = this.createCodeActionsOnSave(settingItems).filter(x => !NotebookCodeAction.contains(x));
139+
const notebookCodeActionsOnSave = this.createCodeActionsOnSave(settingItems).filter(x => NotebookCodeAction.contains(x));
137140

138-
// TODO: potentially modify to account for new `Notebook` code action kind
139141
// prioritize `source.fixAll` code actions
140142
if (!Array.isArray(setting)) {
141143
codeActionsOnSave.sort((a, b) => {
@@ -152,6 +154,9 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
152154
});
153155
}
154156

157+
158+
159+
155160
if (!codeActionsOnSave.length) {
156161
return undefined;
157162
}
@@ -162,9 +167,28 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
162167
.filter(x => setting[x] === false)
163168
.map(x => new CodeActionKind(x));
164169

170+
const nbDisposable = new DisposableStore();
171+
172+
// run notebook code actions
173+
progress.report({ message: localize('notebookSaveParticipants.notebookCodeActions', "Running 'Notebook' code actions") });
174+
try {
175+
const cell = notebookModel.cells[0];
176+
const ref = await this.textModelService.createModelReference(cell.uri);
177+
nbDisposable.add(ref);
178+
179+
const textEditorModel = ref.object.textEditorModel;
180+
181+
await this.applyOnSaveActions(textEditorModel, notebookCodeActionsOnSave, excludedActions, progress, token);
182+
} catch {
183+
this.logService.error('Failed to apply notebook code action on save');
184+
} finally {
185+
progress.report({ increment: 100 });
186+
nbDisposable.dispose();
187+
}
165188

166-
progress.report({ message: localize('notebookSaveParticipants.codeActions', "Running code actions") });
189+
// run cell level code actions
167190
const disposable = new DisposableStore();
191+
progress.report({ message: localize('notebookSaveParticipants.cellCodeActions', "Running code actions") });
168192
try {
169193
await Promise.all(notebookModel.cells.map(async cell => {
170194
const ref = await this.textModelService.createModelReference(cell.uri);
@@ -224,14 +248,16 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
224248
for (const action of actionsToRun.validActions) {
225249
const codeActionEdits = action.action.edit?.edits;
226250
let breakFlag = false;
227-
for (const edit of codeActionEdits ?? []) {
228-
const workspaceTextEdit = edit as IWorkspaceTextEdit;
229-
if (workspaceTextEdit.resource && isEqual(workspaceTextEdit.resource, model.uri)) {
230-
continue;
231-
} else {
232-
// error -> applied to multiple resources
233-
breakFlag = true;
234-
break;
251+
if (!action.action.kind?.includes('notebook')) {
252+
for (const edit of codeActionEdits ?? []) {
253+
const workspaceTextEdit = edit as IWorkspaceTextEdit;
254+
if (workspaceTextEdit.resource && isEqual(workspaceTextEdit.resource, model.uri)) {
255+
continue;
256+
} else {
257+
// error -> applied to multiple resources
258+
breakFlag = true;
259+
break;
260+
}
235261
}
236262
}
237263
if (breakFlag) {

src/vs/workbench/services/extensions/common/extensionsApiProposals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const allApiProposals = Object.freeze({
5555
interactiveWindow: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.interactiveWindow.d.ts',
5656
ipc: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.ipc.d.ts',
5757
notebookCellExecutionState: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.notebookCellExecutionState.d.ts',
58+
notebookCodeActions: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.notebookCodeActions.d.ts',
5859
notebookControllerAffinityHidden: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.notebookControllerAffinityHidden.d.ts',
5960
notebookDeprecated: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.notebookDeprecated.d.ts',
6061
notebookExecution: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.notebookExecution.d.ts',
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
declare module 'vscode' {
7+
8+
// https://github.com/microsoft/vscode/issues/179213
9+
10+
export class NotebookCodeActionKind {
11+
// can only return MULTI CELL workspaceEdits
12+
// ex: notebook.organizeImprots
13+
static readonly Notebook: CodeActionKind;
14+
15+
constructor(value: string);
16+
}
17+
}

0 commit comments

Comments
 (0)