Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/contextkey/browser/contextKeyService.ts
5240 views
1
/*---------------------------------------------------------------------------------------------
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, PauseableEmitter } from '../../../base/common/event.js';
7
import { Iterable } from '../../../base/common/iterator.js';
8
import { Disposable, DisposableStore, IDisposable, MutableDisposable } from '../../../base/common/lifecycle.js';
9
import { MarshalledObject } from '../../../base/common/marshalling.js';
10
import { MarshalledId } from '../../../base/common/marshallingIds.js';
11
import { cloneAndChange, distinct, equals } from '../../../base/common/objects.js';
12
import { TernarySearchTree } from '../../../base/common/ternarySearchTree.js';
13
import { URI } from '../../../base/common/uri.js';
14
import { localize } from '../../../nls.js';
15
import { CommandsRegistry } from '../../commands/common/commands.js';
16
import { ConfigurationTarget, IConfigurationService } from '../../configuration/common/configuration.js';
17
import { ContextKeyExpression, ContextKeyInfo, ContextKeyValue, IContext, IContextKey, IContextKeyChangeEvent, IContextKeyService, IContextKeyServiceTarget, IReadableSet, IScopedContextKeyService, RawContextKey } from '../common/contextkey.js';
18
import { ServicesAccessor } from '../../instantiation/common/instantiation.js';
19
import { InputFocusedContext } from '../common/contextkeys.js';
20
import { mainWindow } from '../../../base/browser/window.js';
21
import { addDisposableListener, EventType, getActiveWindow, isEditableElement, onDidRegisterWindow, trackFocus } from '../../../base/browser/dom.js';
22
23
const KEYBINDING_CONTEXT_ATTR = 'data-keybinding-context';
24
25
export class Context implements IContext {
26
27
protected _parent: Context | null;
28
protected _value: Record<string, any>;
29
protected _id: number;
30
31
constructor(id: number, parent: Context | null) {
32
this._id = id;
33
this._parent = parent;
34
this._value = Object.create(null);
35
this._value['_contextId'] = id;
36
}
37
38
public get value(): Record<string, any> {
39
return { ...this._value };
40
}
41
42
public setValue(key: string, value: any): boolean {
43
// console.log('SET ' + key + ' = ' + value + ' ON ' + this._id);
44
if (!equals(this._value[key], value)) {
45
this._value[key] = value;
46
return true;
47
}
48
return false;
49
}
50
51
public removeValue(key: string): boolean {
52
// console.log('REMOVE ' + key + ' FROM ' + this._id);
53
if (key in this._value) {
54
delete this._value[key];
55
return true;
56
}
57
return false;
58
}
59
60
public getValue<T>(key: string): T | undefined {
61
const ret = this._value[key];
62
if (typeof ret === 'undefined' && this._parent) {
63
return this._parent.getValue<T>(key);
64
}
65
return ret;
66
}
67
68
public updateParent(parent: Context): void {
69
this._parent = parent;
70
}
71
72
public collectAllValues(): Record<string, any> {
73
let result = this._parent ? this._parent.collectAllValues() : Object.create(null);
74
result = { ...result, ...this._value };
75
delete result['_contextId'];
76
return result;
77
}
78
}
79
80
class NullContext extends Context {
81
82
static readonly INSTANCE = new NullContext();
83
84
constructor() {
85
super(-1, null);
86
}
87
88
public override setValue(key: string, value: any): boolean {
89
return false;
90
}
91
92
public override removeValue(key: string): boolean {
93
return false;
94
}
95
96
public override getValue<T>(key: string): T | undefined {
97
return undefined;
98
}
99
100
override collectAllValues(): { [key: string]: any } {
101
return Object.create(null);
102
}
103
}
104
105
class ConfigAwareContextValuesContainer extends Context {
106
private static readonly _keyPrefix = 'config.';
107
108
private readonly _values = TernarySearchTree.forConfigKeys<any>();
109
private readonly _listener: IDisposable;
110
111
constructor(
112
id: number,
113
private readonly _configurationService: IConfigurationService,
114
emitter: Emitter<IContextKeyChangeEvent>
115
) {
116
super(id, null);
117
118
this._listener = this._configurationService.onDidChangeConfiguration(event => {
119
if (event.source === ConfigurationTarget.DEFAULT) {
120
// new setting, reset everything
121
const allKeys = Array.from(this._values, ([k]) => k);
122
this._values.clear();
123
emitter.fire(new ArrayContextKeyChangeEvent(allKeys));
124
} else {
125
const changedKeys: string[] = [];
126
for (const configKey of event.affectedKeys) {
127
const contextKey = `config.${configKey}`;
128
129
const cachedItems = this._values.findSuperstr(contextKey);
130
if (cachedItems !== undefined) {
131
changedKeys.push(...Iterable.map(cachedItems, ([key]) => key));
132
this._values.deleteSuperstr(contextKey);
133
}
134
135
if (this._values.has(contextKey)) {
136
changedKeys.push(contextKey);
137
this._values.delete(contextKey);
138
}
139
}
140
141
emitter.fire(new ArrayContextKeyChangeEvent(changedKeys));
142
}
143
});
144
}
145
146
dispose(): void {
147
this._listener.dispose();
148
}
149
150
override getValue(key: string): any {
151
152
if (key.indexOf(ConfigAwareContextValuesContainer._keyPrefix) !== 0) {
153
return super.getValue(key);
154
}
155
156
if (this._values.has(key)) {
157
return this._values.get(key);
158
}
159
160
const configKey = key.substr(ConfigAwareContextValuesContainer._keyPrefix.length);
161
const configValue = this._configurationService.getValue(configKey);
162
let value: any = undefined;
163
switch (typeof configValue) {
164
case 'number':
165
case 'boolean':
166
case 'string':
167
value = configValue;
168
break;
169
default:
170
if (Array.isArray(configValue)) {
171
value = JSON.stringify(configValue);
172
} else {
173
value = configValue;
174
}
175
}
176
177
this._values.set(key, value);
178
return value;
179
}
180
181
override setValue(key: string, value: any): boolean {
182
return super.setValue(key, value);
183
}
184
185
override removeValue(key: string): boolean {
186
return super.removeValue(key);
187
}
188
189
override collectAllValues(): { [key: string]: any } {
190
const result: { [key: string]: any } = Object.create(null);
191
this._values.forEach((value, index) => result[index] = value);
192
return { ...result, ...super.collectAllValues() };
193
}
194
}
195
196
class ContextKey<T extends ContextKeyValue> implements IContextKey<T> {
197
198
private _service: AbstractContextKeyService;
199
private _key: string;
200
private _defaultValue: T | undefined;
201
202
constructor(service: AbstractContextKeyService, key: string, defaultValue: T | undefined) {
203
this._service = service;
204
this._key = key;
205
this._defaultValue = defaultValue;
206
this.reset();
207
}
208
209
public set(value: T): void {
210
this._service.setContext(this._key, value);
211
}
212
213
public reset(): void {
214
if (typeof this._defaultValue === 'undefined') {
215
this._service.removeContext(this._key);
216
} else {
217
this._service.setContext(this._key, this._defaultValue);
218
}
219
}
220
221
public get(): T | undefined {
222
return this._service.getContextKeyValue<T>(this._key);
223
}
224
}
225
226
class SimpleContextKeyChangeEvent implements IContextKeyChangeEvent {
227
constructor(readonly key: string) { }
228
affectsSome(keys: IReadableSet<string>): boolean {
229
return keys.has(this.key);
230
}
231
allKeysContainedIn(keys: IReadableSet<string>): boolean {
232
return this.affectsSome(keys);
233
}
234
}
235
236
class ArrayContextKeyChangeEvent implements IContextKeyChangeEvent {
237
constructor(readonly keys: string[]) { }
238
affectsSome(keys: IReadableSet<string>): boolean {
239
for (const key of this.keys) {
240
if (keys.has(key)) {
241
return true;
242
}
243
}
244
return false;
245
}
246
allKeysContainedIn(keys: IReadableSet<string>): boolean {
247
return this.keys.every(key => keys.has(key));
248
}
249
}
250
251
class CompositeContextKeyChangeEvent implements IContextKeyChangeEvent {
252
constructor(readonly events: IContextKeyChangeEvent[]) { }
253
affectsSome(keys: IReadableSet<string>): boolean {
254
for (const e of this.events) {
255
if (e.affectsSome(keys)) {
256
return true;
257
}
258
}
259
return false;
260
}
261
allKeysContainedIn(keys: IReadableSet<string>): boolean {
262
return this.events.every(evt => evt.allKeysContainedIn(keys));
263
}
264
}
265
266
function allEventKeysInContext(event: IContextKeyChangeEvent, context: Record<string, any>): boolean {
267
return event.allKeysContainedIn(new Set(Object.keys(context)));
268
}
269
270
export abstract class AbstractContextKeyService extends Disposable implements IContextKeyService {
271
declare _serviceBrand: undefined;
272
273
protected _isDisposed: boolean;
274
protected _myContextId: number;
275
276
protected _onDidChangeContext = this._register(new PauseableEmitter<IContextKeyChangeEvent>({ merge: input => new CompositeContextKeyChangeEvent(input) }));
277
get onDidChangeContext() { return this._onDidChangeContext.event; }
278
279
constructor(myContextId: number) {
280
super();
281
this._isDisposed = false;
282
this._myContextId = myContextId;
283
}
284
285
public get contextId(): number {
286
return this._myContextId;
287
}
288
289
public createKey<T extends ContextKeyValue>(key: string, defaultValue: T | undefined): IContextKey<T> {
290
if (this._isDisposed) {
291
throw new Error(`AbstractContextKeyService has been disposed`);
292
}
293
return new ContextKey(this, key, defaultValue);
294
}
295
296
297
bufferChangeEvents(callback: Function): void {
298
this._onDidChangeContext.pause();
299
try {
300
callback();
301
} finally {
302
this._onDidChangeContext.resume();
303
}
304
}
305
306
public createScoped(domNode: IContextKeyServiceTarget): IScopedContextKeyService {
307
if (this._isDisposed) {
308
throw new Error(`AbstractContextKeyService has been disposed`);
309
}
310
return new ScopedContextKeyService(this, domNode);
311
}
312
313
createOverlay(overlay: Iterable<[string, any]> = Iterable.empty()): IContextKeyService {
314
if (this._isDisposed) {
315
throw new Error(`AbstractContextKeyService has been disposed`);
316
}
317
return new OverlayContextKeyService(this, overlay);
318
}
319
320
public contextMatchesRules(rules: ContextKeyExpression | undefined): boolean {
321
if (this._isDisposed) {
322
throw new Error(`AbstractContextKeyService has been disposed`);
323
}
324
const context = this.getContextValuesContainer(this._myContextId);
325
const result = (rules ? rules.evaluate(context) : true);
326
// console.group(rules.serialize() + ' -> ' + result);
327
// rules.keys().forEach(key => { console.log(key, ctx[key]); });
328
// console.groupEnd();
329
return result;
330
}
331
332
public getContextKeyValue<T>(key: string): T | undefined {
333
if (this._isDisposed) {
334
return undefined;
335
}
336
return this.getContextValuesContainer(this._myContextId).getValue<T>(key);
337
}
338
339
public setContext(key: string, value: any): void {
340
if (this._isDisposed) {
341
return;
342
}
343
const myContext = this.getContextValuesContainer(this._myContextId);
344
if (!myContext) {
345
return;
346
}
347
if (myContext.setValue(key, value)) {
348
this._onDidChangeContext.fire(new SimpleContextKeyChangeEvent(key));
349
}
350
}
351
352
public removeContext(key: string): void {
353
if (this._isDisposed) {
354
return;
355
}
356
if (this.getContextValuesContainer(this._myContextId).removeValue(key)) {
357
this._onDidChangeContext.fire(new SimpleContextKeyChangeEvent(key));
358
}
359
}
360
361
public getContext(target: IContextKeyServiceTarget | null): IContext {
362
if (this._isDisposed) {
363
return NullContext.INSTANCE;
364
}
365
return this.getContextValuesContainer(findContextAttr(target));
366
}
367
368
public abstract getContextValuesContainer(contextId: number): Context;
369
public abstract createChildContext(parentContextId?: number): number;
370
public abstract disposeContext(contextId: number): void;
371
public abstract updateParent(parentContextKeyService?: IContextKeyService): void;
372
373
public override dispose(): void {
374
super.dispose();
375
this._isDisposed = true;
376
}
377
}
378
379
export class ContextKeyService extends AbstractContextKeyService implements IContextKeyService {
380
381
private _lastContextId: number;
382
private readonly _contexts = new Map<number, Context>();
383
384
private inputFocusedContext: IContextKey<boolean>;
385
386
constructor(@IConfigurationService configurationService: IConfigurationService) {
387
super(0);
388
this._lastContextId = 0;
389
this.inputFocusedContext = InputFocusedContext.bindTo(this);
390
391
const myContext = this._register(new ConfigAwareContextValuesContainer(this._myContextId, configurationService, this._onDidChangeContext));
392
this._contexts.set(this._myContextId, myContext);
393
394
// Uncomment this to see the contexts continuously logged
395
// let lastLoggedValue: string | null = null;
396
// setInterval(() => {
397
// let values = Object.keys(this._contexts).map((key) => this._contexts[key]);
398
// let logValue = values.map(v => JSON.stringify(v._value, null, '\t')).join('\n');
399
// if (lastLoggedValue !== logValue) {
400
// lastLoggedValue = logValue;
401
// console.log(lastLoggedValue);
402
// }
403
// }, 2000);
404
405
this._register(Event.runAndSubscribe(onDidRegisterWindow, ({ window, disposables }) => {
406
const onFocusDisposables = disposables.add(new MutableDisposable<DisposableStore>());
407
disposables.add(addDisposableListener(window, EventType.FOCUS_IN, () => {
408
onFocusDisposables.value = new DisposableStore();
409
this.updateInputContextKeys(window.document, onFocusDisposables.value);
410
}, true));
411
}, { window: mainWindow, disposables: this._store }));
412
}
413
414
private updateInputContextKeys(ownerDocument: Document, disposables: DisposableStore): void {
415
416
function activeElementIsInput(): boolean {
417
return !!ownerDocument.activeElement && isEditableElement(ownerDocument.activeElement);
418
}
419
420
const isInputFocused = activeElementIsInput();
421
this.inputFocusedContext.set(isInputFocused);
422
423
if (isInputFocused) {
424
const tracker = disposables.add(trackFocus(ownerDocument.activeElement as HTMLElement));
425
Event.once(tracker.onDidBlur)(() => {
426
427
// Ensure we are only updating the context key if we are
428
// still in the same document that we are tracking. This
429
// fixes a race condition in multi-window setups where
430
// the blur event arrives in the inactive window overwriting
431
// the context key of the active window. This is because
432
// blur events from the focus tracker are emitted with a
433
// timeout of 0.
434
435
if (getActiveWindow().document === ownerDocument) {
436
this.inputFocusedContext.set(activeElementIsInput());
437
}
438
439
tracker.dispose();
440
}, undefined, disposables);
441
}
442
}
443
444
public getContextValuesContainer(contextId: number): Context {
445
if (this._isDisposed) {
446
return NullContext.INSTANCE;
447
}
448
return this._contexts.get(contextId) || NullContext.INSTANCE;
449
}
450
451
public createChildContext(parentContextId: number = this._myContextId): number {
452
if (this._isDisposed) {
453
throw new Error(`ContextKeyService has been disposed`);
454
}
455
const id = (++this._lastContextId);
456
this._contexts.set(id, new Context(id, this.getContextValuesContainer(parentContextId)));
457
return id;
458
}
459
460
public disposeContext(contextId: number): void {
461
if (!this._isDisposed) {
462
this._contexts.delete(contextId);
463
}
464
}
465
466
public updateParent(_parentContextKeyService: IContextKeyService): void {
467
throw new Error('Cannot update parent of root ContextKeyService');
468
}
469
}
470
471
class ScopedContextKeyService extends AbstractContextKeyService {
472
473
private _parent: AbstractContextKeyService;
474
private _domNode: IContextKeyServiceTarget;
475
476
private readonly _parentChangeListener = this._register(new MutableDisposable());
477
478
constructor(parent: AbstractContextKeyService, domNode: IContextKeyServiceTarget) {
479
super(parent.createChildContext());
480
this._parent = parent;
481
this._updateParentChangeListener();
482
483
this._domNode = domNode;
484
if (this._domNode.hasAttribute(KEYBINDING_CONTEXT_ATTR)) {
485
let extraInfo = '';
486
if ((this._domNode as HTMLElement).classList) {
487
extraInfo = Array.from((this._domNode as HTMLElement).classList.values()).join(', ');
488
}
489
490
console.error(`Element already has context attribute${extraInfo ? ': ' + extraInfo : ''}`);
491
}
492
this._domNode.setAttribute(KEYBINDING_CONTEXT_ATTR, String(this._myContextId));
493
}
494
495
private _updateParentChangeListener(): void {
496
// Forward parent events to this listener. Parent will change.
497
this._parentChangeListener.value = this._parent.onDidChangeContext(e => {
498
const thisContainer = this._parent.getContextValuesContainer(this._myContextId);
499
const thisContextValues = thisContainer.value;
500
501
if (!allEventKeysInContext(e, thisContextValues)) {
502
this._onDidChangeContext.fire(e);
503
}
504
});
505
}
506
507
public override dispose(): void {
508
if (this._isDisposed) {
509
return;
510
}
511
512
this._parent.disposeContext(this._myContextId);
513
this._domNode.removeAttribute(KEYBINDING_CONTEXT_ATTR);
514
super.dispose();
515
}
516
517
public getContextValuesContainer(contextId: number): Context {
518
if (this._isDisposed) {
519
return NullContext.INSTANCE;
520
}
521
return this._parent.getContextValuesContainer(contextId);
522
}
523
524
public createChildContext(parentContextId: number = this._myContextId): number {
525
if (this._isDisposed) {
526
throw new Error(`ScopedContextKeyService has been disposed`);
527
}
528
return this._parent.createChildContext(parentContextId);
529
}
530
531
public disposeContext(contextId: number): void {
532
if (this._isDisposed) {
533
return;
534
}
535
this._parent.disposeContext(contextId);
536
}
537
538
public updateParent(parentContextKeyService: AbstractContextKeyService): void {
539
if (this._parent === parentContextKeyService) {
540
return;
541
}
542
543
const thisContainer = this._parent.getContextValuesContainer(this._myContextId);
544
const oldAllValues = thisContainer.collectAllValues();
545
this._parent = parentContextKeyService;
546
this._updateParentChangeListener();
547
const newParentContainer = this._parent.getContextValuesContainer(this._parent.contextId);
548
thisContainer.updateParent(newParentContainer);
549
550
const newAllValues = thisContainer.collectAllValues();
551
const allValuesDiff = {
552
...distinct(oldAllValues, newAllValues),
553
...distinct(newAllValues, oldAllValues)
554
};
555
const changedKeys = Object.keys(allValuesDiff);
556
557
this._onDidChangeContext.fire(new ArrayContextKeyChangeEvent(changedKeys));
558
}
559
}
560
561
class OverlayContext implements IContext {
562
563
constructor(private parent: IContext, private overlay: ReadonlyMap<string, any>) { }
564
565
getValue<T extends ContextKeyValue>(key: string): T | undefined {
566
return this.overlay.has(key) ? this.overlay.get(key) : this.parent.getValue<T>(key);
567
}
568
}
569
570
class OverlayContextKeyService implements IContextKeyService {
571
572
declare _serviceBrand: undefined;
573
private overlay: Map<string, any>;
574
575
get contextId(): number {
576
return this.parent.contextId;
577
}
578
579
get onDidChangeContext(): Event<IContextKeyChangeEvent> {
580
return this.parent.onDidChangeContext;
581
}
582
583
constructor(private parent: AbstractContextKeyService | OverlayContextKeyService, overlay: Iterable<[string, any]>) {
584
this.overlay = new Map(overlay);
585
}
586
587
bufferChangeEvents(callback: Function): void {
588
this.parent.bufferChangeEvents(callback);
589
}
590
591
createKey<T extends ContextKeyValue>(): IContextKey<T> {
592
throw new Error('Not supported.');
593
}
594
595
getContext(target: IContextKeyServiceTarget | null): IContext {
596
return new OverlayContext(this.parent.getContext(target), this.overlay);
597
}
598
599
getContextValuesContainer(contextId: number): IContext {
600
const parentContext = this.parent.getContextValuesContainer(contextId);
601
return new OverlayContext(parentContext, this.overlay);
602
}
603
604
contextMatchesRules(rules: ContextKeyExpression | undefined): boolean {
605
const context = this.getContextValuesContainer(this.contextId);
606
const result = (rules ? rules.evaluate(context) : true);
607
return result;
608
}
609
610
getContextKeyValue<T>(key: string): T | undefined {
611
return this.overlay.has(key) ? this.overlay.get(key) : this.parent.getContextKeyValue(key);
612
}
613
614
createScoped(): IScopedContextKeyService {
615
throw new Error('Not supported.');
616
}
617
618
createOverlay(overlay: Iterable<[string, any]> = Iterable.empty()): IContextKeyService {
619
return new OverlayContextKeyService(this, overlay);
620
}
621
622
updateParent(): void {
623
throw new Error('Not supported.');
624
}
625
}
626
627
function findContextAttr(domNode: IContextKeyServiceTarget | null): number {
628
while (domNode) {
629
if (domNode.hasAttribute(KEYBINDING_CONTEXT_ATTR)) {
630
const attr = domNode.getAttribute(KEYBINDING_CONTEXT_ATTR);
631
if (attr) {
632
return parseInt(attr, 10);
633
}
634
return NaN;
635
}
636
domNode = domNode.parentElement;
637
}
638
return 0;
639
}
640
641
export function setContext(accessor: ServicesAccessor, contextKey: any, contextValue: any) {
642
const contextKeyService = accessor.get(IContextKeyService);
643
contextKeyService.createKey(String(contextKey), stringifyURIs(contextValue));
644
}
645
646
function stringifyURIs(contextValue: any): any {
647
return cloneAndChange(contextValue, (obj) => {
648
if (typeof obj === 'object' && (<MarshalledObject>obj).$mid === MarshalledId.Uri) {
649
return URI.revive(obj).toString();
650
}
651
if (obj instanceof URI) {
652
return obj.toString();
653
}
654
return undefined;
655
});
656
}
657
658
CommandsRegistry.registerCommand('_setContext', setContext);
659
660
CommandsRegistry.registerCommand({
661
id: 'getContextKeyInfo',
662
handler() {
663
return [...RawContextKey.all()].sort((a, b) => a.key.localeCompare(b.key));
664
},
665
metadata: {
666
description: localize('getContextKeyInfo', "A command that returns information about context keys"),
667
args: []
668
}
669
});
670
671
CommandsRegistry.registerCommand('_generateContextKeyInfo', function () {
672
const result: ContextKeyInfo[] = [];
673
const seen = new Set<string>();
674
for (const info of RawContextKey.all()) {
675
if (!seen.has(info.key)) {
676
seen.add(info.key);
677
result.push(info);
678
}
679
}
680
result.sort((a, b) => a.key.localeCompare(b.key));
681
console.log(JSON.stringify(result, undefined, 2));
682
});
683
684