Skip to content

Remove buggy await tw.init(); call #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions packages/tailwindcss-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ function getContentDocumentSelectorFromConfigFile(
}

class TW {
private initialized = false
private initPromise: Promise<void>
private lspHandlersAdded = false
private workspaces: Map<string, { name: string; workspaceFsPath: string }>
private projects: Map<string, ProjectService>
Expand All @@ -1631,12 +1631,15 @@ class TW {
}

async init(): Promise<void> {
if (this.initialized) return
if (!this.initPromise) {
this.initPromise = this._init()
}
await this.initPromise
}

private async _init(): Promise<void> {
clearRequireCache()

this.initialized = true

let base: string
if (this.initializeParams.rootUri) {
base = URI.parse(this.initializeParams.rootUri).fsPath
Expand Down Expand Up @@ -2131,7 +2134,7 @@ class TW {
}
}

private setupLSPHandlers() {
setupLSPHandlers() {
if (this.lspHandlersAdded) {
return
}
Expand All @@ -2147,6 +2150,10 @@ class TW {
}

private updateCapabilities() {
if (!supportsDynamicRegistration(this.initializeParams)) {
return
}

if (this.registrations) {
this.registrations.then((r) => r.dispose())
}
Expand Down Expand Up @@ -2221,30 +2228,37 @@ class TW {
}

async onDocumentColor(params: DocumentColorParams): Promise<ColorInformation[]> {
await this.init()
return this.getProject(params.textDocument)?.onDocumentColor(params) ?? []
}

async onColorPresentation(params: ColorPresentationParams): Promise<ColorPresentation[]> {
await this.init()
return this.getProject(params.textDocument)?.onColorPresentation(params) ?? []
}

async onHover(params: TextDocumentPositionParams): Promise<Hover> {
await this.init()
return this.getProject(params.textDocument)?.onHover(params) ?? null
}

async onCompletion(params: CompletionParams): Promise<CompletionList> {
await this.init()
return this.getProject(params.textDocument)?.onCompletion(params) ?? null
}

async onCompletionResolve(item: CompletionItem): Promise<CompletionItem> {
await this.init()
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
}

onDocumentLinks(params: DocumentLinkParams): DocumentLink[] {
async onDocumentLinks(params: DocumentLinkParams): Promise<DocumentLink[]> {
await this.init()
return this.getProject(params.textDocument)?.onDocumentLinks(params) ?? null
}

Expand Down Expand Up @@ -2274,7 +2288,7 @@ class TW {
restart(): void {
console.log('----------\nRESTARTING\n----------')
this.dispose()
this.initialized = false
this.initPromise = undefined
this.init()
}
}
Expand Down Expand Up @@ -2306,9 +2320,8 @@ class DocumentService {
}
}

function supportsDynamicRegistration(connection: Connection, params: InitializeParams): boolean {
function supportsDynamicRegistration(params: InitializeParams): boolean {
return (
connection.onInitialized &&
params.capabilities.textDocument.hover?.dynamicRegistration &&
params.capabilities.textDocument.colorProvider?.dynamicRegistration &&
params.capabilities.textDocument.codeAction?.dynamicRegistration &&
Expand All @@ -2322,15 +2335,15 @@ const tw = new TW(connection)
connection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
tw.initializeParams = params

if (supportsDynamicRegistration(connection, params)) {
if (supportsDynamicRegistration(params)) {
return {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Full,
},
}
}

await tw.init()
tw.setupLSPHandlers()

return {
capabilities: {
Expand Down