From e7fd3852d7a62fc79294094bf056cc0b594e4b4a Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Fri, 17 Apr 2020 22:16:26 +0800 Subject: [PATCH] fix ensureConfigurationForDocument could not work Document could not exists on BufEnter --- src/server/commands.ts | 16 +++++---- .../features/fileConfigurationManager.ts | 1 - src/server/index.ts | 33 ++++++++++--------- src/server/languageProvider.ts | 13 -------- 4 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/server/commands.ts b/src/server/commands.ts index 408d9a9..d84443f 100644 --- a/src/server/commands.ts +++ b/src/server/commands.ts @@ -7,6 +7,7 @@ import { TextEdit, Range } from 'vscode-languageserver-types' import { installModules } from './utils/modules' import { nodeModules } from './utils/helper' import { PluginManager } from '../utils/plugins' +import { languageIds } from './utils/languageModeIds' export interface Command { readonly id: string | string[] @@ -47,15 +48,17 @@ export class TypeScriptGoToProjectConfigCommand implements Command { public async execute(): Promise { let doc = await workspace.document + let { filetype } = doc + if (languageIds.indexOf(filetype) == -1) { + workspace.showMessage(`Could not determine TypeScript or JavaScript project. Unsupported file type: ${filetype}`, 'warning') + return + } + // doc.filetype await goToProjectConfig(this.client, doc.uri) } } async function goToProjectConfig(clientHost: TypeScriptServiceClientHost, uri: string): Promise { - if (!clientHost.handles(uri)) { - workspace.showMessage('Could not determine TypeScript or JavaScript project. Unsupported file type', 'warning') - return - } const client = clientHost.serviceClient const file = client.toPath(uri) let res @@ -96,8 +99,9 @@ export class AutoFixCommand implements Command { public async execute(): Promise { let document = await workspace.document - if (!this.client.handles(document.uri)) { - workspace.showMessage('Document is not handled by tsserver.', 'warning') + let { uri } = document + if (!this.client.handles(uri)) { + workspace.showMessage(`Document ${uri} is not handled by tsserver.`, 'warning') return } let file = this.client.serviceClient.toPath(document.uri) diff --git a/src/server/features/fileConfigurationManager.ts b/src/server/features/fileConfigurationManager.ts index 673138a..80051a5 100644 --- a/src/server/features/fileConfigurationManager.ts +++ b/src/server/features/fileConfigurationManager.ts @@ -65,7 +65,6 @@ export default class FileConfigurationManager { } public async ensureConfigurationForDocument(document: TextDocument): Promise { - if (!this.client.bufferSyncSupport.has(document.uri)) return let opts = await workspace.getFormatOptions(document.uri) return this.ensureConfigurationOptions(document, opts.insertSpaces, opts.tabSize) } diff --git a/src/server/index.ts b/src/server/index.ts index c903df3..c7ac259 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -3,14 +3,7 @@ import { Disposable, DocumentSelector, Emitter, Event } from 'vscode-languageser import TypeScriptServiceClientHost from './typescriptServiceClientHost' import { LanguageDescription, standardLanguageDescriptions } from './utils/languageDescription' import { PluginManager } from '../utils/plugins' - -function wait(ms: number): Promise { - return new Promise(resolve => { - setTimeout(() => { - resolve() - }, ms) - }) -} +import { TextDocument } from 'vscode-languageserver-textdocument' export default class TsserverService implements IServiceProvider { public id = 'tsserver' @@ -35,12 +28,24 @@ export default class TsserverService implements IServiceProvider { this.selector = this.descriptions.reduce((arr, c) => { return arr.concat(c.modeIds) }, []) + workspace.onDidOpenTextDocument(doc => { + this.ensureConfigurationForDocument(doc) + }, null, this.disposables) } public get config(): WorkspaceConfiguration { return workspace.getConfiguration('tsserver') } + public ensureConfigurationForDocument(document: TextDocument): void { + let uri = Uri.parse(document.uri) + let language = this.clientHost.findLanguage(uri) + if (!language) return + language.fileConfigurationManager.ensureConfigurationForDocument(document).catch(_e => { + // noop + }) + } + public start(): Promise { if (this.clientHost) return this.state = ServiceStat.Starting @@ -56,7 +61,7 @@ export default class TsserverService implements IServiceProvider { } }) this._onDidServiceReady.fire(void 0) - this.ensureConfiguration() // tslint:disable-line + this.ensureConfiguration() if (!started) { started = true resolve() @@ -65,13 +70,11 @@ export default class TsserverService implements IServiceProvider { }) } - private async ensureConfiguration(): Promise { + private ensureConfiguration(): void { if (!this.clientHost) return - let document = await workspace.document - let uri = Uri.parse(document.uri) - let language = this.clientHost.findLanguage(uri) - if (!language) return - await language.fileConfigurationManager.ensureConfigurationForDocument(document.textDocument) + for (let doc of workspace.documents) { + this.ensureConfigurationForDocument(doc.textDocument) + } } public dispose(): void { diff --git a/src/server/languageProvider.ts b/src/server/languageProvider.ts index ef35820..b394ad9 100644 --- a/src/server/languageProvider.ts +++ b/src/server/languageProvider.ts @@ -49,23 +49,10 @@ export default class LanguageProvider { workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables) this.configurationChanged() - - events.on('BufEnter', bufnr => { - let doc = workspace.getDocument(bufnr) - if (!doc || client.state !== ServiceStat.Running) return - if (description.modeIds.indexOf(doc.filetype) == -1) return - this.fileConfigurationManager.ensureConfigurationForDocument(doc.textDocument) // tslint:disable-line - }, this, this.disposables) - let initialized = false client.onTsServerStarted(async () => { // tslint:disable-line if (!initialized) { - for (let doc of workspace.documents) { - if (description.modeIds.indexOf(doc.filetype) !== -1) { - this.fileConfigurationManager.ensureConfigurationForDocument(doc.textDocument) // tslint:disable-line - } - } initialized = true this.registerProviders(client, typingsStatus) } else {