|
| 1 | +import { workspace, RelativePattern, CancellationToken, Uri, WorkspaceFolder } from 'vscode' |
| 2 | +import braces from 'braces' |
| 3 | +import { CONFIG_GLOB, CSS_GLOB } from '@tailwindcss/language-server/src/lib/constants' |
| 4 | +import { getExcludePatterns } from './exclusions' |
| 5 | + |
| 6 | +export interface SearchOptions { |
| 7 | + folders: readonly WorkspaceFolder[] |
| 8 | + token: CancellationToken |
| 9 | +} |
| 10 | + |
| 11 | +export async function anyWorkspaceFoldersNeedServer({ folders, token }: SearchOptions) { |
| 12 | + // An explicit config file setting means we need the server |
| 13 | + for (let folder of folders) { |
| 14 | + let settings = workspace.getConfiguration('tailwindCSS', folder) |
| 15 | + let configFilePath = settings.get('experimental.configFile') |
| 16 | + |
| 17 | + // No setting provided |
| 18 | + if (!configFilePath) continue |
| 19 | + |
| 20 | + // Ths config file may be a string: |
| 21 | + // A path pointing to a CSS or JS config file |
| 22 | + if (typeof configFilePath === 'string') return true |
| 23 | + |
| 24 | + // Ths config file may be an object: |
| 25 | + // A map of config files to one or more globs |
| 26 | + // |
| 27 | + // If we get an empty object the language server will do a search anyway so |
| 28 | + // we'll act as if no option was passed to be consistent |
| 29 | + if (typeof configFilePath === 'object' && Object.values(configFilePath).length > 0) return true |
| 30 | + } |
| 31 | + |
| 32 | + let configs: Array<() => Thenable<Uri[]>> = [] |
| 33 | + let stylesheets: Array<() => Thenable<Uri[]>> = [] |
| 34 | + |
| 35 | + for (let folder of folders) { |
| 36 | + let exclusions = getExcludePatterns(folder).flatMap((pattern) => braces.expand(pattern)) |
| 37 | + let exclude = `{${exclusions.join(',').replace(/{/g, '%7B').replace(/}/g, '%7D')}}` |
| 38 | + |
| 39 | + configs.push(() => |
| 40 | + workspace.findFiles( |
| 41 | + new RelativePattern(folder, `**/${CONFIG_GLOB}`), |
| 42 | + exclude, |
| 43 | + undefined, |
| 44 | + token, |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + stylesheets.push(() => |
| 49 | + workspace.findFiles(new RelativePattern(folder, `**/${CSS_GLOB}`), exclude, undefined, token), |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + // If we find a config file then we need the server |
| 54 | + let configUrls = await Promise.all(configs.map((fn) => fn())) |
| 55 | + for (let group of configUrls) { |
| 56 | + if (group.length > 0) { |
| 57 | + return true |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // If we find a possibly-related stylesheet then we need the server |
| 62 | + // The step is done last because it requires reading individual files |
| 63 | + // to determine if the server should be started. |
| 64 | + // |
| 65 | + // This is also, unfortunately, prone to starting the server unncessarily |
| 66 | + // in projects that don't use TailwindCSS so we do this one-by-one instead |
| 67 | + // of all at once to keep disk I/O low. |
| 68 | + let stylesheetUrls = await Promise.all(stylesheets.map((fn) => fn())) |
| 69 | + for (let group of stylesheetUrls) { |
| 70 | + for (let file of group) { |
| 71 | + if (await fileMayBeTailwindRelated(file)) { |
| 72 | + return true |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +let HAS_CONFIG = /@config\s*['"]/ |
| 79 | +let HAS_IMPORT = /@import\s*['"]/ |
| 80 | +let HAS_TAILWIND = /@tailwind\s*[^;]+;/ |
| 81 | +let HAS_THEME = /@theme\s*\{/ |
| 82 | + |
| 83 | +export async function fileMayBeTailwindRelated(uri: Uri) { |
| 84 | + let buffer = await workspace.fs.readFile(uri) |
| 85 | + let contents = buffer.toString() |
| 86 | + |
| 87 | + return ( |
| 88 | + HAS_CONFIG.test(contents) || |
| 89 | + HAS_IMPORT.test(contents) || |
| 90 | + HAS_TAILWIND.test(contents) || |
| 91 | + HAS_THEME.test(contents) |
| 92 | + ) |
| 93 | +} |
0 commit comments