From 07c89b9feb32684ff0c47770efe1f5fe70ccf553 Mon Sep 17 00:00:00 2001 From: Keegan Crankshaw Date: Fri, 20 Aug 2021 12:40:42 +0200 Subject: [PATCH] adds the ability to format case statements in the linux kernel style --- VHDLFormatter.js | 9 +++++++-- VHDLFormatter.ts | 10 ++++++++-- index.html | 12 ++++++++++-- main.js | 1 + main.ts | 1 + tests/VHDLFormatterUnitTests.ts | 4 ++-- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/VHDLFormatter.js b/VHDLFormatter.js index b79cd6f..b45508d 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -258,7 +258,7 @@ class signAlignSettings { } exports.signAlignSettings = signAlignSettings; class BeautifierSettings { - constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine, addNewLine) { + constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine, addNewLine, caseWhenIndent) { this.RemoveComments = removeComments; this.RemoveAsserts = removeReport; this.CheckAlias = checkAlias; @@ -269,6 +269,7 @@ class BeautifierSettings { this.NewLineSettings = newLineSettings; this.EndOfLine = endOfLine; this.AddNewLine = addNewLine; + this.CaseWhenIndent = caseWhenIndent; } } exports.BeautifierSettings = BeautifierSettings; @@ -610,7 +611,11 @@ function beautifyCaseBlock(block, result, settings, indent) { } result.push(new FormattedLine(block.lines[block.cursor], indent)); block.cursor++; - beautify3(block, result, settings, indent + 2); + let caseindent = 2; + if (settings.CaseWhenIndent == false) { + caseindent = 1; + } + beautify3(block, result, settings, indent + caseindent); result[block.cursor].Indent = indent; } exports.beautifyCaseBlock = beautifyCaseBlock; diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index d120fb5..c0d1b82 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -306,9 +306,10 @@ export class BeautifierSettings { NewLineSettings: NewLineSettings; EndOfLine: string; AddNewLine: boolean; + CaseWhenIndent: boolean; constructor(removeComments: boolean, removeReport: boolean, checkAlias: boolean, signAlignSettings: signAlignSettings, keywordCase: string, typeNameCase: string, indentation: string, - newLineSettings: NewLineSettings, endOfLine: string, addNewLine: boolean) { + newLineSettings: NewLineSettings, endOfLine: string, addNewLine: boolean, caseWhenIndent: boolean) { this.RemoveComments = removeComments; this.RemoveAsserts = removeReport; this.CheckAlias = checkAlias; @@ -319,6 +320,7 @@ export class BeautifierSettings { this.NewLineSettings = newLineSettings; this.EndOfLine = endOfLine; this.AddNewLine = addNewLine; + this.CaseWhenIndent = caseWhenIndent; } } @@ -690,7 +692,11 @@ export function beautifyCaseBlock(block: CodeBlock, result: (FormattedLine | For } result.push(new FormattedLine(block.lines[block.cursor], indent)); block.cursor++; - beautify3(block, result, settings, indent + 2); + let caseindent = 2; + if (settings.CaseWhenIndent == false){ + caseindent = 1; + } + beautify3(block, result, settings, indent + caseindent); (result[block.cursor]).Indent = indent; } diff --git a/index.html b/index.html index 17f3eeb..e2352ee 100644 --- a/index.html +++ b/index.html @@ -170,6 +170,10 @@

VHDL Beautifier, Formatter

+
+ + +
Sign Alignment
@@ -370,6 +374,7 @@

VHDL Beautifier, Formatter

document.getElementById("keyword_div").elements.namedItem("keywordcase").value = beautifierSettings.KeywordCase; document.getElementById("typename_div").elements.namedItem("typenamecase").value = beautifierSettings.TypeNameCase; document.getElementById("mix_letter").checked = setting.mixLetter; + document.getElementById("caseWhenIndent").checked = beautifierSettings.CaseWhenIndent; var eof = beautifierSettings.EndOfLine eof = eof.replace(/\r/g, "\\r"); eof = eof.replace(/\n/g, "\\n"); @@ -409,8 +414,9 @@

VHDL Beautifier, Formatter

var remove_lines = document.getElementById("remove_lines").checked; var mix_letter = document.getElementById("mix_letter").checked; + var caseWhenIndent = document.getElementById("caseWhenIndent").checked; [beautifierSettings, compress] = CreateSettings(); - vhdlSettings = new VhdlSettings(beautifierSettings, remove_lines, compress, mix_letter); + vhdlSettings = new VhdlSettings(beautifierSettings, remove_lines, compress, mix_letter, caseWhenIndent); saveSetting(vhdlSettings); input = beautify(input, beautifierSettings); @@ -455,6 +461,7 @@

VHDL Beautifier, Formatter

var keywordcase = document.getElementById("keyword_div").elements.namedItem("keywordcase").value; var typenamecase = document.getElementById("typename_div").elements.namedItem("typenamecase").value; var endOfLine = document.getElementById("cust_eol").value; + var caseWhenIndent = document.getElementById("caseWhenIndent").checked; endOfLine = endOfLine.replace(/\\r/g, "\r"); endOfLine = endOfLine.replace(/\\n/g, "\n"); if (compress) { @@ -500,7 +507,8 @@

VHDL Beautifier, Formatter

indentation, newLineSettings, endOfLine, - addNewLine); + addNewLine, + caseWhenIndent); return [beautifierSettings, compress]; } diff --git a/main.js b/main.js index ffcd1af..30af7ef 100644 --- a/main.js +++ b/main.js @@ -15,6 +15,7 @@ function noFormat() { "customise_indentation", "compress", "mix_letter", + "caseWhenIndent", "cust_eol", "sign_align_mode", "keyword", diff --git a/main.ts b/main.ts index f0056f8..65bef9e 100644 --- a/main.ts +++ b/main.ts @@ -15,6 +15,7 @@ function noFormat() { "customise_indentation", "compress", "mix_letter", + "caseWhenIndent", "cust_eol", "sign_align_mode", "keyword", diff --git a/tests/VHDLFormatterUnitTests.ts b/tests/VHDLFormatterUnitTests.ts index ba5eb12..58329ff 100644 --- a/tests/VHDLFormatterUnitTests.ts +++ b/tests/VHDLFormatterUnitTests.ts @@ -1365,7 +1365,7 @@ function IntegrationTest78() { } function IntegrationTest79() { - let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n", false); + let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n", false, true); let input = "case when others;\r\nx : STRING;\r\ny : BIT;"; let actual = beautify(input, settings); assertAndCountTest("uppercase typename and lowercase keyword", input, actual); @@ -1435,7 +1435,7 @@ function GetDefaultSettings(indentation: string = " "): BeautifierSettings { } function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings { - return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false); + return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false, true); }