Skip to content

Commit f9775c5

Browse files
committed
Strict null checking findWidget
1 parent 1541353 commit f9775c5

5 files changed

Lines changed: 101 additions & 88 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
"./vs/editor/contrib/find/findModel.ts",
296296
"./vs/editor/contrib/find/findOptionsWidget.ts",
297297
"./vs/editor/contrib/find/findState.ts",
298+
"./vs/editor/contrib/find/findWidget.ts",
298299
"./vs/editor/contrib/find/replaceAllCommand.ts",
299300
"./vs/editor/contrib/find/replacePattern.ts",
300301
"./vs/editor/contrib/folding/folding.ts",
@@ -505,6 +506,7 @@
505506
"./vs/platform/statusbar/common/statusbar.ts",
506507
"./vs/platform/storage/common/storage.ts",
507508
"./vs/platform/storage/node/storageMainService.ts",
509+
"./vs/platform/storage/node/storageService.ts",
508510
"./vs/platform/telemetry/browser/errorTelemetry.ts",
509511
"./vs/platform/telemetry/common/telemetry.ts",
510512
"./vs/platform/telemetry/common/telemetryService.ts",
@@ -746,4 +748,4 @@
746748
"exclude": [
747749
"./typings/require-monaco.d.ts"
748750
]
749-
}
751+
}

src/vs/base/browser/ui/findinput/findInput.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface IFindInputOptions extends IFindInputStyles {
3232
}
3333

3434
export interface IFindInputStyles extends IInputBoxStyles {
35-
inputActiveOptionBorder?: Color;
35+
inputActiveOptionBorder?: Color | null;
3636
}
3737

