enhance: improve command filter (#2492)

This commit is contained in:
Baptiste Augrain
2025-09-03 16:20:23 +02:00
committed by GitHub
parent e648323548
commit f574039dc4

View File

@@ -1,5 +1,5 @@
diff --git a/src/vs/workbench/contrib/commands/common/commands.contribution.ts b/src/vs/workbench/contrib/commands/common/commands.contribution.ts diff --git a/src/vs/workbench/contrib/commands/common/commands.contribution.ts b/src/vs/workbench/contrib/commands/common/commands.contribution.ts
index 3fd6b59..04bb34f 100644 index 3fd6b59..97a0e04 100644
--- a/src/vs/workbench/contrib/commands/common/commands.contribution.ts --- a/src/vs/workbench/contrib/commands/common/commands.contribution.ts
+++ b/src/vs/workbench/contrib/commands/common/commands.contribution.ts +++ b/src/vs/workbench/contrib/commands/common/commands.contribution.ts
@@ -9,2 +9,3 @@ import { Action2, registerAction2 } from '../../../../platform/actions/common/ac @@ -9,2 +9,3 @@ import { Action2, registerAction2 } from '../../../../platform/actions/common/ac
@@ -10,7 +10,7 @@ index 3fd6b59..04bb34f 100644
import { INotificationService } from '../../../../platform/notification/common/notification.js'; import { INotificationService } from '../../../../platform/notification/common/notification.js';
+import { Registry } from '../../../../platform/registry/common/platform.js'; +import { Registry } from '../../../../platform/registry/common/platform.js';
@@ -156,2 +158,31 @@ class RunCommands extends Action2 { @@ -156,2 +158,30 @@ class RunCommands extends Action2 {
+Registry.as<IConfigurationRegistry>('base.contributions.configuration') +Registry.as<IConfigurationRegistry>('base.contributions.configuration')
+ .registerConfiguration({ + .registerConfiguration({
@@ -20,17 +20,16 @@ index 3fd6b59..04bb34f 100644
+ type: 'object', + type: 'object',
+ properties: { + properties: {
+ 'commands.filters': { + 'commands.filters': {
+ enum: ['off', 'on',], + additionalProperties: {
+ enumItemLabels: [ + type: 'string',
+ // nls.localize('ask', "Ask"), + enum: ['ask', 'off', 'on'],
+ nls.localize('off', "Never authorized"), + enumDescriptions: [
+ nls.localize('on', "Always authorized"), + nls.localize('commands.filters.ask', 'Ask the user before executing the command.'),
+ ], + nls.localize('commands.filters.off', 'The command is never authorized.'),
+ enumDescriptions: [ + nls.localize('commands.filters.on', 'The command is always authorized.'),
+ // nls.localize('commands.filters.ask', 'Ask the user before executing the command.'), + ],
+ nls.localize('commands.filters.off', 'The command is never authorized.'), + description: nls.localize('commands.filters.value', "Authorization for the command."),
+ nls.localize('commands.filters.on', 'The command is always authorized.'), + },
+ ],
+ description: nls.localize('commands.filters', "Controls which commands are authorized to be executed."), + description: nls.localize('commands.filters', "Controls which commands are authorized to be executed."),
+ default: { + default: {
+ 'workbench.action.terminal.newLocal': 'off' + 'workbench.action.terminal.newLocal': 'off'
@@ -43,52 +42,78 @@ index 3fd6b59..04bb34f 100644
+ +
registerAction2(RunCommands); registerAction2(RunCommands);
diff --git a/src/vs/workbench/services/commands/common/commandService.ts b/src/vs/workbench/services/commands/common/commandService.ts diff --git a/src/vs/workbench/services/commands/common/commandService.ts b/src/vs/workbench/services/commands/common/commandService.ts
index 93d1631..f21ca94 100644 index 93d1631..0533cf0 100644
--- a/src/vs/workbench/services/commands/common/commandService.ts --- a/src/vs/workbench/services/commands/common/commandService.ts
+++ b/src/vs/workbench/services/commands/common/commandService.ts +++ b/src/vs/workbench/services/commands/common/commandService.ts
@@ -9,2 +9,3 @@ import { Disposable } from '../../../../base/common/lifecycle.js'; @@ -8,3 +8,6 @@ import { Emitter, Event } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
+import Severity from '../../../../base/common/severity.js';
import { CommandsRegistry, ICommandEvent, ICommandService } from '../../../../platform/commands/common/commands.js'; import { CommandsRegistry, ICommandEvent, ICommandService } from '../../../../platform/commands/common/commands.js';
+import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js'; +import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
+import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
@@ -30,3 +31,4 @@ export class CommandService extends Disposable implements ICommandService { @@ -20,2 +23,3 @@ export class CommandService extends Disposable implements ICommandService {
private _starActivation: CancelablePromise<void> | null;
+ private _commandFilters: Record<string, "ask" | "off" | "on">
@@ -30,3 +34,5 @@ export class CommandService extends Disposable implements ICommandService {
@IExtensionService private readonly _extensionService: IExtensionService, @IExtensionService private readonly _extensionService: IExtensionService,
- @ILogService private readonly _logService: ILogService - @ILogService private readonly _logService: ILogService
+ @ILogService private readonly _logService: ILogService, + @ILogService private readonly _logService: ILogService,
+ @IConfigurationService private readonly configurationService: IConfigurationService + @IConfigurationService private readonly _configurationService: IConfigurationService,
+ @IDialogService private readonly _dialogService: IDialogService
) { ) {
@@ -56,2 +58,20 @@ export class CommandService extends Disposable implements ICommandService { @@ -35,2 +41,9 @@ export class CommandService extends Disposable implements ICommandService {
const commandIsRegistered = !!CommandsRegistry.getCommand(id); this._starActivation = null;
+ const commandFilters = this.configurationService.getValue<Record<string, 'ask' | 'off' | 'on'>>('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' }; + this._commandFilters = this._configurationService.getValue('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' };
+ +
+ const filter = commandFilters[id]; + this._configurationService.onDidChangeConfiguration(async (event) => {
+ if (event.affectsConfiguration('commands.filters')) {
+ this._commandFilters = this._configurationService.getValue('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' };
+ }
+ })
}
@@ -57,2 +70,27 @@ export class CommandService extends Disposable implements ICommandService {
+ const filter = this._commandFilters[id];
+ if (filter === 'off') { + if (filter === 'off') {
+ return Promise.reject(new Error(`command '${id}' not authorized`)); + return Promise.reject(new Error(`command '${id}' not authorized`));
+ } + }
+ // if (filter === 'ask') { + else if (filter === 'ask') {
+ // const result = await showWarningMessage( + const { result } = await this._dialogService.prompt({
+ // `Are you sure you want to execute the command "${id}"?`, + type: Severity.Error,
+ // { modal: true }, // this makes the dialog modal (blocks other input) + message: `Are you sure you want to execute the command "${id}"?`,
+ // 'Yes', + buttons: [
+ // 'No' + {
+ // ); + label: 'Yes',
+ run: () => true
+ },
+ {
+ label: 'No',
+ run: () => false
+ }
+ ],
+ });
+ +
+ // if (result === 'No') { + if (!result) {
+ // return Promise.reject(new Error(`command '${id}' not authorized`)); + return Promise.reject(new Error(`command '${id}' not authorized`));
+ // } + }
+ // } + }
+
if (commandIsRegistered) {
diff --git a/src/vs/workbench/services/commands/test/common/commandService.test.ts b/src/vs/workbench/services/commands/test/common/commandService.test.ts diff --git a/src/vs/workbench/services/commands/test/common/commandService.test.ts b/src/vs/workbench/services/commands/test/common/commandService.test.ts
index ca3be11..38b0474 100644 index ca3be11..fb456a3 100644
--- a/src/vs/workbench/services/commands/test/common/commandService.test.ts --- a/src/vs/workbench/services/commands/test/common/commandService.test.ts
+++ b/src/vs/workbench/services/commands/test/common/commandService.test.ts +++ b/src/vs/workbench/services/commands/test/common/commandService.test.ts
@@ -12,2 +12,6 @@ import { NullExtensionService } from '../../../extensions/common/extensions.js'; @@ -12,2 +12,7 @@ import { NullExtensionService } from '../../../extensions/common/extensions.js';
import { CommandService } from '../../common/commandService.js'; import { CommandService } from '../../common/commandService.js';
+import { NullPolicyService } from '../../../../../platform/policy/common/policy.js'; +import { NullPolicyService } from '../../../../../platform/policy/common/policy.js';
+import { FileService } from '../../../../../platform/files/common/fileService.js'; +import { FileService } from '../../../../../platform/files/common/fileService.js';
+import { ConfigurationService } from '../../../../../platform/configuration/common/configurationService.js';
+import { URI } from '../../../../../base/common/uri.js'; +import { URI } from '../../../../../base/common/uri.js';
+import { ConfigurationService } from '../../../../../platform/configuration/common/configurationService.js';
+import { TestDialogService } from '../../../../../platform/dialogs/test/common/testDialogService.js';
@@ -16,4 +20,16 @@ suite('CommandService', function () { @@ -16,4 +21,16 @@ suite('CommandService', function () {
const store = ensureNoDisposablesAreLeakedInTestSuite(); const store = ensureNoDisposablesAreLeakedInTestSuite();
+ const testDisposables = ensureNoDisposablesAreLeakedInTestSuite(); + const testDisposables = ensureNoDisposablesAreLeakedInTestSuite();
+ let nullConfigService: ConfigurationService + let nullConfigService: ConfigurationService
@@ -105,38 +130,38 @@ index ca3be11..38b0474 100644
+ )); + ));
+ +
store.add(CommandsRegistry.registerCommand('foo', function () { })); store.add(CommandsRegistry.registerCommand('foo', function () { }));
@@ -30,3 +46,3 @@ suite('CommandService', function () { @@ -30,3 +47,3 @@ suite('CommandService', function () {
} }
- }, new NullLogService())); - }, new NullLogService()));
+ }, new NullLogService(), nullConfigService)); + }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -50,3 +66,3 @@ suite('CommandService', function () { @@ -50,3 +67,3 @@ suite('CommandService', function () {
- const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService())); - const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService()));
+ const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService)); + const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -68,3 +84,3 @@ suite('CommandService', function () { @@ -68,3 +85,3 @@ suite('CommandService', function () {
} }
- }, new NullLogService())); - }, new NullLogService()));
+ }, new NullLogService(), nullConfigService)); + }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -85,3 +101,3 @@ suite('CommandService', function () { @@ -85,3 +102,3 @@ suite('CommandService', function () {
} }
- }, new NullLogService())); - }, new NullLogService()));
+ }, new NullLogService(), nullConfigService)); + }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -125,3 +141,3 @@ suite('CommandService', function () { @@ -125,3 +142,3 @@ suite('CommandService', function () {
- }, new NullLogService())); - }, new NullLogService()));
+ }, new NullLogService(), nullConfigService)); + }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -166,3 +182,3 @@ suite('CommandService', function () { @@ -166,3 +183,3 @@ suite('CommandService', function () {
- }, new NullLogService())); - }, new NullLogService()));
+ }, new NullLogService(), nullConfigService)); + }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -187,3 +203,3 @@ suite('CommandService', function () { @@ -187,3 +204,3 @@ suite('CommandService', function () {
}; };
- const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService())); - const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService()));
+ const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService)); + const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService, new TestDialogService()));