From 7105e65ab30f54e6d65b34b8faaec058288d0230 Mon Sep 17 00:00:00 2001 From: luca cappa Date: Thu, 5 Sep 2024 16:03:40 -0700 Subject: [PATCH] - register #cpp only upon expFeatures enabled. - dispose the token callback. - get rid of magic numbers with consts. --- Extension/src/LanguageServer/client.ts | 6 ++---- Extension/src/LanguageServer/extension.ts | 2 +- Extension/src/LanguageServer/utils.ts | 11 +++++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Extension/src/LanguageServer/client.ts b/Extension/src/LanguageServer/client.ts index ab8efb0bfe..736353a7fa 100644 --- a/Extension/src/LanguageServer/client.ts +++ b/Extension/src/LanguageServer/client.ts @@ -58,7 +58,7 @@ import { cachedEditorConfigSettings, getEditorConfigSettings } from './editorCon import { CppSourceStr, clients, configPrefix, updateLanguageConfigurations, usesCrashHandler, watchForCrashes } from './extension'; import { LocalizeStringParams, getLocaleId, getLocalizedString } from './localization'; import { PersistentFolderState, PersistentWorkspaceState } from './persistentState'; -import { createProtocolFilter } from './protocolFilter'; +import { RequestCancelled, ServerCancelled, createProtocolFilter } from './protocolFilter'; import * as refs from './references'; import { CppSettings, OtherSettings, SettingsParams, WorkspaceFolderSettingsParams } from './settings'; import { SettingsTracker } from './settingsTracker'; @@ -2250,9 +2250,7 @@ export class DefaultClient implements Client { try { result = await this.languageClient.sendRequest(CppContextRequest, null, token); } catch (e: any) { - // From - // RequestCancelled = -32800, ServerCancelled = -32802, - if (e instanceof ResponseError && (e.code === -32800 /*RequestCancelled*/ || e.code === -32802 /*ServerCancelled*/)) { + if (e instanceof ResponseError && (e.code === RequestCancelled || e.code === ServerCancelled)) { throw new vscode.CancellationError(); } diff --git a/Extension/src/LanguageServer/extension.ts b/Extension/src/LanguageServer/extension.ts index a4d7887093..470ca51fe5 100644 --- a/Extension/src/LanguageServer/extension.ts +++ b/Extension/src/LanguageServer/extension.ts @@ -250,7 +250,7 @@ export async function activate(): Promise { activeDocument = activeEditor.document; } - if (util.extensionContext) { + if (util.extensionContext && new CppSettings().experimentalFeatures) { const tool = vscode.lm.registerTool('cpptools-lmtool-configuration', new CppConfigurationLanguageModelTool()); disposables.push(tool); } diff --git a/Extension/src/LanguageServer/utils.ts b/Extension/src/LanguageServer/utils.ts index 4f4d1384b0..3e769ba065 100644 --- a/Extension/src/LanguageServer/utils.ts +++ b/Extension/src/LanguageServer/utils.ts @@ -102,7 +102,14 @@ const docsChangedFromCppToC: Set = new Set(); export async function withCancellation(promise: Promise, token: vscode.CancellationToken): Promise { return new Promise((resolve, reject) => { - token.onCancellationRequested(() => reject(new vscode.CancellationError())); - promise.then((value) => resolve(value), (reason) => reject(reason)); + const disposable = token.onCancellationRequested(() => reject(new vscode.CancellationError())); + promise.then((value) => { + disposable.dispose(); + resolve(value); + }, (reason) => + { + disposable.dispose(); + reject(reason); + }); }); }