From a6a8f7e53698d418ceabaf5146bffeee366ad62b Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Thu, 18 Jun 2020 19:52:17 +0100 Subject: [PATCH] fix language boundary ranges --- src/lsp/util/getLanguageBoundaries.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/lsp/util/getLanguageBoundaries.ts b/src/lsp/util/getLanguageBoundaries.ts index dfef230..c309aae 100644 --- a/src/lsp/util/getLanguageBoundaries.ts +++ b/src/lsp/util/getLanguageBoundaries.ts @@ -16,15 +16,21 @@ export function getLanguageBoundaries( if (isVueDoc(doc)) { let text = doc.getText() let blocks = findAll( - /<(?template|style|script)\b[^>]*>.*?(<\/\k>|$)/gis, + /(?<(?template|style|script)\b[^>]*>).*?(?<\/\k>|$)/gis, text ) let htmlRanges: Range[] = [] let cssRanges: Range[] = [] for (let i = 0; i < blocks.length; i++) { let range = { - start: indexToPosition(text, blocks[i].index), - end: indexToPosition(text, blocks[i].index + blocks[i][0].length), + start: indexToPosition( + 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') { cssRanges.push(range) @@ -41,7 +47,10 @@ export function getLanguageBoundaries( if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) { let text = doc.getText() - let styleBlocks = findAll(/]*>|>).*?(<\/style>|$)/gis, text) + let styleBlocks = findAll( + /(?]*>|>)).*?(?<\/style>|$)/gis, + text + ) let htmlRanges: Range[] = [] let cssRanges: Range[] = [] let currentIndex = 0 @@ -52,10 +61,15 @@ export function getLanguageBoundaries( end: indexToPosition(text, styleBlocks[i].index), }) cssRanges.push({ - start: indexToPosition(text, styleBlocks[i].index), + start: indexToPosition( + text, + styleBlocks[i].index + styleBlocks[i].groups.open.length + ), end: indexToPosition( 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