From 9550d003a66ea3568fc57aa4a3ca3503d936a2c8 Mon Sep 17 00:00:00 2001 From: Alexandru Dima Date: Fri, 4 Jun 2021 22:33:41 +0200 Subject: [PATCH] Fixes #121188: Only delete empty final lines --- .../contrib/codeEditor/browser/saveParticipants.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts b/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts index 5f4725e8f56..ce600feb10c 100644 --- a/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts +++ b/src/vs/workbench/contrib/codeEditor/browser/saveParticipants.ts @@ -156,12 +156,12 @@ export class TrimFinalNewLinesParticipant implements ITextFileSaveParticipant { } /** - * returns 0 if the entire file is empty or whitespace only + * returns 0 if the entire file is empty */ - private findLastLineWithContent(model: ITextModel): number { + private findLastNonEmptyLine(model: ITextModel): number { for (let lineNumber = model.getLineCount(); lineNumber >= 1; lineNumber--) { const lineContent = model.getLineContent(lineNumber); - if (strings.lastNonWhitespaceIndex(lineContent) !== -1) { + if (lineContent.length > 0) { // this line has content return lineNumber; } @@ -193,8 +193,8 @@ export class TrimFinalNewLinesParticipant implements ITextFileSaveParticipant { } } - const lastLineNumberWithContent = this.findLastLineWithContent(model); - const deleteFromLineNumber = Math.max(lastLineNumberWithContent + 1, cannotTouchLineNumber + 1); + const lastNonEmptyLine = this.findLastNonEmptyLine(model); + const deleteFromLineNumber = Math.max(lastNonEmptyLine + 1, cannotTouchLineNumber + 1); const deletionRange = model.validateRange(new Range(deleteFromLineNumber, 1, lineCount, model.getLineMaxColumn(lineCount))); if (deletionRange.isEmpty()) {