use Tsserver_path for resolved tsc path

Closes #306
This commit is contained in:
Qiming Zhao 2022-02-18 03:46:45 +08:00
parent 2d62d7da5f
commit 464adfcb0a
No known key found for this signature in database
GPG key ID: 9722CD0E8D4DCB8C
4 changed files with 23 additions and 8 deletions

View file

@ -99,20 +99,21 @@ export default class TsserverService implements IServiceProvider {
})
}
public start(): Promise<void> {
public async start(): Promise<void> {
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)
})
})
}

View file

@ -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() }

View file

@ -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 => {

View file

@ -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) {