Skip to content

Commit 3f45f85

Browse files
committed
Refactor document hover provider
1 parent 5e18fe7 commit 3f45f85

File tree

2 files changed

+31
-60
lines changed

2 files changed

+31
-60
lines changed

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

+30-59
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,36 @@ import type { Hover, MarkupContent, Position, Range } from 'vscode-languageserve
33
import { stringifyCss, stringifyConfigValue } from './util/stringify'
44
import dlv from 'dlv'
55
import { isCssContext } from './util/css'
6-
import {
7-
findAll,
8-
findClassNameAtPosition,
9-
findHelperFunctionsInRange,
10-
indexToPosition,
11-
} from './util/find'
6+
import { findAll, indexToPosition } from './util/find'
127
import { validateApply } from './util/validateApply'
138
import { getClassNameParts } from './util/getClassNameAtPosition'
149
import * as jit from './util/jit'
1510
import { validateConfigPath } from './diagnostics/getInvalidConfigPathDiagnostics'
1611
import { isWithinRange } from './util/isWithinRange'
17-
import type { TextDocument } from 'vscode-languageserver-textdocument'
1812
import { addPixelEquivalentsToValue } from './util/pixelEquivalents'
1913
import { getTextWithoutComments } from './util/doc'
2014
import braces from 'braces'
2115
import { absoluteRange } from './util/absoluteRange'
2216
import { segment } from './util/segment'
17+
import type { Document } from './documents/document'
2318

24-
export async function doHover(
25-
state: State,
26-
document: TextDocument,
27-
position: Position,
28-
): Promise<Hover> {
19+
export async function doHover(doc: Document, position: Position): Promise<Hover> {
2920
return (
30-
(await provideClassNameHover(state, document, position)) ||
31-
(await provideThemeDirectiveHover(state, document, position)) ||
32-
(await provideCssHelperHover(state, document, position)) ||
33-
(await provideSourceGlobHover(state, document, position))
21+
(await provideClassNameHover(doc, position)) ||
22+
(await provideThemeDirectiveHover(doc, position)) ||
23+
(await provideCssHelperHover(doc, position)) ||
24+
(await provideSourceGlobHover(doc, position))
3425
)
3526
}
3627

37-
async function provideCssHelperHover(
38-
state: State,
39-
document: TextDocument,
40-
position: Position,
41-
): Promise<Hover> {
42-
if (!isCssContext(state, document, position)) {
43-
return null
44-
}
45-
46-
const settings = await state.editor.getConfiguration(document.uri)
47-
48-
let helperFns = findHelperFunctionsInRange(document, {
49-
start: { line: position.line, character: 0 },
50-
end: { line: position.line + 1, character: 0 },
51-
})
52-
53-
for (let helperFn of helperFns) {
28+
async function provideCssHelperHover(doc: Document, position: Position): Promise<Hover> {
29+
for (let helperFn of doc.helperFnsAt(position)) {
5430
if (!isWithinRange(position, helperFn.ranges.path)) continue
5531

56-
if (helperFn.helper === 'var' && !state.v4) continue
32+
if (helperFn.helper === 'var' && !doc.state.v4) continue
5733

5834
let validated = validateConfigPath(
59-
state,
35+
doc.state,
6036
helperFn.path,
6137
helperFn.helper === 'theme' ? ['theme'] : [],
6238
)
@@ -65,13 +41,13 @@ async function provideCssHelperHover(
6541
let value = validated.isValid ? stringifyConfigValue(validated.value) : null
6642
if (value === null) return null
6743

68-
if (settings.tailwindCSS.showPixelEquivalents) {
69-
value = addPixelEquivalentsToValue(value, settings.tailwindCSS.rootFontSize)
44+
if (doc.settings.tailwindCSS.showPixelEquivalents) {
45+
value = addPixelEquivalentsToValue(value, doc.settings.tailwindCSS.rootFontSize)
7046
}
7147

7248
let lines = ['```plaintext', value, '```']
7349

74-
if (state.v4 && helperFn.path.startsWith('--')) {
50+
if (doc.state.v4 && helperFn.path.startsWith('--')) {
7551
lines = [
7652
//
7753
'```css',
@@ -91,14 +67,11 @@ async function provideCssHelperHover(
9167
return null
9268
}
9369

94-
async function provideClassNameHover(
95-
state: State,
96-
document: TextDocument,
97-
position: Position,
98-
): Promise<Hover> {
99-
let className = await findClassNameAtPosition(state, document, position)
100-
if (className === null) return null
70+
async function provideClassNameHover(doc: Document, position: Position): Promise<Hover> {
71+
let className = Array.from(doc.classNamesAt(position))[0]
72+
if (!className) return null
10173

74+
let state = doc.state
10275
if (state.v4) {
10376
let root = state.designSystem.compile([className.className])[0]
10477

@@ -109,7 +82,7 @@ async function provideClassNameHover(
10982
return {
11083
contents: {
11184
language: 'css',
112-
value: await jit.stringifyRoot(state, root, document.uri),
85+
value: await jit.stringifyRoot(state, root, doc.uri),
11386
},
11487
range: className.range,
11588
}
@@ -125,7 +98,7 @@ async function provideClassNameHover(
12598
return {
12699
contents: {
127100
language: 'css',
128-
value: await jit.stringifyRoot(state, root, document.uri),
101+
value: await jit.stringifyRoot(state, root, doc.uri),
129102
},
130103
range: className.range,
131104
}
@@ -134,14 +107,14 @@ async function provideClassNameHover(
134107
const parts = getClassNameParts(state, className.className)
135108
if (!parts) return null
136109

137-
if (isCssContext(state, document, position)) {
110+
if (isCssContext(state, doc.storage, position)) {
138111
let validated = validateApply(state, parts)
139112
if (validated === null || validated.isApplyable === false) {
140113
return null
141114
}
142115
}
143116

144-
const settings = await state.editor.getConfiguration(document.uri)
117+
const settings = await state.editor.getConfiguration(doc.uri)
145118

146119
const css = stringifyCss(
147120
className.className,
@@ -167,11 +140,10 @@ function markdown(lines: string[]): MarkupContent {
167140
}
168141
}
169142

170-
async function provideSourceGlobHover(
171-
state: State,
172-
document: TextDocument,
173-
position: Position,
174-
): Promise<Hover> {
143+
async function provideSourceGlobHover(doc: Document, position: Position): Promise<Hover> {
144+
let state = doc.state
145+
let document = doc.storage
146+
175147
if (!isCssContext(state, document, position)) {
176148
return null
177149
}
@@ -230,11 +202,10 @@ async function provideSourceGlobHover(
230202
const PATTERN_AT_THEME = /@(?<directive>theme)\s+(?<parts>[^{]+)\s*\{/dg
231203
const PATTERN_IMPORT_THEME = /@(?<directive>import)\s*[^;]+?theme\((?<parts>[^)]+)\)/dg
232204

233-
async function provideThemeDirectiveHover(
234-
state: State,
235-
document: TextDocument,
236-
position: Position,
237-
): Promise<Hover> {
205+
async function provideThemeDirectiveHover(doc: Document, position: Position): Promise<Hover> {
206+
let state = doc.state
207+
let document = doc.storage
208+
238209
if (!state.v4) return null
239210

240211
let range = {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async function createLanguageDocument(
125125
async hover(position: Position) {
126126
if (!state.enabled || !settings.tailwindCSS.hovers) return null
127127

128-
return doHover(state, doc.storage, position)
128+
return doHover(doc, position)
129129
},
130130

131131
async documentLinks() {

0 commit comments

Comments
 (0)