3838
const NLS_DEFAULT_LABEL = nls.localize('defaultLabel', "input");
@@ -48,20 +48,20 @@ export class FindInput extends Widget {
4848
private label: string;
4949
private fixFocusOnOptionClickEnabled = true;
5050

51-
private inputActiveOptionBorder?: Color;
52-
private inputBackground?: Color;
53-
private inputForeground?: Color;
54-
private inputBorder?: Color;
55-
56-
private inputValidationInfoBorder?: Color;
57-
private inputValidationInfoBackground?: Color;
58-
private inputValidationInfoForeground?: Color;
59-
private inputValidationWarningBorder?: Color;
60-
private inputValidationWarningBackground?: Color;
61-
private inputValidationWarningForeground?: Color;
62-
private inputValidationErrorBorder?: Color;
63-
private inputValidationErrorBackground?: Color;
64-
private inputValidationErrorForeground?: Color;
51+
private inputActiveOptionBorder?: Color | null;
52+
private inputBackground?: Color | null;
53+
private inputForeground?: Color | null;
54+
private inputBorder?: Color | null;
55+
56+
private inputValidationInfoBorder?: Color | null;
57+
private inputValidationInfoBackground?: Color | null;
58+
private inputValidationInfoForeground?: Color | null;
59+
private inputValidationWarningBorder?: Color | null;
60+
private inputValidationWarningBackground?: Color | null;
61+
private inputValidationWarningForeground?: Color | null;
62+
private inputValidationErrorBorder?: Color | null;
63+
private inputValidationErrorBackground?: Color | null;
64+
private inputValidationErrorForeground?: Color | null;
6565

6666
private regex: RegexCheckbox;
6767
private wholeWords: WholeWordsCheckbox;
@@ -90,7 +90,7 @@ export class FindInput extends Widget {
9090
private _onRegexKeyDown = this._register(new Emitter<IKeyboardEvent>());
9191
public readonly onRegexKeyDown: Event<IKeyboardEvent> = this._onRegexKeyDown.event;
9292

93-
constructor(parent: HTMLElement, contextViewProvider: IContextViewProvider, private readonly _showOptionButtons: boolean, options: IFindInputOptions) {
93+
constructor(parent: HTMLElement | null, contextViewProvider: IContextViewProvider, private readonly _showOptionButtons: boolean, options: IFindInputOptions) {
9494
super();
9595
this.contextViewProvider = contextViewProvider;
9696
this.width = options.width || 100;
@@ -115,7 +115,7 @@ export class FindInput extends Widget {
115115

116116
this.buildDomNode(options.appendCaseSensitiveLabel || '', options.appendWholeWordsLabel || '', options.appendRegexLabel || '', options.history || [], !!options.flexibleHeight);
117117

118-
if (Boolean(parent)) {
118+
if (parent) {
119119
parent.appendChild(this.domNode);
120120
}
121121

@@ -202,7 +202,7 @@ export class FindInput extends Widget {
202202
protected applyStyles(): void {
203203
if (this.domNode) {
204204
const checkBoxStyles: ICheckboxStyles = {
205-
inputActiveOptionBorder: this.inputActiveOptionBorder,
205+
inputActiveOptionBorder: this.inputActiveOptionBorder || undefined,
206206
};
207207
this.regex.style(checkBoxStyles);
208208
this.wholeWords.style(checkBoxStyles);
@@ -313,7 +313,7 @@ export class FindInput extends Widget {
313313
this.regex = this._register(new RegexCheckbox({
314314
appendTitle: appendRegexLabel,
315315
isChecked: false,
316-
inputActiveOptionBorder: this.inputActiveOptionBorder
316+
inputActiveOptionBorder: this.inputActiveOptionBorder || undefined
317317
}));
318318
this._register(this.regex.onChange(viaKeyboard => {
319319
this._onDidOptionChange.fire(viaKeyboard);
@@ -330,7 +330,7 @@ export class FindInput extends Widget {
330330
this.wholeWords = this._register(new WholeWordsCheckbox({
331331
appendTitle: appendWholeWordsLabel,
332332
isChecked: false,
333-
inputActiveOptionBorder: this.inputActiveOptionBorder
333+
inputActiveOptionBorder: this.inputActiveOptionBorder || undefined
334334
}));
335335
this._register(this.wholeWords.onChange(viaKeyboard => {
336336
this._onDidOptionChange.fire(viaKeyboard);
@@ -344,7 +344,7 @@ export class FindInput extends Widget {
344344
this.caseSensitive = this._register(new CaseSensitiveCheckbox({
345345
appendTitle: appendCaseSensitiveLabel,
346346
isChecked: false,
347-
inputActiveOptionBorder: this.inputActiveOptionBorder
347+
inputActiveOptionBorder: this.inputActiveOptionBorder || undefined
348348
}));
349349
this._register(this.caseSensitive.onChange(viaKeyboard => {
350350
this._onDidOptionChange.fire(viaKeyboard);

src/vs/base/browser/ui/inputbox/inputBox.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ export interface IInputOptions extends IInputBoxStyles {
3232
}
3333

3434
export interface IInputBoxStyles {
35-
readonly inputBackground?: Color;
36-
readonly inputForeground?: Color;
37-
readonly inputBorder?: Color;
38-
readonly inputValidationInfoBorder?: Color;
39-
readonly inputValidationInfoBackground?: Color;
40-
readonly inputValidationInfoForeground?: Color;
41-
readonly inputValidationWarningBorder?: Color;
42-
readonly inputValidationWarningBackground?: Color;
43-
readonly inputValidationWarningForeground?: Color;
44-
readonly inputValidationErrorBorder?: Color;
45-
readonly inputValidationErrorBackground?: Color;
46-
readonly inputValidationErrorForeground?: Color;
35+
readonly inputBackground?: Color | null;
36+
readonly inputForeground?: Color | null;
37+
readonly inputBorder?: Color | null;
38+
readonly inputValidationInfoBorder?: Color | null;
39+
readonly inputValidationInfoBackground?: Color | null;
40+
readonly inputValidationInfoForeground?: Color | null;
41+
readonly inputValidationWarningBorder?: Color | null;
42+
readonly inputValidationWarningBackground?: Color | null;
43+
readonly inputValidationWarningForeground?: Color | null;
44+
readonly inputValidationErrorBorder?: Color | null;
45+
readonly inputValidationErrorBackground?: Color | null;
46+
readonly inputValidationErrorForeground?: Color | null;
4747
}
4848

4949
export interface IInputValidator {
50-
(value: string): IMessage;
50+
(value: string): IMessage | null;
5151
}
5252

5353
export interface IMessage {
@@ -96,27 +96,27 @@ export class InputBox extends Widget {
9696
private state: string | null = 'idle';
9797
private cachedHeight: number | null;
9898

99-
private inputBackground?: Color;
100-
private inputForeground?: Color;
101-
private inputBorder?: Color;
99+
private inputBackground?: Color | null;
100+
private inputForeground?: Color | null;
101+
private inputBorder?: Color | null;
102102

103-
private inputValidationInfoBorder?: Color;
104-
private inputValidationInfoBackground?: Color;
105-
private inputValidationInfoForeground?: Color;
106-
private inputValidationWarningBorder?: Color;
107-
private inputValidationWarningBackground?: Color;
108-
private inputValidationWarningForeground?: Color;
109-
private inputValidationErrorBorder?: Color;
110-
private inputValidationErrorBackground?: Color;
111-
private inputValidationErrorForeground?: Color;
103+
private inputValidationInfoBorder?: Color | null;
104+
private inputValidationInfoBackground?: Color | null;
105+
private inputValidationInfoForeground?: Color | null;
106+
private inputValidationWarningBorder?: Color | null;
107+
private inputValidationWarningBackground?: Color | null;
108+
private inputValidationWarningForeground?: Color | null;
109+
private inputValidationErrorBorder?: Color | null;
110+
private inputValidationErrorBackground?: Color | null;
111+
private inputValidationErrorForeground?: Color | null;
112112

113113
private _onDidChange = this._register(new Emitter<string>());
114114
public readonly onDidChange: Event<string> = this._onDidChange.event;
115115

116116
private _onDidHeightChange = this._register(new Emitter<number>());
117117
public readonly onDidHeightChange: Event<number> = this._onDidHeightChange.event;
118118

119-
constructor(container: HTMLElement, contextViewProvider: IContextViewProvider, options?: IInputOptions) {
119+
constructor(container: HTMLElement, contextViewProvider: IContextViewProvider | undefined, options?: IInputOptions) {
120120
super();
121121

122122
this.contextViewProvider = contextViewProvider;
@@ -361,7 +361,7 @@ export class InputBox extends Widget {
361361
return !errorMsg;
362362
}
363363

364-
private stylesForType(type: MessageType | undefined): { border: Color | undefined; background: Color | undefined; foreground: Color | undefined } {
364+
private stylesForType(type: MessageType | undefined): { border: Color | undefined | null; background: Color | undefined | null; foreground: Color | undefined | null } {
365365
switch (type) {
366366
case MessageType.INFO: return { border: this.inputValidationInfoBorder, background: this.inputValidationInfoBackground, foreground: this.inputValidationInfoForeground };
367367
case MessageType.WARNING: return { border: this.inputValidationWarningBorder, background: this.inputValidationWarningBackground, foreground: this.inputValidationWarningForeground };
@@ -533,7 +533,7 @@ export class HistoryInputBox extends InputBox implements IHistoryNavigationWidge
533533

534534
private readonly history: HistoryNavigator<string>;
535535

536-
constructor(container: HTMLElement, contextViewProvider: IContextViewProvider, options: IHistoryInputOptions) {
536+
constructor(container: HTMLElement, contextViewProvider: IContextViewProvider | undefined, options: IHistoryInputOptions) {
537537
super(container, contextViewProvider, options);
538538
this.history = new HistoryNavigator<string>(options.history, 100);
539539
}

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

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
111111
private _replaceFocusTracker: dom.IFocusTracker;
112112
private _replaceInputFocused: IContextKey<boolean>;
113113
private _viewZone: FindWidgetViewZone;
114-
private _viewZoneId: number;
114+
private _viewZoneId?: number;
115115

116116
private _resizeSash: Sash;
117117
private _resized: boolean;
@@ -210,7 +210,9 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
210210
return;
211211
}
212212
this._codeEditor.changeViewZones((accessor) => {
213-
accessor.removeZone(this._viewZoneId);
213+
if (this._viewZoneId) {
214+
accessor.removeZone(this._viewZoneId);
215+
}
214216
this._viewZoneId = undefined;
215217
});
216218
}));
@@ -239,7 +241,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
239241
return this._domNode;
240242
}
241243

242-
public getPosition(): IOverlayWidgetPosition {
244+
public getPosition(): IOverlayWidgetPosition | null {
243245
if (this._isVisible) {
244246
return {
245247
preference: OverlayWidgetPositionPreference.TOP_RIGHT_CORNER
@@ -401,8 +403,8 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
401403
if (!this._isVisible) {
402404
this._isVisible = true;
403405

404-
let selection = this._codeEditor.getSelection();
405-
let isSelection = selection ? (selection.startLineNumber !== selection.endLineNumber || selection.startColumn !== selection.endColumn) : false;
406+
const selection = this._codeEditor.getSelection();
407+
const isSelection = selection ? (selection.startLineNumber !== selection.endLineNumber || selection.startColumn !== selection.endColumn) : false;
406408
if (isSelection && this._codeEditor.getConfiguration().contribInfo.find.autoFindInSelection) {
407409
this._toggleSelectionFind.checked = true;
408410
} else {
@@ -425,24 +427,27 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
425427

426428
let adjustEditorScrollTop = true;
427429
if (this._codeEditor.getConfiguration().contribInfo.find.seedSearchStringFromSelection && selection) {
428-
let editorCoords = dom.getDomNodePagePosition(this._codeEditor.getDomNode());
429-
let startCoords = this._codeEditor.getScrolledVisiblePosition(selection.getStartPosition());
430-
let startLeft = editorCoords.left + startCoords.left;
431-
let startTop = startCoords.top;
432-
433-
if (startTop < this._viewZone.heightInPx) {
434-
if (selection.endLineNumber > selection.startLineNumber) {
435-
adjustEditorScrollTop = false;
436-
}
430+
const domNode = this._codeEditor.getDomNode();
431+
if (domNode) {
432+
const editorCoords = dom.getDomNodePagePosition(domNode);
433+
const startCoords = this._codeEditor.getScrolledVisiblePosition(selection.getStartPosition());
434+
const startLeft = editorCoords.left + (startCoords ? startCoords.left : 0);
435+
const startTop = startCoords ? startCoords.top : 0;
436+
437+
if (startTop < this._viewZone.heightInPx) {
438+
if (selection.endLineNumber > selection.startLineNumber) {
439+
adjustEditorScrollTop = false;
440+
}
437441

438-
let leftOfFindWidget = dom.getTopLeftOffset(this._domNode).left;
439-
if (startLeft > leftOfFindWidget) {
440-
adjustEditorScrollTop = false;
441-
}
442-
let endCoords = this._codeEditor.getScrolledVisiblePosition(selection.getEndPosition());
443-
let endLeft = editorCoords.left + endCoords.left;
444-
if (endLeft > leftOfFindWidget) {
445-
adjustEditorScrollTop = false;
442+
const leftOfFindWidget = dom.getTopLeftOffset(this._domNode).left;
443+
if (startLeft > leftOfFindWidget) {
444+
adjustEditorScrollTop = false;
445+
}
446+
const endCoords = this._codeEditor.getScrolledVisiblePosition(selection.getEndPosition());
447+
const endLeft = editorCoords.left + (endCoords ? endCoords.left : 0);
448+
if (endLeft > leftOfFindWidget) {
449+
adjustEditorScrollTop = false;
450+
}
446451
}
447452
}
448453
}
@@ -609,12 +614,16 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
609614
}
610615

611616
private _updateSearchScope(): void {
617+
if (!this._codeEditor.hasModel()) {
618+
return;
619+
}
620+
612621
if (this._toggleSelectionFind.checked) {
613622
let selection = this._codeEditor.getSelection();
614623
if (selection.endColumn === 1 && selection.endLineNumber > selection.startLineNumber) {
615624
selection = selection.setEndPosition(selection.endLineNumber - 1, this._codeEditor.getModel().getLineMaxColumn(selection.endLineNumber - 1));
616625
}
617-
let currentMatch = this._state.currentMatch;
626+
const currentMatch = this._state.currentMatch;
618627
if (selection.startLineNumber !== selection.endLineNumber) {
619628
if (!Range.equalsRange(selection, currentMatch)) {
620629
// Reseed find scope
@@ -725,7 +734,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
725734
appendCaseSensitiveLabel: this._keybindingLabelFor(FIND_IDS.ToggleCaseSensitiveCommand),
726735
appendWholeWordsLabel: this._keybindingLabelFor(FIND_IDS.ToggleWholeWordCommand),
727736
appendRegexLabel: this._keybindingLabelFor(FIND_IDS.ToggleRegexCommand),
728-
validation: (value: string): InputBoxMessage => {
737+
validation: (value: string): InputBoxMessage | null => {
729738
if (value.length === 0) {
730739
return null;
731740
}
@@ -806,15 +815,17 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
806815
title: NLS_TOGGLE_SELECTION_FIND_TITLE + this._keybindingLabelFor(FIND_IDS.ToggleSearchScopeCommand),
807816
onChange: () => {
808817
if (this._toggleSelectionFind.checked) {
809-
let selection = this._codeEditor.getSelection();
810-
if (selection.endColumn === 1 && selection.endLineNumber > selection.startLineNumber) {
811-
selection = selection.setEndPosition(selection.endLineNumber - 1, this._codeEditor.getModel().getLineMaxColumn(selection.endLineNumber - 1));
812-
}
813-
if (!selection.isEmpty()) {
814-
this._state.change({ searchScope: selection }, true);
818+
if (this._codeEditor.hasModel()) {
819+
let selection = this._codeEditor.getSelection();
820+
if (selection.endColumn === 1 && selection.endLineNumber > selection.startLineNumber) {
821+
selection = selection.setEndPosition(selection.endLineNumber - 1, this._codeEditor.getModel().getLineMaxColumn(selection.endLineNumber - 1));
822+
}
823+
if (!selection.isEmpty()) {
824+
this._state.change({ searchScope: selection }, true);
825+
}
815826
}
816827
} else {
817-
this._state.change({ searchScope: null }, true);
828+
this._state.change({ searchScope: undefined }, true);
818829
}
819830
}
820831
}));
@@ -824,7 +835,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
824835
label: NLS_CLOSE_BTN_LABEL + this._keybindingLabelFor(FIND_IDS.CloseFindWidgetCommand),
825836
className: 'close-fw',
826837
onTrigger: () => {
827-
this._state.change({ isRevealed: false, searchScope: null }, false);
838+
this._state.change({ isRevealed: false, searchScope: undefined }, false);
828839
},
829840
onKeyDown: (e) => {
830841
if (e.equals(KeyCode.Tab)) {
@@ -850,7 +861,7 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
850861
let replaceInput = document.createElement('div');
851862
replaceInput.className = 'replace-input';
852863
replaceInput.style.width = REPLACE_INPUT_AREA_WIDTH + 'px';
853-
this._replaceInputBox = this._register(new ContextScopedHistoryInputBox(replaceInput, null, {
864+
this._replaceInputBox = this._register(new ContextScopedHistoryInputBox(replaceInput, void 0, {
854865
ariaLabel: NLS_REPLACE_INPUT_LABEL,
855866
placeholder: NLS_REPLACE_INPUT_PLACEHOLDER,
856867
history: []
@@ -950,8 +961,8 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
950961
return;
951962
}
952963

953-
let inputBoxWidth = width - FIND_ALL_CONTROLS_WIDTH;
954-
let maxWidth = parseFloat(dom.getComputedStyle(this._domNode).maxWidth) || 0;
964+
const inputBoxWidth = width - FIND_ALL_CONTROLS_WIDTH;
965+
const maxWidth = parseFloat(dom.getComputedStyle(this._domNode).maxWidth!) || 0;
955966
if (width > maxWidth) {
956967
return;
957968
}
@@ -1119,7 +1130,7 @@ export class SimpleButton extends Widget {
11191130
// theming
11201131

11211132
registerThemingParticipant((theme, collector) => {
1122-
const addBackgroundColorRule = (selector: string, color: Color): void => {
1133+
const addBackgroundColorRule = (selector: string, color: Color | null): void => {
11231134
if (color) {
11241135
collector.addRule(`.monaco-editor ${selector} { background-color: ${color}; }`);
11251136
}

0 commit comments

Comments
 (0)