src/vs/base/common/actions.ts

294 LOC · 173 covered · 121 uncovered · 28 ranges · 2384 concepts · 4 introducers · 1350 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.

1 > /*--------------------------------------------------------------------------------------------- actions.ts ×25
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 { Emitter, Event } from './event.js';
7 > import { Disposable, IDisposable } from './lifecycle.js';
8 > import * as nls from '../../nls.js';
9 >
10 > export interface ITelemetryData {
11 > readonly from?: string;
12 > readonly target?: string;
13 > [key: string]: unknown;
14 > }
15 >
16 > export type WorkbenchActionExecutedClassification = {
17 > id: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The identifier of the action that was run.' };
18 > from: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The name of the component the action was run from.' };
19 > detail?: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'Optional details about how the action was run, e.g which keybinding was used.' };
20 > owner: 'isidorn';
21 > comment: 'Provides insight into actions that are executed within the workbench.';
22 > };
23 >
24 > export type WorkbenchActionExecutedEvent = {
25 > id: string;
26 > from: string;
27 > detail?: string;
28 > };
29 >
30 > export interface IAction {
31 > readonly id: string;
32 > label: string;
33 > tooltip: string;
34 > class: string | undefined;
35 > enabled: boolean;
36 > checked?: boolean;
37 > run(...args: unknown[]): unknown;
38 > }
39 >
40 > export interface IActionRunner extends IDisposable {
41 > readonly onDidRun: Event<IRunEvent>;
42 > readonly onWillRun: Event<IRunEvent>;
43 >
44 > run(action: IAction, context?: unknown): unknown;
45 > }
46 >
47 > export interface IActionChangeEvent {
48 > readonly label?: string;
49 > readonly tooltip?: string;
50 > readonly class?: string;
51 > readonly enabled?: boolean;
52 > readonly checked?: boolean;
53 > }
54 >
55 > /**
56 > * A concrete implementation of {@link IAction}.
57 > *
58 > * Note that in most cases you should use the lighter-weight {@linkcode toAction} function instead.
59 > */
60 > export class Action extends Disposable implements IAction {
61 >
62 > protected _onDidChange = this._register(new Emitter<IActionChangeEvent>());
63 > get onDidChange() { return this._onDidChange.event; }
64 >
65 > protected readonly _id: string;
66 > protected _label: string;
67 > protected _tooltip: string | undefined;
68 > protected _cssClass: string | undefined;
69 > protected _enabled: boolean = true;
70 > protected _checked?: boolean;
71 > protected readonly _actionCallback?: (event?: unknown) => unknown;
72 >
73 > constructor(id: string, label: string = '', cssClass: string = '', enabled: boolean = true, actionCallback?: (event?: unknown) => unknown) {
74 > super(); notifications.ts ×25
75 > this._id = id;
76 > this._label = label;
77 > this._cssClass = cssClass;
78 > this._enabled = enabled;
79 > this._actionCallback = actionCallback;
80 > }
82 > get id(): string {
83 return this._id;
84 }
86 > get label(): string {
87 return this._label;
88 }
90 > set label(value: string) {
91 this._setLabel(value);
92 }
94 > private _setLabel(value: string): void {
95 if (this._label !== value) {
96 this._label = value;
97 this._onDidChange.fire({ label: value });
98 }
99 }
101 > get tooltip(): string {
102 return this._tooltip || '';
103 }
105 > set tooltip(value: string) {
106 this._setTooltip(value);
107 }
109 > protected _setTooltip(value: string): void {
110 if (this._tooltip !== value) {
111 this._tooltip = value;
112 this._onDidChange.fire({ tooltip: value });
113 }
114 }
116 > get class(): string | undefined {
117 return this._cssClass;
118 }
120 > set class(value: string | undefined) {
121 this._setClass(value);
122 }
124 > protected _setClass(value: string | undefined): void {
125 if (this._cssClass !== value) {
126 this._cssClass = value;
127 this._onDidChange.fire({ class: value });
128 }
129 }
131 > get enabled(): boolean {
132 return this._enabled;
133 }
135 > set enabled(value: boolean) {
136 this._setEnabled(value);
137 }
139 > protected _setEnabled(value: boolean): void {
140 if (this._enabled !== value) {
141 this._enabled = value;
142 this._onDidChange.fire({ enabled: value });
143 }
144 }
146 > get checked(): boolean | undefined {
147 return this._checked;
148 }
150 > set checked(value: boolean | undefined) {
151 this._setChecked(value);
152 }
154 > protected _setChecked(value: boolean | undefined): void {
155 if (this._checked !== value) {
156 this._checked = value;
157 this._onDidChange.fire({ checked: value });
158 }
159 }
161 > async run(event?: unknown, data?: ITelemetryData): Promise<void> {
162 if (this._actionCallback) {
163 await this._actionCallback(event);
164 }
165 }
166 > } actions.ts ×25
167 >
168 > export interface IRunEvent {
169 > readonly action: IAction;
170 > readonly error?: Error;
171 > }
172 >
173 > export class ActionRunner extends Disposable implements IActionRunner {
174
175 private readonly _onWillRun = this._register(new Emitter<IRunEvent>());
176 get onWillRun() { return this._onWillRun.event; }
177
178 private readonly _onDidRun = this._register(new Emitter<IRunEvent>());
179 > get onDidRun() { return this._onDidRun.event; } actions.ts ×25
180 >
181 > async run(action: IAction, context?: unknown): Promise<void> {
182 if (!action.enabled) {
183 return;
184 }
185
186 this._onWillRun.fire({ action });
187
188 let error: Error | undefined = undefined;
189 try {
190 await this.runAction(action, context);
191 } catch (e) {
192 error = e;
193 }
194
195 this._onDidRun.fire({ action, error });
196 }
198 > protected async runAction(action: IAction, context?: unknown): Promise<void> {
199 await action.run(context);
200 }
201 > } actions.ts ×25
202 >
203 > export class Separator implements IAction {
204
205 /**
206 * Joins all non-empty lists of actions with separators.
207 */
208 public static join(...actionLists: readonly IAction[][]) {
209 let out: IAction[] = [];
210 for (const list of actionLists) {
211 if (!list.length) {
212 // skip
213 } else if (out.length) {
214 out = [...out, new Separator(), ...list];
215 } else {
216 out = list;
217 }
218 }
219
220 return out;
221 }
222
223 /**
224 * Removes leading, trailing, and consecutive duplicate separators in-place and returns the actions.
225 */
226 public static clean(actions: IAction[]): IAction[] {
227 while (actions.length > 0 && actions[0].id === Separator.ID) {
228 actions.shift();
229 }
230 while (actions.length > 0 && actions[actions.length - 1].id === Separator.ID) {
231 actions.pop();
232 }
233 for (let i = actions.length - 2; i >= 0; i--) {
234 if (actions[i].id === Separator.ID && actions[i + 1].id === Separator.ID) {
235 actions.splice(i + 1, 1);
236 }
237 }
238 return actions;
239 }
240
241 static readonly ID = 'vs.actions.separator';
242
243 readonly id: string = Separator.ID;
244
245 readonly label: string = '';
246 readonly tooltip: string = '';
247 readonly class: string = 'separator';
248 readonly enabled: boolean = false;
249 readonly checked: undefined = undefined;
250 > async run() { } actions.ts ×25
251 > }
252 >
253 > export class SubmenuAction implements IAction {
254 >
255 > readonly id: string;
256 > readonly label: string;
257 > readonly class: string | undefined;
258 > readonly tooltip: string = '';
259 > readonly enabled: boolean = true;
260 > readonly checked: undefined = undefined;
261 >
262 > private readonly _actions: readonly IAction[];
263 > get actions(): readonly IAction[] { return this._actions; }
264 >
265 > constructor(id: string, label: string, actions: readonly IAction[], cssClass?: string) {
266 > this.id = id; actions.ts ×1
267 > this.label = label;
268 > this.class = cssClass;
269 > this._actions = actions;
270 > }
272 > async run(): Promise<void> { }
273 > }
274 >
275 > export class EmptySubmenuAction extends Action {
276 >
277 > static readonly ID = 'vs.actions.empty';
278 >
279 > constructor() {
280 super(EmptySubmenuAction.ID, nls.localize('submenu.empty', '(empty)'), undefined, false);
281 }
282 > } actions.ts ×25
283 >
284 > export function toAction(props: { id: string; label: string; tooltip?: string; enabled?: boolean; checked?: boolean; class?: string; run: Function }): IAction {
285 > return { menuService.ts ×29
286 > id: props.id,
287 > label: props.label,
288 > tooltip: props.tooltip ?? props.label,
289 > class: props.class,
290 > enabled: props.enabled ?? true,
291 > checked: props.checked,
292 > run: async (...args: unknown[]) => props.run(...args),
293 > };
294 > }