Browse Source

add new lines at the end of file

master
g2384 4 years ago
parent
commit
e026893a3e
5 changed files with 32 additions and 6 deletions
  1. +5
    -1
      VHDLFormatter.js
  2. +6
    -1
      VHDLFormatter.ts
  3. +10
    -1
      index.html
  4. +9
    -1
      tests/VHDLFormatter.test.ts
  5. +2
    -2
      tests/VHDLFormatterUnitTests.ts

+ 5
- 1
VHDLFormatter.js View File

@ -256,7 +256,7 @@ class signAlignSettings {
}
exports.signAlignSettings = signAlignSettings;
class BeautifierSettings {
constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine) {
constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine, addNewLine) {
this.RemoveComments = removeComments;
this.RemoveAsserts = removeReport;
this.CheckAlias = checkAlias;
@ -266,6 +266,7 @@ class BeautifierSettings {
this.Indentation = indentation;
this.NewLineSettings = newLineSettings;
this.EndOfLine = endOfLine;
this.AddNewLine = addNewLine;
}
}
exports.BeautifierSettings = BeautifierSettings;
@ -351,6 +352,9 @@ function beautify(input, settings) {
var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g");
input = input.replace(escapedTexts, "");
input = input.replace(/\r\n/g, settings.EndOfLine);
if (settings.AddNewLine && !input.endsWith(settings.EndOfLine)) {
input += settings.EndOfLine;
}
return input;
}
exports.beautify = beautify;


+ 6
- 1
VHDLFormatter.ts View File

@ -303,9 +303,10 @@ export class BeautifierSettings {
Indentation: string;
NewLineSettings: NewLineSettings;
EndOfLine: string;
AddNewLine: boolean;
constructor(removeComments: boolean, removeReport: boolean, checkAlias: boolean,
signAlignSettings: signAlignSettings, keywordCase: string, typeNameCase: string, indentation: string,
newLineSettings: NewLineSettings, endOfLine: string) {
newLineSettings: NewLineSettings, endOfLine: string, addNewLine: boolean) {
this.RemoveComments = removeComments;
this.RemoveAsserts = removeReport;
this.CheckAlias = checkAlias;
@ -315,6 +316,7 @@ export class BeautifierSettings {
this.Indentation = indentation;
this.NewLineSettings = newLineSettings;
this.EndOfLine = endOfLine;
this.AddNewLine = addNewLine;
}
}
@ -407,6 +409,9 @@ export function beautify(input: string, settings: BeautifierSettings) {
var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g");
input = input.replace(escapedTexts, "");
input = input.replace(/\r\n/g, settings.EndOfLine);
if (settings.AddNewLine && !input.endsWith(settings.EndOfLine)) {
input += settings.EndOfLine;
}
return input;
}


+ 10
- 1
index.html View File

@ -212,6 +212,12 @@
<input type="text" id="cust_eol" size="8" onKeyUp="counterDecode('cust_eol', 'eol_s')" value="\r\n" /> (
<span id="eol_s">one \r & one \n</span>)
</div>
<div id="customise_extraEOL_div">
<div class="checkbox inline" id="add_extraEOL_div">
<input type="checkbox" id="add_extraEOL" checked=true>
<label for="add_extraEOL">Add a new line at the end of file</label>
</div>
</div>
<div class="checkbox" id="compress_div">
<input type="checkbox" id="compress">
<label for="compress">! EVIL - compress VHDL (\r\n, comments will be removed)</label>
@ -349,6 +355,7 @@
document.getElementById("compress").checked = setting.compress;
var indentation = beautifierSettings.Indentation;
document.getElementById("use_space").checked = indentation != "\t";
document.getElementById("add_extraEOL").checked = beautifierSettings.AddNewLine;
document.getElementById("customise_indentation").value = indentation;
document.getElementById("keyword_div").elements.namedItem("keywordcase").value = beautifierSettings.KeywordCase;
document.getElementById("typename_div").elements.namedItem("typenamecase").value = beautifierSettings.TypeNameCase;
@ -433,6 +440,7 @@
var use_space = document.getElementById("use_space").checked;
var compress = document.getElementById("compress").checked;
var cust_indent = document.getElementById("customise_indentation").value;
var addNewLine = document.getElementById("add_extraEOL").checked;
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;
@ -480,7 +488,8 @@
typenamecase,
indentation,
newLineSettings,
endOfLine);
endOfLine,
addNewLine);
return [beautifierSettings, compress];
}


+ 9
- 1
tests/VHDLFormatter.test.ts View File

@ -36,6 +36,14 @@ describe('VHDLFormatter', function () {
let result = beautify(input, settings);
expect(result).toBe(input);
});
it('add new line at the end of the file', function () {
let settings = GetDefaultSettings();
settings.AddNewLine = true;
let input = 'test';
let result = beautify(input, settings);
expect(result).toBe("test\r\n");
});
});
function GetDefaultSettings(indentation: string = " "): BeautifierSettings {
@ -48,5 +56,5 @@ 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");
return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false);
}

+ 2
- 2
tests/VHDLFormatterUnitTests.ts View File

@ -1368,7 +1368,7 @@ function IntegrationTest78() {
}
function IntegrationTest79() {
let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n");
let settings = new BeautifierSettings(false, false, false, null, "lowercase", "uppercase", null, null, "\r\n", false);
let input = "case when others;\r\nx : STRING;\r\ny : BIT;";
let actual = beautify(input, settings);
assertAndCountTest("uppercase typename and lowercase keyword", input, actual);
@ -1438,7 +1438,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");
return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n", false);
}


Loading…
Cancel
Save