/
pug.ts
42 lines (32 loc) · 1.06 KB
/
pug.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
import { VueLanguagePlugin } from '../typescriptRuntime';
export default function (): VueLanguagePlugin {
return {
compileTemplate(template, lang) {
if (lang === 'pug') {
let pug: typeof import('@volar/pug-language-service') | undefined;
try {
pug = require('@volar/pug-language-service');
} catch { }
const pugDoc = pug?.baseParse(template);
if (pugDoc) {
return {
html: pugDoc.htmlCode,
mapping: (htmlStart, htmlEnd) => {
const pugRange = pugDoc.sourceMap.getSourceRange(htmlStart, htmlEnd, data => !data?.isEmptyTagCompletion)?.[0];
if (pugRange) {
return pugRange;
}
else {
const pugStart = pugDoc.sourceMap.getSourceRange(htmlStart, htmlStart, data => !data?.isEmptyTagCompletion)?.[0]?.start;
const pugEnd = pugDoc.sourceMap.getSourceRange(htmlEnd, htmlEnd, data => !data?.isEmptyTagCompletion)?.[0]?.end;
if (pugStart !== undefined && pugEnd !== undefined) {
return { start: pugStart, end: pugEnd };
}
}
},
};
}
}
}
}
}