diff --git a/README.md b/README.md index 050c9ac..82240b9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,11 @@ VHDL formatter web online written in javascript ## Release Notes +### 2.3 [2018-02-22] + +- bugfix "remove non-comment code by mistake" +- add `tests` folder, improve the project management + ### 2.2 [2018-10-16] - support enumerated types diff --git a/VHDLFormatter.js b/VHDLFormatter.js index 0920106..434e02d 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -153,19 +153,11 @@ function MixLetters(input) { function EscapeComments(arr, comments, commentIndex) { for (var i = 0; i < arr.length; i++) { let line = arr[i]; - var firstCharIndex = line.regexIndexOf(/[a-zA-Z0-9\(\&\)%_\+'"|\\]/); var commentStartIndex = line.indexOf("--"); - if (firstCharIndex < commentStartIndex && firstCharIndex >= 0) { + if (commentStartIndex >= 0) { comments.push(line.substr(commentStartIndex)); - arr[i] = line.substr(firstCharIndex, commentStartIndex - firstCharIndex) + ILCommentPrefix + (commentIndex++); - } - else if ((firstCharIndex > commentStartIndex && commentStartIndex >= 0) || (firstCharIndex < 0 && commentStartIndex >= 0)) { - comments.push(line.substr(commentStartIndex)); - arr[i] = ILCommentPrefix + (commentIndex++); - } - else { - firstCharIndex = firstCharIndex < 0 ? 0 : firstCharIndex; - arr[i] = line.substr(firstCharIndex); + arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + commentIndex; + commentIndex++; } } return commentIndex; @@ -253,6 +245,7 @@ function beautify(input, settings) { var arr = input.split("\r\n"); var comments = [], commentsIndex = 0; commentsIndex = EscapeComments(arr, comments, commentsIndex); + RemoveLeadingWhitespaces(arr); input = arr.join("\r\n"); if (settings.RemoveComments) { input = input.replace(/\r\n[ \t]*@@comments[0-9]+[ \t]*\r\n/g, '\r\n'); @@ -321,6 +314,11 @@ function beautify(input, settings) { return input; } exports.beautify = beautify; +function RemoveLeadingWhitespaces(arr) { + for (var i = 0; i < arr.length; i++) { + arr[i] = arr[i].replace(/^\s+/, ""); + } +} class FormattedLine { constructor(line, indent) { this.Line = line; diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 39fb11a..a7ab9b5 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -62,7 +62,7 @@ declare global { regexCount: (pattern: RegExp) => number; convertToRegexBlockWords: () => RegExp; } - interface Array{ + interface Array { convertToRegexBlockWords: () => RegExp; } } @@ -186,17 +186,11 @@ function MixLetters(input: string) { function EscapeComments(arr: Array, comments: Array, commentIndex: number): number { for (var i = 0; i < arr.length; i++) { let line: string = arr[i]; - var firstCharIndex = line.regexIndexOf(/[a-zA-Z0-9\(\&\)%_\+'"|\\]/); var commentStartIndex = line.indexOf("--"); - if (firstCharIndex < commentStartIndex && firstCharIndex >= 0) { + if (commentStartIndex >= 0) { comments.push(line.substr(commentStartIndex)); - arr[i] = line.substr(firstCharIndex, commentStartIndex - firstCharIndex) + ILCommentPrefix + (commentIndex++); - } else if ((firstCharIndex > commentStartIndex && commentStartIndex >= 0) || (firstCharIndex < 0 && commentStartIndex >= 0)) { - comments.push(line.substr(commentStartIndex)); - arr[i] = ILCommentPrefix + (commentIndex++); - } else { - firstCharIndex = firstCharIndex < 0 ? 0 : firstCharIndex; - arr[i] = line.substr(firstCharIndex); + arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + commentIndex; + commentIndex++ } } return commentIndex @@ -304,6 +298,7 @@ export function beautify(input: string, settings: BeautifierSettings) { var comments = [], commentsIndex = 0; commentsIndex = EscapeComments(arr, comments, commentsIndex); + RemoveLeadingWhitespaces(arr); input = arr.join("\r\n"); if (settings.RemoveComments) { @@ -383,6 +378,12 @@ export function beautify(input: string, settings: BeautifierSettings) { return input; } +function RemoveLeadingWhitespaces(arr: Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] = arr[i].replace(/^\s+/, ""); + } +} + export class FormattedLine { Line: string; Indent: number; diff --git a/tests/VHDLFormatterUnitTests.ts b/tests/VHDLFormatterUnitTests.ts index a73d42e..f3615fc 100644 --- a/tests/VHDLFormatterUnitTests.ts +++ b/tests/VHDLFormatterUnitTests.ts @@ -908,6 +908,7 @@ function IntegrationTest() { IntegrationTest67(); IntegrationTest68(); IntegrationTest69(); + IntegrationTest70(); } function IntegrationTest23() { @@ -1339,6 +1340,14 @@ function IntegrationTest69() { assertAndCountTest("multiline enumerated type is", expected, actual); } +function IntegrationTest70() { + let settings = GetDefaultSettings(); + let input = 'test\r\n := test'; + let expected = 'test\r\n:= test'; + let actual = beautify(input, settings); + assertAndCountTest("multiline assignment", expected, actual); +} + function GetDefaultSettings() { let new_line_after_symbols = new NewLineSettings(); new_line_after_symbols.newLineAfter = ["then", ";"];