From 0fab387f77f6aa0711e020a6dbc140ebe7278981 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 27 Jan 2023 10:33:35 +0000 Subject: [PATCH] Update `context.getClassList` usage (#707) * Update `context.getClassList` usage * fix default metadata --- .../tailwindcss-language-server/src/server.ts | 20 +++++++++++-------- .../src/completionProvider.ts | 7 ++----- .../src/util/state.ts | 1 + 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/tailwindcss-language-server/src/server.ts b/packages/tailwindcss-language-server/src/server.ts index 190503a..897acb5 100644 --- a/packages/tailwindcss-language-server/src/server.ts +++ b/packages/tailwindcss-language-server/src/server.ts @@ -1003,15 +1003,19 @@ async function createProjectService( state.jitContext = state.modules.jit.createContext.module(state) state.jitContext.tailwindConfig.separator = state.config.separator if (state.jitContext.getClassList) { - state.classList = state.jitContext - .getClassList() + let classList = state.jitContext + .getClassList({ includeMetadata: true }) .filter((className) => className !== '*') - .map((className) => { - if (Array.isArray(className)) { - return [className[0], { color: getColor(state, className[0]), ...className[1] }] - } - return [className, { color: getColor(state, className) }] - }) + state.classListContainsMetadata = classList.some((cls) => Array.isArray(cls)) + state.classList = classList.map((className) => { + if (Array.isArray(className)) { + return [ + className[0], + { color: getColor(state, className[0]), ...(className[1] ?? {}) }, + ] + } + return [className, { color: getColor(state, className) }] + }) } } else { delete state.jitContext diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts index 9f7a246..0ca4912 100644 --- a/packages/tailwindcss-language-service/src/completionProvider.ts +++ b/packages/tailwindcss-language-service/src/completionProvider.ts @@ -75,15 +75,12 @@ export function completionsFromClassList( // modifiers let modifiers: string[] let beforeSlash = partialClassName.split('/').slice(0, -1).join('/') - let classListContainsModifiers = state.classList.some( - (cls) => Array.isArray(cls) && cls[1].modifiers - ) - if (classListContainsModifiers) { + if (state.classListContainsMetadata) { let baseClassName = beforeSlash.slice(offset) modifiers = state.classList.find( (cls) => Array.isArray(cls) && cls[0] === baseClassName - )?.[1].modifiers + )?.[1]?.modifiers } else { let testClass = beforeSlash + '/[0]' let { rules } = jit.generateRules(state, [testClass]) diff --git a/packages/tailwindcss-language-service/src/util/state.ts b/packages/tailwindcss-language-service/src/util/state.ts index cba7c64..51677af 100644 --- a/packages/tailwindcss-language-service/src/util/state.ts +++ b/packages/tailwindcss-language-service/src/util/state.ts @@ -118,6 +118,7 @@ export interface State { jit?: boolean jitContext?: any classList?: Array<[string, { color: culori.Color | KeywordColor | null; modifiers?: string[] }]> + classListContainsMetadata?: boolean pluginVersions?: string completionItemData?: Record // postcssPlugins?: { before: any[]; after: any[] }