From 463eaf27d4ca6e036aa3540e6053a20036bd9d82 Mon Sep 17 00:00:00 2001 From: g2384 Date: Fri, 22 Jan 2021 19:22:33 +0000 Subject: [PATCH] indent after assignment symbol --- VHDLFormatter.js | 7 ++++ VHDLFormatter.ts | 9 ++++- tests/VHDLFormatter.test.ts | 78 ++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/VHDLFormatter.js b/VHDLFormatter.js index 6ed10cd..6a530a0 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -784,6 +784,13 @@ function beautify3(block, result, settings, indent) { Mode = modeCache; continue; } + if (input.regexIndexOf(/:=(\s*@@comments\d+\s*)?$/) > 0) { + let modeCache = Mode; + Mode = FormatMode.EndsWithSemicolon; + beautifySemicolonBlock(block, result, settings, 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 51906da..3dda213 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -516,7 +516,7 @@ function GetCloseparentheseEndIndex(block: CodeBlock) { let openParentheseCount: number = 0; let closeParentheseCount: number = 0; let startIndex = block.cursor; - for (;block.cursor <= block.end; block.cursor++) { + for (; block.cursor <= block.end; block.cursor++) { let input = block.lines[block.cursor]; openParentheseCount += input.count("("); closeParentheseCount += input.count(")"); @@ -866,6 +866,13 @@ export function beautify3(block: CodeBlock, result: (FormattedLine | FormattedLi Mode = modeCache; continue; } + if (input.regexIndexOf(/:=(\s*@@comments\d+\s*)?$/) > 0) { + let modeCache = Mode; + Mode = FormatMode.EndsWithSemicolon; + beautifySemicolonBlock(block, result, settings, 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 a225c3b..03fb466 100644 --- a/tests/VHDLFormatter.test.ts +++ b/tests/VHDLFormatter.test.ts @@ -119,7 +119,7 @@ describe('VHDLFormatter', function () { let result = beautify(input, settings); expect(result).toBe(input); }); - + it('support invalid line', function () { let settings = GetDefaultSettings(); settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); @@ -135,7 +135,7 @@ describe('VHDLFormatter', function () { let result = beautify(input, settings); expect(result).toBe(input); }); - + it('one-line initial values for constant', function () { let settings = GetDefaultSettings(); settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); @@ -143,8 +143,82 @@ describe('VHDLFormatter', function () { let result = beautify(input, settings); expect(result).toBe(input); }); + + it('indent assignment statement (multiline, no comment)', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = "CONSTANT Vcc : SIGNAL :=\r\n'1'; --logic 1 constant\r\nCONSTANT zero4 : bit_vector(0 TO 3) :=\r\n('0', '0', '0', '0');"; + let output = "CONSTANT Vcc : SIGNAL :=\r\n '1'; --logic 1 constant\r\nCONSTANT zero4 : bit_vector(0 TO 3) :=\r\n ('0', '0', '0', '0');"; + let result = beautify(input, settings); + expect(result).toBe(output); + }) + + it('indent assignment statement (with comment)', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = "CONSTANT Vcc : SIGNAL := --logic 1 constant\r\n'1';\r\nCONSTANT zero4 : bit_vector(0 TO 3) :=--test\r\n('0', '0', '0', '0');"; + let output = "CONSTANT Vcc : SIGNAL := --logic 1 constant\r\n '1';\r\nCONSTANT zero4 : bit_vector(0 TO 3) := --test\r\n ('0', '0', '0', '0');"; + let result = beautify(input, settings); + expect(result).toBe(output); + }) + + it('indent assignment statement (multi line)', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = [ + "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=", + " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));", + "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=", + " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));" + ]; + let output = [ + "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=", + " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));", + "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=", + " to_bitvector(STD_ULOGIC_VECTOR(to_unsigned(C_M_AXI_BURST_LEN - 1, 13)));" + ]; + let result = beautifyTestHelper(input, settings); + expect(result).toStrictEqual(output); + }) + + it('indent assignment statement (multi line (2))', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = [ + "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=", + "to_bitvector(", + "STD_ULOGIC_VECTOR(", + "to_unsigned(", + "C_M_AXI_BURST_LEN - 1, 13)));", + "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=", + "to_bitvector(", + "STD_ULOGIC_VECTOR(", + "to_unsigned(", + "C_M_AXI_BURST_LEN - 1, 13)));" + ]; + let output = [ + "CONSTANT ALMOST_EMPTY_OFFSET : bit_vector(12 DOWNTO 0) :=", + " to_bitvector(", + " STD_ULOGIC_VECTOR(", + " to_unsigned(", + " C_M_AXI_BURST_LEN - 1, 13)));", + "CONSTANT ALMOST_FULL_OFFSET : bit_vector(12 DOWNTO 0) :=", + " to_bitvector(", + " STD_ULOGIC_VECTOR(", + " to_unsigned(", + " C_M_AXI_BURST_LEN - 1, 13)));" + ]; + let result = beautifyTestHelper(input, settings); + expect(result).toStrictEqual(output); + }) }); +function beautifyTestHelper(array: Array, settings: BeautifierSettings): Array { + let input = array.join("\r\n"); + let result = beautify(input, settings); + return result.split("\r\n"); +} + function GetDefaultSettings(indentation: string = " "): BeautifierSettings { let new_line_after_symbols = new NewLineSettings(); new_line_after_symbols.newLineAfter = ["then", ";"];