Path: blob/main/src/vs/editor/common/services/completionsEnablement.ts
5252 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import product from '../../../platform/product/common/product.js';6import { isObject } from '../../../base/common/types.js';7import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';8import { ITextResourceConfigurationService } from './textResourceConfiguration.js';9import { URI } from '../../../base/common/uri.js';1011/**12* Get the completions enablement setting name from product configuration.13*/14function getCompletionsEnablementSettingName(): string | undefined {15return product.defaultChatAgent?.completionsEnablementSetting;16}1718/**19* Checks if completions (e.g., Copilot) are enabled for a given language ID20* using `IConfigurationService`.21*22* @param configurationService The configuration service to read settings from.23* @param modeId The language ID to check. Defaults to '*' which checks the global setting.24* @returns `true` if completions are enabled for the language, `false` otherwise.25*/26export function isCompletionsEnabled(configurationService: IConfigurationService, modeId: string = '*'): boolean {27const settingName = getCompletionsEnablementSettingName();28if (!settingName) {29return false;30}3132return isCompletionsEnabledFromObject(33configurationService.getValue<Record<string, boolean>>(settingName),34modeId35);36}3738/**39* Checks if completions (e.g., Copilot) are enabled for a given language ID40* using `ITextResourceConfigurationService`.41*42* @param configurationService The text resource configuration service to read settings from.43* @param modeId The language ID to check. Defaults to '*' which checks the global setting.44* @returns `true` if completions are enabled for the language, `false` otherwise.45*/46export function isCompletionsEnabledWithTextResourceConfig(configurationService: ITextResourceConfigurationService, resource: URI, modeId: string = '*'): boolean {47const settingName = getCompletionsEnablementSettingName();48if (!settingName) {49return false;50}5152// Pass undefined as resource to get the global setting53return isCompletionsEnabledFromObject(54configurationService.getValue<Record<string, boolean>>(resource, settingName),55modeId56);57}5859/**60* Checks if completions are enabled for a given language ID using a pre-fetched61* completions enablement object.62*63* @param completionsEnablementObject The object containing per-language enablement settings.64* @param modeId The language ID to check. Defaults to '*' which checks the global setting.65* @returns `true` if completions are enabled for the language, `false` otherwise.66*/67export function isCompletionsEnabledFromObject(completionsEnablementObject: Record<string, boolean> | undefined, modeId: string = '*'): boolean {68if (!isObject(completionsEnablementObject)) {69return false; // default to disabled if setting is not available70}7172if (typeof completionsEnablementObject[modeId] !== 'undefined') {73return Boolean(completionsEnablementObject[modeId]); // go with setting if explicitly defined74}7576return Boolean(completionsEnablementObject['*']); // fallback to global setting otherwise77}787980