diff --git a/packages/tailwindcss-class-names/src/index.js b/packages/tailwindcss-class-names/src/index.js index 1835e66efab64cbc1e93c7335b0d9cadb7a836b1..f9f39c4c9e30d741c28f7e84e2e10fc642f46f89 100644 --- a/packages/tailwindcss-class-names/src/index.js +++ b/packages/tailwindcss-class-names/src/index.js @@ -17,7 +17,7 @@ return new Promise((resolve, reject) => { let g = new nodeGlob.Glob(pattern, options) let matches = [] let max = dlv(options, 'max', Infinity) - g.on('match', (match) => { + g.on('match', match => { matches.push(path.resolve(options.cwd || process.cwd(), match)) if (matches.length === max) { g.abort() @@ -39,42 +39,47 @@ ) } import dlv from 'dlv' -import * as path from 'path' - '**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js' - -import dlv from 'dlv' import semver from 'semver' cwd = process.cwd(), { onChange = () => {} } = {} ) { - async function run() { + let configPath + let postcss + let tailwindcss - let configPath + let version +import extractClassNames from './extractClassNames.mjs' import dset from 'dset' -import importFrom from 'import-from' + try { - let tailwindcss + configPath = await glob( - let version + '**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js', import extractClassNames from './extractClassNames.mjs' -import dset from 'dset' + if (matches.length === max) { - configPath = await glob(CONFIG_GLOB, { + cwd, +import getVariants from './getVariants' import dset from 'dset' -import semver from 'semver' +import getVariants from './getVariants' import importFrom from 'import-from' +import Hook from './hook.mjs' import importFrom from 'import-from' -import extractClassNames from './extractClassNames.mjs' - }) + ) invariant(configPath.length === 1, 'No Tailwind CSS config found.') configPath = configPath[0] postcss = importFrom(cwd, 'postcss') tailwindcss = importFrom(cwd, 'tailwindcss') version = importFrom(cwd, 'tailwindcss/package.json').version import extractClassNames from './extractClassNames.mjs' +import invariant from 'tiny-invariant' + return null + } +import extractClassNames from './extractClassNames.mjs' import dset from 'dset' + async function run() { const sepLocation = semver.gte(version, '0.99.0') ? ['separator'] : ['options', 'separator'] let userSeperator +import getVariants from './getVariants' import nodeGlob from 'glob' -import extractClassNames from './extractClassNames.mjs' userSeperator = dlv(exports, sepLocation) dset(exports, sepLocation, '__TAILWIND_SEPARATOR__') return exports @@ -101,63 +107,47 @@ } return { import chokidar from 'chokidar' -import Hook from './hook.mjs' -import chokidar from 'chokidar' import dlv from 'dlv' separator: typeof userSeperator === 'undefined' ? ':' : userSeperator, classNames: await extractClassNames(ast), - dependencies: hook.deps, + dependencies: [configPath, ...hook.deps], plugins: getPlugins(config), -import chokidar from 'chokidar' +import getVariants from './getVariants' import chokidar from 'chokidar' } } let watcher +import getVariants from './getVariants' import semver from 'semver' -import extractClassNames from './extractClassNames.mjs' if (watcher) watcher.close() watcher = chokidar - .watch([CONFIG_GLOB, ...files]) + .watch(files) .on('change', handleChange) .on('unlink', handleChange) } - async function handleChange() { - const prevDeps = result ? result.dependencies : [] - try { import extractClassNames from './extractClassNames.mjs' -import invariant from 'tiny-invariant' +import dlv from 'dlv' import extractClassNames from './extractClassNames.mjs' import extractClassNames from './extractClassNames.mjs' -import Hook from './hook.mjs' import extractClassNames from './extractClassNames.mjs' import dlv from 'dlv' - } import extractClassNames from './extractClassNames.mjs' import dset from 'dset' + async function handleChange() { import extractClassNames from './extractClassNames.mjs' -import importFrom from 'import-from' - } +function arraysEqual(arr1, arr2) { import invariant from 'tiny-invariant' -import nodeGlob from 'glob' -import chokidar from 'chokidar' import semver from 'semver' import extractClassNames from './extractClassNames.mjs' import dset from 'dset' import invariant from 'tiny-invariant' -import * as path from 'path' +import importFrom from 'import-from' -import invariant from 'tiny-invariant' import chokidar from 'chokidar' -import extractClassNames from './extractClassNames.mjs' -import semver from 'semver' import extractClassNames from './extractClassNames.mjs' -import extractClassNames from './extractClassNames.mjs' - watch() - return null +import nodeGlob from 'glob' } - - watch(result.dependencies) return result } diff --git a/packages/tailwindcss-language-server/src/server.ts b/packages/tailwindcss-language-server/src/server.ts index 729d1f0c30862343cf77f8ab1c278230155392d7..58fada32d5f70b0653c5bb9228fcd71053621143 100644 --- a/packages/tailwindcss-language-server/src/server.ts +++ b/packages/tailwindcss-language-server/src/server.ts @@ -18,8 +18,8 @@ TextDocumentPositionParams, DidChangeConfigurationNotification, } from 'vscode-languageserver' import getTailwindState from 'tailwindcss-class-names' - * Copyright (c) Microsoft Corporation. All rights reserved. /* -------------------------------------------------------------------------------------------- + getDocumentSettings(state, event.document.uri) import { provideCompletions, resolveCompletionItem, @@ -28,7 +28,7 @@ import { provideHover } from './providers/hoverProvider' import { URI } from 'vscode-uri' import { getDocumentSettings } from './util/getDocumentSettings' -let state: State = { enabled: false } +let state: State = null let connection = createConnection(ProposedFeatures.all) let documents = new TextDocuments() let workspaceFolder: string | null @@ -47,63 +47,45 @@ documents.listen(connection) connection.onInitialize( async (params: InitializeParams): Promise => { - * ------------------------------------------------------------------------------------------ */ +/* -------------------------------------------------------------------------------------------- * Licensed under the MIT License. See License.txt in the project root for license information. + TextDocuments, - const editorState: EditorState = { - * ------------------------------------------------------------------------------------------ */ - * ------------------------------------------------------------------------------------------ */ + import { - * ------------------------------------------------------------------------------------------ */ + createConnection, - globalSettings, - * ------------------------------------------------------------------------------------------ */ + InitializeResult, ProposedFeatures, - + CompletionParams, - + CompletionParams, /* -------------------------------------------------------------------------------------------- - + CompletionParams, * Copyright (c) Microsoft Corporation. All rights reserved. - + CompletionParams, * Licensed under the MIT License. See License.txt in the project root for license information. - - + CompletionParams, * ------------------------------------------------------------------------------------------ */ - params.rootPath || URI.parse(params.rootUri).path, + }, - { + } - createConnection, + * Copyright (c) Microsoft Corporation. All rights reserved. - TextDocuments, + const capabilities = params.capabilities - ProposedFeatures, - connection.sendNotification('tailwindcss/configUpdated', [ -import { /* -------------------------------------------------------------------------------------------- - state.config, + connection, - state.plugins, -import { * ------------------------------------------------------------------------------------------ */ -import { -import { + * ------------------------------------------------------------------------------------------ */ import { -import { + * ------------------------------------------------------------------------------------------ */ createConnection, -import { + * ------------------------------------------------------------------------------------------ */ TextDocuments, - } - }, - createConnection, /* -------------------------------------------------------------------------------------------- - ) - - if (tailwindState) { - createConnection, * ------------------------------------------------------------------------------------------ */ - } else { - createConnection, import { } @@ -116,23 +98,9 @@ // }, textDocumentSync: documents.syncKind, completionProvider: { resolveProvider: true, - triggerCharacters: [ - // class attributes - '"', - "'", - '`', - // between class names - ProposedFeatures, /* -------------------------------------------------------------------------------------------- - // @apply and emmet-style - '.', - ProposedFeatures, * ------------------------------------------------------------------------------------------ */ - '[', - // TODO: restart server if separater changes? - ProposedFeatures, createConnection, - ], }, hoverProvider: true, }, @@ -151,7 +119,7 @@ } connection.sendNotification('tailwindcss/configUpdated', [ /* -------------------------------------------------------------------------------------------- - TextDocumentSyncKind, + globalSettings, state.config, state.plugins, ]) @@ -175,22 +143,18 @@ connection.onCompletion( (params: CompletionParams): Promise => { InitializeParams, - createConnection, - InitializeParams, TextDocuments, } ) connection.onCompletionResolve( (item: CompletionItem): CompletionItem => { - if (!state.enabled) return null return resolveCompletionItem(state, item) } ) connection.onHover( (params: TextDocumentPositionParams): Hover => { - if (!state.enabled) return null return provideHover(state, params) } ) diff --git a/packages/tailwindcss-language-server/src/util/state.ts b/packages/tailwindcss-language-server/src/util/state.ts index 175dade6ed563cc36605c1fc3c55f5b56e900573..4b258320520a2dc616f627a4d1e5f73ad3742383 100644 --- a/packages/tailwindcss-language-server/src/util/state.ts +++ b/packages/tailwindcss-language-server/src/util/state.ts @@ -28,20 +28,17 @@ emmetCompletions: boolean } export type State = null | { - enabled: boolean - export type ClassNamesTree = { - config?: any - separator?: string - export type ClassNamesContext = { - +export type ClassNamesTree = { [key: string]: string[] - +export type ClassNamesTree = { export type ClassNames = { - +export type ClassNamesTree = { context: ClassNamesContext -export type ClassNamesTree = { + [key: string]: ClassNamesTree + dependencies: string[] + editor: EditorState } export type DocumentClassList = {