From 0f3e462afd2bb700a25f77b3daec78648f6ba248 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Mon, 22 Feb 2021 18:42:35 +0800 Subject: [PATCH] fix untitled buffer not work Closes #261 --- src/server/typescriptServiceClient.ts | 16 ++++++++++++---- src/server/typescriptServiceClientHost.ts | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/server/typescriptServiceClient.ts b/src/server/typescriptServiceClient.ts index 97eaf48..301d7f4 100644 --- a/src/server/typescriptServiceClient.ts +++ b/src/server/typescriptServiceClient.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import cp from 'child_process' -import { ServiceStat, Uri, window, workspace } from 'coc.nvim' +import { Document, ServiceStat, Uri, window, workspace } from 'coc.nvim' import fs from 'fs' import os from 'os' import path from 'path' @@ -491,7 +491,7 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient public toResource(filepath: string): string { if (this._apiVersion.gte(API.v213)) { - if (filepath.startsWith('untitled:')) { + if (filepath.startsWith(this.inMemoryResourcePrefix + 'untitled:')) { let resource = Uri.parse(filepath) if (this.inMemoryResourcePrefix) { const dirName = path.dirname(resource.path) @@ -524,14 +524,22 @@ export default class TypeScriptServiceClient implements ITypeScriptServiceClient } } + public getDocument(resource: string): Document | undefined { + if (resource.startsWith('untitled:')) { + let bufnr = parseInt(resource.split(':', 2)[1], 10) + return workspace.getDocument(bufnr) + } + return workspace.getDocument(resource) + } + private get inMemoryResourcePrefix(): string { return this._apiVersion.gte(API.v270) ? '^' : '' } public asUrl(filepath: string): Uri { if (this._apiVersion.gte(API.v213)) { - if (filepath.startsWith('untitled:')) { - let resource = Uri.parse(filepath) + if (filepath.startsWith(this.inMemoryResourcePrefix + 'untitled:')) { + let resource = Uri.parse(filepath.slice(this.inMemoryResourcePrefix.length)) if (this.inMemoryResourcePrefix) { const dirName = path.dirname(resource.path) const fileName = path.basename(resource.path) diff --git a/src/server/typescriptServiceClientHost.ts b/src/server/typescriptServiceClientHost.ts index 2f62757..5f1479a 100644 --- a/src/server/typescriptServiceClientHost.ts +++ b/src/server/typescriptServiceClientHost.ts @@ -181,7 +181,7 @@ export default class TypeScriptServiceClientHost implements Disposable { public async findLanguage(uri: string): Promise { try { - let doc = await workspace.loadFile(uri) + let doc = this.client.getDocument(uri) if (!doc) return undefined let languages = Array.from(this.languagePerId.values()) return languages.find(language => language.handles(uri, doc.textDocument))