fix language boundary ranges

master
Brad Cornes 2020-06-18 19:52:17 +01:00
parent 983cfe5ac7
commit a6a8f7e536
1 changed files with 20 additions and 6 deletions

View File

@ -16,15 +16,21 @@ export function getLanguageBoundaries(
if (isVueDoc(doc)) { if (isVueDoc(doc)) {
let text = doc.getText() let text = doc.getText()
let blocks = findAll( let blocks = findAll(
/<(?<type>template|style|script)\b[^>]*>.*?(<\/\k<type>>|$)/gis, /(?<open><(?<type>template|style|script)\b[^>]*>).*?(?<close><\/\k<type>>|$)/gis,
text text
) )
let htmlRanges: Range[] = [] let htmlRanges: Range[] = []
let cssRanges: Range[] = [] let cssRanges: Range[] = []
for (let i = 0; i < blocks.length; i++) { for (let i = 0; i < blocks.length; i++) {
let range = { let range = {
start: indexToPosition(text, blocks[i].index), start: indexToPosition(
end: indexToPosition(text, blocks[i].index + blocks[i][0].length), text,
blocks[i].index + blocks[i].groups.open.length
),
end: indexToPosition(
text,
blocks[i].index + blocks[i][0].length - blocks[i].groups.close.length
),
} }
if (blocks[i].groups.type === 'style') { if (blocks[i].groups.type === 'style') {
cssRanges.push(range) cssRanges.push(range)
@ -41,7 +47,10 @@ export function getLanguageBoundaries(
if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) { if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) {
let text = doc.getText() let text = doc.getText()
let styleBlocks = findAll(/<style(?:\s[^>]*>|>).*?(<\/style>|$)/gis, text) let styleBlocks = findAll(
/(?<open><style(?:\s[^>]*>|>)).*?(?<close><\/style>|$)/gis,
text
)
let htmlRanges: Range[] = [] let htmlRanges: Range[] = []
let cssRanges: Range[] = [] let cssRanges: Range[] = []
let currentIndex = 0 let currentIndex = 0
@ -52,10 +61,15 @@ export function getLanguageBoundaries(
end: indexToPosition(text, styleBlocks[i].index), end: indexToPosition(text, styleBlocks[i].index),
}) })
cssRanges.push({ cssRanges.push({
start: indexToPosition(text, styleBlocks[i].index), start: indexToPosition(
text,
styleBlocks[i].index + styleBlocks[i].groups.open.length
),
end: indexToPosition( end: indexToPosition(
text, text,
styleBlocks[i].index + styleBlocks[i][0].length styleBlocks[i].index +
styleBlocks[i][0].length -
styleBlocks[i].groups.close.length
), ),
}) })
currentIndex = styleBlocks[i].index + styleBlocks[i][0].length currentIndex = styleBlocks[i].index + styleBlocks[i][0].length