From c155de778a42a8a91ea1f2044e09c8ee0f6bb7fc Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 29 Feb 2016 17:11:01 -0800 Subject: [PATCH 1/2] Avoid removing indentation on a new line as trailing white spaces --- src/services/formatting/formatting.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 78d225be845a9..eb40d3aa6abf9 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -72,12 +72,14 @@ namespace ts.formatting { if (line === 0) { return []; } - // get the span for the previous\current line + // After the enter key, the cursor is now at a new line. The new line should not be formatted, + // otherwise the indentation would be treated as trailing whitespaces and removed. The previous + // line should be formatted, and the one before that should be used as reference. let span = { - // get start position for the previous line - pos: getStartPositionOfLine(line - 1, sourceFile), - // get end position for the current line (end value is exclusive so add 1 to the result) - end: getEndLinePosition(line, sourceFile) + 1 + // get start position for the line before previous line + pos: getStartPositionOfLine(line - 2, sourceFile), + // get end position for the previous line (end value is exclusive so add 1 to the result) + end: getEndLinePosition(line - 1, sourceFile) + 1 } return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnEnter); } From 1224013f77df06da893e84951b8be4b925f9858d Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 1 Mar 2016 16:45:56 -0800 Subject: [PATCH 2/2] Update the format span end position for formatOnEnter --- src/services/formatting/formatting.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index eb40d3aa6abf9..066bcf7cda615 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -72,14 +72,20 @@ namespace ts.formatting { if (line === 0) { return []; } - // After the enter key, the cursor is now at a new line. The new line should not be formatted, - // otherwise the indentation would be treated as trailing whitespaces and removed. The previous - // line should be formatted, and the one before that should be used as reference. + // After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters. + // If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as + // trailing whitespaces. So the end of the formatting span should be the later one between: + // 1. the end of the previous line + // 2. the last non-whitespace character in the current line + let endOfFormatSpan = getEndLinePosition(line, sourceFile); + while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { + endOfFormatSpan--; + } let span = { - // get start position for the line before previous line - pos: getStartPositionOfLine(line - 2, sourceFile), - // get end position for the previous line (end value is exclusive so add 1 to the result) - end: getEndLinePosition(line - 1, sourceFile) + 1 + // get start position for the previous line + pos: getStartPositionOfLine(line - 1, sourceFile), + // end value is exclusive so add 1 to the result + end: endOfFormatSpan + 1 } return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnEnter); }