Skip to content

Commit 23634c9

Browse files
committed
Add completion middleware to match built-in CSS provider
1 parent 7d55922 commit 23634c9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

packages/vscode-tailwindcss/src/extension.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import {
1919
RelativePattern,
2020
ConfigurationScope,
2121
WorkspaceConfiguration,
22+
CompletionItem,
23+
CompletionItemKind,
24+
CompletionList,
25+
ProviderResult,
2226
} from 'vscode'
2327
import {
2428
LanguageClient,
@@ -207,6 +211,45 @@ export async function activate(context: ExtensionContext) {
207211
{
208212
documentSelector: [{ language: 'tailwindcss' }],
209213
outputChannelName: 'Tailwind CSS Language Mode',
214+
middleware: {
215+
provideCompletionItem(document, position, context, token, next) {
216+
function updateRanges(item: CompletionItem) {
217+
const range = item.range
218+
if (
219+
range instanceof Range &&
220+
range.end.isAfter(position) &&
221+
range.start.isBeforeOrEqual(position)
222+
) {
223+
item.range = { inserting: new Range(range.start, position), replacing: range }
224+
}
225+
}
226+
function updateLabel(item: CompletionItem) {
227+
if (item.kind === CompletionItemKind.Color) {
228+
item.label = {
229+
label: item.label as string,
230+
description: item.documentation as string,
231+
}
232+
}
233+
}
234+
function updateProposals(
235+
r: CompletionItem[] | CompletionList | null | undefined
236+
): CompletionItem[] | CompletionList | null | undefined {
237+
if (r) {
238+
;(Array.isArray(r) ? r : r.items).forEach(updateRanges)
239+
;(Array.isArray(r) ? r : r.items).forEach(updateLabel)
240+
}
241+
return r
242+
}
243+
const isThenable = <T>(obj: ProviderResult<T>): obj is Thenable<T> =>
244+
obj && (<any>obj)['then']
245+
246+
const r = next(document, position, context, token)
247+
if (isThenable<CompletionItem[] | CompletionList | null | undefined>(r)) {
248+
return r.then(updateProposals)
249+
}
250+
return updateProposals(r)
251+
},
252+
},
210253
}
211254
)
212255
context.subscriptions.push(client.start())

0 commit comments

Comments
 (0)