Skip to content

Commit a6a8f7e

Browse files
committed
fix language boundary ranges
1 parent 983cfe5 commit a6a8f7e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/lsp/util/getLanguageBoundaries.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ export function getLanguageBoundaries(
1616
if (isVueDoc(doc)) {
1717
let text = doc.getText()
1818
let blocks = findAll(
19-
/<(?<type>template|style|script)\b[^>]*>.*?(<\/\k<type>>|$)/gis,
19+
/(?<open><(?<type>template|style|script)\b[^>]*>).*?(?<close><\/\k<type>>|$)/gis,
2020
text
2121
)
2222
let htmlRanges: Range[] = []
2323
let cssRanges: Range[] = []
2424
for (let i = 0; i < blocks.length; i++) {
2525
let range = {
26-
start: indexToPosition(text, blocks[i].index),
27-
end: indexToPosition(text, blocks[i].index + blocks[i][0].length),
26+
start: indexToPosition(
27+
text,
28+
blocks[i].index + blocks[i].groups.open.length
29+
),
30+
end: indexToPosition(
31+
text,
32+
blocks[i].index + blocks[i][0].length - blocks[i].groups.close.length
33+
),
2834
}
2935
if (blocks[i].groups.type === 'style') {
3036
cssRanges.push(range)
@@ -41,7 +47,10 @@ export function getLanguageBoundaries(
4147

4248
if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) {
4349
let text = doc.getText()
44-
let styleBlocks = findAll(/<style(?:\s[^>]*>|>).*?(<\/style>|$)/gis, text)
50+
let styleBlocks = findAll(
51+
/(?<open><style(?:\s[^>]*>|>)).*?(?<close><\/style>|$)/gis,
52+
text
53+
)
4554
let htmlRanges: Range[] = []
4655
let cssRanges: Range[] = []
4756
let currentIndex = 0
@@ -52,10 +61,15 @@ export function getLanguageBoundaries(
5261
end: indexToPosition(text, styleBlocks[i].index),
5362
})
5463
cssRanges.push({
55-
start: indexToPosition(text, styleBlocks[i].index),
64+
start: indexToPosition(
65+
text,
66+
styleBlocks[i].index + styleBlocks[i].groups.open.length
67+
),
5668
end: indexToPosition(
5769
text,
58-
styleBlocks[i].index + styleBlocks[i][0].length
70+
styleBlocks[i].index +
71+
styleBlocks[i][0].length -
72+
styleBlocks[i].groups.close.length
5973
),
6074
})
6175
currentIndex = styleBlocks[i].index + styleBlocks[i][0].length

0 commit comments

Comments
 (0)