src/vs/platform/keybinding/common/keybindingsRegistry.ts
274 LOC · 127 covered · 147 uncovered · 9 ranges · 200 concepts · 1 introducers · 184 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
actions.ts ×20
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { decodeKeybinding, Keybinding } from '../../../base/common/keybindings.js';
import { OperatingSystem, OS } from '../../../base/common/platform.js';
import { CommandsRegistry, ICommandHandler, ICommandMetadata } from '../../commands/common/commands.js';
import { ContextKeyExpression } from '../../contextkey/common/contextkey.js';
import { Registry } from '../../registry/common/platform.js';
import { combinedDisposable, DisposableStore, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
import { LinkedList } from '../../../base/common/linkedList.js';
export interface IKeybindingItem {
keybinding: Keybinding | null;
command: string | null;
commandArgs?: any;
when: ContextKeyExpression | null | undefined;
weight1: number;
weight2: number;
extensionId: string | null;
isBuiltinExtension: boolean;
}
export interface IKeybindings {
primary?: number;
secondary?: number[];
win?: {
primary: number;
secondary?: number[];
};
linux?: {
primary: number;
secondary?: number[];
};
mac?: {
primary: number;
secondary?: number[];
};
}
export interface IKeybindingRule extends IKeybindings {
id: string;
weight: number;
args?: any;
/**
* Keybinding is disabled if expression returns false.
*/
when?: ContextKeyExpression | null | undefined;
}
export interface IExtensionKeybindingRule {
keybinding: Keybinding | null;
id: string;
args?: any;
weight: number;
when: ContextKeyExpression | undefined;
extensionId?: string;
isBuiltinExtension?: boolean;
}
export const enum KeybindingWeight {
EditorCore = 0,
EditorContrib = 100,
WorkbenchContrib = 200,
SessionsContrib = 250,
BuiltinExtension = 300,
ExternalExtension = 400
}
export interface ICommandAndKeybindingRule<Args extends unknown[] = unknown[]> extends IKeybindingRule {
handler: ICommandHandler<Args>;
metadata?: ICommandMetadata | null;
}
export interface IKeybindingsRegistry {
registerKeybindingRule(rule: IKeybindingRule): IDisposable;
setExtensionKeybindings(rules: IExtensionKeybindingRule[]): void;
registerCommandAndKeybindingRule<Args extends unknown[] = unknown[]>(desc: ICommandAndKeybindingRule<Args>): IDisposable;
getDefaultKeybindings(): IKeybindingItem[];
getDefaultKeybindingsForOS(os: OperatingSystem): IKeybindingItem[];
}
/**
* Stores all built-in and extension-provided keybindings (but not ones that user defines themselves)
*/
class KeybindingsRegistryImpl implements IKeybindingsRegistry {
private _coreKeybindings: LinkedList<IKeybindingItem>;
private _coreKeybindingRules: LinkedList<IKeybindingRule>;
private _extensionKeybindings: IKeybindingItem[];
private _cachedMergedKeybindings: IKeybindingItem[] | null;
constructor() {
this._coreKeybindings = new LinkedList();
this._coreKeybindingRules = new LinkedList();
this._extensionKeybindings = [];
this._cachedMergedKeybindings = null;
}
private static bindToPlatform(kb: IKeybindings, os: OperatingSystem): { primary?: number; secondary?: number[] } {
if (os === OperatingSystem.Windows) {
if (kb && kb.win) {
return kb.win;
}
} else if (os === OperatingSystem.Macintosh) {
if (kb && kb.mac) {
return kb.mac;
}
} else {
if (kb && kb.linux) {
return kb.linux;
}
}
return kb;
}
/**
* Take current platform into account and reduce to primary & secondary.
*/
private static bindToCurrentPlatform(kb: IKeybindings): { primary?: number; secondary?: number[] } {
return KeybindingsRegistryImpl.bindToPlatform(kb, OS);
}
public registerKeybindingRule(rule: IKeybindingRule): IDisposable {
const actualKb = KeybindingsRegistryImpl.bindToCurrentPlatform(rule);
const result = new DisposableStore();
if (actualKb && actualKb.primary) {
const kk = decodeKeybinding(actualKb.primary, OS);
if (kk) {
result.add(this._registerDefaultKeybinding(kk, rule.id, rule.args, rule.weight, 0, rule.when));
}
}
if (actualKb && Array.isArray(actualKb.secondary)) {
for (let i = 0, len = actualKb.secondary.length; i < len; i++) {
const k = actualKb.secondary[i];
const kk = decodeKeybinding(k, OS);
if (kk) {
result.add(this._registerDefaultKeybinding(kk, rule.id, rule.args, rule.weight, -i - 1, rule.when));
}
}
}
const removeRule = this._coreKeybindingRules.push(rule);
result.add(toDisposable(() => { removeRule(); }));
return result;
}
public setExtensionKeybindings(rules: IExtensionKeybindingRule[]): void {
const result: IKeybindingItem[] = [];
let keybindingsLen = 0;
for (const rule of rules) {
if (rule.keybinding) {
result[keybindingsLen++] = {
keybinding: rule.keybinding,
command: rule.id,
commandArgs: rule.args,
when: rule.when,
weight1: rule.weight,
weight2: 0,
extensionId: rule.extensionId || null,
isBuiltinExtension: rule.isBuiltinExtension || false
};
}
}
this._extensionKeybindings = result;
this._cachedMergedKeybindings = null;
}
public registerCommandAndKeybindingRule(desc: ICommandAndKeybindingRule): IDisposable {
return combinedDisposable(
this.registerKeybindingRule(desc),
CommandsRegistry.registerCommand(desc)
);
}
private _registerDefaultKeybinding(keybinding: Keybinding, commandId: string, commandArgs: any, weight1: number, weight2: number, when: ContextKeyExpression | null | undefined): IDisposable {
const remove = this._coreKeybindings.push({
keybinding: keybinding,
command: commandId,
commandArgs: commandArgs,
when: when,
weight1: weight1,
weight2: weight2,
extensionId: null,
isBuiltinExtension: false
});
this._cachedMergedKeybindings = null;
return toDisposable(() => {
remove();
this._cachedMergedKeybindings = null;
});
}
public getDefaultKeybindings(): IKeybindingItem[] {
if (!this._cachedMergedKeybindings) {
this._cachedMergedKeybindings = Array.from(this._coreKeybindings).concat(this._extensionKeybindings);
this._cachedMergedKeybindings.sort(sorter);
}
return this._cachedMergedKeybindings.slice(0);
}
public getDefaultKeybindingsForOS(os: OperatingSystem): IKeybindingItem[] {
const result: IKeybindingItem[] = [];
for (const rule of this._coreKeybindingRules) {
const actualKb = KeybindingsRegistryImpl.bindToPlatform(rule, os);
if (actualKb && actualKb.primary) {
const kk = decodeKeybinding(actualKb.primary, os);
if (kk) {
result.push({
keybinding: kk,
command: rule.id,
commandArgs: rule.args,
when: rule.when,
weight1: rule.weight,
weight2: 0,
extensionId: null,
isBuiltinExtension: false
});
}
}
if (actualKb && Array.isArray(actualKb.secondary)) {
for (let i = 0, len = actualKb.secondary.length; i < len; i++) {
const k = actualKb.secondary[i];
const kk = decodeKeybinding(k, os);
if (kk) {
result.push({
keybinding: kk,
command: rule.id,
commandArgs: rule.args,
when: rule.when,
weight1: rule.weight,
weight2: -i - 1,
extensionId: null,
isBuiltinExtension: false
});
}
}
}
}
result.sort(sorter);
return result;
}
export const KeybindingsRegistry: IKeybindingsRegistry = new KeybindingsRegistryImpl();
// Define extension point ids
export const Extensions = {
EditorModes: 'platform.keybindingsRegistry'
};
Registry.add(Extensions.EditorModes, KeybindingsRegistry);
function sorter(a: IKeybindingItem, b: IKeybindingItem): number {
if (a.weight1 !== b.weight1) {
return a.weight1 - b.weight1;
}
if (a.command && b.command) {
if (a.command < b.command) {
return -1;
}
if (a.command > b.command) {
return 1;
}
}
return a.weight2 - b.weight2;
}