replace `experimental.showPixelValues` setting with `showPixelEquivalents` and enable by default (#200)

master
Brad Cornes 2021-02-05 14:49:06 +00:00
parent 6304d223ae
commit 14f0fe9f3f
7 changed files with 50 additions and 18 deletions

View File

@ -163,8 +163,15 @@
"type": "array", "type": "array",
"scope": "language-overridable" "scope": "language-overridable"
}, },
"tailwindCSS.experimental.showPixelValues": { "tailwindCSS.showPixelEquivalents": {
"type": "boolean" "type": "boolean",
"default": true,
"markdownDescription": "Show `px` equivalents for `rem` CSS values."
},
"tailwindCSS.rootFontSize": {
"type": "number",
"default": 16,
"markdownDescription": "Root font size in pixels. Used to convert `rem` CSS values to their `px` equivalents. See `#tailwindCSS.showPixelEquivalents#`."
} }
} }
} }

View File

@ -48,8 +48,9 @@ const defaultSettings: Settings = {
includeLanguages: {}, includeLanguages: {},
experimental: { experimental: {
classRegex: [], classRegex: [],
showPixelValues: false,
}, },
showPixelEquivalents: true,
rootFontSize: 16,
validate: true, validate: true,
lint: { lint: {
cssConflict: 'warning', cssConflict: 'warning',

View File

@ -900,8 +900,9 @@ export async function resolveCompletionItem(
if (!item.documentation) { if (!item.documentation) {
const settings = await getDocumentSettings(state) const settings = await getDocumentSettings(state)
const css = stringifyCss(item.data.join(':'), className, { const css = stringifyCss(item.data.join(':'), className, {
tabSize: dlv(settings, 'tabSize'), tabSize: dlv(settings, 'tabSize', 2),
showPixelValues: dlv(settings, 'experimental.showPixelValues'), showPixelEquivalents: dlv(settings, 'showPixelEquivalents', true),
rootFontSize: dlv(settings, 'rootFontSize', 16),
}) })
if (css) { if (css) {
item.documentation = { item.documentation = {
@ -932,7 +933,10 @@ function isContextItem(state: State, keys: string[]): boolean {
function stringifyDecls( function stringifyDecls(
obj: any, obj: any,
{ showPixelValues = false }: Partial<{ showPixelValues: boolean }> = {} {
showPixelEquivalents = false,
rootFontSize = 16,
}: Partial<{ showPixelEquivalents: boolean; rootFontSize: number }> = {}
): string { ): 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('--'))
@ -945,7 +949,9 @@ function stringifyDecls(
.map((prop) => .map((prop) =>
ensureArray(obj[prop]) ensureArray(obj[prop])
.map((value) => { .map((value) => {
const px = showPixelValues ? remToPx(value) : undefined const px = showPixelEquivalents
? remToPx(value, rootFontSize)
: undefined
return `${prop}: ${value}${px ? ` /*${px}*/` : ''};` return `${prop}: ${value}${px ? ` /*${px}*/` : ''};`
}) })
.join(' ') .join(' ')
@ -960,7 +966,8 @@ async function getCssDetail(state: State, className: any): Promise<string> {
if (className.__rule === true) { if (className.__rule === true) {
const settings = await getDocumentSettings(state) const settings = await getDocumentSettings(state)
return stringifyDecls(removeMeta(className), { return stringifyDecls(removeMeta(className), {
showPixelValues: dlv(settings, 'experimental.showPixelValues', false), showPixelEquivalents: dlv(settings, 'showPixelEquivalents', true),
rootFontSize: dlv(settings, 'rootFontSize', 16),
}) })
} }
return null return null

View File

@ -96,8 +96,9 @@ async function provideClassNameHover(
className.className, className.className,
dlv(state.classNames.classNames, [...parts, '__info']), dlv(state.classNames.classNames, [...parts, '__info']),
{ {
tabSize: dlv(settings, 'tabSize'), tabSize: dlv(settings, 'tabSize', 2),
showPixelValues: dlv(settings, 'experimental.showPixelValues'), showPixelEquivalents: dlv(settings, 'showPixelEquivalents', true),
rootFontSize: dlv(settings, 'rootFontSize', 16),
} }
) )

View File

@ -2,7 +2,11 @@ export function remToPx(
value: string, value: string,
rootSize: number = 16 rootSize: number = 16
): string | undefined { ): string | undefined {
return /^-?[0-9.]+rem$/.test(value) if (/^-?[0-9.]+rem$/.test(value)) {
? `${parseFloat(value.substr(0, value.length - 3)) * rootSize}px` let number = parseFloat(value.substr(0, value.length - 3))
: undefined if (!isNaN(number)) {
return `${number * rootSize}px`
}
}
return undefined
} }

View File

@ -33,6 +33,8 @@ export type Settings = {
emmetCompletions: boolean emmetCompletions: boolean
includeLanguages: Record<string, string> includeLanguages: Record<string, string>
validate: boolean validate: boolean
showPixelEquivalents: boolean
rootFontSize: number
lint: { lint: {
cssConflict: DiagnosticSeveritySetting cssConflict: DiagnosticSeveritySetting
invalidApply: DiagnosticSeveritySetting invalidApply: DiagnosticSeveritySetting
@ -43,7 +45,6 @@ export type Settings = {
} }
experimental: { experimental: {
classRegex: string[] classRegex: string[]
showPixelValues: boolean
} }
} }

View File

@ -21,14 +21,25 @@ export function stringifyCss(
obj: any, obj: any,
{ {
tabSize = 2, tabSize = 2,
showPixelValues = false, showPixelEquivalents = false,
}: Partial<{ tabSize: number; showPixelValues: boolean }> = {} rootFontSize = 16,
}: Partial<{
tabSize: number
showPixelEquivalents: boolean
rootFontSize: number
}> = {}
): string { ): 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, { tabSize, showPixelValues })) .map((x) =>
stringifyCss(className, x, {
tabSize,
showPixelEquivalents,
rootFontSize,
})
)
.filter(Boolean) .filter(Boolean)
if (rules.length === 0) return null if (rules.length === 0) return null
return rules.join('\n\n') return rules.join('\n\n')
@ -49,7 +60,7 @@ 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 = showPixelValues ? remToPx(val) : undefined const px = showPixelEquivalents ? remToPx(val, rootFontSize) : undefined
return `${indentStr + indent}${curr}: ${val}${px ? ` /*${px}*/` : ''};` return `${indentStr + indent}${curr}: ${val}${px ? ` /*${px}*/` : ''};`
}) })
.join('\n') .join('\n')