Skip to content

Commit 93027cf

Browse files
committed
Emit code lenses from the language service
1 parent a46bfe3 commit 93027cf

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

packages/tailwindcss-language-server/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function getDefaultSettings(): Settings {
1616
emmetCompletions: false,
1717
classAttributes: ['class', 'className', 'ngClass', 'class:list'],
1818
codeActions: true,
19+
codeLens: true,
1920
hovers: true,
2021
suggestions: true,
2122
validate: true,

packages/tailwindcss-language-server/src/projects.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import type {
1515
Disposable,
1616
DocumentLinkParams,
1717
DocumentLink,
18+
CodeLensParams,
19+
CodeLens,
1820
} from 'vscode-languageserver/node'
1921
import { FileChangeType } from 'vscode-languageserver/node'
2022
import type { TextDocument } from 'vscode-languageserver-textdocument'
@@ -35,6 +37,7 @@ import stackTrace from 'stack-trace'
3537
import extractClassNames from './lib/extractClassNames'
3638
import { klona } from 'klona/full'
3739
import { doHover } from '@tailwindcss/language-service/src/hoverProvider'
40+
import { getCodeLens } from '@tailwindcss/language-service/src/codeLensProvider'
3841
import { Resolver } from './resolver'
3942
import {
4043
doComplete,
@@ -110,6 +113,7 @@ export interface ProjectService {
110113
onColorPresentation(params: ColorPresentationParams): Promise<ColorPresentation[]>
111114
onCodeAction(params: CodeActionParams): Promise<CodeAction[]>
112115
onDocumentLinks(params: DocumentLinkParams): Promise<DocumentLink[]>
116+
onCodeLens(params: CodeLensParams): Promise<CodeLens[]>
113117
sortClassLists(classLists: string[]): string[]
114118

115119
dependencies(): Iterable<string>
@@ -1177,6 +1181,17 @@ export async function createProjectService(
11771181
return doHover(state, document, params.position)
11781182
}, null)
11791183
},
1184+
async onCodeLens(params: CodeLensParams): Promise<CodeLens[]> {
1185+
return withFallback(async () => {
1186+
if (!state.enabled) return null
1187+
let document = documentService.getDocument(params.textDocument.uri)
1188+
if (!document) return null
1189+
let settings = await state.editor.getConfiguration(document.uri)
1190+
if (!settings.tailwindCSS.codeLens) return null
1191+
if (await isExcluded(state, document)) return null
1192+
return getCodeLens(state, document)
1193+
}, null)
1194+
},
11801195
async onCompletion(params: CompletionParams): Promise<CompletionList> {
11811196
return withFallback(async () => {
11821197
if (!state.enabled) return null

packages/tailwindcss-language-server/src/tw.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import type {
1919
DocumentLink,
2020
InitializeResult,
2121
WorkspaceFolder,
22+
CodeLensParams,
23+
CodeLens,
2224
} from 'vscode-languageserver/node'
2325
import {
2426
CompletionRequest,
@@ -30,6 +32,7 @@ import {
3032
FileChangeType,
3133
DocumentLinkRequest,
3234
TextDocumentSyncKind,
35+
CodeLensRequest,
3336
} from 'vscode-languageserver/node'
3437
import { URI } from 'vscode-uri'
3538
import normalizePath from 'normalize-path'
@@ -757,6 +760,7 @@ export class TW {
757760
this.connection.onDocumentColor(this.onDocumentColor.bind(this))
758761
this.connection.onColorPresentation(this.onColorPresentation.bind(this))
759762
this.connection.onCodeAction(this.onCodeAction.bind(this))
763+
this.connection.onCodeLens(this.onCodeLens.bind(this))
760764
this.connection.onDocumentLinks(this.onDocumentLinks.bind(this))
761765
this.connection.onRequest(this.onRequest.bind(this))
762766
}
@@ -809,6 +813,7 @@ export class TW {
809813
capabilities.add(HoverRequest.type, { documentSelector: null })
810814
capabilities.add(DocumentColorRequest.type, { documentSelector: null })
811815
capabilities.add(CodeActionRequest.type, { documentSelector: null })
816+
capabilities.add(CodeLensRequest.type, { documentSelector: null })
812817
capabilities.add(DocumentLinkRequest.type, { documentSelector: null })
813818

814819
capabilities.add(CompletionRequest.type, {
@@ -931,6 +936,11 @@ export class TW {
931936
return this.getProject(params.textDocument)?.onCodeAction(params) ?? null
932937
}
933938

939+
async onCodeLens(params: CodeLensParams): Promise<CodeLens[]> {
940+
await this.init()
941+
return this.getProject(params.textDocument)?.onCodeLens(params) ?? null
942+
}
943+
934944
async onDocumentLinks(params: DocumentLinkParams): Promise<DocumentLink[]> {
935945
await this.init()
936946
return this.getProject(params.textDocument)?.onDocumentLinks(params) ?? null
@@ -961,6 +971,9 @@ export class TW {
961971
hoverProvider: true,
962972
colorProvider: true,
963973
codeActionProvider: true,
974+
codeLensProvider: {
975+
resolveProvider: false,
976+
},
964977
documentLinkProvider: {},
965978
completionProvider: {
966979
resolveProvider: true,

packages/tailwindcss-language-server/tests/utils/client.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Settings } from '@tailwindcss/language-service/src/util/state'
22
import {
33
ClientCapabilities,
4+
CodeLens,
5+
CodeLensRequest,
46
CompletionList,
57
CompletionParams,
68
Diagnostic,
@@ -94,6 +96,11 @@ export interface ClientDocument {
9496
*/
9597
reopen(): Promise<void>
9698

99+
/**
100+
* Code lenses in the document
101+
*/
102+
codeLenses(): Promise<CodeLens[] | null>
103+
97104
/**
98105
* The diagnostics for the current version of this document
99106
*/
@@ -677,6 +684,14 @@ export async function createClientWorkspace({
677684
return results
678685
}
679686

687+
async function codeLenses() {
688+
return await conn.sendRequest(CodeLensRequest.type, {
689+
textDocument: {
690+
uri: uri.toString(),
691+
},
692+
})
693+
}
694+
680695
return {
681696
uri,
682697
reopen,
@@ -687,6 +702,7 @@ export async function createClientWorkspace({
687702
symbols,
688703
completions,
689704
diagnostics,
705+
codeLenses,
690706
}
691707
}
692708

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { TextDocument } from 'vscode-languageserver-textdocument'
2+
import type { State } from './util/state'
3+
import type { CodeLens } from 'vscode-languageserver'
4+
5+
export async function getCodeLens(state: State, doc: TextDocument): Promise<CodeLens[]> {
6+
if (!state.enabled) return []
7+
8+
let groups: CodeLens[][] = await Promise.all([
9+
//
10+
])
11+
12+
return groups.flat()
13+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type TailwindCssSettings = {
4848
classAttributes: string[]
4949
suggestions: boolean
5050
hovers: boolean
51+
codeLens: boolean
5152
codeActions: boolean
5253
validate: boolean
5354
showPixelEquivalents: boolean

packages/vscode-tailwindcss/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@
202202
"markdownDescription": "Enable code actions.",
203203
"scope": "language-overridable"
204204
},
205+
"tailwindCSS.codeLens": {
206+
"type": "boolean",
207+
"default": true,
208+
"markdownDescription": "Enable code lens.",
209+
"scope": "language-overridable"
210+
},
205211
"tailwindCSS.colorDecorators": {
206212
"type": "boolean",
207213
"default": true,

0 commit comments

Comments
 (0)