From 6c2dbf73559cb4d052ff9652891ffef31fa9aa28 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Mon, 27 Mar 2023 20:20:21 +0100 Subject: [PATCH] Fix `theme` helper handling when specifying default value (#747) * Fix `theme` helper handling when specifying default value * Tidy --- .../src/util/find.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss-language-service/src/util/find.ts b/packages/tailwindcss-language-service/src/util/find.ts index f9ffd20..57141d5 100644 --- a/packages/tailwindcss-language-service/src/util/find.ts +++ b/packages/tailwindcss-language-service/src/util/find.ts @@ -348,6 +348,22 @@ export function findHelperFunctionsInDocument( ) } +function getFirstCommaIndex(str: string): number | null { + let quoteChar: string | undefined + for (let i = 0; i < str.length; i++) { + let char = str[i] + if (char === ',' && !quoteChar) { + return i + } + if (!quoteChar && (char === '"' || char === "'")) { + quoteChar = char + } else if (char === quoteChar) { + quoteChar = undefined + } + } + return null +} + export function findHelperFunctionsInRange( doc: TextDocument, range?: Range @@ -360,7 +376,12 @@ export function findHelperFunctionsInRange( return matches.map((match) => { let quotesBefore = '' - let path = match.groups.path.replace(/['"]+$/, '').replace(/^['"]+/, (m) => { + let path = match.groups.path + let commaIndex = getFirstCommaIndex(path) + if (commaIndex !== null) { + path = path.slice(0, commaIndex).trimEnd() + } + path = path.replace(/['"]+$/, '').replace(/^['"]+/, (m) => { quotesBefore = m return '' })