From 76f13114ec7875e24a63325d43e0972aaf376f48 Mon Sep 17 00:00:00 2001 From: aiday-mar Date: Fri, 8 Jul 2022 10:02:11 +0200 Subject: [PATCH] Resolving the reviews and simplifying the code with the function `multipleLineBreaks` --- .vscode/extensions.json | 13 ------ CHANGELOG.md | 2 +- src/impl/format.ts | 92 +++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 64 deletions(-) delete mode 100644 .vscode/extensions.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index d1987c7..0000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. - // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp - - // List of extensions which should be recommended for users of this workspace. - "recommendations": [ - - ], - // List of extensions recommended by VS Code that should not be recommended for users of this workspace. - "unwantedRecommendations": [ - - ] -} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 391ed67..f8be906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ 3.1.0 2022-07-07 ================== - * Added the possibility to have multi-line formatting + * Added the variable `keepLines` in the FormattingOptions which keeps the empty break lines/new lines in the formatting. 3.0.0 2020-11-13 ================== diff --git a/src/impl/format.ts b/src/impl/format.ts index 7cc06ce..62415f4 100644 --- a/src/impl/format.ts +++ b/src/impl/format.ts @@ -4,6 +4,7 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; +import { mainModule } from 'process'; import { Range, FormattingOptions, Edit, SyntaxKind, ScanError } from '../main'; import { createScanner } from './scanner'; @@ -37,7 +38,7 @@ export function format(documentText: string, range: Range | undefined, options: let eol = getEOL(options, documentText); let numberLineBreaks = 0; - let lineBreak = false; + let needsLineBreak = false; let indentLevel = 0; let indentValue: string; if (options.insertSpaces) { @@ -50,21 +51,38 @@ export function format(documentText: string, range: Range | undefined, options: let hasError = false; function newLinesAndIndent(): string { - if (numberLineBreaks > 0) { + if (numberLineBreaks > 1) { return repeat(eol, numberLineBreaks) + repeat(indentValue, initialIndentLevel + indentLevel); } else { return eol + repeat(indentValue, initialIndentLevel + indentLevel); } } + function multipleLineBreaks(useVariableNeedsLineBreak = false) { + + if (numberLineBreaks > 0) { + return newLinesAndIndent(); + } else { + if (!useVariableNeedsLineBreak) { + return ' '; + } else if (useVariableNeedsLineBreak && !needsLineBreak) { + return ' '; + } else { + return ''; + } + } + } + function scanNext(): SyntaxKind { let token = scanner.scan(); numberLineBreaks = 0; - lineBreak = false; while (token === SyntaxKind.Trivia || token === SyntaxKind.LineBreakTrivia) { - lineBreak = lineBreak || (token === SyntaxKind.LineBreakTrivia); - if (token === SyntaxKind.LineBreakTrivia && options.keepLines) { - numberLineBreaks += 1; + if (token === SyntaxKind.LineBreakTrivia) { + if (options.keepLines) { + numberLineBreaks += 1; + } else { + numberLineBreaks = 1; + } } token = scanner.scan(); } @@ -91,8 +109,9 @@ export function format(documentText: string, range: Range | undefined, options: let secondToken = scanNext(); let replaceContent = ''; - let needsLineBreak = false; - while (!lineBreak && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) { + needsLineBreak = false; + + while (numberLineBreaks === 0 && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) { // comments on the same line: keep them on the same line, but ignore them otherwise let commentTokenStart = scanner.getTokenOffset() + formatTextStart; addEdit(' ', firstTokenEnd, commentTokenStart); @@ -103,53 +122,34 @@ export function format(documentText: string, range: Range | undefined, options: } if (secondToken === SyntaxKind.CloseBraceToken) { + if (firstToken !== SyntaxKind.OpenBraceToken) { indentLevel--; }; if (options.keepLines) { - if (firstToken !== SyntaxKind.OpenBraceToken) { indentLevel--; }; - if (lineBreak) { - replaceContent = newLinesAndIndent(); - } else { - replaceContent = ' '; - } - } else if (!options.keepLines && firstToken !== SyntaxKind.OpenBraceToken){ - indentLevel--; + replaceContent = multipleLineBreaks(); + } else if (firstToken !== SyntaxKind.OpenBraceToken) { replaceContent = newLinesAndIndent(); } } else if (secondToken === SyntaxKind.CloseBracketToken) { + if (firstToken !== SyntaxKind.OpenBracketToken) { indentLevel--; }; if (options.keepLines) { - if(firstToken !== SyntaxKind.OpenBracketToken) {indentLevel--;}; - if (lineBreak) { - replaceContent = newLinesAndIndent(); - } else { - replaceContent = ' '; - } + replaceContent = multipleLineBreaks(); } else if (firstToken !== SyntaxKind.OpenBracketToken) { - indentLevel--; replaceContent = newLinesAndIndent(); } } else { - switch (firstToken) { case SyntaxKind.OpenBracketToken: case SyntaxKind.OpenBraceToken: indentLevel++; if (options.keepLines) { - if (lineBreak) { - replaceContent = newLinesAndIndent(); - } else { - replaceContent = " "; - } + replaceContent = multipleLineBreaks(); } else { replaceContent = newLinesAndIndent(); } break; case SyntaxKind.CommaToken: - if(options.keepLines) { - if (lineBreak) { - replaceContent = newLinesAndIndent(); - } else { - replaceContent = " "; - } + if (options.keepLines) { + replaceContent = multipleLineBreaks(); } else { replaceContent = newLinesAndIndent(); } @@ -158,19 +158,11 @@ export function format(documentText: string, range: Range | undefined, options: replaceContent = newLinesAndIndent(); break; case SyntaxKind.BlockCommentTrivia: - if (lineBreak) { - replaceContent = newLinesAndIndent(); - } else if (!needsLineBreak) { - replaceContent = ' '; - } + replaceContent = multipleLineBreaks(true); break; case SyntaxKind.ColonToken: if (options.keepLines) { - if (lineBreak) { - replaceContent = newLinesAndIndent(); - } else if (!needsLineBreak) { - replaceContent = ' '; - } + replaceContent = multipleLineBreaks(true); } else { if (!needsLineBreak) { replaceContent = ' '; @@ -179,7 +171,7 @@ export function format(documentText: string, range: Range | undefined, options: break; case SyntaxKind.StringLiteral: if (options.keepLines) { - if (lineBreak) { + if (numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else { if (secondToken === SyntaxKind.ColonToken) { @@ -203,7 +195,7 @@ export function format(documentText: string, range: Range | undefined, options: case SyntaxKind.CloseBraceToken: case SyntaxKind.CloseBracketToken: if (options.keepLines) { - if (lineBreak) { + if (numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else { if (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia) { @@ -229,13 +221,13 @@ export function format(documentText: string, range: Range | undefined, options: break; } - if (lineBreak && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) { + if (numberLineBreaks > 0 && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) { replaceContent = newLinesAndIndent(); } } if (secondToken === SyntaxKind.EOF) { if (options.keepLines) { - if (lineBreak) { + if (numberLineBreaks > 0) { replaceContent = newLinesAndIndent(); } else { replaceContent = options.insertFinalNewline ? eol : ''; @@ -294,4 +286,4 @@ function getEOL(options: FormattingOptions, text: string): string { export function isEOL(text: string, offset: number) { return '\r\n'.indexOf(text.charAt(offset)) !== -1; -} \ No newline at end of file +}