From 01f37e2c30f4acbd263245adcb7b55da01cca624 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Mon, 27 Apr 2020 22:48:30 +0100 Subject: [PATCH] add @screen completion item detail --- .../src/providers/completionProvider.ts | 19 +++++++--- .../src/util/screens.ts | 35 +++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 packages/tailwindcss-language-server/src/util/screens.ts diff --git a/packages/tailwindcss-language-server/src/providers/completionProvider.ts b/packages/tailwindcss-language-server/src/providers/completionProvider.ts index 3c77de0..066ae6a 100644 --- a/packages/tailwindcss-language-server/src/providers/completionProvider.ts +++ b/packages/tailwindcss-language-server/src/providers/completionProvider.ts @@ -14,6 +14,7 @@ import { isHtmlContext } from '../util/html' import { isCssContext } from '../util/css' import { findLast, findJsxStrings, arrFindLast } from '../util/find' import { stringifyConfigValue, stringifyCss } from '../util/stringify' +import { stringifyScreen, Screen } from '../util/screens' import isObject from '../util/isObject' import * as emmetHelper from 'emmet-helper' import { isValidLocationForEmmetAbbreviation } from '../util/isValidLocationForEmmetAbbreviation' @@ -380,6 +381,7 @@ function provideScreenDirectiveCompletions( items: Object.keys(screens).map((screen) => ({ label: screen, kind: CompletionItemKind.Constant, + data: 'screen', textEdit: { newText: screen, range: { @@ -564,11 +566,18 @@ export function resolveCompletionItem( state: State, item: CompletionItem ): CompletionItem { - if ( - item.data === 'helper' || - item.data === 'directive' || - item.data === 'variant' - ) { + if (['helper', 'directive', 'variant'].includes(item.data)) { + return item + } + + if (item.data === 'screen') { + let screens = dlv( + state.config, + ['theme', 'screens'], + dlv(state.config, ['screens'], {}) + ) + if (!isObject(screens)) screens = {} + item.detail = stringifyScreen(screens[item.label] as Screen) return item } diff --git a/packages/tailwindcss-language-server/src/util/screens.ts b/packages/tailwindcss-language-server/src/util/screens.ts new file mode 100644 index 0000000..2b029b2 --- /dev/null +++ b/packages/tailwindcss-language-server/src/util/screens.ts @@ -0,0 +1,35 @@ +import isObject from './isObject' + +export type MinMaxScreen = { + min?: string + max?: string +} + +export type RawScreen = { + raw: string +} + +export type Screen = string | RawScreen | MinMaxScreen | MinMaxScreen[] + +function isRawScreen(screen: unknown): screen is RawScreen { + return isObject(screen) && (screen as RawScreen).raw !== undefined +} + +export function stringifyScreen(screen: Screen): string { + if (!screen) return + if (typeof screen === 'string') return `@media (min-width: ${screen})` + if (isRawScreen(screen)) { + return `@media ${(screen as RawScreen).raw}` + } + let str = (Array.isArray(screen) ? screen : [screen]) + .map((range) => { + return [ + typeof range.min === 'string' ? `(min-width: ${range.min})` : null, + typeof range.max === 'string' ? `(max-width: ${range.max})` : null, + ] + .filter(Boolean) + .join(' and ') + }) + .join(', ') + return str ? `@media ${str}` : undefined +}