Use `itemDefaults` to reduce size of completion lists (#706)

* Use completion list `itemDefaults`

* more defaults
master
Brad Cornes 2023-01-27 10:30:27 +00:00 committed by GitHub
parent 7235aeab48
commit 637f838725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 520 additions and 422 deletions

View File

@ -378,8 +378,21 @@ async function createProjectService(
const disposables: Array<Disposable | Promise<Disposable>> = [] const disposables: Array<Disposable | Promise<Disposable>> = []
let documentSelector = projectConfig.documentSelector let documentSelector = projectConfig.documentSelector
let itemDefaults =
params.capabilities.textDocument?.completion?.completionList?.itemDefaults ?? []
// VS Code _does_ support `itemDefaults.data` since at least 1.67.0 (this extension's min version)
// but it doesn't advertise it in its capabilities. So we manually add it here.
// See also: https://github.com/microsoft/vscode-languageserver-node/issues/1181
if (params.clientInfo?.name === 'Visual Studio Code' && !itemDefaults.includes('data')) {
itemDefaults.push('data')
}
let state: State = { let state: State = {
enabled: false, enabled: false,
completionItemData: {
_projectKey: projectKey,
},
editor: { editor: {
connection, connection,
folder, folder,
@ -390,6 +403,7 @@ async function createProjectService(
capabilities: { capabilities: {
configuration: true, configuration: true,
diagnosticRelatedInformation: true, diagnosticRelatedInformation: true,
itemDefaults,
}, },
documents: documentService.documents, documents: documentService.documents,
getConfiguration, getConfiguration,
@ -1114,21 +1128,13 @@ async function createProjectService(
let settings = await state.editor.getConfiguration(document.uri) let settings = await state.editor.getConfiguration(document.uri)
if (!settings.tailwindCSS.suggestions) return null if (!settings.tailwindCSS.suggestions) return null
if (await isExcluded(state, document)) return null if (await isExcluded(state, document)) return null
let result = await doComplete(state, document, params.position, params.context) return doComplete(state, document, params.position, params.context)
if (!result) return result
return {
isIncomplete: result.isIncomplete,
items: result.items.map((item) => ({
...item,
data: { projectKey, originalData: item.data },
})),
}
}, null) }, null)
}, },
onCompletionResolve(item: CompletionItem): Promise<CompletionItem> { onCompletionResolve(item: CompletionItem): Promise<CompletionItem> {
return withFallback(() => { return withFallback(() => {
if (!state.enabled) return null if (!state.enabled) return null
return resolveCompletionItem(state, { ...item, data: item.data?.originalData }) return resolveCompletionItem(state, item)
}, null) }, null)
}, },
async onCodeAction(params: CodeActionParams): Promise<CodeAction[]> { async onCodeAction(params: CodeActionParams): Promise<CodeAction[]> {
@ -2162,7 +2168,7 @@ class TW {
} }
async onCompletionResolve(item: CompletionItem): Promise<CompletionItem> { async onCompletionResolve(item: CompletionItem): Promise<CompletionItem> {
return this.projects.get(item.data.projectKey)?.onCompletionResolve(item) ?? null return this.projects.get(item.data?._projectKey)?.onCompletionResolve(item) ?? null
} }
onCodeAction(params: CodeActionParams): Promise<CodeAction[]> { onCodeAction(params: CodeActionParams): Promise<CodeAction[]> {

View File

@ -1,8 +1,4 @@
function pad(n: string): string { export function naturalExpand(value: number, total?: number): string {
return ('00000000' + n).substr(-8) let length = typeof total === 'number' ? total.toString().length : 8
} return ('0'.repeat(length) + value).slice(-length)
export function naturalExpand(value: number | string): string {
let str = typeof value === 'string' ? value : value.toString()
return str.replace(/\d+/g, pad)
} }

View File

@ -25,6 +25,7 @@ export type EditorState = {
capabilities: { capabilities: {
configuration: boolean configuration: boolean
diagnosticRelatedInformation: boolean diagnosticRelatedInformation: boolean
itemDefaults: string[]
} }
getConfiguration: (uri?: string) => Promise<Settings> getConfiguration: (uri?: string) => Promise<Settings>
getDocumentSymbols: (uri: string) => Promise<SymbolInformation[]> getDocumentSymbols: (uri: string) => Promise<SymbolInformation[]>
@ -118,6 +119,7 @@ export interface State {
jitContext?: any jitContext?: any
classList?: Array<[string, { color: culori.Color | KeywordColor | null; modifiers?: string[] }]> classList?: Array<[string, { color: culori.Color | KeywordColor | null; modifiers?: string[] }]>
pluginVersions?: string pluginVersions?: string
completionItemData?: Record<string, any>
// postcssPlugins?: { before: any[]; after: any[] } // postcssPlugins?: { before: any[]; after: any[] }
} }

View File

@ -24,7 +24,7 @@
"vscode" "vscode"
], ],
"engines": { "engines": {
"vscode": "^1.65.0" "vscode": "^1.67.0"
}, },
"categories": [ "categories": [
"Linters", "Linters",