From f0912cbe51b67ff9c63752a5fb178e4142401ebe Mon Sep 17 00:00:00 2001 From: g2384 Date: Sat, 23 Feb 2019 20:10:07 +0000 Subject: [PATCH] fix unary operator formatting --- README.md | 1 + VHDLFormatter.js | 16 +++++++++------- VHDLFormatter.ts | 17 +++++++++-------- index.html | 2 +- tests/VHDLFormatterUnitTests.ts | 19 +++++++++++++++++++ 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b014fa4..1544da3 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ VHDL formatter web online written in javascript - fix exponential notation - user can choose EOL symbols (or use system's by default) - align comments (when user chooses "align" option) +- bugfix "extra whitespaces around unary minus or plus" Many thanks to [@MihaiBabiac](https://github.com/MihaiBabiac) diff --git a/VHDLFormatter.js b/VHDLFormatter.js index b4c24f4..32236c9 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -181,6 +181,8 @@ function beautify(input, settings) { var arr = input.split("\r\n"); var comments = EscapeComments(arr); var backslashes = escapeText(arr, "\\\\[^\\\\]+\\\\", ILBackslash); + let quotes = escapeText(arr, '"([^"]+)"', ILQuote); + let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote); RemoveLeadingWhitespaces(arr); input = arr.join("\r\n"); if (settings.RemoveComments) { @@ -188,17 +190,13 @@ function beautify(input, settings) { input = input.replace(/@@comments[0-9]+/g, ''); comments = []; } + input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames); input = RemoveExtraNewLines(input); input = input.replace(/[\t ]+/g, ' '); input = input.replace(/\([\t ]+/g, '\('); input = input.replace(/[ ]+;/g, ';'); input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1'); arr = input.split("\r\n"); - let quotes = escapeText(arr, '"([^"]+)"', ILQuote); - let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote); - input = arr.join("\r\n"); - input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames); - arr = input.split("\r\n"); if (settings.RemoveAsserts) { RemoveAsserts(arr); //RemoveAsserts must be after EscapeQuotes } @@ -212,7 +210,7 @@ function beautify(input, settings) { input = arr.join("\r\n"); input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end'); input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 '); - input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3'); // Fix exponential notation format broken by previous step + input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3'); // fix exponential notation format broken by previous step input = input.replace(/[ ]?([,])[ ]?/g, '$1 '); input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2'); input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 '); @@ -226,7 +224,11 @@ function beautify(input, settings) { input = input.replace(/\r\n\r\n\r\n/g, '\r\n'); input = input.replace(/[\r\n\s]+$/g, ''); input = input.replace(/[ \t]+\)/g, ')'); - input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1'); //function(..\r\n)return type; -> function(..\r\n)return type; + input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1'); //function(..)\r\nreturn type; -> function(..\r\n)return type; + let keywordAndSignRegex = new RegExp("(\\b" + KeyWords.join("\\b|\\b") + "\\b) +([\\-+]) +(\\w)", "g"); + input = input.replace(keywordAndSignRegex, "$1 $2$3"); // `WHEN - 2` -> `WHEN -2` + input = input.replace(/([,|]) +([+\-]) +(\w)/g, '$1 $2$3'); // `1, - 2)` -> `1, -2)` + input = input.replace(/(\() +([+\-]) +(\w)/g, '$1$2$3'); // `( - 2)` -> `(-2)` arr = input.split("\r\n"); let result = []; beautify3(arr, result, settings, 0, 0); diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 7f751a2..a7e8d6c 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -230,6 +230,8 @@ export function beautify(input: string, settings: BeautifierSettings) { var arr = input.split("\r\n"); var comments = EscapeComments(arr); var backslashes = escapeText(arr, "\\\\[^\\\\]+\\\\", ILBackslash); + let quotes = escapeText(arr, '"([^"]+)"', ILQuote); + let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote); RemoveLeadingWhitespaces(arr); input = arr.join("\r\n"); @@ -239,18 +241,13 @@ export function beautify(input: string, settings: BeautifierSettings) { comments = []; } + input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames); input = RemoveExtraNewLines(input); input = input.replace(/[\t ]+/g, ' '); input = input.replace(/\([\t ]+/g, '\('); input = input.replace(/[ ]+;/g, ';'); input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1'); - arr = input.split("\r\n"); - let quotes = escapeText(arr, '"([^"]+)"', ILQuote); - let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote); - input = arr.join("\r\n"); - input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames); - arr = input.split("\r\n"); if (settings.RemoveAsserts) { RemoveAsserts(arr);//RemoveAsserts must be after EscapeQuotes @@ -267,7 +264,7 @@ export function beautify(input: string, settings: BeautifierSettings) { input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end'); input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 '); - input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3');// Fix exponential notation format broken by previous step + input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3');// fix exponential notation format broken by previous step input = input.replace(/[ ]?([,])[ ]?/g, '$1 '); input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2'); input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 '); @@ -281,7 +278,11 @@ export function beautify(input: string, settings: BeautifierSettings) { input = input.replace(/\r\n\r\n\r\n/g, '\r\n'); input = input.replace(/[\r\n\s]+$/g, ''); input = input.replace(/[ \t]+\)/g, ')'); - input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1');//function(..\r\n)return type; -> function(..\r\n)return type; + input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1');//function(..)\r\nreturn type; -> function(..\r\n)return type; + let keywordAndSignRegex = new RegExp("(\\b" + KeyWords.join("\\b|\\b") + "\\b) +([\\-+]) +(\\w)", "g"); + input = input.replace(keywordAndSignRegex, "$1 $2$3");// `WHEN - 2` -> `WHEN -2` + input = input.replace(/([,|]) +([+\-]) +(\w)/g, '$1 $2$3');// `1, - 2)` -> `1, -2)` + input = input.replace(/(\() +([+\-]) +(\w)/g, '$1$2$3');// `( - 2)` -> `(-2)` arr = input.split("\r\n"); let result: (FormattedLine | FormattedLine[])[] = []; beautify3(arr, result, settings, 0, 0); diff --git a/index.html b/index.html index 0fd1a26..15881df 100644 --- a/index.html +++ b/index.html @@ -311,7 +311,7 @@
- +
Align signs in diff --git a/tests/VHDLFormatterUnitTests.ts b/tests/VHDLFormatterUnitTests.ts index 83373f6..bf94c06 100644 --- a/tests/VHDLFormatterUnitTests.ts +++ b/tests/VHDLFormatterUnitTests.ts @@ -915,6 +915,8 @@ function IntegrationTest() { IntegrationTest74(); IntegrationTest75(); IntegrationTest76(); + IntegrationTest77(); + IntegrationTest78(); } function IntegrationTest23() { @@ -1339,6 +1341,23 @@ function IntegrationTest76() { assertAndCountTest("align <= => signs", expected, actual); } +function IntegrationTest77() { + let settings = GetDefaultSettings(); + settings.SignAlignAll = true; + let input = "WHEN -2;\r\nSIGNAL +0;"; + let actual = beautify(input, settings); + assertAndCountTest("negative sign and number", input, actual); +} + +function IntegrationTest78() { + let settings = GetDefaultSettings(); + settings.SignAlignAll = true; + let input = "sfixed(4 downto - 18);\r\nx <= to_sfixed( - 2.3, x);\r\nresize(x + 1, 4, - 18) when others;"; + let expected = "sfixed(4 DOWNTO -18);\r\nx <= to_sfixed(-2.3, x);\r\nresize(x + 1, 4, -18) WHEN OTHERS;"; + let actual = beautify(input, settings); + assertAndCountTest("negative sign and number", expected, actual); +} + function GetDefaultSettings(indentation: string = " "): BeautifierSettings { let new_line_after_symbols = new NewLineSettings(); new_line_after_symbols.newLineAfter = ["then", ";"];