Browse Source

fix single line PACKAGE indentation

PACKAGE can be ended with semicolon
master
g2384 4 years ago
parent
commit
4e91e71c1c
3 changed files with 66 additions and 1 deletions
  1. +26
    -1
      VHDLFormatter.js
  2. +26
    -0
      VHDLFormatter.ts
  3. +14
    -0
      tests/VHDLFormatter.test.ts

+ 26
- 1
VHDLFormatter.js View File

@ -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;


+ 26
- 0
VHDLFormatter.ts View File

@ -677,6 +677,25 @@ export function beautifyComponentBlock(inputs: Array<string>, result: (Formatted
return [endIndex, parentEndIndex];
}
export function beautifyPackageIsNewBlock(inputs: Array<string>, 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<string>, 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<string>, 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;


+ 14
- 0
tests/VHDLFormatter.test.ts View File

@ -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 {


Loading…
Cancel
Save