Skip to content

Commit 7b8ee54

Browse files
author
Brad Cornes
committed
add initial css helper hover
1 parent 97c4a29 commit 7b8ee54

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

packages/tailwindcss-language-server/src/providers/hoverProvider.ts

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,83 @@ import {
44
getClassNameAtPosition,
55
getClassNameParts,
66
} from '../util/getClassNameAtPosition'
7-
import { stringifyCss } from '../util/stringify'
7+
import { stringifyCss, stringifyConfigValue } from '../util/stringify'
88
const dlv = require('dlv')
99
import escapeClassName from 'css.escape'
1010
import { isHtmlContext } from '../util/html'
11+
import { isCssContext } from '../util/css'
1112

1213
export function provideHover(
1314
state: State,
1415
params: TextDocumentPositionParams
1516
): Hover {
16-
let doc = state.editor.documents.get(params.textDocument.uri)
17+
return (
18+
provideClassNameHover(state, params) || provideCssHelperHover(state, params)
19+
)
20+
}
21+
22+
function provideCssHelperHover(
23+
state: State,
24+
{ textDocument, position }: TextDocumentPositionParams
25+
): Hover {
26+
let doc = state.editor.documents.get(textDocument.uri)
27+
28+
if (!isCssContext(doc, position)) return null
29+
30+
const line = doc.getText({
31+
start: { line: position.line, character: 0 },
32+
end: { line: position.line + 1, character: 0 },
33+
})
34+
35+
const match = line.match(
36+
/(?<helper>theme|config)\((?<quote>['"])(?<key>[^)]+)\k<quote>\)/
37+
)
38+
39+
if (match === null) return null
40+
41+
const startChar = match.index + 7
42+
const endChar = startChar + match.groups.key.length
1743

18-
if (isHtmlContext(doc, params.position)) {
19-
return provideClassNameHover(state, params)
44+
if (position.character < startChar || position.character >= endChar) {
45+
return null
2046
}
2147

22-
return null
48+
let key = match.groups.key
49+
.split(/(\[[^\]]+\]|\.)/)
50+
.filter(Boolean)
51+
.filter((x) => x !== '.')
52+
.map((x) => x.replace(/^\[([^\]]+)\]$/, '$1'))
53+
54+
if (key.length === 0) return null
55+
56+
if (match.groups.helper === 'theme') {
57+
key = ['theme', ...key]
58+
}
59+
60+
const value = stringifyConfigValue(dlv(state.config, key))
61+
62+
if (value === null) return null
63+
64+
return {
65+
contents: { kind: 'plaintext', value },
66+
range: {
67+
start: { line: position.line, character: startChar },
68+
end: {
69+
line: position.line,
70+
character: endChar,
71+
},
72+
},
73+
}
2374
}
2475

2576
function provideClassNameHover(
2677
state: State,
2778
{ textDocument, position }: TextDocumentPositionParams
2879
): Hover {
2980
let doc = state.editor.documents.get(textDocument.uri)
81+
82+
if (!isHtmlContext(doc, position)) return null
83+
3084
let hovered = getClassNameAtPosition(doc, position)
3185
if (!hovered) return null
3286

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function stringifyConfigValue(x: any): string {
99
.filter(Boolean)
1010
.join(', ')
1111
}
12-
return ''
12+
return null
1313
}
1414

1515
export function stringifyCss(

0 commit comments

Comments
 (0)