/
baseParse.ts
385 lines (342 loc) · 9.31 KB
/
baseParse.ts
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import * as shared from '@volar/shared';
import * as SourceMap from '@volar/source-map';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { CodeGen } from '@volar/code-gen';
import * as pugLex from 'pug-lexer';
const pugParser = require('pug-parser');
export function baseParse(pugCode: string) {
const fileName = 'foo.pug';
const pugTextDocument = TextDocument.create(shared.fsPathToUri('foo.pug'), 'jade', 0, pugCode);
const codeGen = new CodeGen<{ isEmptyTagCompletion: boolean; } | undefined>();
let error: {
code: string,
msg: string,
line: number,
column: number,
filename: string,
} | undefined;
let fullPugTagEnd: number;
let emptyLineEnds: ReturnType<typeof collectEmptyLineEnds>;
let attrsBlocks: ReturnType<typeof collectAttrsBlocks>;
let ast: Node | undefined;
try {
const tokens = pugLex(pugCode, { filename: fileName });
emptyLineEnds = collectEmptyLineEnds(tokens);
attrsBlocks = collectAttrsBlocks(tokens);
ast = pugParser(tokens, { filename: fileName, src: pugCode }) as Node;
visitNode(ast, undefined, undefined);
// support tag auto-complete in empty lines
for (const emptyLineEnd of emptyLineEnds) {
codeGen.addText('<');
codeGen.addCode(
'x__VLS_',
{
start: emptyLineEnd,
end: emptyLineEnd,
},
SourceMap.Mode.Totally,
{ isEmptyTagCompletion: true },
);
codeGen.addText(' />');
}
codeGen.addCode(
'',
{
start: pugCode.trimEnd().length,
end: pugCode.trimEnd().length,
},
SourceMap.Mode.Totally,
undefined,
);
}
catch (e) {
const _error = e as NonNullable<typeof error>;
error = {
..._error,
line: _error.line - 1,
column: _error.column - 1,
};
};
const htmlCode = codeGen.getText();
const sourceMap = new SourceMap.SourceMapBase(codeGen.getMappings());
return {
htmlCode,
pugTextDocument,
sourceMap,
error,
ast,
};
function visitNode(node: Node, next: Node | undefined, parent: Node | undefined) {
if (node.type === 'Block') {
for (let i = 0; i < node.nodes.length; i++) {
visitNode(node.nodes[i], node.nodes[i + 1], node);
}
}
else if (node.type === 'Tag') {
const pugTagRange = getDocRange(node.line, node.column, node.name.length);
const fullHtmlStart = codeGen.getText().length;
fullPugTagEnd = pugTagRange.end;
const selfClosing = node.block.nodes.length === 0;
addStartTag(node, selfClosing);
if (!selfClosing) {
visitNode(node.block, next, parent);
addEndTag(node, next, parent);
}
const fullHtmlEnd = codeGen.getText().length;
codeGen.addMapping2({
data: undefined,
sourceRange: {
start: pugTagRange.start,
end: fullPugTagEnd,
},
mappedRange: {
start: fullHtmlStart,
end: fullHtmlEnd,
},
mode: SourceMap.Mode.Totally,
});
}
else if (node.type === 'Text') {
codeGen.addCode(
node.val,
getDocRange(node.line, node.column, node.val.length),
SourceMap.Mode.Offset,
undefined,
);
}
}
function addStartTag(node: TagNode, selfClosing: boolean) {
codeGen.addCode(
'',
getDocRange(node.line, node.column, 0),
SourceMap.Mode.Totally,
undefined,
);
codeGen.addText('<');
const tagRange = getDocRange(node.line, node.column, node.name.length);
if (pugCode.substring(tagRange.start, tagRange.end) === node.name) {
codeGen.addCode(
node.name,
tagRange,
SourceMap.Mode.Offset,
undefined,
);
}
else {
codeGen.addText(node.name);
}
const noTitleAttrs = node.attrs.filter(attr => !attr.mustEscape && attr.name !== 'class');
const noTitleClassAttrs = node.attrs.filter(attr => !attr.mustEscape && attr.name === 'class');
const attrsBlock = attrsBlocks.get(getDocOffset(node.line, node.column)); // support attr auto-complete in spaces
const hasClassAttr = attrsBlock && attrsBlock.text.match(/\bclass\b\s*=/i);
if (!hasClassAttr) {
addClassesOrStyles(noTitleClassAttrs, 'class');
}
for (const attr of noTitleAttrs) {
codeGen.addText(' ');
codeGen.addText(attr.name);
if (typeof attr.val !== 'boolean') {
codeGen.addText('=');
codeGen.addCode(
attr.val,
getDocRange(attr.line, attr.column, attr.val.length),
SourceMap.Mode.Offset,
undefined
);
}
}
if (attrsBlock) {
codeGen.addText(' ');
codeGen.addCode(
attrsBlock.text,
{ start: attrsBlock.offset, end: attrsBlock.offset + attrsBlock.text.length },
SourceMap.Mode.Offset,
undefined,
);
}
if (selfClosing) {
codeGen.addText(' />');
}
else {
codeGen.addText('>');
}
}
function addEndTag(node: TagNode, next: Node | undefined, parent: Node | undefined) {
let nextStart: number | undefined;
if (next) {
if (next.type === 'Block') {
nextStart = getDocOffset(next.line, 1);
}
else {
nextStart = getDocOffset(next.line, next.column);
}
}
else if (!parent) {
nextStart = pugCode.length;
}
if (nextStart !== undefined) {
fullPugTagEnd = nextStart;
codeGen.addCode(
'',
{
start: nextStart,
end: nextStart,
},
SourceMap.Mode.Totally,
undefined,
);
}
codeGen.addText(`</${node.name}>`);
}
function addClassesOrStyles(attrs: TagNode['attrs'], attrName: string) {
if (!attrs.length) return;
codeGen.addText(' ');
codeGen.addText(attrName);
codeGen.addText('=');
codeGen.addText('"');
for (const attr of attrs) {
if (typeof attr.val !== 'boolean') {
codeGen.addText(' ');
codeGen.addCode(
attr.val.slice(1, -1), // remove "
getDocRange(attr.line, attr.column + 1, attr.val.length - 2),
SourceMap.Mode.Offset,
undefined
);
}
}
codeGen.addText('"');
}
function collectEmptyLineEnds(tokens: pugLex.Token[]) {
const ends: number[] = [];
for (const token of tokens) {
if (token.type === 'newline' || token.type === 'outdent') {
let currentLine = token.loc.start.line - 2;
let prevLine = shared.getLineText(pugTextDocument, currentLine);
while (prevLine.trim() === '') {
ends.push(pugTextDocument.offsetAt({ line: currentLine + 1, character: 0 }) - 1);
if (currentLine <= 0) break;
currentLine--;
prevLine = shared.getLineText(pugTextDocument, currentLine);
}
}
}
return ends.sort((a, b) => a - b);
}
function collectAttrsBlocks(tokens: pugLex.Token[]) {
const blocks = new Map<number, { offset: number, text: string; }>();
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (token.type === 'start-attributes') {
let tagStart: pugLex.Token = token;
for (let j = i - 1; j >= 0; j--) {
const prevToken = tokens[j];
if (
prevToken.type === 'newline'
|| prevToken.type === 'indent'
|| prevToken.type === 'outdent'
|| prevToken.type === ':'
) break;
tagStart = prevToken;
if (prevToken.type === 'tag')
break;
}
let prevToken: pugLex.Token = token;
let text = '';
for (i++; i < tokens.length; i++) {
const attrToken = tokens[i];
addPrevSpace(attrToken);
if (attrToken.type === 'attribute') {
let attrText = pugCode.substring(
getDocOffset(attrToken.loc.start.line, attrToken.loc.start.column),
getDocOffset(attrToken.loc.end.line, attrToken.loc.end.column),
);
if (typeof attrToken.val === 'string' && attrText.indexOf('=') >= 0) {
let valText = attrToken.val;
if (valText.startsWith('`') && valText.endsWith('`')) {
valText = `"${valText.slice(1, -1)}"`;
}
valText = valText.replace(/ \\\n/g, '//\n');
text += attrText.substring(0, attrText.lastIndexOf(attrToken.val)) + valText;
}
else {
text += attrText;
}
}
else if (attrToken.type === 'end-attributes') {
blocks.set(getDocOffset(tagStart.loc.start.line, tagStart.loc.start.column), {
offset: getDocOffset(token.loc.end.line, token.loc.end.column),
text,
});
break;
}
prevToken = attrToken;
}
function addPrevSpace(currentToken: pugLex.Token) {
text += pugCode.substring(
getDocOffset(prevToken.loc.end.line, prevToken.loc.end.column),
getDocOffset(currentToken.loc.start.line, currentToken.loc.start.column),
).replace(/,/g, '\n');
}
}
}
return blocks;
}
function getDocOffset(pugLine: number, pugColumn: number) {
return pugTextDocument.offsetAt({ line: pugLine - 1, character: pugColumn - 1 });
}
function getDocRange(pugLine: number, pugColumn: number, length: number) {
const start = pugTextDocument.offsetAt({ line: pugLine - 1, character: pugColumn - 1 });
const end = start + length;
return {
start,
end,
};
}
}
export type Node = BlockNode | TagNode | TextNode | CommentNode | BlockCommentNode;
export interface BlockNode {
type: 'Block',
nodes: Node[],
line: number,
}
export interface TagNode {
type: 'Tag',
name: string,
selfClosing: boolean,
block: BlockNode,
attrs: {
name: string,
val: string | true,
line: number,
column: number,
mustEscape: boolean,
}[],
attributeBlocks: {
// ?
}[],
isInline: boolean,
line: number,
column: number,
}
export interface TextNode {
type: 'Text',
val: string,
line: number,
column: number,
}
export interface CommentNode {
type: 'Comment',
val: string,
buffer: boolean,
line: number,
column: number,
}
export interface BlockCommentNode {
type: 'BlockComment',
block: BlockNode,
val: string,
buffer: boolean,
line: number,
column: number,
}