forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-vue.ts
61 lines (48 loc) · 1.49 KB
/
file-vue.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
import { VueLanguagePlugin } from '../types';
import { parse } from '../utils/parseSfc';
const plugin: VueLanguagePlugin = (ctx) => {
return {
version: 1,
parseSFC(fileName, content) {
if (ctx.vueCompilerOptions.extensions.some(ext => fileName.endsWith(ext))) {
return parse(content);
}
},
updateSFC(sfc, change) {
/**
* ./utils/parseSfc don't use cache anymore
*/
// // avoid broken @vue/compiler-sfc cache
// if (!(sfc as any).__volar_clone) {
// sfc = JSON.parse(JSON.stringify(sfc));
// (sfc as any).__volar_clone = true;
// }
const blocks = [
sfc.descriptor.template,
sfc.descriptor.script,
sfc.descriptor.scriptSetup,
...sfc.descriptor.styles,
...sfc.descriptor.customBlocks,
].filter((block): block is NonNullable<typeof block> => !!block);
const hitBlock = blocks.find(block => change.start >= block.loc.start.offset && change.end <= block.loc.end.offset);
if (!hitBlock) {
return;
}
hitBlock.content =
hitBlock.content.substring(0, change.start - hitBlock.loc.start.offset)
+ change.newText
+ hitBlock.content.substring(change.end - hitBlock.loc.start.offset);
const lengthDiff = change.newText.length - (change.end - change.start);
for (const block of blocks) {
if (block.loc.start.offset > change.end) {
block.loc.start.offset += lengthDiff;
}
if (block.loc.end.offset >= change.end) {
block.loc.end.offset += lengthDiff;
}
}
return sfc;
},
};
};
export = plugin;