add experimental showPixelValues setting

master
Brad Cornes 2020-12-01 19:05:58 +00:00
parent 340336ed99
commit 615fd7f630
7 changed files with 50 additions and 12 deletions

View File

@ -162,6 +162,9 @@
"tailwindCSS.experimental.classRegex": { "tailwindCSS.experimental.classRegex": {
"type": "array", "type": "array",
"scope": "language-overridable" "scope": "language-overridable"
},
"tailwindCSS.experimental.showPixelValues": {
"type": "boolean"
} }
} }
} }

View File

@ -48,6 +48,7 @@ const defaultSettings: Settings = {
includeLanguages: {}, includeLanguages: {},
experimental: { experimental: {
classRegex: [], classRegex: [],
showPixelValues: false,
}, },
validate: true, validate: true,
lint: { lint: {

View File

@ -32,6 +32,7 @@ import {
import { validateApply } from './util/validateApply' import { validateApply } from './util/validateApply'
import { flagEnabled } from './util/flagEnabled' import { flagEnabled } from './util/flagEnabled'
import MultiRegexp from 'multi-regexp2' import MultiRegexp from 'multi-regexp2'
import { remToPx } from './util/remToPx'
export function completionsFromClassList( export function completionsFromClassList(
state: State, state: State,
@ -876,10 +877,13 @@ export async function resolveCompletionItem(
item.data[item.data.length - 1] item.data[item.data.length - 1]
].join(', ') ].join(', ')
} else { } else {
item.detail = getCssDetail(state, className) item.detail = await getCssDetail(state, className)
if (!item.documentation) { if (!item.documentation) {
const { tabSize } = await getDocumentSettings(state) const settings = await getDocumentSettings(state)
const css = stringifyCss(item.data.join(':'), className, tabSize) const css = stringifyCss(item.data.join(':'), className, {
tabSize: dlv(settings, 'tabSize'),
showPixelValues: dlv(settings, 'experimental.showPixelValues'),
})
if (css) { if (css) {
item.documentation = { item.documentation = {
kind: 'markdown' as typeof MarkupKind.Markdown, kind: 'markdown' as typeof MarkupKind.Markdown,
@ -907,7 +911,10 @@ function isContextItem(state: State, keys: string[]): boolean {
return isObject(item.__info) && !item.__info.__rule return isObject(item.__info) && !item.__info.__rule
} }
function stringifyDecls(obj: any): string { function stringifyDecls(
obj: any,
{ showPixelValues = false }: Partial<{ showPixelValues: boolean }> = {}
): 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('--'))
@ -918,18 +925,24 @@ function stringifyDecls(obj: any): string {
return props return props
.map((prop) => .map((prop) =>
ensureArray(obj[prop]) ensureArray(obj[prop])
.map((value) => `${prop}: ${value};`) .map((value) => {
const px = showPixelValues ? remToPx(value) : undefined
return `${prop}: ${value}${px ? ` /*${px}*/` : ''};`
})
.join(' ') .join(' ')
) )
.join(' ') .join(' ')
} }
function getCssDetail(state: State, className: any): string { async function getCssDetail(state: State, className: any): Promise<string> {
if (Array.isArray(className)) { if (Array.isArray(className)) {
return `${className.length} rules` return `${className.length} rules`
} }
if (className.__rule === true) { if (className.__rule === true) {
return stringifyDecls(removeMeta(className)) const settings = await getDocumentSettings(state)
return stringifyDecls(removeMeta(className), {
showPixelValues: dlv(settings, 'experimental.showPixelValues', false),
})
} }
return null return null
} }

View File

@ -90,12 +90,15 @@ async function provideClassNameHover(
} }
} }
const { tabSize } = await getDocumentSettings(state, document) const settings = await getDocumentSettings(state, document)
const css = stringifyCss( const css = stringifyCss(
className.className, className.className,
dlv(state.classNames.classNames, [...parts, '__info']), dlv(state.classNames.classNames, [...parts, '__info']),
tabSize {
tabSize: dlv(settings, 'tabSize'),
showPixelValues: dlv(settings, 'experimental.showPixelValues'),
}
) )
if (!css) return null if (!css) return null

View File

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

View File

@ -43,6 +43,7 @@ export type Settings = {
} }
experimental: { experimental: {
classRegex: string[] classRegex: string[]
showPixelValues: boolean
} }
} }

View File

@ -2,6 +2,7 @@ import removeMeta from './removeMeta'
const dlv = require('dlv') const dlv = require('dlv')
import escapeClassName from 'css.escape' import escapeClassName from 'css.escape'
import { ensureArray } from './array' import { ensureArray } from './array'
import { remToPx } from './remToPx'
export function stringifyConfigValue(x: any): string { export function stringifyConfigValue(x: any): string {
if (typeof x === 'string') return x if (typeof x === 'string') return x
@ -18,12 +19,17 @@ export function stringifyConfigValue(x: any): string {
export function stringifyCss( export function stringifyCss(
className: string, className: string,
obj: any, obj: any,
tabSize: number = 2 {
tabSize = 2,
showPixelValues = false,
}: Partial<{ tabSize: number; showPixelValues: boolean }> = {}
): 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.map((x) => stringifyCss(className, x)).filter(Boolean) const rules = obj
.map((x) => stringifyCss(className, x, { tabSize, showPixelValues }))
.filter(Boolean)
if (rules.length === 0) return null if (rules.length === 0) return null
return rules.join('\n\n') return rules.join('\n\n')
} }
@ -42,7 +48,10 @@ export function stringifyCss(
const indentStr = indent.repeat(context.length) const indentStr = indent.repeat(context.length)
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) => `${indentStr + indent}${curr}: ${val};`) .map((val) => {
const px = showPixelValues ? remToPx(val) : undefined
return `${indentStr + indent}${curr}: ${val}${px ? ` /*${px}*/` : ''};`
})
.join('\n') .join('\n')
return `${acc}${i === 0 ? '' : '\n'}${propStr}` return `${acc}${i === 0 ? '' : '\n'}${propStr}`
}, '') }, '')