Skip to content

Commit d58dcf2

Browse files
committed
Replacing null with undefined around ContextKeyExpr
1 parent 91570a0 commit d58dcf2

19 files changed

Lines changed: 34 additions & 35 deletions

File tree

src/vs/editor/browser/editorExtensions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { IConstructorSignature1, ServicesAccessor } from 'vs/platform/instantiat
2020
import { IKeybindings, KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
2121
import { Registry } from 'vs/platform/registry/common/platform';
2222
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
23-
import { withUndefinedAsNull } from 'vs/base/common/types';
23+
import { withNullAsUndefined } from 'vs/base/common/types';
2424

2525
export type ServicesAccessor = ServicesAccessor;
2626
export type IEditorContributionCtor = IConstructorSignature1<ICodeEditor, IEditorContribution>;
@@ -89,7 +89,7 @@ export abstract class Command {
8989
id: this.id,
9090
handler: (accessor, args) => this.runCommand(accessor, args),
9191
weight: this._kbOpts.weight,
92-
when: withUndefinedAsNull(kbWhen),
92+
when: kbWhen,
9393
primary: this._kbOpts.primary,
9494
secondary: this._kbOpts.secondary,
9595
win: this._kbOpts.win,
@@ -157,7 +157,7 @@ export abstract class EditorCommand extends Command {
157157

158158
return editor.invokeWithinContext((editorAccessor) => {
159159
const kbService = editorAccessor.get(IContextKeyService);
160-
if (!kbService.contextMatchesRules(this.precondition)) {
160+
if (!kbService.contextMatchesRules(withNullAsUndefined(this.precondition))) {
161161
// precondition does not hold
162162
return;
163163
}

src/vs/editor/browser/widget/codeEditorWidget.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle
4949
import { INotificationService } from 'vs/platform/notification/common/notification';
5050
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
5151
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
52+
import { withNullAsUndefined } from 'vs/base/common/types';
5253

5354
let EDITOR_ID = 0;
5455

@@ -308,7 +309,7 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
308309
action.id,
309310
action.label,
310311
action.alias,
311-
action.precondition,
312+
withNullAsUndefined(action.precondition),
312313
(): Promise<void> => {
313314
return this._instantiationService.invokeFunction((accessor) => {
314315
return Promise.resolve(action.runEditorCommand(accessor, this, null));

src/vs/editor/common/editorAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ export class InternalEditorAction implements IEditorAction {
1212
public readonly label: string;
1313
public readonly alias: string;
1414

15-
private readonly _precondition: ContextKeyExpr | null;
15+
private readonly _precondition: ContextKeyExpr | undefined;
1616
private readonly _run: () => Promise<void>;
1717
private readonly _contextKeyService: IContextKeyService;
1818

1919
constructor(
2020
id: string,
2121
label: string,
2222
alias: string,
23-
precondition: ContextKeyExpr | null,
23+
precondition: ContextKeyExpr | undefined,
2424
run: () => Promise<void>,
2525
contextKeyService: IContextKeyService
2626
) {

src/vs/editor/standalone/browser/simpleServices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
280280
}));
281281
}
282282

283-
public addDynamicKeybinding(commandId: string, _keybinding: number, handler: ICommandHandler, when: ContextKeyExpr | null): IDisposable {
283+
public addDynamicKeybinding(commandId: string, _keybinding: number, handler: ICommandHandler, when: ContextKeyExpr | undefined): IDisposable {
284284
const keybinding = createKeybinding(_keybinding, OS);
285285
if (!keybinding) {
286286
throw new Error(`Invalid keybinding`);
@@ -342,7 +342,7 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
342342
private _toNormalizedKeybindingItems(items: IKeybindingItem[], isDefault: boolean): ResolvedKeybindingItem[] {
343343
let result: ResolvedKeybindingItem[] = [], resultLen = 0;
344344
for (const item of items) {
345-
const when = (item.when ? item.when.normalize() : null);
345+
const when = (item.when ? item.when.normalize() : undefined);
346346
const keybinding = item.keybinding;
347347

348348
if (!keybinding) {

src/vs/editor/standalone/test/browser/simpleServices.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ suite('StandaloneKeybindingService', () => {
3939
let commandInvoked = false;
4040
keybindingService.addDynamicKeybinding('testCommand', KeyCode.F9, () => {
4141
commandInvoked = true;
42-
}, null);
42+
}, undefined);
4343

4444
keybindingService.testDispatch({
4545
_standardKeyboardEventBrand: true,

src/vs/platform/actions/common/menuService.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { dispose, IDisposable } from 'vs/base/common/lifecycle';
88
import { IMenu, IMenuActionOptions, IMenuItem, IMenuService, isIMenuItem, ISubmenuItem, MenuId, MenuItemAction, MenuRegistry, SubmenuItemAction } from 'vs/platform/actions/common/actions';
99
import { ICommandService } from 'vs/platform/commands/common/commands';
1010
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
11-
import { withUndefinedAsNull } from 'vs/base/common/types';
1211

1312
export class MenuService implements IMenuService {
1413

@@ -111,7 +110,7 @@ class Menu implements IMenu {
111110
const [id, items] = group;
112111
const activeActions: Array<MenuItemAction | SubmenuItemAction> = [];
113112
for (const item of items) {
114-
if (this._contextKeyService.contextMatchesRules(withUndefinedAsNull(item.when))) {
113+
if (this._contextKeyService.contextMatchesRules(item.when)) {
115114
const action = isIMenuItem(item) ? new MenuItemAction(item.command, item.alt, options, this._contextKeyService, this._commandService) : new SubmenuItemAction(item);
116115
activeActions.push(action);
117116
}

src/vs/platform/contextkey/browser/contextKeyService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export abstract class AbstractContextKeyService implements IContextKeyService {
252252
return new ScopedContextKeyService(this, this._onDidChangeContextKey, domNode);
253253
}
254254

255-
public contextMatchesRules(rules: ContextKeyExpr | null): boolean {
255+
public contextMatchesRules(rules: ContextKeyExpr | undefined): boolean {
256256
if (this._isDisposed) {
257257
throw new Error(`AbstractContextKeyService has been disposed`);
258258
}

src/vs/platform/contextkey/common/contextkey.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export abstract class ContextKeyExpr {
5050
return new ContextKeyAndExpr(expr);
5151
}
5252

53-
public static deserialize(serialized: string | null | undefined, strict: boolean = false): ContextKeyExpr | null {
53+
public static deserialize(serialized: string | null | undefined, strict: boolean = false): ContextKeyExpr | undefined {
5454
if (!serialized) {
55-
return null;
55+
return undefined;
5656
}
5757

5858
let pieces = serialized.split('&&');
@@ -143,7 +143,7 @@ export abstract class ContextKeyExpr {
143143
public abstract getType(): ContextKeyExprType;
144144
public abstract equals(other: ContextKeyExpr): boolean;
145145
public abstract evaluate(context: IContext): boolean;
146-
public abstract normalize(): ContextKeyExpr | null;
146+
public abstract normalize(): ContextKeyExpr | undefined;
147147
public abstract serialize(): string;
148148
public abstract keys(): string[];
149149
public abstract map(mapFnc: IContextKeyExprMapper): ContextKeyExpr;
@@ -519,9 +519,9 @@ export class ContextKeyAndExpr implements ContextKeyExpr {
519519
return expr;
520520
}
521521

522-
public normalize(): ContextKeyExpr | null {
522+
public normalize(): ContextKeyExpr | undefined {
523523
if (this.expr.length === 0) {
524-
return null;
524+
return undefined;
525525
}
526526

527527
if (this.expr.length === 1) {
@@ -622,7 +622,7 @@ export interface IContextKeyService {
622622

623623
onDidChangeContext: Event<IContextKeyChangeEvent>;
624624
createKey<T>(key: string, defaultValue: T | undefined): IContextKey<T>;
625-
contextMatchesRules(rules: ContextKeyExpr | null): boolean;
625+
contextMatchesRules(rules: ContextKeyExpr | undefined): boolean;
626626
getContextKeyValue<T>(key: string): T | undefined;
627627

628628
createScoped(target?: IContextKeyServiceTarget): IContextKeyService;

src/vs/platform/keybinding/common/keybindingResolver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class KeybindingResolver {
5050
}
5151
}
5252

53-
private static _isTargetedForRemoval(defaultKb: ResolvedKeybindingItem, keypressFirstPart: string | null, keypressChordPart: string | null, command: string, when: ContextKeyExpr | null): boolean {
53+
private static _isTargetedForRemoval(defaultKb: ResolvedKeybindingItem, keypressFirstPart: string | null, keypressChordPart: string | null, command: string, when: ContextKeyExpr | undefined): boolean {
5454
if (defaultKb.command !== command) {
5555
return false;
5656
}
@@ -172,7 +172,7 @@ export class KeybindingResolver {
172172
* Returns true if it is provable `a` implies `b`.
173173
* **Precondition**: Assumes `a` and `b` are normalized!
174174
*/
175-
public static whenIsEntirelyIncluded(a: ContextKeyExpr | null, b: ContextKeyExpr | null): boolean {
175+
public static whenIsEntirelyIncluded(a: ContextKeyExpr | null | undefined, b: ContextKeyExpr | null | undefined): boolean {
176176
if (!b) {
177177
return true;
178178
}
@@ -304,7 +304,7 @@ export class KeybindingResolver {
304304
return null;
305305
}
306306

307-
public static contextMatchesRules(context: IContext, rules: ContextKeyExpr | null): boolean {
307+
public static contextMatchesRules(context: IContext, rules: ContextKeyExpr | null | undefined): boolean {
308308
if (!rules) {
309309
return true;
310310
}

src/vs/platform/keybinding/common/keybindingsRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface IKeybindingRule2 {
4949
id: string;
5050
args?: any;
5151
weight: number;
52-
when: ContextKeyExpr | null;
52+
when: ContextKeyExpr | undefined;
5353
}
5454

5555
export const enum KeybindingWeight {

0 commit comments

Comments
 (0)