From e026893a3e7b25b56a6b6fa2d0576f7e7ae7b3d1 Mon Sep 17 00:00:00 2001 From: g2384 Date: Sat, 17 Oct 2020 10:36:13 +0100 Subject: [PATCH] add new lines at the end of file --- VHDLFormatter.js | 6 +++++- VHDLFormatter.ts | 7 ++++++- index.html | 11 ++++++++++- tests/VHDLFormatter.test.ts | 10 +++++++++- tests/VHDLFormatterUnitTests.ts | 4 ++-- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/VHDLFormatter.js b/VHDLFormatter.js index 2a0482c..c5fff1a 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -256,7 +256,7 @@ class signAlignSettings { } exports.signAlignSettings = signAlignSettings; class BeautifierSettings { - constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine) { + constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine, addNewLine) { this.RemoveComments = removeComments; this.RemoveAsserts = removeReport; this.CheckAlias = checkAlias; @@ -266,6 +266,7 @@ class BeautifierSettings { this.Indentation = indentation; this.NewLineSettings = newLineSettings; this.EndOfLine = endOfLine; + this.AddNewLine = addNewLine; } } exports.BeautifierSettings = BeautifierSettings; @@ -351,6 +352,9 @@ function beautify(input, settings) { var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g"); input = input.replace(escapedTexts, ""); input = input.replace(/\r\n/g, settings.EndOfLine); + if (settings.AddNewLine && !input.endsWith(settings.EndOfLine)) { + input += settings.EndOfLine; + } return input; } exports.beautify = beautify; diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 202cbf3..541bb38 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -303,9 +303,10 @@ export class BeautifierSettings { Indentation: string; NewLineSettings: NewLineSettings; EndOfLine: string; + AddNewLine: boolean; constructor(removeComments: boolean, removeReport: boolean, checkAlias: boolean, signAlignSettings: signAlignSettings, keywordCase: string, typeNameCase: string, indentation: string, - newLineSettings: NewLineSettings, endOfLine: string) { + newLineSettings: NewLineSettings, endOfLine: string, addNewLine: boolean) { this.RemoveComments = removeComments; this.RemoveAsserts = removeReport; this.CheckAlias = checkAlias; @@ -315,6 +316,7 @@ export class BeautifierSettings { this.Indentation = indentation; this.NewLineSettings = newLineSettings; this.EndOfLine = endOfLine; + this.AddNewLine = addNewLine; } } @@ -407,6 +409,9 @@ export function beautify(input: string, settings: BeautifierSettings) { var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g"); input = input.replace(escapedTexts, ""); input = input.replace(/\r\n/g, settings.EndOfLine); + if (settings.AddNewLine && !input.endsWith(settings.EndOfLine)) { + input += settings.EndOfLine; + } return input; } diff --git a/index.html b/index.html index de1249c..8b71992 100644 --- a/index.html +++ b/index.html @@ -212,6 +212,12 @@ ( one \r & one \n) +
+
+ + +
+
@@ -349,6 +355,7 @@ document.getElementById("compress").checked = setting.compress; var indentation = beautifierSettings.Indentation; document.getElementById("use_space").checked = indentation != "\t"; + document.getElementById("add_extraEOL").checked = beautifierSettings.AddNewLine; document.getElementById("customise_indentation").value = indentation; document.getElementById("keyword_div").elements.namedItem("keywordcase").value = beautifierSettings.KeywordCase; document.getElementById("typename_div").elements.namedItem("typenamecase").value = beautifierSettings.TypeNameCase; @@ -433,6 +440,7 @@ var use_space = document.getElementById("use_space").checked; var compress = document.getElementById("compress").checked; var cust_indent = document.getElementById("customise_indentation").value; + var addNewLine = document.getElementById("add_extraEOL").checked; var keywordcase = document.getElementById("keyword_div").elements.namedItem("keywordcase").value; var typenamecase = document.getElementById("typename_div").elements.namedItem("typenamecase").value; var endOfLine = document.getElementById("cust_eol").value; @@ -480,7 +488,8 @@ typenamecase, indentation, newLineSettings, - endOfLine); + endOfLine, + addNewLine); return [beautifierSettings, compress]; } diff --git a/tests/VHDLFormatter.test.ts b/tests/VHDLFormatter.test.ts index 2c4ccd6..18d08d1 100644 --- a/tests/VHDLFormatter.test.ts +++ b/tests/VHDLFormatter.test.ts @@ -36,6 +36,14 @@ describe('VHDLFormatter', function () { let result = beautify(input, settings); expect(result).toBe(input); }); + + it('add new line at the end of the file', function () { + let settings = GetDefaultSettings(); + settings.AddNewLine = true; + let input = 'test'; + let result = beautify(input, settings); + expect(result).toBe("test\r\n"); + }); }); function GetDefaultSettings(indentation: string = " "): BeautifierSettings { @@ -48,5 +56,5 @@ function GetDefaultSettings(indentation: string = " "): BeautifierSettings { } function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings { - return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n"); + return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false); } \ No newline at end of file diff --git a/tests/VHDLFormatterUnitTests.ts b/tests/VHDLFormatterUnitTests.ts index c0e7f75..f51ee66 100644 --- a/tests/VHDLFormatterUnitTests.ts +++ b/tests/VHDLFormatterUnitTests.ts @@ -1368,7 +1368,7 @@ function IntegrationTest78() { } function IntegrationTest79() { - let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n"); + let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n", false); let input = "case when others;\r\nx : STRING;\r\ny : BIT;"; let actual = beautify(input, settings); assertAndCountTest("uppercase typename and lowercase keyword", input, actual); @@ -1438,7 +1438,7 @@ function GetDefaultSettings(indentation: string = " "): BeautifierSettings { } function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings { - return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n"); + return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false); }