Merge commit 'be3e8236086165e5e45a5a10783823874b3f3ebd' as 'lib/vscode'

This commit is contained in:
Joe Previte
2020-12-15 15:52:33 -07:00
4649 changed files with 1311795 additions and 0 deletions

View File

@@ -0,0 +1,493 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event, PauseableEmitter } from 'vs/base/common/event';
import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { distinct } from 'vs/base/common/objects';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContext, IContextKey, IContextKeyChangeEvent, IContextKeyService, IContextKeyServiceTarget, IReadableSet, SET_CONTEXT_COMMAND_ID, ContextKeyExpression } from 'vs/platform/contextkey/common/contextkey';
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
const KEYBINDING_CONTEXT_ATTR = 'data-keybinding-context';
export class Context implements IContext {
protected _parent: Context | null;
protected _value: { [key: string]: any; };
protected _id: number;
constructor(id: number, parent: Context | null) {
this._id = id;
this._parent = parent;
this._value = Object.create(null);
this._value['_contextId'] = id;
}
public setValue(key: string, value: any): boolean {
// console.log('SET ' + key + ' = ' + value + ' ON ' + this._id);
if (this._value[key] !== value) {
this._value[key] = value;
return true;
}
return false;
}
public removeValue(key: string): boolean {
// console.log('REMOVE ' + key + ' FROM ' + this._id);
if (key in this._value) {
delete this._value[key];
return true;
}
return false;
}
public getValue<T>(key: string): T | undefined {
const ret = this._value[key];
if (typeof ret === 'undefined' && this._parent) {
return this._parent.getValue<T>(key);
}
return ret;
}
public updateParent(parent: Context): void {
this._parent = parent;
}
public collectAllValues(): { [key: string]: any; } {
let result = this._parent ? this._parent.collectAllValues() : Object.create(null);
result = { ...result, ...this._value };
delete result['_contextId'];
return result;
}
}
class NullContext extends Context {
static readonly INSTANCE = new NullContext();
constructor() {
super(-1, null);
}
public setValue(key: string, value: any): boolean {
return false;
}
public removeValue(key: string): boolean {
return false;
}
public getValue<T>(key: string): T | undefined {
return undefined;
}
collectAllValues(): { [key: string]: any; } {
return Object.create(null);
}
}
class ConfigAwareContextValuesContainer extends Context {
private static readonly _keyPrefix = 'config.';
private readonly _values = new Map<string, any>();
private readonly _listener: IDisposable;
constructor(
id: number,
private readonly _configurationService: IConfigurationService,
emitter: Emitter<IContextKeyChangeEvent>
) {
super(id, null);
this._listener = this._configurationService.onDidChangeConfiguration(event => {
if (event.source === ConfigurationTarget.DEFAULT) {
// new setting, reset everything
const allKeys = Array.from(this._values.keys());
this._values.clear();
emitter.fire(new ArrayContextKeyChangeEvent(allKeys));
} else {
const changedKeys: string[] = [];
for (const configKey of event.affectedKeys) {
const contextKey = `config.${configKey}`;
if (this._values.has(contextKey)) {
this._values.delete(contextKey);
changedKeys.push(contextKey);
}
}
emitter.fire(new ArrayContextKeyChangeEvent(changedKeys));
}
});
}
dispose(): void {
this._listener.dispose();
}
getValue(key: string): any {
if (key.indexOf(ConfigAwareContextValuesContainer._keyPrefix) !== 0) {
return super.getValue(key);
}
if (this._values.has(key)) {
return this._values.get(key);
}
const configKey = key.substr(ConfigAwareContextValuesContainer._keyPrefix.length);
const configValue = this._configurationService.getValue(configKey);
let value: any = undefined;
switch (typeof configValue) {
case 'number':
case 'boolean':
case 'string':
value = configValue;
break;
default:
if (Array.isArray(configValue)) {
value = JSON.stringify(configValue);
}
}
this._values.set(key, value);
return value;
}
setValue(key: string, value: any): boolean {
return super.setValue(key, value);
}
removeValue(key: string): boolean {
return super.removeValue(key);
}
collectAllValues(): { [key: string]: any; } {
const result: { [key: string]: any } = Object.create(null);
this._values.forEach((value, index) => result[index] = value);
return { ...result, ...super.collectAllValues() };
}
}
class ContextKey<T> implements IContextKey<T> {
private _service: AbstractContextKeyService;
private _key: string;
private _defaultValue: T | undefined;
constructor(service: AbstractContextKeyService, key: string, defaultValue: T | undefined) {
this._service = service;
this._key = key;
this._defaultValue = defaultValue;
this.reset();
}
public set(value: T): void {
this._service.setContext(this._key, value);
}
public reset(): void {
if (typeof this._defaultValue === 'undefined') {
this._service.removeContext(this._key);
} else {
this._service.setContext(this._key, this._defaultValue);
}
}
public get(): T | undefined {
return this._service.getContextKeyValue<T>(this._key);
}
}
class SimpleContextKeyChangeEvent implements IContextKeyChangeEvent {
constructor(readonly key: string) { }
affectsSome(keys: IReadableSet<string>): boolean {
return keys.has(this.key);
}
}
class ArrayContextKeyChangeEvent implements IContextKeyChangeEvent {
constructor(readonly keys: string[]) { }
affectsSome(keys: IReadableSet<string>): boolean {
for (const key of this.keys) {
if (keys.has(key)) {
return true;
}
}
return false;
}
}
class CompositeContextKeyChangeEvent implements IContextKeyChangeEvent {
constructor(readonly events: IContextKeyChangeEvent[]) { }
affectsSome(keys: IReadableSet<string>): boolean {
for (const e of this.events) {
if (e.affectsSome(keys)) {
return true;
}
}
return false;
}
}
export abstract class AbstractContextKeyService implements IContextKeyService {
public _serviceBrand: undefined;
protected _isDisposed: boolean;
protected _onDidChangeContext = new PauseableEmitter<IContextKeyChangeEvent>({ merge: input => new CompositeContextKeyChangeEvent(input) });
protected _myContextId: number;
constructor(myContextId: number) {
this._isDisposed = false;
this._myContextId = myContextId;
}
public get contextId(): number {
return this._myContextId;
}
abstract dispose(): void;
public createKey<T>(key: string, defaultValue: T | undefined): IContextKey<T> {
if (this._isDisposed) {
throw new Error(`AbstractContextKeyService has been disposed`);
}
return new ContextKey(this, key, defaultValue);
}
public get onDidChangeContext(): Event<IContextKeyChangeEvent> {
return this._onDidChangeContext.event;
}
bufferChangeEvents(callback: Function): void {
this._onDidChangeContext.pause();
try {
callback();
} finally {
this._onDidChangeContext.resume();
}
}
public createScoped(domNode: IContextKeyServiceTarget): IContextKeyService {
if (this._isDisposed) {
throw new Error(`AbstractContextKeyService has been disposed`);
}
return new ScopedContextKeyService(this, domNode);
}
public contextMatchesRules(rules: ContextKeyExpression | undefined): boolean {
if (this._isDisposed) {
throw new Error(`AbstractContextKeyService has been disposed`);
}
const context = this.getContextValuesContainer(this._myContextId);
const result = KeybindingResolver.contextMatchesRules(context, rules);
// console.group(rules.serialize() + ' -> ' + result);
// rules.keys().forEach(key => { console.log(key, ctx[key]); });
// console.groupEnd();
return result;
}
public getContextKeyValue<T>(key: string): T | undefined {
if (this._isDisposed) {
return undefined;
}
return this.getContextValuesContainer(this._myContextId).getValue<T>(key);
}
public setContext(key: string, value: any): void {
if (this._isDisposed) {
return;
}
const myContext = this.getContextValuesContainer(this._myContextId);
if (!myContext) {
return;
}
if (myContext.setValue(key, value)) {
this._onDidChangeContext.fire(new SimpleContextKeyChangeEvent(key));
}
}
public removeContext(key: string): void {
if (this._isDisposed) {
return;
}
if (this.getContextValuesContainer(this._myContextId).removeValue(key)) {
this._onDidChangeContext.fire(new SimpleContextKeyChangeEvent(key));
}
}
public getContext(target: IContextKeyServiceTarget | null): IContext {
if (this._isDisposed) {
return NullContext.INSTANCE;
}
return this.getContextValuesContainer(findContextAttr(target));
}
public abstract getContextValuesContainer(contextId: number): Context;
public abstract createChildContext(parentContextId?: number): number;
public abstract disposeContext(contextId: number): void;
public abstract updateParent(parentContextKeyService?: IContextKeyService): void;
}
export class ContextKeyService extends AbstractContextKeyService implements IContextKeyService {
private _lastContextId: number;
private readonly _contexts = new Map<number, Context>();
private readonly _toDispose = new DisposableStore();
constructor(@IConfigurationService configurationService: IConfigurationService) {
super(0);
this._lastContextId = 0;
const myContext = new ConfigAwareContextValuesContainer(this._myContextId, configurationService, this._onDidChangeContext);
this._contexts.set(this._myContextId, myContext);
this._toDispose.add(myContext);
// Uncomment this to see the contexts continuously logged
// let lastLoggedValue: string | null = null;
// setInterval(() => {
// let values = Object.keys(this._contexts).map((key) => this._contexts[key]);
// let logValue = values.map(v => JSON.stringify(v._value, null, '\t')).join('\n');
// if (lastLoggedValue !== logValue) {
// lastLoggedValue = logValue;
// console.log(lastLoggedValue);
// }
// }, 2000);
}
public dispose(): void {
this._isDisposed = true;
this._toDispose.dispose();
}
public getContextValuesContainer(contextId: number): Context {
if (this._isDisposed) {
return NullContext.INSTANCE;
}
return this._contexts.get(contextId) || NullContext.INSTANCE;
}
public createChildContext(parentContextId: number = this._myContextId): number {
if (this._isDisposed) {
throw new Error(`ContextKeyService has been disposed`);
}
let id = (++this._lastContextId);
this._contexts.set(id, new Context(id, this.getContextValuesContainer(parentContextId)));
return id;
}
public disposeContext(contextId: number): void {
if (!this._isDisposed) {
this._contexts.delete(contextId);
}
}
public updateParent(_parentContextKeyService: IContextKeyService): void {
throw new Error('Cannot update parent of root ContextKeyService');
}
}
class ScopedContextKeyService extends AbstractContextKeyService {
private _parent: AbstractContextKeyService;
private _domNode: IContextKeyServiceTarget | undefined;
private _parentChangeListener: IDisposable | undefined;
constructor(parent: AbstractContextKeyService, domNode?: IContextKeyServiceTarget) {
super(parent.createChildContext());
this._parent = parent;
this.updateParentChangeListener();
if (domNode) {
this._domNode = domNode;
this._domNode.setAttribute(KEYBINDING_CONTEXT_ATTR, String(this._myContextId));
}
}
private updateParentChangeListener(): void {
if (this._parentChangeListener) {
this._parentChangeListener.dispose();
}
this._parentChangeListener = this._parent.onDidChangeContext(e => {
// Forward parent events to this listener. Parent will change.
this._onDidChangeContext.fire(e);
});
}
public dispose(): void {
this._isDisposed = true;
this._parent.disposeContext(this._myContextId);
this._parentChangeListener?.dispose();
if (this._domNode) {
this._domNode.removeAttribute(KEYBINDING_CONTEXT_ATTR);
this._domNode = undefined;
}
}
public get onDidChangeContext(): Event<IContextKeyChangeEvent> {
return this._onDidChangeContext.event;
}
public getContextValuesContainer(contextId: number): Context {
if (this._isDisposed) {
return NullContext.INSTANCE;
}
return this._parent.getContextValuesContainer(contextId);
}
public createChildContext(parentContextId: number = this._myContextId): number {
if (this._isDisposed) {
throw new Error(`ScopedContextKeyService has been disposed`);
}
return this._parent.createChildContext(parentContextId);
}
public disposeContext(contextId: number): void {
if (this._isDisposed) {
return;
}
this._parent.disposeContext(contextId);
}
public updateParent(parentContextKeyService: AbstractContextKeyService): void {
const thisContainer = this._parent.getContextValuesContainer(this._myContextId);
const oldAllValues = thisContainer.collectAllValues();
this._parent = parentContextKeyService;
this.updateParentChangeListener();
const newParentContainer = this._parent.getContextValuesContainer(this._parent.contextId);
thisContainer.updateParent(newParentContainer);
const newAllValues = thisContainer.collectAllValues();
const allValuesDiff = {
...distinct(oldAllValues, newAllValues),
...distinct(newAllValues, oldAllValues)
};
const changedKeys = Object.keys(allValuesDiff);
this._onDidChangeContext.fire(new ArrayContextKeyChangeEvent(changedKeys));
}
}
function findContextAttr(domNode: IContextKeyServiceTarget | null): number {
while (domNode) {
if (domNode.hasAttribute(KEYBINDING_CONTEXT_ATTR)) {
const attr = domNode.getAttribute(KEYBINDING_CONTEXT_ATTR);
if (attr) {
return parseInt(attr, 10);
}
return NaN;
}
domNode = domNode.parentElement;
}
return 0;
}
CommandsRegistry.registerCommand(SET_CONTEXT_COMMAND_ID, function (accessor, contextKey: any, contextValue: any) {
accessor.get(IContextKeyService).createKey(String(contextKey), contextValue);
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,19 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { isMacintosh, isLinux, isWindows, isWeb } from 'vs/base/common/platform';
export const IsMacContext = new RawContextKey<boolean>('isMac', isMacintosh);
export const IsLinuxContext = new RawContextKey<boolean>('isLinux', isLinux);
export const IsWindowsContext = new RawContextKey<boolean>('isWindows', isWindows);
export const IsWebContext = new RawContextKey<boolean>('isWeb', isWeb);
export const IsMacNativeContext = new RawContextKey<boolean>('isMacNative', isMacintosh && !isWeb);
export const IsDevelopmentContext = new RawContextKey<boolean>('isDevelopment', false);
export const InputFocusedContextKey = 'inputFocus';
export const InputFocusedContext = new RawContextKey<boolean>(InputFocusedContextKey, false);

View File

@@ -0,0 +1,53 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ContextKeyService } from 'vs/platform/contextkey/browser/contextKeyService';
import * as assert from 'assert';
suite('ContextKeyService', () => {
test('updateParent', () => {
const root = new ContextKeyService(new TestConfigurationService());
const parent1 = root.createScoped(document.createElement('div'));
const parent2 = root.createScoped(document.createElement('div'));
const child = parent1.createScoped(document.createElement('div'));
parent1.createKey('testA', 1);
parent1.createKey('testB', 2);
parent1.createKey('testD', 0);
parent2.createKey('testA', 3);
parent2.createKey('testC', 4);
parent2.createKey('testD', 0);
let complete: () => void;
let reject: (err: Error) => void;
const p = new Promise<void>((_complete, _reject) => {
complete = _complete;
reject = _reject;
});
child.onDidChangeContext(e => {
try {
assert.ok(e.affectsSome(new Set(['testA'])), 'testA changed');
assert.ok(e.affectsSome(new Set(['testB'])), 'testB changed');
assert.ok(e.affectsSome(new Set(['testC'])), 'testC changed');
assert.ok(!e.affectsSome(new Set(['testD'])), 'testD did not change');
assert.equal(child.getContextKeyValue('testA'), 3);
assert.equal(child.getContextKeyValue('testB'), undefined);
assert.equal(child.getContextKeyValue('testC'), 4);
assert.equal(child.getContextKeyValue('testD'), 0);
} catch (err) {
reject(err);
return;
}
complete();
});
child.updateParent(parent2);
return p;
});
});

View File

@@ -0,0 +1,189 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { isMacintosh, isLinux, isWindows } from 'vs/base/common/platform';
function createContext(ctx: any) {
return {
getValue: (key: string) => {
return ctx[key];
}
};
}
suite('ContextKeyExpr', () => {
test('ContextKeyExpr.equals', () => {
let a = ContextKeyExpr.and(
ContextKeyExpr.has('a1'),
ContextKeyExpr.and(ContextKeyExpr.has('and.a')),
ContextKeyExpr.has('a2'),
ContextKeyExpr.regex('d3', /d.*/),
ContextKeyExpr.regex('d4', /\*\*3*/),
ContextKeyExpr.equals('b1', 'bb1'),
ContextKeyExpr.equals('b2', 'bb2'),
ContextKeyExpr.notEquals('c1', 'cc1'),
ContextKeyExpr.notEquals('c2', 'cc2'),
ContextKeyExpr.not('d1'),
ContextKeyExpr.not('d2')
)!;
let b = ContextKeyExpr.and(
ContextKeyExpr.equals('b2', 'bb2'),
ContextKeyExpr.notEquals('c1', 'cc1'),
ContextKeyExpr.not('d1'),
ContextKeyExpr.regex('d4', /\*\*3*/),
ContextKeyExpr.notEquals('c2', 'cc2'),
ContextKeyExpr.has('a2'),
ContextKeyExpr.equals('b1', 'bb1'),
ContextKeyExpr.regex('d3', /d.*/),
ContextKeyExpr.has('a1'),
ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)),
ContextKeyExpr.not('d2')
)!;
assert(a.equals(b), 'expressions should be equal');
});
test('normalize', () => {
let key1IsTrue = ContextKeyExpr.equals('key1', true);
let key1IsNotFalse = ContextKeyExpr.notEquals('key1', false);
let key1IsFalse = ContextKeyExpr.equals('key1', false);
let key1IsNotTrue = ContextKeyExpr.notEquals('key1', true);
assert.ok(key1IsTrue.equals(ContextKeyExpr.has('key1')));
assert.ok(key1IsNotFalse.equals(ContextKeyExpr.has('key1')));
assert.ok(key1IsFalse.equals(ContextKeyExpr.not('key1')));
assert.ok(key1IsNotTrue.equals(ContextKeyExpr.not('key1')));
});
test('evaluate', () => {
let context = createContext({
'a': true,
'b': false,
'c': '5',
'd': 'd'
});
function testExpression(expr: string, expected: boolean): void {
// console.log(expr + ' ' + expected);
let rules = ContextKeyExpr.deserialize(expr);
assert.equal(rules!.evaluate(context), expected, expr);
}
function testBatch(expr: string, value: any): void {
/* eslint-disable eqeqeq */
testExpression(expr, !!value);
testExpression(expr + ' == true', !!value);
testExpression(expr + ' != true', !value);
testExpression(expr + ' == false', !value);
testExpression(expr + ' != false', !!value);
testExpression(expr + ' == 5', value == <any>'5');
testExpression(expr + ' != 5', value != <any>'5');
testExpression('!' + expr, !value);
testExpression(expr + ' =~ /d.*/', /d.*/.test(value));
testExpression(expr + ' =~ /D/i', /D/i.test(value));
/* eslint-enable eqeqeq */
}
testBatch('a', true);
testBatch('b', false);
testBatch('c', '5');
testBatch('d', 'd');
testBatch('z', undefined);
testExpression('true', true);
testExpression('false', false);
testExpression('a && !b', true && !false);
testExpression('a && b', true && false);
testExpression('a && !b && c == 5', true && !false && '5' === '5');
testExpression('d =~ /e.*/', false);
// precedence test: false && true || true === true because && is evaluated first
testExpression('b && a || a', true);
testExpression('a || b', true);
testExpression('b || b', false);
testExpression('b && a || a && b', false);
});
test('negate', () => {
function testNegate(expr: string, expected: string): void {
const actual = ContextKeyExpr.deserialize(expr)!.negate().serialize();
assert.strictEqual(actual, expected);
}
testNegate('true', 'false');
testNegate('false', 'true');
testNegate('a', '!a');
testNegate('a && b || c', '!a && !c || !b && !c');
testNegate('a && b || c || d', '!a && !c && !d || !b && !c && !d');
testNegate('!a && !b || !c && !d', 'a && c || a && d || b && c || b && d');
testNegate('!a && !b || !c && !d || !e && !f', 'a && c && e || a && c && f || a && d && e || a && d && f || b && c && e || b && c && f || b && d && e || b && d && f');
});
test('false, true', () => {
function testNormalize(expr: string, expected: string): void {
const actual = ContextKeyExpr.deserialize(expr)!.serialize();
assert.strictEqual(actual, expected);
}
testNormalize('true', 'true');
testNormalize('!true', 'false');
testNormalize('false', 'false');
testNormalize('!false', 'true');
testNormalize('a && true', 'a');
testNormalize('a && false', 'false');
testNormalize('a || true', 'true');
testNormalize('a || false', 'a');
testNormalize('isMac', isMacintosh ? 'true' : 'false');
testNormalize('isLinux', isLinux ? 'true' : 'false');
testNormalize('isWindows', isWindows ? 'true' : 'false');
});
test('issue #101015: distribute OR', () => {
function t(expr1: string, expr2: string, expected: string | undefined): void {
const e1 = ContextKeyExpr.deserialize(expr1);
const e2 = ContextKeyExpr.deserialize(expr2);
const actual = ContextKeyExpr.and(e1, e2)?.serialize();
assert.strictEqual(actual, expected);
}
t('a', 'b', 'a && b');
t('a || b', 'c', 'a && c || b && c');
t('a || b', 'c || d', 'a && c || a && d || b && c || b && d');
t('a || b', 'c && d', 'a && c && d || b && c && d');
t('a || b', 'c && d || e', 'a && e || b && e || a && c && d || b && c && d');
});
test('ContextKeyInExpr', () => {
const ainb = ContextKeyExpr.deserialize('a in b')!;
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': [3, 2, 1] })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': [1, 2, 3] })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': [1, 2] })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 3 })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 3, 'b': null })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': ['x'] })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': ['y'] })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': {} })), false);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': { 'x': false } })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 'x', 'b': { 'x': true } })), true);
assert.equal(ainb.evaluate(createContext({ 'a': 'prototype', 'b': {} })), false);
});
test('issue #106524: distributing AND should normalize', () => {
const actual = ContextKeyExpr.and(
ContextKeyExpr.or(
ContextKeyExpr.has('a'),
ContextKeyExpr.has('b')
),
ContextKeyExpr.has('c')
);
const expected = ContextKeyExpr.or(
ContextKeyExpr.and(
ContextKeyExpr.has('a'),
ContextKeyExpr.has('c')
),
ContextKeyExpr.and(
ContextKeyExpr.has('b'),
ContextKeyExpr.has('c')
)
);
assert.equal(actual!.equals(expected!), true);
});
});