Skip to content

Commit

Permalink
Fix notebook insertFinalNewline behavior (microsoft#194442)
Browse files Browse the repository at this point in the history
fix typing newline bug
  • Loading branch information
Yoyokrazy committed Sep 28, 2023
1 parent d5c578c commit 00651c4
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class FormatOnSaveParticipant implements IStoredFileWorkingCopySaveParticipant {
@ITextModelService private readonly textModelService: ITextModelService,
@IBulkEditService private readonly bulkEditService: IBulkEditService,
@IConfigurationService private readonly configurationService: IConfigurationService,
) {
}
) { }

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

async participate(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, context: { reason: SaveReason }, progress: IProgress<IProgressStep>, _token: CancellationToken): Promise<void> {
if (this.configurationService.getValue<boolean>('files.trimFinalNewlines')) {
this.doTrimFinalNewLines(workingCopy, context.reason === SaveReason.AUTO, progress);
await this.doTrimFinalNewLines(workingCopy, context.reason === SaveReason.AUTO, progress);
}
}

Expand Down Expand Up @@ -250,22 +249,30 @@ class FinalNewLineParticipant implements IStoredFileWorkingCopySaveParticipant {
constructor(
@IConfigurationService private readonly configurationService: IConfigurationService,
@IBulkEditService private readonly bulkEditService: IBulkEditService,
@IEditorService private readonly editorService: IEditorService,
) { }

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

private async doInsertFinalNewLine(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, context: { reason: SaveReason }, progress: IProgress<IProgressStep>): Promise<void> {
private async doInsertFinalNewLine(workingCopy: IStoredFileWorkingCopy<IStoredFileWorkingCopyModel>, isAutoSaved: boolean, progress: IProgress<IProgressStep>): Promise<void> {
if (!workingCopy.model || !(workingCopy.model instanceof NotebookFileWorkingCopyModel)) {
return;
}

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

// get initial cursor positions
const activeCellEditor = getActiveCellCodeEditor(this.editorService);
let selections;
if (activeCellEditor) {
selections = activeCellEditor.getSelections() ?? [];
}

try {
const allCellEdits = await Promise.all(notebook.cells.map(async (cell) => {
if (cell.cellKind !== CellKind.Code) {
Expand All @@ -285,6 +292,10 @@ class FinalNewLineParticipant implements IStoredFileWorkingCopySaveParticipant {
const filteredEdits = allCellEdits.filter(edit => edit !== undefined) as ResourceEdit[];
await this.bulkEditService.apply(filteredEdits, { label: localize('insertFinalNewLine', "Insert Final New Line"), code: 'undoredo.insertFinalNewLine' });

// set cursor back to initial position after inserting final new line
if (activeCellEditor && selections) {
activeCellEditor.setSelections(selections);
}
} finally {
progress.report({ increment: 100 });
disposable.dispose();
Expand Down

0 comments on commit 00651c4

Please sign in to comment.