@@ -45,8 +45,7 @@ class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
45
45
@ITextModelService private readonly textModelService : ITextModelService ,
46
46
@IBulkEditService private readonly bulkEditService : IBulkEditService ,
47
47
@IConfigurationService private readonly configurationService : IConfigurationService ,
48
- ) {
49
- }
48
+ ) { }
50
49
51
50
async participate ( workingCopy : IStoredFileWorkingCopy < IStoredFileWorkingCopyModel > , context : { reason : SaveReason } , progress : IProgress < IProgressStep > , token : CancellationToken ) : Promise < void > {
52
51
if ( ! workingCopy . model || ! ( workingCopy . model instanceof NotebookFileWorkingCopyModel ) ) {
@@ -178,7 +177,7 @@ class TrimFinalNewLinesParticipant implements IStoredFileWorkingCopySaveParticip
178
177
179
178
async participate ( workingCopy : IStoredFileWorkingCopy < IStoredFileWorkingCopyModel > , context : { reason : SaveReason } , progress : IProgress < IProgressStep > , _token : CancellationToken ) : Promise < void > {
180
179
if ( this . configurationService . getValue < boolean > ( 'files.trimFinalNewlines' ) ) {
181
- this . doTrimFinalNewLines ( workingCopy , context . reason === SaveReason . AUTO , progress ) ;
180
+ await this . doTrimFinalNewLines ( workingCopy , context . reason === SaveReason . AUTO , progress ) ;
182
181
}
183
182
}
184
183
@@ -250,22 +249,30 @@ class FinalNewLineParticipant implements IStoredFileWorkingCopySaveParticipant {
250
249
constructor (
251
250
@IConfigurationService private readonly configurationService : IConfigurationService ,
252
251
@IBulkEditService private readonly bulkEditService : IBulkEditService ,
252
+ @IEditorService private readonly editorService : IEditorService ,
253
253
) { }
254
254
255
255
async participate ( workingCopy : IStoredFileWorkingCopy < IStoredFileWorkingCopyModel > , context : { reason : SaveReason } , progress : IProgress < IProgressStep > , _token : CancellationToken ) : Promise < void > {
256
256
if ( this . configurationService . getValue ( 'files.insertFinalNewline' ) ) {
257
- this . doInsertFinalNewLine ( workingCopy , context , progress ) ;
257
+ await this . doInsertFinalNewLine ( workingCopy , context . reason === SaveReason . AUTO , progress ) ;
258
258
}
259
259
}
260
260
261
- private async doInsertFinalNewLine ( workingCopy : IStoredFileWorkingCopy < IStoredFileWorkingCopyModel > , context : { reason : SaveReason } , progress : IProgress < IProgressStep > ) : Promise < void > {
261
+ private async doInsertFinalNewLine ( workingCopy : IStoredFileWorkingCopy < IStoredFileWorkingCopyModel > , isAutoSaved : boolean , progress : IProgress < IProgressStep > ) : Promise < void > {
262
262
if ( ! workingCopy . model || ! ( workingCopy . model instanceof NotebookFileWorkingCopyModel ) ) {
263
263
return ;
264
264
}
265
265
266
266
const disposable = new DisposableStore ( ) ;
267
267
const notebook = workingCopy . model . notebookModel ;
268
268
269
+ // get initial cursor positions
270
+ const activeCellEditor = getActiveCellCodeEditor ( this . editorService ) ;
271
+ let selections ;
272
+ if ( activeCellEditor ) {
273
+ selections = activeCellEditor . getSelections ( ) ?? [ ] ;
274
+ }
275
+
269
276
try {
270
277
const allCellEdits = await Promise . all ( notebook . cells . map ( async ( cell ) => {
271
278
if ( cell . cellKind !== CellKind . Code ) {
@@ -285,6 +292,10 @@ class FinalNewLineParticipant implements IStoredFileWorkingCopySaveParticipant {
285
292
const filteredEdits = allCellEdits . filter ( edit => edit !== undefined ) as ResourceEdit [ ] ;
286
293
await this . bulkEditService . apply ( filteredEdits , { label : localize ( 'insertFinalNewLine' , "Insert Final New Line" ) , code : 'undoredo.insertFinalNewLine' } ) ;
287
294
295
+ // set cursor back to initial position after inserting final new line
296
+ if ( activeCellEditor && selections ) {
297
+ activeCellEditor . setSelections ( selections ) ;
298
+ }
288
299
} finally {
289
300
progress . report ( { increment : 100 } ) ;
290
301
disposable . dispose ( ) ;
0 commit comments