Skip to content

Commit 00651c4

Browse files
authored
Fix notebook insertFinalNewline behavior (microsoft#194442)
fix typing newline bug
1 parent d5c578c commit 00651c4

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
4545
@ITextModelService private readonly textModelService: ITextModelService,
4646
@IBulkEditService private readonly bulkEditService: IBulkEditService,
4747
@IConfigurationService private readonly configurationService: IConfigurationService,
48-
) {
49-
}
48+
) { }
5049

5150
async participate(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, context: { reason: SaveReason }, progress: IProgress<IProgressStep>, token: CancellationToken): Promise<void> {
5251
if (!workingCopy.model || !(workingCopy.model instanceof NotebookFileWorkingCopyModel)) {
@@ -178,7 +177,7 @@ class TrimFinalNewLinesParticipant implements IStoredFileWorkingCopySaveParticip
178177

179178
async participate(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, context: { reason: SaveReason }, progress: IProgress<IProgressStep>, _token: CancellationToken): Promise<void> {
180179
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);
182181
}
183182
}
184183

@@ -250,22 +249,30 @@ class FinalNewLineParticipant implements IStoredFileWorkingCopySaveParticipant {
250249
constructor(
251250
@IConfigurationService private readonly configurationService: IConfigurationService,
252251
@IBulkEditService private readonly bulkEditService: IBulkEditService,
252+
@IEditorService private readonly editorService: IEditorService,
253253
) { }
254254

255255
async participate(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, context: { reason: SaveReason }, progress: IProgress<IProgressStep>, _token: CancellationToken): Promise<void> {
256256
if (this.configurationService.getValue('files.insertFinalNewline')) {
257-
this.doInsertFinalNewLine(workingCopy, context, progress);
257+
await this.doInsertFinalNewLine(workingCopy, context.reason === SaveReason.AUTO, progress);
258258
}
259259
}
260260

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> {
262262
if (!workingCopy.model || !(workingCopy.model instanceof NotebookFileWorkingCopyModel)) {
263263
return;
264264
}
265265

266266
const disposable = new DisposableStore();
267267
const notebook = workingCopy.model.notebookModel;
268268

269+
// get initial cursor positions
270+
const activeCellEditor = getActiveCellCodeEditor(this.editorService);
271+
let selections;
272+
if (activeCellEditor) {
273+
selections = activeCellEditor.getSelections() ?? [];
274+
}
275+
269276
try {
270277
const allCellEdits = await Promise.all(notebook.cells.map(async (cell) => {
271278
if (cell.cellKind !== CellKind.Code) {
@@ -285,6 +292,10 @@ class FinalNewLineParticipant implements IStoredFileWorkingCopySaveParticipant {
285292
const filteredEdits = allCellEdits.filter(edit => edit !== undefined) as ResourceEdit[];
286293
await this.bulkEditService.apply(filteredEdits, { label: localize('insertFinalNewLine', "Insert Final New Line"), code: 'undoredo.insertFinalNewLine' });
287294

295+
// set cursor back to initial position after inserting final new line
296+
if (activeCellEditor && selections) {
297+
activeCellEditor.setSelections(selections);
298+
}
288299
} finally {
289300
progress.report({ increment: 100 });
290301
disposable.dispose();

0 commit comments

Comments
 (0)