Handle TypeScript server arguments (#234139)

* Handle TypeScript server arguments

* Pass token as last parameter
This commit is contained in:
Dirk Bäumer 2024-11-20 00:03:02 +01:00 коммит произвёл GitHub
Родитель e57cc506b2
Коммит 46afc4d3b9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 30 добавлений и 10 удалений

Просмотреть файл

@ -2,6 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { TypeScriptRequests } from '../typescriptService';
import TypeScriptServiceClientHost from '../typeScriptServiceClientHost';
@ -9,6 +10,14 @@ import { nulToken } from '../utils/cancellation';
import { Lazy } from '../utils/lazy';
import { Command } from './commandManager';
function isCancellationToken(value: any): value is vscode.CancellationToken {
return value && typeof value.isCancellationRequested === 'boolean' && typeof value.onCancellationRequested === 'function';
}
interface RequestArgs {
readonly file?: unknown;
}
export class TSServerRequestCommand implements Command {
public readonly id = 'typescript.tsserverRequest';
@ -16,9 +25,20 @@ export class TSServerRequestCommand implements Command {
private readonly lazyClientHost: Lazy<TypeScriptServiceClientHost>
) { }
public async execute(command: keyof TypeScriptRequests, args?: any, config?: any): Promise<unknown> {
// A cancellation token cannot be passed through the command infrastructure
const token = nulToken;
public async execute(command: keyof TypeScriptRequests, args?: any, config?: any, token?: vscode.CancellationToken): Promise<unknown> {
if (!isCancellationToken(token)) {
token = nulToken;
}
if (args && typeof args === 'object' && !Array.isArray(args)) {
const requestArgs = args as RequestArgs;
let newArgs: any = undefined;
if (requestArgs.file instanceof vscode.Uri) {
newArgs = { ...args };
const client = this.lazyClientHost.value.serviceClient;
newArgs.file = client.toOpenTsFilePath(requestArgs.file);
args = newArgs;
}
}
// The list can be found in the TypeScript compiler as `const enum CommandTypes`,
// to avoid extensions making calls which could affect the internal tsserver state
@ -42,4 +62,3 @@ export class TSServerRequestCommand implements Command {
return undefined;
}
}

Просмотреть файл

@ -151,7 +151,7 @@ export interface ITypeScriptServiceClient {
*
* @return The normalized path or `undefined` if the document is not open on the server.
*/
toOpenTsFilePath(document: vscode.TextDocument, options?: {
toOpenTsFilePath(document: vscode.TextDocument | vscode.Uri, options?: {
suppressAlertOnFailure?: boolean;
}): string | undefined;

Просмотреть файл

@ -778,14 +778,15 @@ export default class TypeScriptServiceClient extends Disposable implements IType
}
public toOpenTsFilePath(document: vscode.TextDocument, options: { suppressAlertOnFailure?: boolean } = {}): string | undefined {
if (!this.bufferSyncSupport.ensureHasBuffer(document.uri)) {
if (!options.suppressAlertOnFailure && !fileSchemes.disabledSchemes.has(document.uri.scheme)) {
console.error(`Unexpected resource ${document.uri}`);
public toOpenTsFilePath(document: vscode.TextDocument | vscode.Uri, options: { suppressAlertOnFailure?: boolean } = {}): string | undefined {
const uri = document instanceof vscode.Uri ? document : document.uri;
if (!this.bufferSyncSupport.ensureHasBuffer(uri)) {
if (!options.suppressAlertOnFailure && !fileSchemes.disabledSchemes.has(uri.scheme)) {
console.error(`Unexpected resource ${uri}`);
}
return undefined;
}
return this.toTsFilePath(document.uri);
return this.toTsFilePath(uri);
}
public hasCapabilityForResource(resource: vscode.Uri, capability: ClientCapability): boolean {