Add trimTrailingWhitespace Editor Option (#36905)

* add trim trailing whitespace option

* set default trimTrailingWhiteSpace to true

* add fourslash tests

* accept new baselines

* only preserce whitespace when setting is explicitly false

* format whitespace
This commit is contained in:
Jesse Trinity 2020-03-13 13:42:49 -07:00 committed by GitHub
parent fc30095e8b
commit 4c160683c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 4 deletions

View file

@ -3089,6 +3089,7 @@ namespace ts.server.protocol {
newLineCharacter?: string;
convertTabsToSpaces?: boolean;
indentStyle?: IndentStyle | ts.IndentStyle;
trimTrailingWhitespace?: boolean;
}
export interface FormatCodeSettings extends EditorSettings {

View file

@ -427,7 +427,9 @@ namespace ts.formatting {
if (leadingTrivia) {
indentTriviaItems(leadingTrivia, initialIndentation, /*indentNextTokenOrTrivia*/ false,
item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!));
trimTrailingWhitespacesForRemainingRange();
if (options.trimTrailingWhitespace !== false) {
trimTrailingWhitespacesForRemainingRange();
}
}
}
@ -982,7 +984,7 @@ namespace ts.formatting {
const rules = getRules(formattingContext);
let trimTrailingWhitespaces = false;
let trimTrailingWhitespaces = formattingContext.options.trimTrailingWhitespace !== false;
let lineAction = LineAction.None;
if (rules) {
// Apply rules in reverse order so that higher priority rules (which are first in the array)
@ -1010,11 +1012,11 @@ namespace ts.formatting {
}
// We need to trim trailing whitespace between the tokens if they were on different lines, and no rule was applied to put them on the same line
trimTrailingWhitespaces = !(rule.action & RuleAction.DeleteSpace) && rule.flags !== RuleFlags.CanDeleteNewLines;
trimTrailingWhitespaces = trimTrailingWhitespaces && !(rule.action & RuleAction.DeleteSpace) && rule.flags !== RuleFlags.CanDeleteNewLines;
});
}
else {
trimTrailingWhitespaces = currentItem.kind !== SyntaxKind.EndOfFileToken;
trimTrailingWhitespaces = trimTrailingWhitespaces && currentItem.kind !== SyntaxKind.EndOfFileToken;
}
if (currentStartLine !== previousStartLine && trimTrailingWhitespaces) {

View file

@ -829,6 +829,7 @@ namespace ts {
newLineCharacter?: string;
convertTabsToSpaces?: boolean;
indentStyle?: IndentStyle;
trimTrailingWhitespace?: boolean;
}
/* @deprecated - consider using FormatCodeSettings instead */
@ -895,6 +896,7 @@ namespace ts {
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
semicolons: SemicolonPreference.Ignore,
trimTrailingWhitespace: true
};
}

View file

@ -5575,6 +5575,7 @@ declare namespace ts {
newLineCharacter?: string;
convertTabsToSpaces?: boolean;
indentStyle?: IndentStyle;
trimTrailingWhitespace?: boolean;
}
interface FormatCodeOptions extends EditorOptions {
InsertSpaceAfterCommaDelimiter: boolean;
@ -8572,6 +8573,7 @@ declare namespace ts.server.protocol {
newLineCharacter?: string;
convertTabsToSpaces?: boolean;
indentStyle?: IndentStyle | ts.IndentStyle;
trimTrailingWhitespace?: boolean;
}
interface FormatCodeSettings extends EditorSettings {
insertSpaceAfterCommaDelimiter?: boolean;

View file

@ -5575,6 +5575,7 @@ declare namespace ts {
newLineCharacter?: string;
convertTabsToSpaces?: boolean;
indentStyle?: IndentStyle;
trimTrailingWhitespace?: boolean;
}
interface FormatCodeOptions extends EditorOptions {
InsertSpaceAfterCommaDelimiter: boolean;

View file

@ -0,0 +1,26 @@
/// <reference path="fourslash.ts" />
////
////var a;
////var b
////
//////
////function b(){
//// while(true){
//// }
////}
////
format.setOption("trimTrailingWhitespace", false);
format.document();
verify.currentFileContentIs(`
var a;
var b
//
function b() {
while (true) {
}
}
`);

View file

@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />
////
/////*begin*/;
////
/////*end*/
////
////
format.setOption("trimTrailingWhitespace", false);
format.selection('begin', 'end');
verify.currentFileContentIs(`
;
`);

View file

@ -126,6 +126,7 @@ declare namespace FourSlashInterface {
newLineCharacter?: string;
convertTabsToSpaces?: boolean;
indentStyle?: IndentStyle;
trimTrailingWhitespace?: boolean;
}
interface FormatCodeOptions extends EditorOptions {
InsertSpaceAfterCommaDelimiter: boolean;