@@ -30,6 +30,8 @@ import { CodeActionTriggerType, CodeActionProvider, IWorkspaceTextEdit } from 'v
30
30
import { applyCodeAction , ApplyCodeActionReason , getCodeActions } from 'vs/editor/contrib/codeAction/browser/codeAction' ;
31
31
import { isEqual } from 'vs/base/common/resources' ;
32
32
33
+ const NotebookCodeAction = new CodeActionKind ( 'notebook' ) ;
34
+
33
35
34
36
class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
35
37
constructor (
@@ -133,9 +135,9 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
133
135
return undefined ;
134
136
}
135
137
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 ) ) ;
137
140
138
- // TODO: potentially modify to account for new `Notebook` code action kind
139
141
// prioritize `source.fixAll` code actions
140
142
if ( ! Array . isArray ( setting ) ) {
141
143
codeActionsOnSave . sort ( ( a , b ) => {
@@ -152,6 +154,9 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
152
154
} ) ;
153
155
}
154
156
157
+
158
+
159
+
155
160
if ( ! codeActionsOnSave . length ) {
156
161
return undefined ;
157
162
}
@@ -162,9 +167,28 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
162
167
. filter ( x => setting [ x ] === false )
163
168
. map ( x => new CodeActionKind ( x ) ) ;
164
169
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
+ }
165
188
166
- progress . report ( { message : localize ( 'notebookSaveParticipants.codeActions' , "Running code actions" ) } ) ;
189
+ // run cell level code actions
167
190
const disposable = new DisposableStore ( ) ;
191
+ progress . report ( { message : localize ( 'notebookSaveParticipants.cellCodeActions' , "Running code actions" ) } ) ;
168
192
try {
169
193
await Promise . all ( notebookModel . cells . map ( async cell => {
170
194
const ref = await this . textModelService . createModelReference ( cell . uri ) ;
@@ -224,14 +248,16 @@ class CodeActionOnSaveParticipant implements IStoredFileWorkingCopySaveParticipa
224
248
for ( const action of actionsToRun . validActions ) {
225
249
const codeActionEdits = action . action . edit ?. edits ;
226
250
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
+ }
235
261
}
236
262
}
237
263
if ( breakFlag ) {
0 commit comments