Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/services/completionsEnablement.ts
5252 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import product from '../../../platform/product/common/product.js';
7
import { isObject } from '../../../base/common/types.js';
8
import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';
9
import { ITextResourceConfigurationService } from './textResourceConfiguration.js';
10
import { URI } from '../../../base/common/uri.js';
11
12
/**
13
* Get the completions enablement setting name from product configuration.
14
*/
15
function getCompletionsEnablementSettingName(): string | undefined {
16
return product.defaultChatAgent?.completionsEnablementSetting;
17
}
18
19
/**
20
* Checks if completions (e.g., Copilot) are enabled for a given language ID
21
* using `IConfigurationService`.
22
*
23
* @param configurationService The configuration service to read settings from.
24
* @param modeId The language ID to check. Defaults to '*' which checks the global setting.
25
* @returns `true` if completions are enabled for the language, `false` otherwise.
26
*/
27
export function isCompletionsEnabled(configurationService: IConfigurationService, modeId: string = '*'): boolean {
28
const settingName = getCompletionsEnablementSettingName();
29
if (!settingName) {
30
return false;
31
}
32
33
return isCompletionsEnabledFromObject(
34
configurationService.getValue<Record<string, boolean>>(settingName),
35
modeId
36
);
37
}
38
39
/**
40
* Checks if completions (e.g., Copilot) are enabled for a given language ID
41
* using `ITextResourceConfigurationService`.
42
*
43
* @param configurationService The text resource configuration service to read settings from.
44
* @param modeId The language ID to check. Defaults to '*' which checks the global setting.
45
* @returns `true` if completions are enabled for the language, `false` otherwise.
46
*/
47
export function isCompletionsEnabledWithTextResourceConfig(configurationService: ITextResourceConfigurationService, resource: URI, modeId: string = '*'): boolean {
48
const settingName = getCompletionsEnablementSettingName();
49
if (!settingName) {
50
return false;
51
}
52
53
// Pass undefined as resource to get the global setting
54
return isCompletionsEnabledFromObject(
55
configurationService.getValue<Record<string, boolean>>(resource, settingName),
56
modeId
57
);
58
}
59
60
/**
61
* Checks if completions are enabled for a given language ID using a pre-fetched
62
* completions enablement object.
63
*
64
* @param completionsEnablementObject The object containing per-language enablement settings.
65
* @param modeId The language ID to check. Defaults to '*' which checks the global setting.
66
* @returns `true` if completions are enabled for the language, `false` otherwise.
67
*/
68
export function isCompletionsEnabledFromObject(completionsEnablementObject: Record<string, boolean> | undefined, modeId: string = '*'): boolean {
69
if (!isObject(completionsEnablementObject)) {
70
return false; // default to disabled if setting is not available
71
}
72
73
if (typeof completionsEnablementObject[modeId] !== 'undefined') {
74
return Boolean(completionsEnablementObject[modeId]); // go with setting if explicitly defined
75
}
76
77
return Boolean(completionsEnablementObject['*']); // fallback to global setting otherwise
78
}
79
80