fix ensureConfigurationForDocument could not work
Document could not exists on BufEnter
This commit is contained in:
parent
3a87cacccc
commit
e7fd3852d7
4 changed files with 28 additions and 35 deletions
|
@ -7,6 +7,7 @@ import { TextEdit, Range } from 'vscode-languageserver-types'
|
||||||
import { installModules } from './utils/modules'
|
import { installModules } from './utils/modules'
|
||||||
import { nodeModules } from './utils/helper'
|
import { nodeModules } from './utils/helper'
|
||||||
import { PluginManager } from '../utils/plugins'
|
import { PluginManager } from '../utils/plugins'
|
||||||
|
import { languageIds } from './utils/languageModeIds'
|
||||||
|
|
||||||
export interface Command {
|
export interface Command {
|
||||||
readonly id: string | string[]
|
readonly id: string | string[]
|
||||||
|
@ -47,15 +48,17 @@ export class TypeScriptGoToProjectConfigCommand implements Command {
|
||||||
|
|
||||||
public async execute(): Promise<void> {
|
public async execute(): Promise<void> {
|
||||||
let doc = await workspace.document
|
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)
|
await goToProjectConfig(this.client, doc.uri)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function goToProjectConfig(clientHost: TypeScriptServiceClientHost, uri: string): Promise<void> {
|
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 client = clientHost.serviceClient
|
||||||
const file = client.toPath(uri)
|
const file = client.toPath(uri)
|
||||||
let res
|
let res
|
||||||
|
@ -96,8 +99,9 @@ export class AutoFixCommand implements Command {
|
||||||
|
|
||||||
public async execute(): Promise<void> {
|
public async execute(): Promise<void> {
|
||||||
let document = await workspace.document
|
let document = await workspace.document
|
||||||
if (!this.client.handles(document.uri)) {
|
let { uri } = document
|
||||||
workspace.showMessage('Document is not handled by tsserver.', 'warning')
|
if (!this.client.handles(uri)) {
|
||||||
|
workspace.showMessage(`Document ${uri} is not handled by tsserver.`, 'warning')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let file = this.client.serviceClient.toPath(document.uri)
|
let file = this.client.serviceClient.toPath(document.uri)
|
||||||
|
|
|
@ -65,7 +65,6 @@ export default class FileConfigurationManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ensureConfigurationForDocument(document: TextDocument): Promise<void> {
|
public async ensureConfigurationForDocument(document: TextDocument): Promise<void> {
|
||||||
if (!this.client.bufferSyncSupport.has(document.uri)) return
|
|
||||||
let opts = await workspace.getFormatOptions(document.uri)
|
let opts = await workspace.getFormatOptions(document.uri)
|
||||||
return this.ensureConfigurationOptions(document, opts.insertSpaces, opts.tabSize)
|
return this.ensureConfigurationOptions(document, opts.insertSpaces, opts.tabSize)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,7 @@ import { Disposable, DocumentSelector, Emitter, Event } from 'vscode-languageser
|
||||||
import TypeScriptServiceClientHost from './typescriptServiceClientHost'
|
import TypeScriptServiceClientHost from './typescriptServiceClientHost'
|
||||||
import { LanguageDescription, standardLanguageDescriptions } from './utils/languageDescription'
|
import { LanguageDescription, standardLanguageDescriptions } from './utils/languageDescription'
|
||||||
import { PluginManager } from '../utils/plugins'
|
import { PluginManager } from '../utils/plugins'
|
||||||
|
import { TextDocument } from 'vscode-languageserver-textdocument'
|
||||||
function wait(ms: number): Promise<any> {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
setTimeout(() => {
|
|
||||||
resolve()
|
|
||||||
}, ms)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class TsserverService implements IServiceProvider {
|
export default class TsserverService implements IServiceProvider {
|
||||||
public id = 'tsserver'
|
public id = 'tsserver'
|
||||||
|
@ -35,12 +28,24 @@ export default class TsserverService implements IServiceProvider {
|
||||||
this.selector = this.descriptions.reduce((arr, c) => {
|
this.selector = this.descriptions.reduce((arr, c) => {
|
||||||
return arr.concat(c.modeIds)
|
return arr.concat(c.modeIds)
|
||||||
}, [])
|
}, [])
|
||||||
|
workspace.onDidOpenTextDocument(doc => {
|
||||||
|
this.ensureConfigurationForDocument(doc)
|
||||||
|
}, null, this.disposables)
|
||||||
}
|
}
|
||||||
|
|
||||||
public get config(): WorkspaceConfiguration {
|
public get config(): WorkspaceConfiguration {
|
||||||
return workspace.getConfiguration('tsserver')
|
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> {
|
public start(): Promise<void> {
|
||||||
if (this.clientHost) return
|
if (this.clientHost) return
|
||||||
this.state = ServiceStat.Starting
|
this.state = ServiceStat.Starting
|
||||||
|
@ -56,7 +61,7 @@ export default class TsserverService implements IServiceProvider {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this._onDidServiceReady.fire(void 0)
|
this._onDidServiceReady.fire(void 0)
|
||||||
this.ensureConfiguration() // tslint:disable-line
|
this.ensureConfiguration()
|
||||||
if (!started) {
|
if (!started) {
|
||||||
started = true
|
started = true
|
||||||
resolve()
|
resolve()
|
||||||
|
@ -65,13 +70,11 @@ export default class TsserverService implements IServiceProvider {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ensureConfiguration(): Promise<void> {
|
private ensureConfiguration(): void {
|
||||||
if (!this.clientHost) return
|
if (!this.clientHost) return
|
||||||
let document = await workspace.document
|
for (let doc of workspace.documents) {
|
||||||
let uri = Uri.parse(document.uri)
|
this.ensureConfigurationForDocument(doc.textDocument)
|
||||||
let language = this.clientHost.findLanguage(uri)
|
}
|
||||||
if (!language) return
|
|
||||||
await language.fileConfigurationManager.ensureConfigurationForDocument(document.textDocument)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public dispose(): void {
|
public dispose(): void {
|
||||||
|
|
|
@ -49,23 +49,10 @@ export default class LanguageProvider {
|
||||||
|
|
||||||
workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables)
|
workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables)
|
||||||
this.configurationChanged()
|
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
|
let initialized = false
|
||||||
|
|
||||||
client.onTsServerStarted(async () => { // tslint:disable-line
|
client.onTsServerStarted(async () => { // tslint:disable-line
|
||||||
if (!initialized) {
|
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
|
initialized = true
|
||||||
this.registerProviders(client, typingsStatus)
|
this.registerProviders(client, typingsStatus)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue