From 464adfcb0a3f5960151b09f1b8d2c2c35ae78e1b Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Fri, 18 Feb 2022 03:46:45 +0800 Subject: [PATCH] use Tsserver_path for resolved tsc path Closes #306 --- src/server/index.ts | 9 +++++---- src/server/typescriptServiceClient.ts | 9 +++++++-- src/server/typescriptServiceClientHost.ts | 4 ++-- src/server/utils/versionProvider.ts | 9 +++++++++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/server/index.ts b/src/server/index.ts index ce7698e..160bce2 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -99,20 +99,21 @@ export default class TsserverService implements IServiceProvider { }) } - public start(): Promise { + public async start(): Promise { if (!this.enable) return if (this.clientHost) { let client = this.clientHost.serviceClient client.restartTsServer() return } + let tscPath = await workspace.nvim.getVar('Tsserver_path') as string this._state = ServiceStat.Starting - this.clientHost = new TypeScriptServiceClientHost(this.descriptions, this.pluginManager) + this.clientHost = new TypeScriptServiceClientHost(this.descriptions, this.pluginManager, tscPath) let client = this.clientHost.serviceClient - return new Promise(resolve => { + await new Promise(resolve => { client.onReady(() => { this._onDidServiceReady.fire(void 0) - resolve() + resolve(undefined) }) }) } diff --git a/src/server/typescriptServiceClient.ts b/src/server/typescriptServiceClient.ts index 4e6dc7d..cd29468 100644 --- a/src/server/typescriptServiceClient.ts +++ b/src/server/typescriptServiceClient.ts @@ -70,7 +70,8 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient constructor( public readonly pluginManager: PluginManager, - public readonly modeIds: string[] + public readonly modeIds: string[], + private readonly tscPathVim: string | undefined ) { this.pathSeparator = path.sep this.lastStart = Date.now() @@ -217,7 +218,10 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient private startService(resendModels = false): ForkedTsServerProcess | undefined { const { ignoreLocalTsserver } = this.configuration let currentVersion: TypeScriptVersion - if (!ignoreLocalTsserver) currentVersion = this.versionProvider.getLocalVersion() + console.log('===========') + console.log(this.tscPathVim) + if (this.tscPathVim) currentVersion = this.versionProvider.getVersionFromTscPath(this.tscPathVim) + if (!currentVersion && !ignoreLocalTsserver) currentVersion = this.versionProvider.getLocalVersion() if (!currentVersion || !fs.existsSync(currentVersion.tsServerPath)) { this.info('Local tsserver not found, using bundled tsserver with coc-tsserver.') currentVersion = this.versionProvider.getDefaultVersion() @@ -232,6 +236,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient } this._apiVersion = currentVersion.version this._tscPath = currentVersion.tscPath + workspace.nvim.setVar('Tsserver_path', this._tscPath, true) this.versionStatus.onDidChangeTypeScriptVersion(currentVersion) const tsServerForkArgs = this.getTsServerArgs(currentVersion) const options = { execArgv: this.getExecArgv() } diff --git a/src/server/typescriptServiceClientHost.ts b/src/server/typescriptServiceClientHost.ts index 277efb5..4113e09 100644 --- a/src/server/typescriptServiceClientHost.ts +++ b/src/server/typescriptServiceClientHost.ts @@ -38,7 +38,7 @@ export default class TypeScriptServiceClientHost implements Disposable { private readonly fileConfigurationManager: FileConfigurationManager private reportStyleCheckAsWarnings = true - constructor(descriptions: LanguageDescription[], pluginManager: PluginManager) { + constructor(descriptions: LanguageDescription[], pluginManager: PluginManager, tscPath: string | undefined) { let timer: NodeJS.Timer const handleProjectChange = () => { if (timer) clearTimeout(timer) @@ -57,7 +57,7 @@ export default class TypeScriptServiceClientHost implements Disposable { packageFileWatcher.onDidChange(handleProjectChange, this, this.disposables) const allModeIds = this.getAllModeIds(descriptions, pluginManager) - this.client = new TypeScriptServiceClient(pluginManager, allModeIds) + this.client = new TypeScriptServiceClient(pluginManager, allModeIds, tscPath) this.disposables.push(this.client) this.client.onDiagnosticsReceived(({ kind, resource, diagnostics }) => { this.diagnosticsReceived(kind, resource, diagnostics).catch(e => { diff --git a/src/server/utils/versionProvider.ts b/src/server/utils/versionProvider.ts index 02e4a21..b488bef 100644 --- a/src/server/utils/versionProvider.ts +++ b/src/server/utils/versionProvider.ts @@ -106,6 +106,15 @@ export class TypeScriptVersionProvider { return undefined } + public getVersionFromTscPath(tscPath: string): TypeScriptVersion | undefined { + if (!tscPath || !fs.existsSync(tscPath)) return undefined + let libFolder = path.resolve(tscPath, '../../lib') + if (fs.existsSync(libFolder)) { + let version = new TypeScriptVersion(libFolder) + if (version.isValid) return version + } + } + public getLocalVersion(): TypeScriptVersion | undefined { let folders = workspace.workspaceFolders.map(f => Uri.parse(f.uri).fsPath) for (let p of folders) {