diff --git a/VHDLFormatter.js b/VHDLFormatter.js index 6cb25fe..a65552d 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.RemoveAsserts = exports.ApplyNoNewLineAfter = exports.beautify3 = exports.beautifySemicolonBlock = exports.beautifyComponentBlock = exports.beautifyCaseBlock = exports.AlignSign = exports.AlignSigns = exports.beautifyPortGenericBlock = exports.FormattedLineToString = exports.FormattedLine = exports.beautify = exports.BeautifierSettings = exports.signAlignSettings = exports.SetNewLinesAfterSymbols = exports.NewLineSettings = void 0; +exports.RemoveAsserts = exports.ApplyNoNewLineAfter = exports.beautify3 = exports.beautifySemicolonBlock = exports.beautifyPackageIsNewBlock = exports.beautifyComponentBlock = exports.beautifyCaseBlock = exports.AlignSign = exports.AlignSigns = exports.beautifyPortGenericBlock = exports.FormattedLineToString = exports.FormattedLine = exports.beautify = exports.BeautifierSettings = exports.signAlignSettings = exports.SetNewLinesAfterSymbols = exports.NewLineSettings = void 0; let isTesting = false; const ILEscape = "@@"; const ILCommentPrefix = ILEscape + "comments"; @@ -609,6 +609,24 @@ function beautifyComponentBlock(inputs, result, settings, startIndex, parentEndI return [endIndex, parentEndIndex]; } exports.beautifyComponentBlock = beautifyComponentBlock; +function beautifyPackageIsNewBlock(inputs, result, settings, startIndex, parentEndIndex, indent) { + let endIndex = startIndex; + for (let i = startIndex; i < inputs.length; i++) { + if (inputs[i].regexIndexOf(/;(\s|$)/) >= 0) { + endIndex = i; + break; + } + } + result.push(new FormattedLine(inputs[startIndex], indent)); + if (endIndex != startIndex) { + let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + let incremental = actualEndIndex - endIndex; + endIndex += incremental; + parentEndIndex += incremental; + } + return [endIndex, parentEndIndex]; +} +exports.beautifyPackageIsNewBlock = beautifyPackageIsNewBlock; function beautifySemicolonBlock(inputs, result, settings, startIndex, parentEndIndex, indent) { let endIndex = startIndex; [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); @@ -681,6 +699,13 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) { Mode = modeCache; continue; } + if (input.regexStartsWith(/PACKAGE[\s\w]+IS\s+NEW/)) { + let modeCache = Mode; + Mode = FormatMode.EndsWithSemicolon; + [i, endIndex] = beautifyPackageIsNewBlock(inputs, result, settings, i, endIndex, indent); + Mode = modeCache; + continue; + } if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) { let modeCache = Mode; Mode = FormatMode.EndsWithSemicolon; diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 8acd3d4..8aee124 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -677,6 +677,25 @@ export function beautifyComponentBlock(inputs: Array, result: (Formatted return [endIndex, parentEndIndex]; } +export function beautifyPackageIsNewBlock(inputs: Array, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, parentEndIndex: number, indent: number): [number, number] { + let endIndex = startIndex; + for (let i = startIndex; i < inputs.length; i++) { + if (inputs[i].regexIndexOf(/;(\s|$)/) >= 0) { + endIndex = i; + break; + } + } + result.push(new FormattedLine(inputs[startIndex], indent)); + if (endIndex != startIndex) { + let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + let incremental = actualEndIndex - endIndex; + endIndex += incremental; + parentEndIndex += incremental; + } + + return [endIndex, parentEndIndex]; +} + export function beautifySemicolonBlock(inputs: Array, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, parentEndIndex: number, indent: number): [number, number] { let endIndex = startIndex; [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); @@ -750,6 +769,13 @@ export function beautify3(inputs: Array, result: (FormattedLine | Format Mode = modeCache; continue; } + if (input.regexStartsWith(/PACKAGE[\s\w]+IS\s+NEW/)) { + let modeCache = Mode; + Mode = FormatMode.EndsWithSemicolon; + [i, endIndex] = beautifyPackageIsNewBlock(inputs, result, settings, i, endIndex, indent); + Mode = modeCache; + continue; + } if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) { let modeCache = Mode; Mode = FormatMode.EndsWithSemicolon; diff --git a/tests/VHDLFormatter.test.ts b/tests/VHDLFormatter.test.ts index c1f9d90..88d2cfa 100644 --- a/tests/VHDLFormatter.test.ts +++ b/tests/VHDLFormatter.test.ts @@ -51,6 +51,20 @@ describe('VHDLFormatter', function () { let result = beautify(input, settings); expect(result).toBe("x : STRING;\r\ny : STD_LOGIC_VECTOR;"); }); + + it('package ends with ;', function () { + let settings = GetDefaultSettings(); + let input = "PACKAGE p IS NEW work.p_template\r\n GENERIC MAP(N => N);\r\nUSE p.ALL;"; + let result = beautify(input, settings); + expect(result).toBe(input); + }); + + it('package ends with ; (same line)', function () { + let settings = GetDefaultSettings(); + let input = "PACKAGE p IS NEW work.p_template;\r\nUSE p.ALL;"; + let result = beautify(input, settings); + expect(result).toBe(input); + }); }); function GetDefaultSettings(indentation: string = " "): BeautifierSettings {