Fix language server initialisation outside of VS Code (#803)

* Remove buggy await call

This is to resolve issue https://github.com/tailwindlabs/tailwindcss-intellisense/issues/802

* Fix unhandled method errors

---------

Co-authored-by: Brad Cornes <hello@bradley.dev>
master
atgote 2023-08-22 14:26:05 +03:00 committed by GitHub
parent ac2f9fabd7
commit d5268fd41e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 12 deletions

View File

@ -1611,7 +1611,7 @@ function getContentDocumentSelectorFromConfigFile(
} }
class TW { class TW {
private initialized = false private initPromise: Promise<void>
private lspHandlersAdded = false private lspHandlersAdded = false
private workspaces: Map<string, { name: string; workspaceFsPath: string }> private workspaces: Map<string, { name: string; workspaceFsPath: string }>
private projects: Map<string, ProjectService> private projects: Map<string, ProjectService>
@ -1631,12 +1631,15 @@ class TW {
} }
async init(): Promise<void> { async init(): Promise<void> {
if (this.initialized) return if (!this.initPromise) {
this.initPromise = this._init()
}
await this.initPromise
}
private async _init(): Promise<void> {
clearRequireCache() clearRequireCache()
this.initialized = true
let base: string let base: string
if (this.initializeParams.rootUri) { if (this.initializeParams.rootUri) {
base = URI.parse(this.initializeParams.rootUri).fsPath base = URI.parse(this.initializeParams.rootUri).fsPath
@ -2131,7 +2134,7 @@ class TW {
} }
} }
private setupLSPHandlers() { setupLSPHandlers() {
if (this.lspHandlersAdded) { if (this.lspHandlersAdded) {
return return
} }
@ -2147,6 +2150,10 @@ class TW {
} }
private updateCapabilities() { private updateCapabilities() {
if (!supportsDynamicRegistration(this.initializeParams)) {
return
}
if (this.registrations) { if (this.registrations) {
this.registrations.then((r) => r.dispose()) this.registrations.then((r) => r.dispose())
} }
@ -2221,30 +2228,37 @@ class TW {
} }
async onDocumentColor(params: DocumentColorParams): Promise<ColorInformation[]> { async onDocumentColor(params: DocumentColorParams): Promise<ColorInformation[]> {
await this.init()
return this.getProject(params.textDocument)?.onDocumentColor(params) ?? [] return this.getProject(params.textDocument)?.onDocumentColor(params) ?? []
} }
async onColorPresentation(params: ColorPresentationParams): Promise<ColorPresentation[]> { async onColorPresentation(params: ColorPresentationParams): Promise<ColorPresentation[]> {
await this.init()
return this.getProject(params.textDocument)?.onColorPresentation(params) ?? [] return this.getProject(params.textDocument)?.onColorPresentation(params) ?? []
} }
async onHover(params: TextDocumentPositionParams): Promise<Hover> { async onHover(params: TextDocumentPositionParams): Promise<Hover> {
await this.init()
return this.getProject(params.textDocument)?.onHover(params) ?? null return this.getProject(params.textDocument)?.onHover(params) ?? null
} }
async onCompletion(params: CompletionParams): Promise<CompletionList> { async onCompletion(params: CompletionParams): Promise<CompletionList> {
await this.init()
return this.getProject(params.textDocument)?.onCompletion(params) ?? null return this.getProject(params.textDocument)?.onCompletion(params) ?? null
} }
async onCompletionResolve(item: CompletionItem): Promise<CompletionItem> { async onCompletionResolve(item: CompletionItem): Promise<CompletionItem> {
await this.init()
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[]> { async onCodeAction(params: CodeActionParams): Promise<CodeAction[]> {
await this.init()
return this.getProject(params.textDocument)?.onCodeAction(params) ?? null return this.getProject(params.textDocument)?.onCodeAction(params) ?? null
} }
onDocumentLinks(params: DocumentLinkParams): DocumentLink[] { async onDocumentLinks(params: DocumentLinkParams): Promise<DocumentLink[]> {
await this.init()
return this.getProject(params.textDocument)?.onDocumentLinks(params) ?? null return this.getProject(params.textDocument)?.onDocumentLinks(params) ?? null
} }
@ -2274,7 +2288,7 @@ class TW {
restart(): void { restart(): void {
console.log('----------\nRESTARTING\n----------') console.log('----------\nRESTARTING\n----------')
this.dispose() this.dispose()
this.initialized = false this.initPromise = undefined
this.init() this.init()
} }
} }
@ -2306,9 +2320,8 @@ class DocumentService {
} }
} }
function supportsDynamicRegistration(connection: Connection, params: InitializeParams): boolean { function supportsDynamicRegistration(params: InitializeParams): boolean {
return ( return (
connection.onInitialized &&
params.capabilities.textDocument.hover?.dynamicRegistration && params.capabilities.textDocument.hover?.dynamicRegistration &&
params.capabilities.textDocument.colorProvider?.dynamicRegistration && params.capabilities.textDocument.colorProvider?.dynamicRegistration &&
params.capabilities.textDocument.codeAction?.dynamicRegistration && params.capabilities.textDocument.codeAction?.dynamicRegistration &&
@ -2322,7 +2335,7 @@ const tw = new TW(connection)
connection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => { connection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
tw.initializeParams = params tw.initializeParams = params
if (supportsDynamicRegistration(connection, params)) { if (supportsDynamicRegistration(params)) {
return { return {
capabilities: { capabilities: {
textDocumentSync: TextDocumentSyncKind.Full, textDocumentSync: TextDocumentSyncKind.Full,
@ -2330,7 +2343,7 @@ connection.onInitialize(async (params: InitializeParams): Promise<InitializeResu
} }
} }
await tw.init() tw.setupLSPHandlers()
return { return {
capabilities: { capabilities: {