|
| 1 | +import type { UnocssAutocomplete } from '@unocss/autocomplete' |
| 2 | +import type { SuggestResult, UnocssPluginContext, UserConfig } from '@unocss/core' |
| 3 | +import type { CompletionItem, CompletionParams, Connection, TextDocuments } from 'vscode-languageserver' |
| 4 | +import type { TextDocument } from 'vscode-languageserver-textdocument' |
| 5 | +import type { ContextManager } from '../core/context' |
| 6 | +import type { ServerSettings } from '../types' |
| 7 | +import { fileURLToPath } from 'node:url' |
| 8 | +import { isCssId } from '#integration/utils' |
| 9 | +import { createAutocomplete } from '@unocss/autocomplete' |
| 10 | +import { CompletionItemKind } from 'vscode-languageserver' |
| 11 | +import { getColorString } from '../utils/color' |
| 12 | +import { getCSS, getPrettiedCSS, getPrettiedMarkdown } from '../utils/css' |
| 13 | +import { isVueWithPug, shouldProvideAutocomplete } from '../utils/position' |
| 14 | + |
| 15 | +const autoCompletes = new Map<UnocssPluginContext, UnocssAutocomplete>() |
| 16 | + |
| 17 | +function getAutocomplete(ctx: UnocssPluginContext, matchType: 'prefix' | 'fuzzy') { |
| 18 | + const cached = autoCompletes.get(ctx) |
| 19 | + if (cached) |
| 20 | + return cached |
| 21 | + |
| 22 | + const autocomplete = createAutocomplete(ctx.uno, { |
| 23 | + matchType, |
| 24 | + throwErrors: false, |
| 25 | + }) |
| 26 | + |
| 27 | + autoCompletes.set(ctx, autocomplete) |
| 28 | + return autocomplete |
| 29 | +} |
| 30 | + |
| 31 | +export function resetAutoCompleteCache(ctx?: UnocssPluginContext<UserConfig<any>>) { |
| 32 | + if (ctx) |
| 33 | + autoCompletes.delete(ctx) |
| 34 | + else |
| 35 | + autoCompletes.clear() |
| 36 | +} |
| 37 | + |
| 38 | +async function getSuggestionResult({ |
| 39 | + ctx, |
| 40 | + code, |
| 41 | + id, |
| 42 | + offset, |
| 43 | + matchType, |
| 44 | +}: { |
| 45 | + ctx: UnocssPluginContext |
| 46 | + code: string |
| 47 | + id: string |
| 48 | + offset: number |
| 49 | + matchType: 'prefix' | 'fuzzy' |
| 50 | +}) { |
| 51 | + const isPug = isVueWithPug(code, id) |
| 52 | + if (!ctx.filter(code, id) && !isCssId(id) && !isPug) |
| 53 | + return null |
| 54 | + |
| 55 | + const autoComplete = getAutocomplete(ctx, matchType) |
| 56 | + |
| 57 | + let result: SuggestResult | undefined |
| 58 | + |
| 59 | + if (isPug) { |
| 60 | + const textBeforeCursor = code.substring(0, offset) |
| 61 | + const dotMatch = textBeforeCursor.match(/\.[-\w]*$/) |
| 62 | + |
| 63 | + if (dotMatch) { |
| 64 | + const matched = dotMatch[0].substring(1) |
| 65 | + const suggestions = await autoComplete.suggest(matched || '') |
| 66 | + |
| 67 | + if (suggestions.length) { |
| 68 | + result = { |
| 69 | + suggestions: suggestions.map(v => [v, v] as [string, string]), |
| 70 | + resolveReplacement: (suggestion: string) => ({ |
| 71 | + start: offset - (matched?.length || 0), |
| 72 | + end: offset, |
| 73 | + replacement: suggestion, |
| 74 | + }), |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + else { |
| 79 | + result = await autoComplete.suggestInFile(code, offset) |
| 80 | + } |
| 81 | + } |
| 82 | + else { |
| 83 | + result = await autoComplete.suggestInFile(code, offset) |
| 84 | + } |
| 85 | + |
| 86 | + return result |
| 87 | +} |
| 88 | + |
| 89 | +export function registerCompletion( |
| 90 | + connection: Connection, |
| 91 | + documents: TextDocuments<TextDocument>, |
| 92 | + getContextManager: () => ContextManager | undefined, |
| 93 | + getSettings: () => ServerSettings, |
| 94 | +) { |
| 95 | + connection.onCompletion(async (params: CompletionParams) => { |
| 96 | + const settings = getSettings() |
| 97 | + const contextManager = getContextManager() |
| 98 | + if (!contextManager) |
| 99 | + return null |
| 100 | + |
| 101 | + const doc = documents.get(params.textDocument.uri) |
| 102 | + if (!doc) |
| 103 | + return null |
| 104 | + |
| 105 | + const id = fileURLToPath(params.textDocument.uri) |
| 106 | + if (!contextManager.isTarget(id)) |
| 107 | + return null |
| 108 | + |
| 109 | + const code = doc.getText() |
| 110 | + if (!code) |
| 111 | + return null |
| 112 | + |
| 113 | + const ctx = await contextManager.resolveClosestContext(code, id) |
| 114 | + if (!ctx) |
| 115 | + return null |
| 116 | + |
| 117 | + const offset = doc.offsetAt(params.position) |
| 118 | + |
| 119 | + if (settings.autocompleteStrict && !shouldProvideAutocomplete(code, id, offset)) |
| 120 | + return null |
| 121 | + |
| 122 | + try { |
| 123 | + const result = await getSuggestionResult({ |
| 124 | + ctx, |
| 125 | + code, |
| 126 | + id, |
| 127 | + offset, |
| 128 | + matchType: settings.autocompleteMatchType, |
| 129 | + }) |
| 130 | + |
| 131 | + if (!result || !result.suggestions.length) |
| 132 | + return null |
| 133 | + |
| 134 | + const isAttributify = ctx.uno.config.presets.some(p => p.name === '@unocss/preset-attributify') |
| 135 | + const suggestions = result.suggestions.slice(0, settings.autocompleteMaxItems) |
| 136 | + |
| 137 | + const items: CompletionItem[] = [] |
| 138 | + for (let i = 0; i < suggestions.length; i++) { |
| 139 | + const [value, label] = suggestions[i] |
| 140 | + const css = await getCSS(ctx.uno, isAttributify ? [value, `[${value}=""]`] : value) |
| 141 | + const colorString = getColorString(css) |
| 142 | + const resolved = result.resolveReplacement(value) |
| 143 | + |
| 144 | + const item: CompletionItem = { |
| 145 | + label, |
| 146 | + kind: colorString ? CompletionItemKind.Color : CompletionItemKind.EnumMember, |
| 147 | + sortText: colorString ? (/-\d$/.test(label) ? '1' : '2') : undefined, |
| 148 | + data: { |
| 149 | + value, |
| 150 | + uri: params.textDocument.uri, |
| 151 | + }, |
| 152 | + textEdit: { |
| 153 | + range: { |
| 154 | + start: doc.positionAt(resolved.start), |
| 155 | + end: doc.positionAt(resolved.end), |
| 156 | + }, |
| 157 | + newText: resolved.replacement, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + if (colorString) { |
| 162 | + item.documentation = colorString |
| 163 | + } |
| 164 | + |
| 165 | + items.push(item) |
| 166 | + } |
| 167 | + |
| 168 | + return { |
| 169 | + isIncomplete: true, |
| 170 | + items, |
| 171 | + } |
| 172 | + } |
| 173 | + catch { |
| 174 | + return null |
| 175 | + } |
| 176 | + }) |
| 177 | + |
| 178 | + connection.onCompletionResolve(async (item: CompletionItem) => { |
| 179 | + if (!item.data?.uri || !item.data?.value) |
| 180 | + return item |
| 181 | + |
| 182 | + const settings = getSettings() |
| 183 | + const contextManager = getContextManager() |
| 184 | + if (!contextManager) |
| 185 | + return item |
| 186 | + |
| 187 | + const id = fileURLToPath(item.data.uri) |
| 188 | + const doc = documents.get(item.data.uri) |
| 189 | + if (!doc) |
| 190 | + return item |
| 191 | + |
| 192 | + const code = doc.getText() |
| 193 | + const ctx = await contextManager.resolveClosestContext(code, id) |
| 194 | + if (!ctx) |
| 195 | + return item |
| 196 | + |
| 197 | + const remToPxRatio = settings.remToPxPreview ? settings.remToPxRatio : -1 |
| 198 | + |
| 199 | + if (item.kind === CompletionItemKind.Color) { |
| 200 | + item.detail = await (await getPrettiedCSS(ctx.uno, item.data.value, remToPxRatio)).prettified |
| 201 | + } |
| 202 | + else { |
| 203 | + item.documentation = { |
| 204 | + kind: 'markdown', |
| 205 | + value: await getPrettiedMarkdown(ctx.uno, item.data.value, remToPxRatio), |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + return item |
| 210 | + }) |
| 211 | +} |
0 commit comments