diff --git a/VHDLFormatter.js b/VHDLFormatter.js index f955dc1..2a0482c 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -68,9 +68,23 @@ String.prototype.regexIndexOf = function (pattern, startIndex) { return (-1 === searchResult) ? -1 : searchResult + startIndex; }; String.prototype.regexLastIndexOf = function (pattern, startIndex) { - startIndex = startIndex === undefined ? this.length : startIndex; - var searchResult = this.substr(0, startIndex).reverse().regexIndexOf(pattern, 0); - return (-1 === searchResult) ? -1 : this.length - ++searchResult; + pattern = (pattern.global) ? pattern : + new RegExp(pattern.source, 'g' + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '')); + if (typeof (startIndex) === 'undefined') { + startIndex = this.length; + } + else if (startIndex < 0) { + startIndex = 0; + } + const stringToWorkWith = this.substring(0, startIndex + 1); + let lastIndexOf = -1; + let nextStop = 0; + let result; + while ((result = pattern.exec(stringToWorkWith)) != null) { + lastIndexOf = result.index; + pattern.lastIndex = ++nextStop; + } + return lastIndexOf; }; String.prototype.reverse = function () { return this.split('').reverse().join(''); @@ -96,6 +110,66 @@ function EscapeComments(arr) { count++; } } + var isInComment = false; + var commentRegex = new RegExp("(?<=" + ILCommentPrefix + "[\\d]+)."); + for (var i = 0; i < arr.length; i++) { + var commentStartIndex = 0; + var hasComment = true; + var commentEndInlineIndex = 0; + while (hasComment) { + var line = arr[i]; + if (!isInComment) { + commentStartIndex = line.indexOf("/*"); + var commentEndIndex = line.indexOf("*/", commentStartIndex); + if (commentStartIndex >= 0) { + if (commentEndIndex >= 0) { + commentEndInlineIndex = commentEndIndex + 2; + isInComment = false; + comments.push(line.substring(commentStartIndex, commentEndInlineIndex)); + arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count + line.substr(commentEndInlineIndex); + count++; + hasComment = true; + if (commentStartIndex + 2 == line.length) { + hasComment = false; + } + } + else { + isInComment = true; + comments.push(line.substr(commentStartIndex)); + arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count; + count++; + hasComment = false; + } + } + else { + hasComment = false; + } + continue; + } + if (isInComment) { + var lastCommentEndIndex = line.regexLastIndexOf(commentRegex, line.length); + if (commentStartIndex == 0) { + var commentEndIndex = line.indexOf("*/", lastCommentEndIndex); + } + else { + var commentEndIndex = line.indexOf("*/", commentStartIndex); + } + if (commentEndIndex >= 0) { + isInComment = false; + comments.push(line.substr(0, commentEndIndex + 2)); + arr[i] = ILCommentPrefix + count + line.substr(commentEndIndex + 2); + count++; + hasComment = true; + } + else { + comments.push(line); + arr[i] = ILCommentPrefix + count; + count++; + hasComment = false; + } + } + } + } return comments; } function ToLowerCases(arr) { @@ -623,6 +697,10 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) { Mode = modeCache; continue; } + if (input.regexStartsWith(/.*?\:\=\s*\($/)) { + [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":="); + continue; + } if (input.regexStartsWith(/[\w\s:]*\bPORT\b([\s]|$)/)) { [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PORT"); continue; @@ -635,10 +713,6 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) { [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "GENERIC"); continue; } - if (input.regexStartsWith(/.*?\:\=\s*\($/)) { - [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":="); - continue; - } if (input.regexStartsWith(/[\w\s:]*PROCEDURE[\s\w]+\($/)) { [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PROCEDURE"); if (inputs[i].regexStartsWith(/.*\)[\s]*IS/)) { @@ -764,4 +838,4 @@ function RemoveExtraNewLines(input) { input = input.replace(/\r\n\r\n\r\n/g, '\r\n'); return input; } -//# sourceMappingURL=VHDLFormatter.js.map +//# sourceMappingURL=VHDLFormatter.js.map \ No newline at end of file diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 738adf2..202cbf3 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -93,9 +93,22 @@ String.prototype.regexIndexOf = function (pattern, startIndex) { } String.prototype.regexLastIndexOf = function (pattern, startIndex) { - startIndex = startIndex === undefined ? this.length : startIndex; - var searchResult = this.substr(0, startIndex).reverse().regexIndexOf(pattern, 0); - return (-1 === searchResult) ? -1 : this.length - ++searchResult; + pattern = (pattern.global) ? pattern : + new RegExp(pattern.source, 'g' + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '')); + if (typeof (startIndex) === 'undefined') { + startIndex = this.length; + } else if (startIndex < 0) { + startIndex = 0; + } + const stringToWorkWith = this.substring(0, startIndex + 1); + let lastIndexOf = -1; + let nextStop = 0; + let result: RegExpExecArray; + while ((result = pattern.exec(stringToWorkWith)) != null) { + lastIndexOf = result.index; + pattern.lastIndex = ++nextStop; + } + return lastIndexOf; } String.prototype.reverse = function () { @@ -117,7 +130,7 @@ function EscapeComments(arr: Array): Array { var comments = []; var count = 0; for (var i = 0; i < arr.length; i++) { - var line: string = arr[i]; + var line = arr[i]; var commentStartIndex = line.indexOf("--"); if (commentStartIndex >= 0) { comments.push(line.substr(commentStartIndex)); @@ -125,6 +138,65 @@ function EscapeComments(arr: Array): Array { count++; } } + var isInComment = false; + var commentRegex = new RegExp("(?<=" + ILCommentPrefix + "[\\d]+)."); + for (var i = 0; i < arr.length; i++) { + var commentStartIndex = 0; + var hasComment = true; + var commentEndInlineIndex = 0; + while (hasComment) { + var line = arr[i]; + if (!isInComment) { + commentStartIndex = line.indexOf("/*"); + var commentEndIndex = line.indexOf("*/", commentStartIndex); + if (commentStartIndex >= 0) { + if (commentEndIndex >= 0) { + commentEndInlineIndex = commentEndIndex + 2; + isInComment = false; + comments.push(line.substring(commentStartIndex, commentEndInlineIndex)); + arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count + line.substr(commentEndInlineIndex); + count++; + hasComment = true; + if (commentStartIndex + 2 == line.length) { + hasComment = false; + } + } + else { + isInComment = true; + comments.push(line.substr(commentStartIndex)); + arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count; + count++; + hasComment = false; + } + } + else { + hasComment = false; + } + continue; + } + if (isInComment) { + var lastCommentEndIndex = line.regexLastIndexOf(commentRegex, line.length); + if (commentStartIndex == 0) { + var commentEndIndex = line.indexOf("*/", lastCommentEndIndex); + } + else { + var commentEndIndex = line.indexOf("*/", commentStartIndex); + } + if (commentEndIndex >= 0) { + isInComment = false; + comments.push(line.substr(0, commentEndIndex + 2)); + arr[i] = ILCommentPrefix + count + line.substr(commentEndIndex + 2); + count++; + hasComment = true; + } else { + comments.push(line); + arr[i] = ILCommentPrefix + count; + count++; + hasComment = false; + } + } + } + } return comments } diff --git a/tests/VHDLFormatter.test.ts b/tests/VHDLFormatter.test.ts new file mode 100644 index 0000000..2c4ccd6 --- /dev/null +++ b/tests/VHDLFormatter.test.ts @@ -0,0 +1,52 @@ +import { beautify, signAlignSettings } from "../VHDLFormatter"; +import { NewLineSettings } from "../VHDLFormatter"; +import { BeautifierSettings } from "../VHDLFormatter"; +import { RemoveAsserts } from "../VHDLFormatter"; +import { ApplyNoNewLineAfter } from "../VHDLFormatter"; +import { SetNewLinesAfterSymbols } from "../VHDLFormatter"; +import { beautify3 } from "../VHDLFormatter"; +import { FormattedLine } from "../VHDLFormatter"; +import { FormattedLineToString } from "../VHDLFormatter"; + +describe('VHDLFormatter', function () { + it('do not format block comments', function () { + let settings = GetDefaultSettings(); + let input = 'a;\r\n/*a; \r\n b;\r\nport ( ) ;\r\n/*\r\n*/c;'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); + + it('do not format block comments 2', function () { + let settings = GetDefaultSettings(); + let input = 'a;/*a;*/b;\r\nc;'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); + + it('do not format block comments 3', function () { + let settings = GetDefaultSettings(); + let input = 'a;\r\n/*a;*//*\r\n*/c;'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); + + it('do not format block comments 4', function () { + let settings = GetDefaultSettings(); + let input = '/*\r\nwoof\r\n*//*\r\nwoof\r\n*//*\r\nwoof\r\n*/'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); +}); + +function GetDefaultSettings(indentation: string = " "): BeautifierSettings { + let new_line_after_symbols = new NewLineSettings(); + new_line_after_symbols.newLineAfter = ["then", ";"]; + new_line_after_symbols.noNewLineAfter = ["generic"]; + let signAligns = new signAlignSettings(false, false, "", []); + let settings = getDefaultBeautifierSettings(new_line_after_symbols, signAligns, indentation); + return settings; +} + +function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings { + return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n"); +} \ No newline at end of file diff --git a/tests/VHDLFormatterUnitTests.ts b/tests/VHDLFormatterUnitTests.ts index 18d1af4..c0e7f75 100644 --- a/tests/VHDLFormatterUnitTests.ts +++ b/tests/VHDLFormatterUnitTests.ts @@ -9,7 +9,6 @@ import { FormattedLine } from "../VHDLFormatter"; import { FormattedLineToString } from "../VHDLFormatter"; import { CompareString } from "./assert"; import { assert } from "./assert"; -import { testDescriptiveCounter } from "./descriptiveCounterTests"; let testCount: number = 0; @@ -25,7 +24,6 @@ if (showUnitTests) { UnitTestFormattedLineToString(); UnitTestbeautify3(); console.log("total tests: " + testCount); - testDescriptiveCounter(); } interface Function { diff --git a/tsconfig.json b/tsconfig.json index cd07f0f..e42ea5e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,6 @@ "descriptiveCounter.ts", "VHDLFormatter.ts", "tests/VHDLFormatterUnitTests.ts", - "tests/descriptiveCounterTests.ts", "tests/assert.ts", ], "exclude": [