coc-tsserver/src/server/commands.ts
2018-09-07 20:40:51 +08:00

79 lines
2.3 KiB
TypeScript

import { CancellationToken } from 'vscode-languageserver-protocol'
import URI from 'vscode-uri'
import { workspace } from 'coc.nvim'
import { ProjectInfoResponse } from './protocol'
import TypeScriptServiceClientHost from './typescriptServiceClientHost'
export interface Command {
readonly id: string | string[]
execute(...args: any[]): void | Promise<any>
}
export class ReloadProjectsCommand implements Command {
public readonly id = 'tsserver.reloadProjects'
public constructor(
private readonly client: TypeScriptServiceClientHost
) { }
public execute(): void {
this.client.reloadProjects()
workspace.showMessage('projects reloaded')
}
}
export class OpenTsServerLogCommand implements Command {
public readonly id = 'tsserver.openTsServerLog'
public constructor(
private readonly client: TypeScriptServiceClientHost
) { }
public execute(): void {
this.client.serviceClient.openTsServerLogFile() // tslint:disable-line
}
}
export class TypeScriptGoToProjectConfigCommand implements Command {
public readonly id = 'tsserver.goToProjectConfig'
public constructor(
private readonly client: TypeScriptServiceClientHost
) { }
public async execute(): Promise<void> {
let doc = await workspace.document
await goToProjectConfig(this.client, doc.uri)
}
}
async function goToProjectConfig(clientHost: TypeScriptServiceClientHost, uri: string): Promise<void> {
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: ProjectInfoResponse | undefined
try {
res = await client.execute('projectInfo', { file, needFileNameList: false }, CancellationToken.None)
} catch {
// noop
}
if (!res || !res.body) {
workspace.showMessage('Could not determine TypeScript or JavaScript project.', 'warning')
return
}
const { configFileName } = res.body
if (configFileName && !isImplicitProjectConfigFile(configFileName)) {
await workspace.openResource(URI.file(configFileName).toString())
return
}
workspace.showMessage('Config file not found', 'warning')
}
function isImplicitProjectConfigFile(configFileName: string): boolean {
return configFileName.indexOf('/dev/null/') === 0
}