Path: blob/main/src/vs/workbench/contrib/notebook/browser/services/notebookKeymapServiceImpl.ts
3296 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 { onUnexpectedError } from '../../../../../base/common/errors.js';6import { Event } from '../../../../../base/common/event.js';7import { Disposable } from '../../../../../base/common/lifecycle.js';8import { localize } from '../../../../../nls.js';9import { IInstantiationService, ServicesAccessor } from '../../../../../platform/instantiation/common/instantiation.js';10import { INotificationService, Severity } from '../../../../../platform/notification/common/notification.js';11import { getInstalledExtensions, IExtensionStatus } from '../../../extensions/common/extensionsUtils.js';12import { INotebookKeymapService } from '../../common/notebookKeymapService.js';13import { EnablementState, IWorkbenchExtensionEnablementService } from '../../../../services/extensionManagement/common/extensionManagement.js';14import { ILifecycleService } from '../../../../services/lifecycle/common/lifecycle.js';15import { IExtensionIdentifier, IExtensionManagementService, InstallOperation } from '../../../../../platform/extensionManagement/common/extensionManagement.js';16import { areSameExtensions } from '../../../../../platform/extensionManagement/common/extensionManagementUtil.js';17import { IStorageService, StorageScope, StorageTarget } from '../../../../../platform/storage/common/storage.js';18import { Memento, MementoObject } from '../../../../common/memento.js';19import { distinct } from '../../../../../base/common/arrays.js';2021function onExtensionChanged(accessor: ServicesAccessor): Event<IExtensionIdentifier[]> {22const extensionService = accessor.get(IExtensionManagementService);23const extensionEnablementService = accessor.get(IWorkbenchExtensionEnablementService);24const onDidInstallExtensions = Event.chain(extensionService.onDidInstallExtensions, $ =>25$.filter(e => e.some(({ operation }) => operation === InstallOperation.Install))26.map(e => e.map(({ identifier }) => identifier))27);28return Event.debounce<IExtensionIdentifier[], IExtensionIdentifier[]>(Event.any(29Event.any(onDidInstallExtensions, Event.map(extensionService.onDidUninstallExtension, e => [e.identifier])),30Event.map(extensionEnablementService.onEnablementChanged, extensions => extensions.map(e => e.identifier))31), (result: IExtensionIdentifier[] | undefined, identifiers: IExtensionIdentifier[]) => {32result = result || (identifiers.length ? [identifiers[0]] : []);33for (const identifier of identifiers) {34if (result.some(l => !areSameExtensions(l, identifier))) {35result.push(identifier);36}37}3839return result;40});41}4243const hasRecommendedKeymapKey = 'hasRecommendedKeymap';4445export class NotebookKeymapService extends Disposable implements INotebookKeymapService {46_serviceBrand: undefined;4748private notebookKeymapMemento: Memento;49private notebookKeymap: MementoObject;5051constructor(52@IInstantiationService private readonly instantiationService: IInstantiationService,53@IWorkbenchExtensionEnablementService private readonly extensionEnablementService: IWorkbenchExtensionEnablementService,54@INotificationService private readonly notificationService: INotificationService,55@IStorageService storageService: IStorageService,56@ILifecycleService lifecycleService: ILifecycleService,57) {58super();5960this.notebookKeymapMemento = new Memento('notebookKeymap', storageService);61this.notebookKeymap = this.notebookKeymapMemento.getMemento(StorageScope.PROFILE, StorageTarget.USER);6263this._register(lifecycleService.onDidShutdown(() => this.dispose()));64this._register(this.instantiationService.invokeFunction(onExtensionChanged)((identifiers => {65Promise.all(identifiers.map(identifier => this.checkForOtherKeymaps(identifier)))66.then(undefined, onUnexpectedError);67})));68}6970private checkForOtherKeymaps(extensionIdentifier: IExtensionIdentifier): Promise<void> {71return this.instantiationService.invokeFunction(getInstalledExtensions).then(extensions => {72const keymaps = extensions.filter(extension => isNotebookKeymapExtension(extension));73const extension = keymaps.find(extension => areSameExtensions(extension.identifier, extensionIdentifier));74if (extension && extension.globallyEnabled) {75// there is already a keymap extension76this.notebookKeymap[hasRecommendedKeymapKey] = true;77this.notebookKeymapMemento.saveMemento();78const otherKeymaps = keymaps.filter(extension => !areSameExtensions(extension.identifier, extensionIdentifier) && extension.globallyEnabled);79if (otherKeymaps.length) {80return this.promptForDisablingOtherKeymaps(extension, otherKeymaps);81}82}83return undefined;84});85}8687private promptForDisablingOtherKeymaps(newKeymap: IExtensionStatus, oldKeymaps: IExtensionStatus[]): void {88const onPrompt = (confirmed: boolean) => {89if (confirmed) {90this.extensionEnablementService.setEnablement(oldKeymaps.map(keymap => keymap.local), EnablementState.DisabledGlobally);91}92};9394this.notificationService.prompt(Severity.Info, localize('disableOtherKeymapsConfirmation', "Disable other keymaps ({0}) to avoid conflicts between keybindings?", distinct(oldKeymaps.map(k => k.local.manifest.displayName)).map(name => `'${name}'`).join(', ')),95[{96label: localize('yes', "Yes"),97run: () => onPrompt(true)98}, {99label: localize('no', "No"),100run: () => onPrompt(false)101}]102);103}104}105106export function isNotebookKeymapExtension(extension: IExtensionStatus): boolean {107if (extension.local.manifest.extensionPack) {108return false;109}110111const keywords = extension.local.manifest.keywords;112if (!keywords) {113return false;114}115116return keywords.indexOf('notebook-keymap') !== -1;117}118119120