Skip to content

Commit 05c5861

Browse files
committed
Change ITheme.getColor(...) to returned Color | undefined
**Problem** `ITheme.getColor` is one of the major users of the type `null` in our codebase. However this function is used in many places that expected an `undefined` type instead. With the strict null work, this means that we have to write code such as `theme.getColor(...) || undefined` in some cases or have to work with `Color | null | undefined` types. **Fix** `undefined` is generally better supported by TypeScript and more natural in JavaScript. This change makes `ITheme.getColor` return `Color | undefined` instead of `Color | null`.
1 parent ea577a7 commit 05c5861

13 files changed

Lines changed: 71 additions & 71 deletions

File tree

src/vs/editor/contrib/find/findOptionsWidget.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class FindOptionsWidget extends Widget implements IOverlayWidget {
4646
this._domNode.setAttribute('role', 'presentation');
4747
this._domNode.setAttribute('aria-hidden', 'true');
4848

49-
const inputActiveOptionBorderColor = themeService.getTheme().getColor(inputActiveOptionBorder) || undefined;
49+
const inputActiveOptionBorderColor = themeService.getTheme().getColor(inputActiveOptionBorder);
5050

5151
this.caseSensitive = this._register(new CaseSensitiveCheckbox({
5252
appendTitle: this._keybindingLabelFor(FIND_IDS.ToggleCaseSensitiveCommand),
@@ -179,7 +179,7 @@ export class FindOptionsWidget extends Widget implements IOverlayWidget {
179179
}
180180

181181
private _applyTheme(theme: ITheme) {
182-
let inputStyles = { inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder) || undefined };
182+
let inputStyles = { inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder) };
183183
this.caseSensitive.style(inputStyles);
184184
this.wholeWords.style(inputStyles);
185185
this.regex.style(inputStyles);

src/vs/editor/contrib/find/findWidget.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,19 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
530530

531531
private _applyTheme(theme: ITheme) {
532532
let inputStyles: IFindInputStyles = {
533-
inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder) || undefined,
534-
inputBackground: theme.getColor(inputBackground) || undefined,
535-
inputForeground: theme.getColor(inputForeground) || undefined,
536-
inputBorder: theme.getColor(inputBorder) || undefined,
537-
inputValidationInfoBackground: theme.getColor(inputValidationInfoBackground) || undefined,
538-
inputValidationInfoForeground: theme.getColor(inputValidationInfoForeground) || undefined,
539-
inputValidationInfoBorder: theme.getColor(inputValidationInfoBorder) || undefined,
540-
inputValidationWarningBackground: theme.getColor(inputValidationWarningBackground) || undefined,
541-
inputValidationWarningForeground: theme.getColor(inputValidationWarningForeground) || undefined,
542-
inputValidationWarningBorder: theme.getColor(inputValidationWarningBorder) || undefined,
543-
inputValidationErrorBackground: theme.getColor(inputValidationErrorBackground) || undefined,
544-
inputValidationErrorForeground: theme.getColor(inputValidationErrorForeground) || undefined,
545-
inputValidationErrorBorder: theme.getColor(inputValidationErrorBorder) || undefined,
533+
inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder),
534+
inputBackground: theme.getColor(inputBackground),
535+
inputForeground: theme.getColor(inputForeground),
536+
inputBorder: theme.getColor(inputBorder),
537+
inputValidationInfoBackground: theme.getColor(inputValidationInfoBackground),
538+
inputValidationInfoForeground: theme.getColor(inputValidationInfoForeground),
539+
inputValidationInfoBorder: theme.getColor(inputValidationInfoBorder),
540+
inputValidationWarningBackground: theme.getColor(inputValidationWarningBackground),
541+
inputValidationWarningForeground: theme.getColor(inputValidationWarningForeground),
542+
inputValidationWarningBorder: theme.getColor(inputValidationWarningBorder),
543+
inputValidationErrorBackground: theme.getColor(inputValidationErrorBackground),
544+
inputValidationErrorForeground: theme.getColor(inputValidationErrorForeground),
545+
inputValidationErrorBorder: theme.getColor(inputValidationErrorBorder),
546546
};
547547
this._findInput.style(inputStyles);
548548
this._replaceInputBox.style(inputStyles);
@@ -1130,7 +1130,7 @@ export class SimpleButton extends Widget {
11301130
// theming
11311131

11321132
registerThemingParticipant((theme, collector) => {
1133-
const addBackgroundColorRule = (selector: string, color: Color | null): void => {
1133+
const addBackgroundColorRule = (selector: string, color: Color | undefined): void => {
11341134
if (color) {
11351135
collector.addRule(`.monaco-editor ${selector} { background-color: ${color}; }`);
11361136
}

src/vs/editor/contrib/find/simpleFindWidget.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,19 @@ export abstract class SimpleFindWidget extends Widget {
160160

161161
public updateTheme(theme: ITheme): void {
162162
const inputStyles: IFindInputStyles = {
163-
inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder) || undefined,
164-
inputBackground: theme.getColor(inputBackground) || undefined,
165-
inputForeground: theme.getColor(inputForeground) || undefined,
166-
inputBorder: theme.getColor(inputBorder) || undefined,
167-
inputValidationInfoBackground: theme.getColor(inputValidationInfoBackground) || undefined,
168-
inputValidationInfoForeground: theme.getColor(inputValidationInfoForeground) || undefined,
169-
inputValidationInfoBorder: theme.getColor(inputValidationInfoBorder) || undefined,
170-
inputValidationWarningBackground: theme.getColor(inputValidationWarningBackground) || undefined,
171-
inputValidationWarningForeground: theme.getColor(inputValidationWarningForeground) || undefined,
172-
inputValidationWarningBorder: theme.getColor(inputValidationWarningBorder) || undefined,
173-
inputValidationErrorBackground: theme.getColor(inputValidationErrorBackground) || undefined,
174-
inputValidationErrorForeground: theme.getColor(inputValidationErrorForeground) || undefined,
175-
inputValidationErrorBorder: theme.getColor(inputValidationErrorBorder) || undefined
163+
inputActiveOptionBorder: theme.getColor(inputActiveOptionBorder),
164+
inputBackground: theme.getColor(inputBackground),
165+
inputForeground: theme.getColor(inputForeground),
166+
inputBorder: theme.getColor(inputBorder),
167+
inputValidationInfoBackground: theme.getColor(inputValidationInfoBackground),
168+
inputValidationInfoForeground: theme.getColor(inputValidationInfoForeground),
169+
inputValidationInfoBorder: theme.getColor(inputValidationInfoBorder),
170+
inputValidationWarningBackground: theme.getColor(inputValidationWarningBackground),
171+
inputValidationWarningForeground: theme.getColor(inputValidationWarningForeground),
172+
inputValidationWarningBorder: theme.getColor(inputValidationWarningBorder),
173+
inputValidationErrorBackground: theme.getColor(inputValidationErrorBackground),
174+
inputValidationErrorForeground: theme.getColor(inputValidationErrorForeground),
175+
inputValidationErrorBorder: theme.getColor(inputValidationErrorBorder)
176176
};
177177
this._findInput.style(inputStyles);
178178
}

src/vs/editor/contrib/gotoError/gotoErrorWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class MarkerNavigationWidget extends ZoneWidget {
185185
}
186186

187187
private _applyTheme(theme: ITheme) {
188-
this._backgroundColor = theme.getColor(editorMarkerNavigationBackground) || undefined;
188+
this._backgroundColor = theme.getColor(editorMarkerNavigationBackground);
189189
let colorId = editorMarkerNavigationError;
190190
if (this._severity === MarkerSeverity.Warning) {
191191
colorId = editorMarkerNavigationWarning;

src/vs/editor/contrib/referenceSearch/referencesWidget.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ export class ReferenceWidget extends PeekViewWidget {
279279
arrowColor: borderColor,
280280
frameColor: borderColor,
281281
headerBackgroundColor: theme.getColor(peekViewTitleBackground) || Color.transparent,
282-
primaryHeadingColor: theme.getColor(peekViewTitleForeground) || undefined,
283-
secondaryHeadingColor: theme.getColor(peekViewTitleInfoForeground) || undefined
282+
primaryHeadingColor: theme.getColor(peekViewTitleForeground),
283+
secondaryHeadingColor: theme.getColor(peekViewTitleInfoForeground)
284284
});
285285
}
286286

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class StandaloneTheme implements IStandaloneTheme {
2828

2929
private themeData: IStandaloneThemeData;
3030
private colors: { [colorId: string]: Color } | null;
31-
private defaultColors: { [colorId: string]: Color | null; };
31+
private defaultColors: { [colorId: string]: Color | undefined; };
3232
private _tokenTheme: TokenTheme | null;
3333

3434
constructor(name: string, standaloneThemeData: IStandaloneThemeData) {
@@ -77,18 +77,18 @@ class StandaloneTheme implements IStandaloneTheme {
7777
return this.colors;
7878
}
7979

80-
public getColor(colorId: ColorIdentifier, useDefault?: boolean): Color | null {
80+
public getColor(colorId: ColorIdentifier, useDefault?: boolean): Color | undefined {
8181
const color = this.getColors()[colorId];
8282
if (color) {
8383
return color;
8484
}
8585
if (useDefault !== false) {
8686
return this.getDefault(colorId);
8787
}
88-
return null;
88+
return undefined;
8989
}
9090

91-
private getDefault(colorId: ColorIdentifier): Color | null {
91+
private getDefault(colorId: ColorIdentifier): Color | undefined {
9292
let color = this.defaultColors[colorId];
9393
if (color) {
9494
return color;

src/vs/platform/theme/common/colorRegistry.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface ColorContribution {
2727

2828

2929
export interface ColorFunction {
30-
(theme: ITheme): Color | null;
30+
(theme: ITheme): Color | undefined;
3131
}
3232

3333
export interface ColorDefaults {
@@ -71,7 +71,7 @@ export interface IColorRegistry {
7171
/**
7272
* Gets the default color of the given id
7373
*/
74-
resolveDefaultColor(id: ColorIdentifier, theme: ITheme): Color | null;
74+
resolveDefaultColor(id: ColorIdentifier, theme: ITheme): Color | undefined;
7575

7676
/**
7777
* JSON schema for an object to assign color values to one of the color contributions.
@@ -131,13 +131,13 @@ class ColorRegistry implements IColorRegistry {
131131
return Object.keys(this.colorsById).map(id => this.colorsById[id]);
132132
}
133133

134-
public resolveDefaultColor(id: ColorIdentifier, theme: ITheme): Color | null {
134+
public resolveDefaultColor(id: ColorIdentifier, theme: ITheme): Color | undefined {
135135
const colorDesc = this.colorsById[id];
136136
if (colorDesc && colorDesc.defaults) {
137137
const colorValue = colorDesc.defaults[theme.type];
138138
return resolveColorValue(colorValue, theme);
139139
}
140-
return null;
140+
return undefined;
141141
}
142142

143143
public getColorSchema(): IJSONSchema {
@@ -385,7 +385,7 @@ export function darken(colorValue: ColorValue, factor: number): ColorFunction {
385385
if (color) {
386386
return color.darken(factor);
387387
}
388-
return null;
388+
return undefined;
389389
};
390390
}
391391

@@ -395,7 +395,7 @@ export function lighten(colorValue: ColorValue, factor: number): ColorFunction {
395395
if (color) {
396396
return color.lighten(factor);
397397
}
398-
return null;
398+
return undefined;
399399
};
400400
}
401401

@@ -405,7 +405,7 @@ export function transparent(colorValue: ColorValue, factor: number): ColorFuncti
405405
if (color) {
406406
return color.transparent(factor);
407407
}
408-
return null;
408+
return undefined;
409409
};
410410
}
411411

@@ -417,7 +417,7 @@ export function oneOf(...colorValues: ColorValue[]): ColorFunction {
417417
return color;
418418
}
419419
}
420-
return null;
420+
return undefined;
421421
};
422422
}
423423

@@ -434,7 +434,7 @@ function lessProminent(colorValue: ColorValue, backgroundColorValue: ColorValue,
434434
}
435435
return from.transparent(factor * transparency);
436436
}
437-
return null;
437+
return undefined;
438438
};
439439
}
440440

@@ -443,9 +443,9 @@ function lessProminent(colorValue: ColorValue, backgroundColorValue: ColorValue,
443443
/**
444444
* @param colorValue Resolve a color value in the context of a theme
445445
*/
446-
function resolveColorValue(colorValue: ColorValue | null, theme: ITheme): Color | null {
446+
function resolveColorValue(colorValue: ColorValue | null, theme: ITheme): Color | undefined {
447447
if (colorValue === null) {
448-
return null;
448+
return undefined;
449449
} else if (typeof colorValue === 'string') {
450450
if (colorValue[0] === '#') {
451451
return Color.fromHex(colorValue);
@@ -456,7 +456,7 @@ function resolveColorValue(colorValue: ColorValue | null, theme: ITheme): Color
456456
} else if (typeof colorValue === 'function') {
457457
return colorValue(theme);
458458
}
459-
return null;
459+
return undefined;
460460
}
461461

462462
export const workbenchColorsSchemaId = 'vscode://schemas/workbench-colors';

src/vs/platform/theme/common/styler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IDisposable } from 'vs/base/common/lifecycle';
99
import { Color } from 'vs/base/common/color';
1010
import { mixin } from 'vs/base/common/objects';
1111

12-
export type styleFn = (colors: { [name: string]: Color | null }) => void;
12+
export type styleFn = (colors: { [name: string]: Color | undefined }) => void;
1313

1414
export interface IStyleOverrides {
1515
[color: string]: ColorIdentifier | undefined;
@@ -24,7 +24,7 @@ export interface IColorMapping {
2424
}
2525

2626
export interface IComputedStyles {
27-
[color: string]: Color | null;
27+
[color: string]: Color | undefined;
2828
}
2929

3030
export function computeStyles(theme: ITheme, styleMap: IColorMapping): IComputedStyles {

src/vs/platform/theme/common/themeService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface ITheme {
5252
* @param color the id of the color
5353
* @param useDefault specifies if the default color should be used. If not set, the default is used.
5454
*/
55-
getColor(color: ColorIdentifier, useDefault?: boolean): Color | null;
55+
getColor(color: ColorIdentifier, useDefault?: boolean): Color | undefined;
5656

5757
/**
5858
* Returns whether the theme defines a value for the color. If not, that means the

src/vs/platform/theme/test/common/testThemeService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export class TestTheme implements ITheme {
1212
constructor(private colors: { [id: string]: string; } = {}, public type = DARK) {
1313
}
1414

15-
getColor(color: string, useDefault?: boolean): Color | null {
15+
getColor(color: string, useDefault?: boolean): Color | undefined {
1616
let value = this.colors[color];
1717
if (value) {
1818
return Color.fromHex(value);
1919
}
20-
return null;
20+
return undefined;
2121
}
2222

2323
defines(color: string): boolean {

0 commit comments

Comments
 (0)