forked from jhipster/jhipster-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.js
118 lines (103 loc) · 3.43 KB
/
formatter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const JDLParser = require('./parser').JDLParser;
const _ = require('lodash');
/**
* Will provide formatting suggestions in the form of
* {
* start: number,
* end: number,
* newText: string
* }
*
* examples:
* 1. {start:44, end: 46, newText: " "} --> replace a double space with a single space
* 2. {start:200, end: 201, newText: ","} --> replace a double space with a single space
*
*
* This is just a naive implementation, a productive one may have additional logic
* 1. Supporting DELETE/INSERT operation too.
* 2. Being able to deal with multiple changes on interwoven offset ranges.
* 3. Configurable to user's preferences.
*
* And a-lot more rules... :)
*/
function formatJDL(cst, orgText) {
// eslint-disable-next-line no-use-before-define
const formatterVisitor = new JDLCstFormatterVisitor(orgText);
formatterVisitor.visit(cst);
const textChanges = formatterVisitor.replaces;
let formattedText = orgText;
let changeOffset = 0;
textChanges.forEach((currReplace) => {
const actualReplaceStart = currReplace.start + changeOffset + 1;
const actualReplaceEnd = currReplace.end + changeOffset;
formattedText = formattedText.substr(0, actualReplaceStart) + currReplace.newText + formattedText.substr(actualReplaceEnd + 1);
changeOffset += -((currReplace.end - currReplace.start) - currReplace.newText.length);
});
return formattedText;
}
const BaseJDLCSTVisitor = new JDLParser().getBaseCstVisitorConstructorWithDefaults();
class JDLCstFormatterVisitor extends BaseJDLCSTVisitor {
constructor(orgText) {
super();
this.orgText = orgText;
this.replaces = [];
this.validateVisitor();
}
entityDecl(ctx) {
const entityKW = ctx.ENTITY[0];
const entityName = ctx.NAME[0];
if (
// not separated by a single character
entityKW.endOffset !== entityName.startOffset - 2 ||
// separated by a single character, but it is not a space char.
this.orgText[entityKW.endOffset + 1] !== ' ') {
this.replaces.push({
start: entityKW.endOffset,
end: entityName.startOffset - 1,
newText: ' '
});
}
this.visit(ctx.entityBody);
}
entityBody(ctx) {
const commas = ctx.COMMA;
// the last field does not have a comma
const fieldsWithCommas = _.dropRight(ctx.fieldDec);
_.forEach(commas, (currComma, idx) => {
const currField = fieldsWithCommas[idx];
const fieldEndOffset = findEndOffset(currField);
if (fieldEndOffset !== currComma.startOffset - 1) {
this.replaces.push({
start: fieldEndOffset,
end: currComma.startOffset - 1,
newText: ''
});
}
});
}
}
function findEndOffset(cstOrCstElemArr, oldMax = -1) {
let newMax = oldMax;
if (cstOrCstElemArr.children !== undefined) {
_.forEach(cstOrCstElemArr.children, (item) => {
_.forEach(item, () => {
newMax = Math.max(newMax, findEndOffset(item, newMax));
});
});
return newMax;
} else if (_.isArray(cstOrCstElemArr)) {
// relying on knowledge that Chevrotain built the CST children arrays in the order encountered.
// but that is not an official API...
const lastElem = _.last(cstOrCstElemArr);
// concrete tokens.
if (lastElem.tokenType !== undefined) {
return Math.max(newMax, lastElem.endOffset);
}
// a cst subnode
return Math.max(newMax, findEndOffset(lastElem));
}
throw Error('non exhaustive match');
}
module.exports = {
formatJDL
};