/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; import * as nls from 'vs/nls'; import URI from 'vs/base/common/uri'; import { Registry } from 'vs/platform/platform'; import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actionRegistry'; import { EditorInput, IEditorRegistry, Extensions as EditorExtensions, IEditorInputFactory } from 'vs/workbench/common/editor'; import { EditorDescriptor } from 'vs/workbench/browser/parts/editor/baseEditor'; import { SyncActionDescriptor } from 'vs/platform/actions/common/actions'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes'; import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; import { DefaultPreferencesEditorInput, PreferencesEditor, PreferencesEditorInput } from 'vs/workbench/parts/preferences/browser/preferencesEditor'; import { KeybindingsEditor, KeybindingsEditorInput } from 'vs/workbench/parts/preferences/browser/keybindingsEditor'; import { OpenGlobalSettingsAction, OpenGlobalKeybindingsAction, OpenWorkspaceSettingsAction, ConfigureLanguageBasedSettingsAction, DefineKeybindingAction, SearchKeybindingAction } from 'vs/workbench/parts/preferences/browser/preferencesActions'; import { IPreferencesService, CONTEXT_KEYBINDING_FOCUS, CONTEXT_KEYBINDINGS_EDITOR } from 'vs/workbench/parts/preferences/common/preferences'; import { PreferencesService } from 'vs/workbench/parts/preferences/browser/preferencesService'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions'; import { PreferencesContentProvider } from 'vs/workbench/parts/preferences/common/preferencesContentProvider'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; registerSingleton(IPreferencesService, PreferencesService); Registry.as(EditorExtensions.Editors).registerEditor( new EditorDescriptor( PreferencesEditor.ID, nls.localize('defaultPreferencesEditor', "Default Preferences Editor"), 'vs/workbench/parts/preferences/browser/preferencesEditor', 'PreferencesEditor' ), [ new SyncDescriptor(PreferencesEditorInput) ] ); Registry.as(EditorExtensions.Editors).registerEditor( new EditorDescriptor( KeybindingsEditor.ID, nls.localize('keybindingsEditor', "Keybindings Editor"), 'vs/workbench/parts/preferences/browser/keybindingsEditor', 'KeybindingsEditor' ), [ new SyncDescriptor(KeybindingsEditorInput) ] ); interface ISerializedPreferencesEditorInput { name: string; description: string; detailsSerialized: string; masterSerialized: string; detailsTypeId: string; masterTypeId: string; } // Register Preferences Editor Input Factory class PreferencesEditorInputFactory implements IEditorInputFactory { public serialize(editorInput: EditorInput): string { const input = editorInput; if (input.details && input.master) { const registry = Registry.as(EditorExtensions.Editors); const detailsInputFactory = registry.getEditorInputFactory(input.details.getTypeId()); const masterInputFactory = registry.getEditorInputFactory(input.master.getTypeId()); if (detailsInputFactory && masterInputFactory) { const detailsSerialized = detailsInputFactory.serialize(input.details); const masterSerialized = masterInputFactory.serialize(input.master); if (detailsSerialized && masterSerialized) { return JSON.stringify({ name: input.getName(), description: input.getDescription(), detailsSerialized, masterSerialized, detailsTypeId: input.details.getTypeId(), masterTypeId: input.master.getTypeId() }); } } } return null; } public deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput { const deserialized: ISerializedPreferencesEditorInput = JSON.parse(serializedEditorInput); const registry = Registry.as(EditorExtensions.Editors); const detailsInputFactory = registry.getEditorInputFactory(deserialized.detailsTypeId); const masterInputFactory = registry.getEditorInputFactory(deserialized.masterTypeId); if (detailsInputFactory && masterInputFactory) { const detailsInput = detailsInputFactory.deserialize(instantiationService, deserialized.detailsSerialized); const masterInput = masterInputFactory.deserialize(instantiationService, deserialized.masterSerialized); if (detailsInput && masterInput) { return new PreferencesEditorInput(deserialized.name, deserialized.description, detailsInput, masterInput); } } return null; } } class KeybindingsEditorInputFactory implements IEditorInputFactory { public serialize(editorInput: EditorInput): string { const input = editorInput; return JSON.stringify({ name: input.getName(), typeId: input.getTypeId() }); } public deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput { return instantiationService.createInstance(KeybindingsEditorInput); } } interface ISerializedDefaultPreferencesEditorInput { resource: string; } // Register Default Preferences Editor Input Factory class DefaultPreferencesEditorInputFactory implements IEditorInputFactory { public serialize(editorInput: EditorInput): string { const input = editorInput; const serialized: ISerializedDefaultPreferencesEditorInput = { resource: input.getResource().toString() }; return JSON.stringify(serialized); } public deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput { const deserialized: ISerializedDefaultPreferencesEditorInput = JSON.parse(serializedEditorInput); return instantiationService.createInstance(DefaultPreferencesEditorInput, URI.parse(deserialized.resource)); } } Registry.as(EditorExtensions.Editors).registerEditorInputFactory(PreferencesEditorInput.ID, PreferencesEditorInputFactory); Registry.as(EditorExtensions.Editors).registerEditorInputFactory(DefaultPreferencesEditorInput.ID, DefaultPreferencesEditorInputFactory); Registry.as(EditorExtensions.Editors).registerEditorInputFactory(KeybindingsEditorInput.ID, KeybindingsEditorInputFactory); // Contribute Global Actions const category = nls.localize('preferences', "Preferences"); const registry = Registry.as(Extensions.WorkbenchActions); registry.registerWorkbenchAction(new SyncActionDescriptor(OpenGlobalSettingsAction, OpenGlobalSettingsAction.ID, OpenGlobalSettingsAction.LABEL, { primary: null, mac: { primary: KeyMod.CtrlCmd | KeyCode.US_COMMA } }), 'Preferences: Open User Settings', category); registry.registerWorkbenchAction(new SyncActionDescriptor(OpenWorkspaceSettingsAction, OpenWorkspaceSettingsAction.ID, OpenWorkspaceSettingsAction.LABEL), 'Preferences: Open Workspace Settings', category); registry.registerWorkbenchAction(new SyncActionDescriptor(OpenGlobalKeybindingsAction, OpenGlobalKeybindingsAction.ID, OpenGlobalKeybindingsAction.LABEL, { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_S) }), 'Preferences: Open Keyboard Shortcuts', category); registry.registerWorkbenchAction(new SyncActionDescriptor(ConfigureLanguageBasedSettingsAction, ConfigureLanguageBasedSettingsAction.ID, ConfigureLanguageBasedSettingsAction.LABEL), 'Preferences: Configure Language Specific Settings', category); registry.registerWorkbenchAction(new SyncActionDescriptor(SearchKeybindingAction, SearchKeybindingAction.ID, SearchKeybindingAction.LABEL, { primary: KeyMod.CtrlCmd | KeyCode.KEY_F, }, CONTEXT_KEYBINDINGS_EDITOR), ''); registry.registerWorkbenchAction(new SyncActionDescriptor(DefineKeybindingAction, DefineKeybindingAction.ID, DefineKeybindingAction.LABEL, { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_K), }, ContextKeyExpr.and(CONTEXT_KEYBINDINGS_EDITOR, CONTEXT_KEYBINDING_FOCUS)), ''); Registry.as(WorkbenchExtensions.Workbench).registerWorkbenchContribution(PreferencesContentProvider);