Skip to content

Commit 615fd7f

Browse files
committed
add experimental showPixelValues setting
1 parent 340336e commit 615fd7f

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

packages/tailwindcss-intellisense/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@
162162
"tailwindCSS.experimental.classRegex": {
163163
"type": "array",
164164
"scope": "language-overridable"
165+
},
166+
"tailwindCSS.experimental.showPixelValues": {
167+
"type": "boolean"
165168
}
166169
}
167170
}

packages/tailwindcss-intellisense/src/lsp/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const defaultSettings: Settings = {
4848
includeLanguages: {},
4949
experimental: {
5050
classRegex: [],
51+
showPixelValues: false,
5152
},
5253
validate: true,
5354
lint: {

packages/tailwindcss-language-service/src/completionProvider.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { validateApply } from './util/validateApply'
3333
import { flagEnabled } from './util/flagEnabled'
3434
import MultiRegexp from 'multi-regexp2'
35+
import { remToPx } from './util/remToPx'
3536

3637
export function completionsFromClassList(
3738
state: State,
@@ -876,10 +877,13 @@ export async function resolveCompletionItem(
876877
item.data[item.data.length - 1]
877878
].join(', ')
878879
} else {
879-
item.detail = getCssDetail(state, className)
880+
item.detail = await getCssDetail(state, className)
880881
if (!item.documentation) {
881-
const { tabSize } = await getDocumentSettings(state)
882-
const css = stringifyCss(item.data.join(':'), className, tabSize)
882+
const settings = await getDocumentSettings(state)
883+
const css = stringifyCss(item.data.join(':'), className, {
884+
tabSize: dlv(settings, 'tabSize'),
885+
showPixelValues: dlv(settings, 'experimental.showPixelValues'),
886+
})
883887
if (css) {
884888
item.documentation = {
885889
kind: 'markdown' as typeof MarkupKind.Markdown,
@@ -907,7 +911,10 @@ function isContextItem(state: State, keys: string[]): boolean {
907911
return isObject(item.__info) && !item.__info.__rule
908912
}
909913

910-
function stringifyDecls(obj: any): string {
914+
function stringifyDecls(
915+
obj: any,
916+
{ showPixelValues = false }: Partial<{ showPixelValues: boolean }> = {}
917+
): string {
911918
let props = Object.keys(obj)
912919
let nonCustomProps = props.filter((prop) => !prop.startsWith('--'))
913920

@@ -918,18 +925,24 @@ function stringifyDecls(obj: any): string {
918925
return props
919926
.map((prop) =>
920927
ensureArray(obj[prop])
921-
.map((value) => `${prop}: ${value};`)
928+
.map((value) => {
929+
const px = showPixelValues ? remToPx(value) : undefined
930+
return `${prop}: ${value}${px ? ` /*${px}*/` : ''};`
931+
})
922932
.join(' ')
923933
)
924934
.join(' ')
925935
}
926936

927-
function getCssDetail(state: State, className: any): string {
937+
async function getCssDetail(state: State, className: any): Promise<string> {
928938
if (Array.isArray(className)) {
929939
return `${className.length} rules`
930940
}
931941
if (className.__rule === true) {
932-
return stringifyDecls(removeMeta(className))
942+
const settings = await getDocumentSettings(state)
943+
return stringifyDecls(removeMeta(className), {
944+
showPixelValues: dlv(settings, 'experimental.showPixelValues', false),
945+
})
933946
}
934947
return null
935948
}

packages/tailwindcss-language-service/src/hoverProvider.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,15 @@ async function provideClassNameHover(
9090
}
9191
}
9292

93-
const { tabSize } = await getDocumentSettings(state, document)
93+
const settings = await getDocumentSettings(state, document)
9494

9595
const css = stringifyCss(
9696
className.className,
9797
dlv(state.classNames.classNames, [...parts, '__info']),
98-
tabSize
98+
{
99+
tabSize: dlv(settings, 'tabSize'),
100+
showPixelValues: dlv(settings, 'experimental.showPixelValues'),
101+
}
99102
)
100103

101104
if (!css) return null
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function remToPx(
2+
value: string,
3+
rootSize: number = 16
4+
): string | undefined {
5+
return /^-?[0-9.]+rem$/.test(value)
6+
? `${parseFloat(value.substr(0, value.length - 3)) * rootSize}px`
7+
: undefined
8+
}

packages/tailwindcss-language-service/src/util/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type Settings = {
4343
}
4444
experimental: {
4545
classRegex: string[]
46+
showPixelValues: boolean
4647
}
4748
}
4849

packages/tailwindcss-language-service/src/util/stringify.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import removeMeta from './removeMeta'
22
const dlv = require('dlv')
33
import escapeClassName from 'css.escape'
44
import { ensureArray } from './array'
5+
import { remToPx } from './remToPx'
56

67
export function stringifyConfigValue(x: any): string {
78
if (typeof x === 'string') return x
@@ -18,12 +19,17 @@ export function stringifyConfigValue(x: any): string {
1819
export function stringifyCss(
1920
className: string,
2021
obj: any,
21-
tabSize: number = 2
22+
{
23+
tabSize = 2,
24+
showPixelValues = false,
25+
}: Partial<{ tabSize: number; showPixelValues: boolean }> = {}
2226
): string {
2327
if (obj.__rule !== true && !Array.isArray(obj)) return null
2428

2529
if (Array.isArray(obj)) {
26-
const rules = obj.map((x) => stringifyCss(className, x)).filter(Boolean)
30+
const rules = obj
31+
.map((x) => stringifyCss(className, x, { tabSize, showPixelValues }))
32+
.filter(Boolean)
2733
if (rules.length === 0) return null
2834
return rules.join('\n\n')
2935
}
@@ -42,7 +48,10 @@ export function stringifyCss(
4248
const indentStr = indent.repeat(context.length)
4349
const decls = props.reduce((acc, curr, i) => {
4450
const propStr = ensureArray(obj[curr])
45-
.map((val) => `${indentStr + indent}${curr}: ${val};`)
51+
.map((val) => {
52+
const px = showPixelValues ? remToPx(val) : undefined
53+
return `${indentStr + indent}${curr}: ${val}${px ? ` /*${px}*/` : ''};`
54+
})
4655
.join('\n')
4756
return `${acc}${i === 0 ? '' : '\n'}${propStr}`
4857
}, '')

0 commit comments

Comments
 (0)