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
|
import type { TextDocument, Range } from 'vscode-languageserver'
import { isVueDoc, isHtmlDoc, isSvelteDoc } from './html'
import { State } from './state'
import { indexToPosition } from './find'
import { isJsDoc } from './js'
import moo from 'moo'
import Cache from 'tmp-cache'
export type LanguageBoundary = { type: 'html' | 'js' | 'css' | string; range: Range }
let text = { text: { match: /[^]/, lineBreaks: true } }
let states = {
main: {
cssBlockStart: { match: '<style', push: 'cssBlock' },
jsBlockStart: { match: '<script', push: 'jsBlock' },
...text,
},
cssBlock: {
styleStart: { match: '>', next: 'style' },
cssBlockEnd: { match: '/>', pop: 1 },
attrStartDouble: { match: '"', push: 'attrDouble' },
attrStartSingle: { match: "'", push: 'attrSingle' },
interp: { match: '{', push: 'interp' },
...text,
},
jsBlock: {
scriptStart: { match: '>', next: 'script' },
jsBlockEnd: { match: '/>', pop: 1 },
langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' },
langAttrStartSingle: { match: "lang='", push: 'langAttrSingle' },
attrStartDouble: { match: '"', push: 'attrDouble' },
attrStartSingle: { match: "'", push: 'attrSingle' },
interp: { match: '{', push: 'interp' },
...text,
},
interp: {
interp: { match: '{', push: 'interp' },
end: { match: '}', pop: 1 },
...text,
},
langAttrDouble: {
langAttrEnd: { match: '"', pop: 1 },
lang: { match: /[^"]+/, lineBreaks: true },
},
langAttrSingle: {
langAttrEnd: { match: "'", pop: 1 },
lang: { match: /[^']+/, lineBreaks: true },
},
attrDouble: {
attrEnd: { match: '"', pop: 1 },
...text,
},
attrSingle: {
attrEnd: { match: "'", pop: 1 },
...text,
},
style: {
cssBlockEnd: { match: '</style>', pop: 1 },
...text,
},
script: {
jsBlockEnd: { match: '</script>', pop: 1 },
...text,
},
}
let vueStates = {
...states,
main: {
htmlBlockStart: { match: '<template', push: 'htmlBlock' },
...states.main,
},
htmlBlock: {
htmlStart: { match: '>', next: 'html' },
htmlBlockEnd: { match: '/>', pop: 1 },
attrStartDouble: { match: '"', push: 'attrDouble' },
attrStartSingle: { match: "'", push: 'attrSingle' },
interp: { match: '{', push: 'interp' },
...text,
},
html: {
htmlBlockEnd: { match: '</template>', pop: 1 },
...text,
},
}
let defaultLexer = moo.states(states)
let vueLexer = moo.states(vueStates)
let cache = new Cache<string, LanguageBoundary[] | null>({ max: 25, maxAge: 1000 })
export function getLanguageBoundaries(
state: State,
doc: TextDocument,
text: string = doc.getText()
): LanguageBoundary[] | null {
let cacheKey = `${doc.languageId}:${text}`
let cachedBoundaries = cache.get(cacheKey)
if (cachedBoundaries !== undefined) {
return cachedBoundaries
}
let defaultType = isVueDoc(doc)
? 'none'
: isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)
? 'html'
: null
if (defaultType === null) {
cache.set(cacheKey, null)
return null
}
let lexer = defaultType === 'none' ? vueLexer : defaultLexer
lexer.reset(text)
let type = defaultType
let boundaries: LanguageBoundary[] = [
{ type: defaultType, range: { start: { line: 0, character: 0 }, end: undefined } },
]
let offset = 0
try {
for (let token of lexer) {
if (token.type.endsWith('BlockStart')) {
let position = indexToPosition(text, offset)
if (!boundaries[boundaries.length - 1].range.end) {
boundaries[boundaries.length - 1].range.end = position
}
type = token.type.replace(/BlockStart$/, '')
boundaries.push({ type, range: { start: position, end: undefined } })
} else if (token.type.endsWith('BlockEnd')) {
let position = indexToPosition(text, offset)
boundaries[boundaries.length - 1].range.end = position
boundaries.push({ type: defaultType, range: { start: position, end: undefined } })
} else if (token.type === 'lang') {
boundaries[boundaries.length - 1].type = token.text
}
offset += token.text.length
}
} catch {
cache.set(cacheKey, null)
return null
}
if (!boundaries[boundaries.length - 1].range.end) {
boundaries[boundaries.length - 1].range.end = indexToPosition(text, offset)
}
cache.set(cacheKey, boundaries)
return boundaries
}
|