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>> = []
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 = {
enabled: false,
completionItemData: {
_projectKey: projectKey,
},
editor: {
connection,
folder,
@ -390,6 +403,7 @@ async function createProjectService(
capabilities: {
configuration: true,
diagnosticRelatedInformation: true,
itemDefaults,
},
documents: documentService.documents,
getConfiguration,
@ -1114,21 +1128,13 @@ async function createProjectService(
let settings = await state.editor.getConfiguration(document.uri)
if (!settings.tailwindCSS.suggestions) return null
if (await isExcluded(state, document)) return null
let result = await 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 },
})),
}
return doComplete(state, document, params.position, params.context)
}, null)
},
onCompletionResolve(item: CompletionItem): Promise<CompletionItem> {
return withFallback(() => {
if (!state.enabled) return null
return resolveCompletionItem(state, { ...item, data: item.data?.originalData })
return resolveCompletionItem(state, item)
}, null)
},
async onCodeAction(params: CodeActionParams): Promise<CodeAction[]> {
@ -2162,7 +2168,7 @@ class TW {
}
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[]> {

View File

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

View File

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

View File

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