Merge settings with defaults (#605)

master
Brad Cornes 2022-09-13 17:31:09 +01:00 committed by GitHub
parent 9ca9359fec
commit 8aa2064684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 162 additions and 156 deletions

109
package-lock.json generated
View File

@ -24,6 +24,7 @@
"css.escape": "1.5.1", "css.escape": "1.5.1",
"culori": "0.20.1", "culori": "0.20.1",
"debounce": "1.2.0", "debounce": "1.2.0",
"deepmerge": "4.2.2",
"detect-indent": "6.0.0", "detect-indent": "6.0.0",
"detective": "5.2.0", "detective": "5.2.0",
"dlv": "1.1.3", "dlv": "1.1.3",
@ -245,9 +246,9 @@
} }
}, },
"node_modules/@babel/helper-define-polyfill-provider": { "node_modules/@babel/helper-define-polyfill-provider": {
"version": "0.3.2", "version": "0.3.3",
"resolved": "https://registry.npmjs.org/@babel/helper-define-polyfill-provider/-/helper-define-polyfill-provider-0.3.2.tgz", "resolved": "https://registry.npmjs.org/@babel/helper-define-polyfill-provider/-/helper-define-polyfill-provider-0.3.3.tgz",
"integrity": "sha512-r9QJJ+uDWrd+94BSPcP6/de67ygLtvVy6cK4luE6MOuDsZIdoaPBnfSpbO/+LTifjPckbKXRuI9BB/Z2/y3iTg==", "integrity": "sha512-z5aQKU4IzbqCC1XH0nAqfsFLMVSo22SBKUc0BxGrLkolTdPTructy0ToNnlO2zA4j9Q/7pjMZf0DSY+DSTYzww==",
"dependencies": { "dependencies": {
"@babel/helper-compilation-targets": "^7.17.7", "@babel/helper-compilation-targets": "^7.17.7",
"@babel/helper-plugin-utils": "^7.16.7", "@babel/helper-plugin-utils": "^7.16.7",
@ -1594,11 +1595,11 @@
} }
}, },
"node_modules/@babel/preset-env/node_modules/babel-plugin-polyfill-regenerator": { "node_modules/@babel/preset-env/node_modules/babel-plugin-polyfill-regenerator": {
"version": "0.4.0", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/babel-plugin-polyfill-regenerator/-/babel-plugin-polyfill-regenerator-0.4.0.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-polyfill-regenerator/-/babel-plugin-polyfill-regenerator-0.4.1.tgz",
"integrity": "sha512-RW1cnryiADFeHmfLS+WW/G431p1PsW5qdRdz0SDRi7TKcUgc7Oh/uXkT7MZ/+tGsT1BkczEAmD5XjUyJ5SWDTw==", "integrity": "sha512-NtQGmyQDXjQqQ+IzRkBVwEOz9lQ4zxAQZgoAYEtU9dJjnl1Oc98qnN7jcp+bE7O7aYzVpavXE3/VKXNzUbh7aw==",
"dependencies": { "dependencies": {
"@babel/helper-define-polyfill-provider": "^0.3.2" "@babel/helper-define-polyfill-provider": "^0.3.3"
}, },
"peerDependencies": { "peerDependencies": {
"@babel/core": "^7.0.0-0" "@babel/core": "^7.0.0-0"
@ -5383,9 +5384,9 @@
"dev": true "dev": true
}, },
"node_modules/autoprefixer": { "node_modules/autoprefixer": {
"version": "10.4.9", "version": "10.4.10",
"resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.9.tgz", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.10.tgz",
"integrity": "sha512-Uu67eduPEmOeA0vyJby5ghu1AAELCCNSsLAjK+lz6kYzNM5sqnBO36MqfsjhPjQF/BaJM5U/UuFYyl7PavY/wQ==", "integrity": "sha512-nMaiDARyp1e74c8IeAXkr+BmFKa8By4Zak7tyaNPF09Iu39WFpNXOWrVirmXjKr+5cOyERwvtbMOLYz6iBJYgQ==",
"funding": [ "funding": [
{ {
"type": "opencollective", "type": "opencollective",
@ -5399,7 +5400,7 @@
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"browserslist": "^4.21.3", "browserslist": "^4.21.3",
"caniuse-lite": "^1.0.30001394", "caniuse-lite": "^1.0.30001399",
"fraction.js": "^4.2.0", "fraction.js": "^4.2.0",
"normalize-range": "^0.1.2", "normalize-range": "^0.1.2",
"picocolors": "^1.0.0", "picocolors": "^1.0.0",
@ -5665,12 +5666,12 @@
} }
}, },
"node_modules/babel-plugin-polyfill-corejs2": { "node_modules/babel-plugin-polyfill-corejs2": {
"version": "0.3.2", "version": "0.3.3",
"resolved": "https://registry.npmjs.org/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.3.2.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.3.3.tgz",
"integrity": "sha512-LPnodUl3lS0/4wN3Rb+m+UK8s7lj2jcLRrjho4gLw+OJs+I4bvGXshINesY5xx/apM+biTnQ9reDI8yj+0M5+Q==", "integrity": "sha512-8hOdmFYFSZhqg2C/JgLUQ+t52o5nirNwaWM2B9LWteozwIvM14VSwdsCAUET10qT+kmySAlseadmfeeSWFCy+Q==",
"dependencies": { "dependencies": {
"@babel/compat-data": "^7.17.7", "@babel/compat-data": "^7.17.7",
"@babel/helper-define-polyfill-provider": "^0.3.2", "@babel/helper-define-polyfill-provider": "^0.3.3",
"semver": "^6.1.1" "semver": "^6.1.1"
}, },
"peerDependencies": { "peerDependencies": {
@ -6147,9 +6148,9 @@
} }
}, },
"node_modules/caniuse-lite": { "node_modules/caniuse-lite": {
"version": "1.0.30001397", "version": "1.0.30001399",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001397.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001399.tgz",
"integrity": "sha512-SW9N2TbCdLf0eiNDRrrQXx2sOkaakNZbCjgNpPyMJJbiOrU5QzMIrXOVMRM1myBXTD5iTkdrtU/EguCrBocHlA==", "integrity": "sha512-4vQ90tMKS+FkvuVWS5/QY1+d805ODxZiKFzsU8o/RsVJz49ZSRR8EjykLJbqhzdPgadbX6wB538wOzle3JniRA==",
"funding": [ "funding": [
{ {
"type": "opencollective", "type": "opencollective",
@ -7363,9 +7364,9 @@
} }
}, },
"node_modules/date-fns": { "node_modules/date-fns": {
"version": "2.29.2", "version": "2.29.3",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.2.tgz", "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
"integrity": "sha512-0VNbwmWJDS/G3ySwFSJA3ayhbURMTJLtwM2DTxf9CWondCnh6DTNlO9JgRSq6ibf4eD0lfMJNBxUdEAHHix+bA==", "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==",
"engines": { "engines": {
"node": ">=0.11" "node": ">=0.11"
}, },
@ -7755,9 +7756,9 @@
} }
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.4.247", "version": "1.4.248",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.247.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.248.tgz",
"integrity": "sha512-FLs6R4FQE+1JHM0hh3sfdxnYjKvJpHZyhQDjc2qFq/xFvmmRt/TATNToZhrcGUFzpF2XjeiuozrA8lI0PZmYYw==" "integrity": "sha512-qShjzEYpa57NnhbW2K+g+Fl+eNoDvQ7I+2MRwWnU6Z6F0HhXekzsECCLv+y2OJUsRodjqoSfwHkIX42VUFtUzg=="
}, },
"node_modules/emoji-regex": { "node_modules/emoji-regex": {
"version": "9.2.2", "version": "9.2.2",
@ -19972,9 +19973,9 @@
} }
}, },
"node_modules/update-browserslist-db": { "node_modules/update-browserslist-db": {
"version": "1.0.7", "version": "1.0.9",
"resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.7.tgz", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.9.tgz",
"integrity": "sha512-iN/XYesmZ2RmmWAiI4Z5rq0YqSiv0brj9Ce9CfhNE4xIW2h+MFxcgkxIzZ+ShkFPUkjU3gQ+3oypadD3RAMtrg==", "integrity": "sha512-/xsqn21EGVdXI3EXSum1Yckj3ZVZugqyOZQ/CxYPBD/R+ko9NSUScf8tFF4dOKY+2pvSSJA/S+5B8s4Zr4kyvg==",
"funding": [ "funding": [
{ {
"type": "opencollective", "type": "opencollective",
@ -21032,9 +21033,9 @@
} }
}, },
"@babel/helper-define-polyfill-provider": { "@babel/helper-define-polyfill-provider": {
"version": "0.3.2", "version": "0.3.3",
"resolved": "https://registry.npmjs.org/@babel/helper-define-polyfill-provider/-/helper-define-polyfill-provider-0.3.2.tgz", "resolved": "https://registry.npmjs.org/@babel/helper-define-polyfill-provider/-/helper-define-polyfill-provider-0.3.3.tgz",
"integrity": "sha512-r9QJJ+uDWrd+94BSPcP6/de67ygLtvVy6cK4luE6MOuDsZIdoaPBnfSpbO/+LTifjPckbKXRuI9BB/Z2/y3iTg==", "integrity": "sha512-z5aQKU4IzbqCC1XH0nAqfsFLMVSo22SBKUc0BxGrLkolTdPTructy0ToNnlO2zA4j9Q/7pjMZf0DSY+DSTYzww==",
"requires": { "requires": {
"@babel/helper-compilation-targets": "^7.17.7", "@babel/helper-compilation-targets": "^7.17.7",
"@babel/helper-plugin-utils": "^7.16.7", "@babel/helper-plugin-utils": "^7.16.7",
@ -21935,11 +21936,11 @@
}, },
"dependencies": { "dependencies": {
"babel-plugin-polyfill-regenerator": { "babel-plugin-polyfill-regenerator": {
"version": "0.4.0", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/babel-plugin-polyfill-regenerator/-/babel-plugin-polyfill-regenerator-0.4.0.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-polyfill-regenerator/-/babel-plugin-polyfill-regenerator-0.4.1.tgz",
"integrity": "sha512-RW1cnryiADFeHmfLS+WW/G431p1PsW5qdRdz0SDRi7TKcUgc7Oh/uXkT7MZ/+tGsT1BkczEAmD5XjUyJ5SWDTw==", "integrity": "sha512-NtQGmyQDXjQqQ+IzRkBVwEOz9lQ4zxAQZgoAYEtU9dJjnl1Oc98qnN7jcp+bE7O7aYzVpavXE3/VKXNzUbh7aw==",
"requires": { "requires": {
"@babel/helper-define-polyfill-provider": "^0.3.2" "@babel/helper-define-polyfill-provider": "^0.3.3"
} }
}, },
"semver": { "semver": {
@ -25022,13 +25023,13 @@
"dev": true "dev": true
}, },
"autoprefixer": { "autoprefixer": {
"version": "10.4.9", "version": "10.4.10",
"resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.9.tgz", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.10.tgz",
"integrity": "sha512-Uu67eduPEmOeA0vyJby5ghu1AAELCCNSsLAjK+lz6kYzNM5sqnBO36MqfsjhPjQF/BaJM5U/UuFYyl7PavY/wQ==", "integrity": "sha512-nMaiDARyp1e74c8IeAXkr+BmFKa8By4Zak7tyaNPF09Iu39WFpNXOWrVirmXjKr+5cOyERwvtbMOLYz6iBJYgQ==",
"peer": true, "peer": true,
"requires": { "requires": {
"browserslist": "^4.21.3", "browserslist": "^4.21.3",
"caniuse-lite": "^1.0.30001394", "caniuse-lite": "^1.0.30001399",
"fraction.js": "^4.2.0", "fraction.js": "^4.2.0",
"normalize-range": "^0.1.2", "normalize-range": "^0.1.2",
"picocolors": "^1.0.0", "picocolors": "^1.0.0",
@ -25229,12 +25230,12 @@
} }
}, },
"babel-plugin-polyfill-corejs2": { "babel-plugin-polyfill-corejs2": {
"version": "0.3.2", "version": "0.3.3",
"resolved": "https://registry.npmjs.org/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.3.2.tgz", "resolved": "https://registry.npmjs.org/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.3.3.tgz",
"integrity": "sha512-LPnodUl3lS0/4wN3Rb+m+UK8s7lj2jcLRrjho4gLw+OJs+I4bvGXshINesY5xx/apM+biTnQ9reDI8yj+0M5+Q==", "integrity": "sha512-8hOdmFYFSZhqg2C/JgLUQ+t52o5nirNwaWM2B9LWteozwIvM14VSwdsCAUET10qT+kmySAlseadmfeeSWFCy+Q==",
"requires": { "requires": {
"@babel/compat-data": "^7.17.7", "@babel/compat-data": "^7.17.7",
"@babel/helper-define-polyfill-provider": "^0.3.2", "@babel/helper-define-polyfill-provider": "^0.3.3",
"semver": "^6.1.1" "semver": "^6.1.1"
}, },
"dependencies": { "dependencies": {
@ -25613,9 +25614,9 @@
} }
}, },
"caniuse-lite": { "caniuse-lite": {
"version": "1.0.30001397", "version": "1.0.30001399",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001397.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001399.tgz",
"integrity": "sha512-SW9N2TbCdLf0eiNDRrrQXx2sOkaakNZbCjgNpPyMJJbiOrU5QzMIrXOVMRM1myBXTD5iTkdrtU/EguCrBocHlA==" "integrity": "sha512-4vQ90tMKS+FkvuVWS5/QY1+d805ODxZiKFzsU8o/RsVJz49ZSRR8EjykLJbqhzdPgadbX6wB538wOzle3JniRA=="
}, },
"capture-exit": { "capture-exit": {
"version": "2.0.0", "version": "2.0.0",
@ -26570,9 +26571,9 @@
} }
}, },
"date-fns": { "date-fns": {
"version": "2.29.2", "version": "2.29.3",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.2.tgz", "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
"integrity": "sha512-0VNbwmWJDS/G3ySwFSJA3ayhbURMTJLtwM2DTxf9CWondCnh6DTNlO9JgRSq6ibf4eD0lfMJNBxUdEAHHix+bA==" "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA=="
}, },
"dateformat": { "dateformat": {
"version": "3.0.3", "version": "3.0.3",
@ -26872,9 +26873,9 @@
} }
}, },
"electron-to-chromium": { "electron-to-chromium": {
"version": "1.4.247", "version": "1.4.248",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.247.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.248.tgz",
"integrity": "sha512-FLs6R4FQE+1JHM0hh3sfdxnYjKvJpHZyhQDjc2qFq/xFvmmRt/TATNToZhrcGUFzpF2XjeiuozrA8lI0PZmYYw==" "integrity": "sha512-qShjzEYpa57NnhbW2K+g+Fl+eNoDvQ7I+2MRwWnU6Z6F0HhXekzsECCLv+y2OJUsRodjqoSfwHkIX42VUFtUzg=="
}, },
"emoji-regex": { "emoji-regex": {
"version": "9.2.2", "version": "9.2.2",
@ -36312,9 +36313,9 @@
"dev": true "dev": true
}, },
"update-browserslist-db": { "update-browserslist-db": {
"version": "1.0.7", "version": "1.0.9",
"resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.7.tgz", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.9.tgz",
"integrity": "sha512-iN/XYesmZ2RmmWAiI4Z5rq0YqSiv0brj9Ce9CfhNE4xIW2h+MFxcgkxIzZ+ShkFPUkjU3gQ+3oypadD3RAMtrg==", "integrity": "sha512-/xsqn21EGVdXI3EXSum1Yckj3ZVZugqyOZQ/CxYPBD/R+ko9NSUScf8tFF4dOKY+2pvSSJA/S+5B8s4Zr4kyvg==",
"requires": { "requires": {
"escalade": "^3.1.1", "escalade": "^3.1.1",
"picocolors": "^1.0.0" "picocolors": "^1.0.0"

View File

@ -44,6 +44,7 @@
"color-name": "1.1.4", "color-name": "1.1.4",
"culori": "0.20.1", "culori": "0.20.1",
"debounce": "1.2.0", "debounce": "1.2.0",
"deepmerge": "4.2.2",
"detective": "5.2.0", "detective": "5.2.0",
"dlv": "1.1.3", "dlv": "1.1.3",
"dset": "3.1.2", "dset": "3.1.2",

View File

@ -78,10 +78,11 @@ import { getColor } from 'tailwindcss-language-service/src/util/color'
import * as culori from 'culori' import * as culori from 'culori'
import namedColors from 'color-name' import namedColors from 'color-name'
import tailwindPlugins from './lib/plugins' import tailwindPlugins from './lib/plugins'
import isExcluded, { DEFAULT_FILES_EXCLUDE } from './util/isExcluded' import isExcluded from './util/isExcluded'
import { getFileFsPath, normalizeFileNameToFsPath } from './util/uri' import { getFileFsPath, normalizeFileNameToFsPath } from './util/uri'
import { equal } from 'tailwindcss-language-service/src/util/array' import { equal } from 'tailwindcss-language-service/src/util/array'
import preflight from 'tailwindcss/lib/css/preflight.css' import preflight from 'tailwindcss/lib/css/preflight.css'
import merge from 'deepmerge'
// @ts-ignore // @ts-ignore
global.__preflight = preflight global.__preflight = preflight
@ -238,7 +239,42 @@ async function createProjectService(
scopeUri: uri, scopeUri: uri,
}), }),
]) ])
let config: Settings = { editor, tailwindCSS } editor = isObject(editor) ? editor : {}
tailwindCSS = isObject(tailwindCSS) ? tailwindCSS : {}
let config: Settings = merge<Settings>(
{
editor: { tabSize: 2 },
tailwindCSS: {
emmetCompletions: false,
classAttributes: ['class', 'className', 'ngClass'],
codeActions: true,
hovers: true,
suggestions: true,
validate: true,
colorDecorators: true,
rootFontSize: 16,
lint: {
cssConflict: 'warning',
invalidApply: 'error',
invalidScreen: 'error',
invalidVariant: 'error',
invalidConfigPath: 'error',
invalidTailwindDirective: 'error',
recommendedVariantOrder: 'warning',
},
showPixelEquivalents: true,
includeLanguages: {},
files: { exclude: ['**/.git/**', '**/node_modules/**', '**/.hg/**', '**/.svn/**'] },
experimental: {
classRegex: [],
configFile: null,
},
},
},
{ editor, tailwindCSS },
{ arrayMerge: (_destinationArray, sourceArray, _options) => sourceArray }
)
documentSettingsCache.set(uri, config) documentSettingsCache.set(uri, config)
return config return config
} }
@ -266,7 +302,7 @@ async function createProjectService(
} }
let chokidarWatcher: chokidar.FSWatcher let chokidarWatcher: chokidar.FSWatcher
let ignore = state.editor.globalSettings.tailwindCSS.files?.exclude ?? DEFAULT_FILES_EXCLUDE let ignore = state.editor.globalSettings.tailwindCSS.files.exclude
function onFileEvents(changes: Array<{ file: string; type: FileChangeType }>): void { function onFileEvents(changes: Array<{ file: string; type: FileChangeType }>): void {
let needsInit = false let needsInit = false
@ -421,7 +457,7 @@ async function createProjectService(
configPath = ( configPath = (
await glob([`**/${CONFIG_FILE_GLOB}`], { await glob([`**/${CONFIG_FILE_GLOB}`], {
cwd: folder, cwd: folder,
ignore: state.editor.globalSettings.tailwindCSS.files?.exclude ?? DEFAULT_FILES_EXCLUDE, ignore: state.editor.globalSettings.tailwindCSS.files.exclude,
onlyFiles: true, onlyFiles: true,
absolute: true, absolute: true,
suppressErrors: true, suppressErrors: true,
@ -937,10 +973,9 @@ async function createProjectService(
}, },
async onUpdateSettings(settings: any): Promise<void> { async onUpdateSettings(settings: any): Promise<void> {
documentSettingsCache.clear() documentSettingsCache.clear()
let previousExclude = let previousExclude = state.editor.globalSettings.tailwindCSS.files.exclude
state.editor.globalSettings.tailwindCSS.files?.exclude ?? DEFAULT_FILES_EXCLUDE
state.editor.globalSettings = await state.editor.getConfiguration() state.editor.globalSettings = await state.editor.getConfiguration()
if (!equal(previousExclude, settings.tailwindCSS.files?.exclude ?? DEFAULT_FILES_EXCLUDE)) { if (!equal(previousExclude, settings.tailwindCSS.files.exclude)) {
tryInit() tryInit()
} else { } else {
if (state.enabled) { if (state.enabled) {

View File

@ -4,13 +4,11 @@ import { State } from 'tailwindcss-language-service/src/util/state'
import { TextDocument } from 'vscode-languageserver-textdocument' import { TextDocument } from 'vscode-languageserver-textdocument'
import { getFileFsPath } from './uri' import { getFileFsPath } from './uri'
export const DEFAULT_FILES_EXCLUDE = ['**/.git/**', '**/.svn/**', '**/.hg/**', '**/node_modules/**']
export default async function isExcluded(state: State, document: TextDocument): Promise<boolean> { export default async function isExcluded(state: State, document: TextDocument): Promise<boolean> {
let settings = await state.editor.getConfiguration(document.uri) let settings = await state.editor.getConfiguration(document.uri)
let file = getFileFsPath(document.uri) let file = getFileFsPath(document.uri)
for (let pattern of settings.tailwindCSS.files?.exclude ?? DEFAULT_FILES_EXCLUDE) { for (let pattern of settings.tailwindCSS.files.exclude) {
if (minimatch(file, path.join(state.editor.folder, pattern))) { if (minimatch(file, path.join(state.editor.folder, pattern))) {
return true return true
} }

View File

@ -1,4 +1,4 @@
import { State } from './util/state' import { Settings, State } from './util/state'
import type { import type {
CompletionItem, CompletionItem,
CompletionItemKind, CompletionItemKind,
@ -400,7 +400,7 @@ async function provideCustomClassNameCompletions(
position: Position position: Position
): Promise<CompletionList> { ): Promise<CompletionList> {
const settings = await state.editor.getConfiguration(document.uri) const settings = await state.editor.getConfiguration(document.uri)
const regexes = dlv(settings, 'tailwindCSS.experimental.classRegex', []) const regexes = settings.tailwindCSS.experimental.classRegex
if (regexes.length === 0) return null if (regexes.length === 0) return null
const positionOffset = document.offsetAt(position) const positionOffset = document.offsetAt(position)
@ -1109,11 +1109,7 @@ export async function resolveCompletionItem(
item.detail = await getCssDetail(state, className) item.detail = await getCssDetail(state, className)
if (!item.documentation) { if (!item.documentation) {
const settings = await state.editor.getConfiguration() const settings = await state.editor.getConfiguration()
const css = stringifyCss(item.data.join(':'), className, { const css = stringifyCss(item.data.join(':'), className, settings)
tabSize: dlv(settings, 'editor.tabSize', 2),
showPixelEquivalents: dlv(settings, 'tailwindCSS.showPixelEquivalents', true),
rootFontSize: dlv(settings, 'tailwindCSS.rootFontSize', 16),
})
if (css) { if (css) {
item.documentation = { item.documentation = {
kind: 'markdown' as typeof MarkupKind.Markdown, kind: 'markdown' as typeof MarkupKind.Markdown,
@ -1141,13 +1137,7 @@ function isContextItem(state: State, keys: string[]): boolean {
return isObject(item.__info) && !item.__info.__rule return isObject(item.__info) && !item.__info.__rule
} }
function stringifyDecls( function stringifyDecls(obj: any, settings: Settings): string {
obj: any,
{
showPixelEquivalents = false,
rootFontSize = 16,
}: Partial<{ showPixelEquivalents: boolean; rootFontSize: number }> = {}
): string {
let props = Object.keys(obj) let props = Object.keys(obj)
let nonCustomProps = props.filter((prop) => !prop.startsWith('--')) let nonCustomProps = props.filter((prop) => !prop.startsWith('--'))
@ -1159,7 +1149,9 @@ function stringifyDecls(
.map((prop) => .map((prop) =>
ensureArray(obj[prop]) ensureArray(obj[prop])
.map((value) => { .map((value) => {
const px = showPixelEquivalents ? remToPx(value, rootFontSize) : undefined const px = settings.tailwindCSS.showPixelEquivalents
? remToPx(value, settings.tailwindCSS.rootFontSize)
: undefined
return `${prop}: ${value}${px ? `/* ${px} */` : ''};` return `${prop}: ${value}${px ? `/* ${px} */` : ''};`
}) })
.join(' ') .join(' ')
@ -1173,10 +1165,7 @@ async function getCssDetail(state: State, className: any): Promise<string> {
} }
if (className.__rule === true) { if (className.__rule === true) {
const settings = await state.editor.getConfiguration() const settings = await state.editor.getConfiguration()
return stringifyDecls(removeMeta(className), { return stringifyDecls(removeMeta(className), settings)
showPixelEquivalents: dlv(settings, 'tailwindCSS.showPixelEquivalents', true),
rootFontSize: dlv(settings, 'tailwindCSS.rootFontSize', 16),
})
} }
return null return null
} }

View File

@ -106,11 +106,7 @@ async function provideClassNameHover(
const css = stringifyCss( const css = stringifyCss(
className.className, className.className,
dlv(state.classNames.classNames, [...parts, '__info']), dlv(state.classNames.classNames, [...parts, '__info']),
{ settings
tabSize: dlv(settings, 'editor.tabSize', 2),
showPixelEquivalents: dlv(settings, 'tailwindCSS.showPixelEquivalents', true),
rootFontSize: dlv(settings, 'tailwindCSS.rootFontSize', 16),
}
) )
if (!css) return null if (!css) return null

View File

@ -124,7 +124,7 @@ async function findCustomClassLists(
range?: Range range?: Range
): Promise<DocumentClassList[]> { ): Promise<DocumentClassList[]> {
const settings = await state.editor.getConfiguration(doc.uri) const settings = await state.editor.getConfiguration(doc.uri)
const regexes = dlv(settings, 'tailwindCSS.experimental.classRegex', []) const regexes = settings.tailwindCSS.experimental.classRegex
if (!Array.isArray(regexes) || regexes.length === 0) return [] if (!Array.isArray(regexes) || regexes.length === 0) return []

View File

@ -29,19 +29,15 @@ export function generateRules(state: State, classNames: string[]): { root: Root;
export async function stringifyRoot(state: State, root: Root, uri?: string): Promise<string> { export async function stringifyRoot(state: State, root: Root, uri?: string): Promise<string> {
let settings = await state.editor.getConfiguration(uri) let settings = await state.editor.getConfiguration(uri)
let tabSize = dlv(settings, 'editor.tabSize', 2)
let showPixelEquivalents = dlv(settings, 'tailwindCSS.showPixelEquivalents', true)
let rootFontSize = dlv(settings, 'tailwindCSS.rootFontSize', 16)
let clone = root.clone() let clone = root.clone()
clone.walkAtRules('defaults', (node) => { clone.walkAtRules('defaults', (node) => {
node.remove() node.remove()
}) })
if (showPixelEquivalents) { if (settings.tailwindCSS.showPixelEquivalents) {
clone.walkDecls((decl) => { clone.walkDecls((decl) => {
let px = remToPx(decl.value, rootFontSize) let px = remToPx(decl.value, settings.tailwindCSS.rootFontSize)
if (px) { if (px) {
decl.value = `${decl.value}/* ${px} */` decl.value = `${decl.value}/* ${px} */`
} }
@ -51,7 +47,9 @@ export async function stringifyRoot(state: State, root: Root, uri?: string): Pro
return clone return clone
.toString() .toString()
.replace(/([^;{}\s])(\n\s*})/g, (_match, before, after) => `${before};${after}`) .replace(/([^;{}\s])(\n\s*})/g, (_match, before, after) => `${before};${after}`)
.replace(/^(?: )+/gm, (indent: string) => ' '.repeat((indent.length / 4) * tabSize)) .replace(/^(?: )+/gm, (indent: string) =>
' '.repeat((indent.length / 4) * settings.editor.tabSize)
)
} }
export function stringifyRules(state: State, rules: Rule[], tabSize: number = 2): string { export function stringifyRules(state: State, rules: Rule[], tabSize: number = 2): string {
@ -63,12 +61,12 @@ export function stringifyRules(state: State, rules: Rule[], tabSize: number = 2)
export async function stringifyDecls(state: State, rule: Rule, uri?: string): Promise<string> { export async function stringifyDecls(state: State, rule: Rule, uri?: string): Promise<string> {
let settings = await state.editor.getConfiguration(uri) let settings = await state.editor.getConfiguration(uri)
let showPixelEquivalents = dlv(settings, 'tailwindCSS.showPixelEquivalents', true)
let rootFontSize = dlv(settings, 'tailwindCSS.rootFontSize', 16)
let result = [] let result = []
rule.walkDecls(({ prop, value }) => { rule.walkDecls(({ prop, value }) => {
let px = showPixelEquivalents ? remToPx(value, rootFontSize) : undefined let px = settings.tailwindCSS.showPixelEquivalents
? remToPx(value, settings.tailwindCSS.rootFontSize)
: undefined
result.push(`${prop}: ${value}${px ? `/* ${px} */` : ''};`) result.push(`${prop}: ${value}${px ? `/* ${px} */` : ''};`)
}) })
return result.join(' ') return result.join(' ')

View File

@ -33,11 +33,11 @@ export type EditorState = {
type DiagnosticSeveritySetting = 'ignore' | 'warning' | 'error' type DiagnosticSeveritySetting = 'ignore' | 'warning' | 'error'
export type Settings = { export type EditorSettings = {
editor: {
tabSize: number tabSize: number
} }
tailwindCSS: {
export type TailwindCssSettings = {
emmetCompletions: boolean emmetCompletions: boolean
includeLanguages: Record<string, string> includeLanguages: Record<string, string>
classAttributes: string[] classAttributes: string[]
@ -65,6 +65,10 @@ export type Settings = {
exclude: string[] exclude: string[]
} }
} }
export type Settings = {
editor: EditorSettings
tailwindCSS: TailwindCssSettings
} }
export interface FeatureFlags { export interface FeatureFlags {

View File

@ -5,6 +5,7 @@ import { ensureArray } from './array'
import { remToPx } from './remToPx' import { remToPx } from './remToPx'
import stringifyObject from 'stringify-object' import stringifyObject from 'stringify-object'
import isObject from './isObject' import isObject from './isObject'
import { Settings } from './state'
export function stringifyConfigValue(x: any): string { export function stringifyConfigValue(x: any): string {
if (isObject(x)) return `${Object.keys(x).length} values` if (isObject(x)) return `${Object.keys(x).length} values`
@ -21,37 +22,17 @@ export function stringifyConfigValue(x: any): string {
}) })
} }
export function stringifyCss( export function stringifyCss(className: string, obj: any, settings: Settings): string {
className: string,
obj: any,
{
tabSize = 2,
showPixelEquivalents = false,
rootFontSize = 16,
}: Partial<{
tabSize: number
showPixelEquivalents: boolean
rootFontSize: number
}> = {}
): string {
if (obj.__rule !== true && !Array.isArray(obj)) return null if (obj.__rule !== true && !Array.isArray(obj)) return null
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
const rules = obj const rules = obj.map((x) => stringifyCss(className, x, settings)).filter(Boolean)
.map((x) =>
stringifyCss(className, x, {
tabSize,
showPixelEquivalents,
rootFontSize,
})
)
.filter(Boolean)
if (rules.length === 0) return null if (rules.length === 0) return null
return rules.join('\n\n') return rules.join('\n\n')
} }
let css = `` let css = ``
const indent = ' '.repeat(tabSize) const indent = ' '.repeat(settings.editor.tabSize)
const context = dlv(obj, '__context', []) const context = dlv(obj, '__context', [])
const props = Object.keys(removeMeta(obj)) const props = Object.keys(removeMeta(obj))
@ -65,7 +46,9 @@ export function stringifyCss(
const decls = props.reduce((acc, curr, i) => { const decls = props.reduce((acc, curr, i) => {
const propStr = ensureArray(obj[curr]) const propStr = ensureArray(obj[curr])
.map((val) => { .map((val) => {
const px = showPixelEquivalents ? remToPx(val, rootFontSize) : undefined const px = settings.tailwindCSS.showPixelEquivalents
? remToPx(val, settings.tailwindCSS.rootFontSize)
: undefined
return `${indentStr + indent}${curr}: ${val}${px ? `/* ${px} */` : ''};` return `${indentStr + indent}${curr}: ${val}${px ? `/* ${px} */` : ''};`
}) })
.join('\n') .join('\n')

View File

@ -70,7 +70,7 @@ This setting allows you to add additional language support. The key of each entr
### `tailwindCSS.files.exclude` ### `tailwindCSS.files.exclude`
Configure glob patterns to exclude from all IntelliSense features. Inherits all glob patterns from the `files.exclude` setting. **Default: ["\*\*/.git/\*\*", "\*\*/node_modules/\*\*", "\*\*/.hg/\*\*"]** Configure glob patterns to exclude from all IntelliSense features. Inherits all glob patterns from the `files.exclude` setting. **Default: ["\*\*/.git/\*\*", "\*\*/node_modules/\*\*", "\*\*/.hg/\*\*", "\*\*/.svn/\*\*"]**
### `tailwindCSS.emmetCompletions` ### `tailwindCSS.emmetCompletions`

View File

@ -150,7 +150,8 @@
"default": [ "default": [
"**/.git/**", "**/.git/**",
"**/node_modules/**", "**/node_modules/**",
"**/.hg/**" "**/.hg/**",
"**/.svn/**"
], ],
"markdownDescription": "Configure glob patterns to exclude from all IntelliSense features. Inherits all glob patterns from the `#files.exclude#` setting." "markdownDescription": "Configure glob patterns to exclude from all IntelliSense features. Inherits all glob patterns from the `#files.exclude#` setting."
}, },