From e9eb144aa76a0dbc5715de427656fd2e66e1882b Mon Sep 17 00:00:00 2001 From: g2384 Date: Sat, 23 Feb 2019 22:06:39 +0000 Subject: [PATCH] better html; align sign locally --- README.md | 2 + VHDLFormatter.js | 49 +++--- VHDLFormatter.ts | 57 ++++--- index.html | 272 ++++++++++++++++++-------------- main.js | 5 +- main.ts | 5 +- tests/VHDLFormatterUnitTests.ts | 57 ++++--- 7 files changed, 269 insertions(+), 178 deletions(-) diff --git a/README.md b/README.md index dfe6e11..0db6daf 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ VHDL formatter web online written in javascript - use local storage to store settings - add `main.js` - treat key words and type names separately +- expand/hide setting options +- align signs locally or globally ### 2.3 [2018-02-22] diff --git a/VHDLFormatter.js b/VHDLFormatter.js index fde25c5..d8fa990 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -155,13 +155,21 @@ function SetNewLinesAfterSymbols(text, newLineSettings) { return text; } exports.SetNewLinesAfterSymbols = SetNewLinesAfterSymbols; +class signAlignSettings { + constructor(isRegional, isAll, mode, keyWords) { + this.isRegional = isRegional; + this.isAll = isAll; + this.mode = mode; + this.keyWords = keyWords; + } +} +exports.signAlignSettings = signAlignSettings; class BeautifierSettings { - constructor(removeComments, removeReport, checkAlias, signAlign, signAlignAll, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine) { + constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine) { this.RemoveComments = removeComments; this.RemoveAsserts = removeReport; this.CheckAlias = checkAlias; - this.SignAlignRegional = signAlign; - this.SignAlignAll = signAlignAll; + this.SignAlignSettings = signAlignSettings; this.KeywordCase = keywordCase; this.TypeNameCase = typeNameCase; this.Indentation = indentation; @@ -233,8 +241,9 @@ function beautify(input, settings) { arr = input.split("\r\n"); let result = []; beautify3(arr, result, settings, 0, 0); - if (settings.SignAlignAll) { - AlignSigns(result, 0, result.length - 1); + var alignSettings = settings.SignAlignSettings; + if (alignSettings != null && alignSettings.isAll) { + AlignSigns(result, 0, result.length - 1, alignSettings.mode); } arr = FormattedLineToString(result, settings.Indentation); input = arr.join("\r\n"); @@ -368,24 +377,27 @@ function beautifyPortGenericBlock(inputs, result, settings, startIndex, parentEn result[i].Indent--; blockBodyEndIndex--; } - if (settings.SignAlignRegional && !settings.SignAlignAll - && settings.SignAlignKeyWords != null - && settings.SignAlignKeyWords.indexOf(mode) >= 0) { - blockBodyStartIndex++; - AlignSigns(result, blockBodyStartIndex, blockBodyEndIndex); + var alignSettings = settings.SignAlignSettings; + if (alignSettings != null) { + if (alignSettings.isRegional && !alignSettings.isAll + && alignSettings.keyWords != null + && alignSettings.keyWords.indexOf(mode) >= 0) { + blockBodyStartIndex++; + AlignSigns(result, blockBodyStartIndex, blockBodyEndIndex, alignSettings.mode); + } } return [i, parentEndIndex]; } exports.beautifyPortGenericBlock = beautifyPortGenericBlock; -function AlignSigns(result, startIndex, endIndex) { - AlignSign_(result, startIndex, endIndex, ":"); - AlignSign_(result, startIndex, endIndex, ":="); - AlignSign_(result, startIndex, endIndex, "<="); - AlignSign_(result, startIndex, endIndex, "=>"); - AlignSign_(result, startIndex, endIndex, "@@comments"); +function AlignSigns(result, startIndex, endIndex, mode) { + AlignSign_(result, startIndex, endIndex, ":", mode); + AlignSign_(result, startIndex, endIndex, ":=", mode); + AlignSign_(result, startIndex, endIndex, "<=", mode); + AlignSign_(result, startIndex, endIndex, "=>", mode); + AlignSign_(result, startIndex, endIndex, "@@comments", mode); } exports.AlignSigns = AlignSigns; -function AlignSign_(result, startIndex, endIndex, symbol) { +function AlignSign_(result, startIndex, endIndex, symbol, mode) { let maxSymbolIndex = -1; let symbolIndices = {}; let startLine = startIndex; @@ -411,7 +423,8 @@ function AlignSign_(result, startIndex, endIndex, symbol) { maxSymbolIndex = Math.max(maxSymbolIndex, colonIndex); symbolIndices[i] = colonIndex; } - else if (!line.startsWith(ILCommentPrefix) && line.length != 0) { + else if ((mode != "local" && !line.startsWith(ILCommentPrefix) && line.length != 0) + || (mode == "local")) { if (startLine < i - 1) // if cannot find the symbol, a block of symbols ends { AlignSign(result, startLine, i - 1, symbol, maxSymbolIndex, symbolIndices); diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 0b6ad4a..3a20250 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -191,26 +191,36 @@ export function SetNewLinesAfterSymbols(text: string, newLineSettings: NewLineSe return text; } +export class signAlignSettings { + isRegional: boolean; + isAll: boolean; + mode: string; + keyWords: Array; + constructor(isRegional: boolean, isAll: boolean, mode: string, keyWords: Array) { + this.isRegional = isRegional; + this.isAll = isAll; + this.mode = mode; + this.keyWords = keyWords; + } +} + export class BeautifierSettings { RemoveComments: boolean; RemoveAsserts: boolean; CheckAlias: boolean; - SignAlignRegional: boolean; - SignAlignAll: boolean; - SignAlignKeyWords: Array; + SignAlignSettings: signAlignSettings; KeywordCase: string; TypeNameCase: string; Indentation: string; NewLineSettings: NewLineSettings; EndOfLine: string; constructor(removeComments: boolean, removeReport: boolean, checkAlias: boolean, - signAlign: boolean, signAlignAll: boolean, keywordCase: string, typeNameCase: string, indentation: string, + signAlignSettings: signAlignSettings, keywordCase: string, typeNameCase: string, indentation: string, newLineSettings: NewLineSettings, endOfLine: string) { this.RemoveComments = removeComments; this.RemoveAsserts = removeReport; this.CheckAlias = checkAlias; - this.SignAlignRegional = signAlign; - this.SignAlignAll = signAlignAll; + this.SignAlignSettings = signAlignSettings; this.KeywordCase = keywordCase; this.TypeNameCase = typeNameCase; this.Indentation = indentation; @@ -287,8 +297,9 @@ export function beautify(input: string, settings: BeautifierSettings) { arr = input.split("\r\n"); let result: (FormattedLine | FormattedLine[])[] = []; beautify3(arr, result, settings, 0, 0); - if (settings.SignAlignAll) { - AlignSigns(result, 0, result.length - 1); + var alignSettings = settings.SignAlignSettings; + if (alignSettings != null && alignSettings.isAll) { + AlignSigns(result, 0, result.length - 1, alignSettings.mode); } arr = FormattedLineToString(result, settings.Indentation); @@ -430,24 +441,27 @@ export function beautifyPortGenericBlock(inputs: Array, result: (Formatt (result[i]).Indent--; blockBodyEndIndex--; } - if (settings.SignAlignRegional && !settings.SignAlignAll - && settings.SignAlignKeyWords != null - && settings.SignAlignKeyWords.indexOf(mode) >= 0) { - blockBodyStartIndex++; - AlignSigns(result, blockBodyStartIndex, blockBodyEndIndex); + var alignSettings = settings.SignAlignSettings; + if (alignSettings != null) { + if (alignSettings.isRegional && !alignSettings.isAll + && alignSettings.keyWords != null + && alignSettings.keyWords.indexOf(mode) >= 0) { + blockBodyStartIndex++; + AlignSigns(result, blockBodyStartIndex, blockBodyEndIndex, alignSettings.mode); + } } return [i, parentEndIndex]; } -export function AlignSigns(result: (FormattedLine | FormattedLine[])[], startIndex: number, endIndex: number) { - AlignSign_(result, startIndex, endIndex, ":"); - AlignSign_(result, startIndex, endIndex, ":="); - AlignSign_(result, startIndex, endIndex, "<="); - AlignSign_(result, startIndex, endIndex, "=>"); - AlignSign_(result, startIndex, endIndex, "@@comments"); +export function AlignSigns(result: (FormattedLine | FormattedLine[])[], startIndex: number, endIndex: number, mode: string) { + AlignSign_(result, startIndex, endIndex, ":", mode); + AlignSign_(result, startIndex, endIndex, ":=", mode); + AlignSign_(result, startIndex, endIndex, "<=", mode); + AlignSign_(result, startIndex, endIndex, "=>", mode); + AlignSign_(result, startIndex, endIndex, "@@comments", mode); } -function AlignSign_(result: (FormattedLine | FormattedLine[])[], startIndex: number, endIndex: number, symbol: string) { +function AlignSign_(result: (FormattedLine | FormattedLine[])[], startIndex: number, endIndex: number, symbol: string, mode: string) { let maxSymbolIndex: number = -1; let symbolIndices = {}; let startLine = startIndex; @@ -474,7 +488,8 @@ function AlignSign_(result: (FormattedLine | FormattedLine[])[], startIndex: num maxSymbolIndex = Math.max(maxSymbolIndex, colonIndex); symbolIndices[i] = colonIndex; } - else if (!line.startsWith(ILCommentPrefix) && line.length != 0) { + else if ((mode != "local" && !line.startsWith(ILCommentPrefix) && line.length != 0) + || (mode == "local")) { if (startLine < i - 1) // if cannot find the symbol, a block of symbols ends { AlignSign(result, startLine, i - 1, symbol, maxSymbolIndex, symbolIndices); diff --git a/index.html b/index.html index 91bb53b..2b2f2b9 100644 --- a/index.html +++ b/index.html @@ -93,6 +93,7 @@ padding: 2px 5px; border-radius: 3px; font-size: 0.8em; + cursor: pointer; } a:hover { @@ -208,6 +209,15 @@ margin-left: 10px; display: inline-block; } + + .hide { + display: none; + } + + #settings_control { + margin: 10px 0px; + display: inline-block; + } @@ -247,7 +257,7 @@ -
Keyword case: + Keyword case: |