Browse Source

bugfix "remove non-comment code by mistake"

master
g2384 5 years ago
parent
commit
15d927ed9e
4 changed files with 34 additions and 21 deletions
  1. +5
    -0
      README.md
  2. +9
    -11
      VHDLFormatter.js
  3. +11
    -10
      VHDLFormatter.ts
  4. +9
    -0
      tests/VHDLFormatterUnitTests.ts

+ 5
- 0
README.md View File

@ -6,6 +6,11 @@ VHDL formatter web online written in javascript
## Release Notes
### 2.3 [2018-02-22]
- bugfix "remove non-comment code by mistake"
- add `tests` folder, improve the project management
### 2.2 [2018-10-16]
- support enumerated types


+ 9
- 11
VHDLFormatter.js View File

@ -153,19 +153,11 @@ function MixLetters(input) {
function EscapeComments(arr, comments, commentIndex) {
for (var i = 0; i < arr.length; i++) {
let line = arr[i];
var firstCharIndex = line.regexIndexOf(/[a-zA-Z0-9\(\&\)%_\+'"|\\]/);
var commentStartIndex = line.indexOf("--");
if (firstCharIndex < commentStartIndex && firstCharIndex >= 0) {
if (commentStartIndex >= 0) {
comments.push(line.substr(commentStartIndex));
arr[i] = line.substr(firstCharIndex, commentStartIndex - firstCharIndex) + ILCommentPrefix + (commentIndex++);
}
else if ((firstCharIndex > commentStartIndex && commentStartIndex >= 0) || (firstCharIndex < 0 && commentStartIndex >= 0)) {
comments.push(line.substr(commentStartIndex));
arr[i] = ILCommentPrefix + (commentIndex++);
}
else {
firstCharIndex = firstCharIndex < 0 ? 0 : firstCharIndex;
arr[i] = line.substr(firstCharIndex);
arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + commentIndex;
commentIndex++;
}
}
return commentIndex;
@ -253,6 +245,7 @@ function beautify(input, settings) {
var arr = input.split("\r\n");
var comments = [], commentsIndex = 0;
commentsIndex = EscapeComments(arr, comments, commentsIndex);
RemoveLeadingWhitespaces(arr);
input = arr.join("\r\n");
if (settings.RemoveComments) {
input = input.replace(/\r\n[ \t]*@@comments[0-9]+[ \t]*\r\n/g, '\r\n');
@ -321,6 +314,11 @@ function beautify(input, settings) {
return input;
}
exports.beautify = beautify;
function RemoveLeadingWhitespaces(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(/^\s+/, "");
}
}
class FormattedLine {
constructor(line, indent) {
this.Line = line;


+ 11
- 10
VHDLFormatter.ts View File

@ -62,7 +62,7 @@ declare global {
regexCount: (pattern: RegExp) => number;
convertToRegexBlockWords: () => RegExp;
}
interface Array<T>{
interface Array<T> {
convertToRegexBlockWords: () => RegExp;
}
}
@ -186,17 +186,11 @@ function MixLetters(input: string) {
function EscapeComments(arr: Array<string>, comments: Array<string>, commentIndex: number): number {
for (var i = 0; i < arr.length; i++) {
let line: string = arr[i];
var firstCharIndex = line.regexIndexOf(/[a-zA-Z0-9\(\&\)%_\+'"|\\]/);
var commentStartIndex = line.indexOf("--");
if (firstCharIndex < commentStartIndex && firstCharIndex >= 0) {
if (commentStartIndex >= 0) {
comments.push(line.substr(commentStartIndex));
arr[i] = line.substr(firstCharIndex, commentStartIndex - firstCharIndex) + ILCommentPrefix + (commentIndex++);
} else if ((firstCharIndex > commentStartIndex && commentStartIndex >= 0) || (firstCharIndex < 0 && commentStartIndex >= 0)) {
comments.push(line.substr(commentStartIndex));
arr[i] = ILCommentPrefix + (commentIndex++);
} else {
firstCharIndex = firstCharIndex < 0 ? 0 : firstCharIndex;
arr[i] = line.substr(firstCharIndex);
arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + commentIndex;
commentIndex++
}
}
return commentIndex
@ -304,6 +298,7 @@ export function beautify(input: string, settings: BeautifierSettings) {
var comments = [],
commentsIndex = 0;
commentsIndex = EscapeComments(arr, comments, commentsIndex);
RemoveLeadingWhitespaces(arr);
input = arr.join("\r\n");
if (settings.RemoveComments) {
@ -383,6 +378,12 @@ export function beautify(input: string, settings: BeautifierSettings) {
return input;
}
function RemoveLeadingWhitespaces(arr: Array<string>) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(/^\s+/, "");
}
}
export class FormattedLine {
Line: string;
Indent: number;


+ 9
- 0
tests/VHDLFormatterUnitTests.ts View File

@ -908,6 +908,7 @@ function IntegrationTest() {
IntegrationTest67();
IntegrationTest68();
IntegrationTest69();
IntegrationTest70();
}
function IntegrationTest23() {
@ -1339,6 +1340,14 @@ function IntegrationTest69() {
assertAndCountTest("multiline enumerated type is", expected, actual);
}
function IntegrationTest70() {
let settings = GetDefaultSettings();
let input = 'test\r\n := test';
let expected = 'test\r\n:= test';
let actual = beautify(input, settings);
assertAndCountTest("multiline assignment", expected, actual);
}
function GetDefaultSettings() {
let new_line_after_symbols = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];


Loading…
Cancel
Save