fix ensureConfigurationForDocument could not work

Document could not exists on BufEnter
This commit is contained in:
Qiming Zhao 2020-04-17 22:16:26 +08:00
parent 3a87cacccc
commit e7fd3852d7
4 changed files with 28 additions and 35 deletions

View file

@ -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<void> {
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<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
@ -96,8 +99,9 @@ export class AutoFixCommand implements Command {
public async execute(): Promise<void> {
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)

View file

@ -65,7 +65,6 @@ export default class FileConfigurationManager {
}
public async ensureConfigurationForDocument(document: TextDocument): Promise<void> {
if (!this.client.bufferSyncSupport.has(document.uri)) return
let opts = await workspace.getFormatOptions(document.uri)
return this.ensureConfigurationOptions(document, opts.insertSpaces, opts.tabSize)
}

View file

@ -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<any> {
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<void> {
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<void> {
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 {

View file

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