From b0c4c68b2cecf52feb22b78a91c6a9291f734325 Mon Sep 17 00:00:00 2001 From: g2384 Date: Mon, 19 Oct 2020 16:32:25 +0100 Subject: [PATCH] align concurrent signal assignment --- VHDLFormatter.js | 18 +++++++++++++++++- VHDLFormatter.ts | 19 ++++++++++++++++++- tests/VHDLFormatter.test.ts | 7 +++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/VHDLFormatter.js b/VHDLFormatter.js index bc3aa0d..cfb4a65 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -656,11 +656,27 @@ function beautifySemicolonBlock(inputs, result, settings, startIndex, parentEndI [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); result.push(new FormattedLine(inputs[startIndex], indent)); if (endIndex != startIndex) { - let i = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + alignSignalAssignmentBlock(settings, inputs, startIndex, endIndex, result); } return [endIndex, parentEndIndex]; } exports.beautifySemicolonBlock = beautifySemicolonBlock; +function alignSignalAssignmentBlock(settings, inputs, startIndex, endIndex, result) { + if (settings.Indentation.replace(/ +/g, "").length == 0) { + let reg = new RegExp("^([\\w\\\\]+[\\s]*<=\\s*)"); + let match = reg.exec(inputs[startIndex]); + if (match != null) { + let length = match[0].length; + let prefixLength = length - settings.Indentation.length; + let prefix = new Array(prefixLength + 1).join(" "); + for (let i = startIndex + 1; i <= endIndex; i++) { + let fl = result[i]; + fl.Line = prefix + fl.Line; + } + } + } +} function beautify3(inputs, result, settings, startIndex, indent, endIndex) { let i; let regexOneLineBlockKeyWords = new RegExp(/(PROCEDURE)[^\w](?!.+[^\w]IS([^\w]|$))/); //match PROCEDURE..; but not PROCEDURE .. IS; diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index c1b6a5c..ff3e211 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -725,12 +725,29 @@ export function beautifySemicolonBlock(inputs: Array, result: (Formatted [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); result.push(new FormattedLine(inputs[startIndex], indent)); if (endIndex != startIndex) { - let i = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + alignSignalAssignmentBlock(settings, inputs, startIndex, endIndex, result); } return [endIndex, parentEndIndex]; } +function alignSignalAssignmentBlock(settings: BeautifierSettings, inputs: string[], startIndex: number, endIndex: number, result: (FormattedLine | FormattedLine[])[]) { + if (settings.Indentation.replace(/ +/g, "").length == 0) { + let reg: RegExp = new RegExp("^([\\w\\\\]+[\\s]*<=\\s*)"); + let match = reg.exec(inputs[startIndex]); + if (match != null) { + let length = match[0].length; + let prefixLength = length - settings.Indentation.length; + let prefix = new Array(prefixLength + 1).join(" "); + for (let i = startIndex + 1; i <= endIndex; i++) { + let fl = (result[i] as FormattedLine); + fl.Line = prefix + fl.Line; + } + } + } +} + export function beautify3(inputs: Array, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number, endIndex?: number): number { let i: number; let regexOneLineBlockKeyWords: RegExp = new RegExp(/(PROCEDURE)[^\w](?!.+[^\w]IS([^\w]|$))/);//match PROCEDURE..; but not PROCEDURE .. IS; diff --git a/tests/VHDLFormatter.test.ts b/tests/VHDLFormatter.test.ts index afc7b3f..81d8d2f 100644 --- a/tests/VHDLFormatter.test.ts +++ b/tests/VHDLFormatter.test.ts @@ -95,6 +95,13 @@ describe('VHDLFormatter', function () { let result = beautify(input, setting); expect(result).toBe(expected); }); + + it('semicolon blocks are aligned', function () { + let settings = GetDefaultSettings(); + let input = 'OUT <= In0 AFTER 2ns WHEN "00",\r\n In1 AFTER 2ns WHEN "01",\r\n In2 AFTER 2ns WHEN "10",\r\n In3 AFTER 2ns WHEN "11";'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); }); function GetDefaultSettings(indentation: string = " "): BeautifierSettings {