Skip to content

Commit 14f0fe9

Browse files
committed
replace experimental.showPixelValues setting with showPixelEquivalents and enable by default (#200)
1 parent 6304d22 commit 14f0fe9

File tree

7 files changed

+50
-18
lines changed

7 files changed

+50
-18
lines changed

packages/tailwindcss-intellisense/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,15 @@
163163
"type": "array",
164164
"scope": "language-overridable"
165165
},
166-
"tailwindCSS.experimental.showPixelValues": {
167-
"type": "boolean"
166+
"tailwindCSS.showPixelEquivalents": {
167+
"type": "boolean",
168+
"default": true,
169+
"markdownDescription": "Show `px` equivalents for `rem` CSS values."
170+
},
171+
"tailwindCSS.rootFontSize": {
172+
"type": "number",
173+
"default": 16,
174+
"markdownDescription": "Root font size in pixels. Used to convert `rem` CSS values to their `px` equivalents. See `#tailwindCSS.showPixelEquivalents#`."
168175
}
169176
}
170177
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ const defaultSettings: Settings = {
4848
includeLanguages: {},
4949
experimental: {
5050
classRegex: [],
51-
showPixelValues: false,
5251
},
52+
showPixelEquivalents: true,
53+
rootFontSize: 16,
5354
validate: true,
5455
lint: {
5556
cssConflict: 'warning',

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,9 @@ export async function resolveCompletionItem(
900900
if (!item.documentation) {
901901
const settings = await getDocumentSettings(state)
902902
const css = stringifyCss(item.data.join(':'), className, {
903-
tabSize: dlv(settings, 'tabSize'),
904-
showPixelValues: dlv(settings, 'experimental.showPixelValues'),
903+
tabSize: dlv(settings, 'tabSize', 2),
904+
showPixelEquivalents: dlv(settings, 'showPixelEquivalents', true),
905+
rootFontSize: dlv(settings, 'rootFontSize', 16),
905906
})
906907
if (css) {
907908
item.documentation = {
@@ -932,7 +933,10 @@ function isContextItem(state: State, keys: string[]): boolean {
932933

933934
function stringifyDecls(
934935
obj: any,
935-
{ showPixelValues = false }: Partial<{ showPixelValues: boolean }> = {}
936+
{
937+
showPixelEquivalents = false,
938+
rootFontSize = 16,
939+
}: Partial<{ showPixelEquivalents: boolean; rootFontSize: number }> = {}
936940
): string {
937941
let props = Object.keys(obj)
938942
let nonCustomProps = props.filter((prop) => !prop.startsWith('--'))
@@ -945,7 +949,9 @@ function stringifyDecls(
945949
.map((prop) =>
946950
ensureArray(obj[prop])
947951
.map((value) => {
948-
const px = showPixelValues ? remToPx(value) : undefined
952+
const px = showPixelEquivalents
953+
? remToPx(value, rootFontSize)
954+
: undefined
949955
return `${prop}: ${value}${px ? ` /*${px}*/` : ''};`
950956
})
951957
.join(' ')
@@ -960,7 +966,8 @@ async function getCssDetail(state: State, className: any): Promise<string> {
960966
if (className.__rule === true) {
961967
const settings = await getDocumentSettings(state)
962968
return stringifyDecls(removeMeta(className), {
963-
showPixelValues: dlv(settings, 'experimental.showPixelValues', false),
969+
showPixelEquivalents: dlv(settings, 'showPixelEquivalents', true),
970+
rootFontSize: dlv(settings, 'rootFontSize', 16),
964971
})
965972
}
966973
return null

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ async function provideClassNameHover(
9696
className.className,
9797
dlv(state.classNames.classNames, [...parts, '__info']),
9898
{
99-
tabSize: dlv(settings, 'tabSize'),
100-
showPixelValues: dlv(settings, 'experimental.showPixelValues'),
99+
tabSize: dlv(settings, 'tabSize', 2),
100+
showPixelEquivalents: dlv(settings, 'showPixelEquivalents', true),
101+
rootFontSize: dlv(settings, 'rootFontSize', 16),
101102
}
102103
)
103104

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export type Settings = {
3333
emmetCompletions: boolean
3434
includeLanguages: Record<string, string>
3535
validate: boolean
36+
showPixelEquivalents: boolean
37+
rootFontSize: number
3638
lint: {
3739
cssConflict: DiagnosticSeveritySetting
3840
invalidApply: DiagnosticSeveritySetting
@@ -43,7 +45,6 @@ export type Settings = {
4345
}
4446
experimental: {
4547
classRegex: string[]
46-
showPixelValues: boolean
4748
}
4849
}
4950

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,25 @@ export function stringifyCss(
2121
obj: any,
2222
{
2323
tabSize = 2,
24-
showPixelValues = false,
25-
}: Partial<{ tabSize: number; showPixelValues: boolean }> = {}
24+
showPixelEquivalents = false,
25+
rootFontSize = 16,
26+
}: Partial<{
27+
tabSize: number
28+
showPixelEquivalents: boolean
29+
rootFontSize: number
30+
}> = {}
2631
): string {
2732
if (obj.__rule !== true && !Array.isArray(obj)) return null
2833

2934
if (Array.isArray(obj)) {
3035
const rules = obj
31-
.map((x) => stringifyCss(className, x, { tabSize, showPixelValues }))
36+
.map((x) =>
37+
stringifyCss(className, x, {
38+
tabSize,
39+
showPixelEquivalents,
40+
rootFontSize,
41+
})
42+
)
3243
.filter(Boolean)
3344
if (rules.length === 0) return null
3445
return rules.join('\n\n')
@@ -49,7 +60,7 @@ export function stringifyCss(
4960
const decls = props.reduce((acc, curr, i) => {
5061
const propStr = ensureArray(obj[curr])
5162
.map((val) => {
52-
const px = showPixelValues ? remToPx(val) : undefined
63+
const px = showPixelEquivalents ? remToPx(val, rootFontSize) : undefined
5364
return `${indentStr + indent}${curr}: ${val}${px ? ` /*${px}*/` : ''};`
5465
})
5566
.join('\n')

0 commit comments

Comments
 (0)