Skip to content

Commit 9d66737

Browse files
author
Matt Bierner
committed
Strict null fixes related to activeEditor now possibly being undefined
1 parent ea755ec commit 9d66737

7 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/vs/workbench/browser/parts/editor/editorCommands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export function splitEditor(editorGroupService: IEditorGroupsService, direction:
422422
const newGroup = editorGroupService.addGroup(sourceGroup, direction);
423423

424424
// Split editor (if it can be split)
425-
let editorToCopy: IEditorInput;
425+
let editorToCopy: IEditorInput | null;
426426
if (context && typeof context.editorIndex === 'number') {
427427
editorToCopy = sourceGroup.getEditor(context.editorIndex);
428428
} else {

src/vs/workbench/browser/parts/editor/editorControl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export class EditorControl extends Disposable {
5353
this.editorOperation = this._register(new LongRunningOperation(progressService));
5454
}
5555

56-
get activeControl(): IActiveEditor {
57-
return this._activeControl;
56+
get activeControl(): IActiveEditor | null {
57+
return this._activeControl as IActiveEditor | null;
5858
}
5959

6060
openEditor(editor: EditorInput, options?: EditorOptions): Promise<IOpenEditorResult> {

src/vs/workbench/browser/parts/editor/tabsTitleControl.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ export class TabsTitleControl extends TitleControl {
562562
disposables.push(addDisposableListener(tab, EventType.DBLCLICK, (e: MouseEvent) => {
563563
EventHelper.stop(e);
564564

565-
this.group.pinEditor(this.group.getEditor(index));
565+
this.group.pinEditor(this.group.getEditor(index) || undefined);
566566
}));
567567

568568
// Context menu
@@ -670,7 +670,8 @@ export class TabsTitleControl extends TitleControl {
670670

671671
private updateDropFeedback(element: HTMLElement, isDND: boolean, index?: number): void {
672672
const isTab = (typeof index === 'number');
673-
const isActiveTab = isTab && this.group.isActive(this.group.getEditor(index));
673+
const editor = typeof index === 'number' ? this.group.getEditor(index) : null;
674+
const isActiveTab = isTab && !!editor && this.group.isActive(editor);
674675

675676
// Background
676677
const noDNDBackgroundColor = isTab ? this.getColor(isActiveTab ? TAB_ACTIVE_BACKGROUND : TAB_INACTIVE_BACKGROUND) : null;
@@ -1032,7 +1033,7 @@ export class TabsTitleControl extends TitleControl {
10321033
}
10331034
}
10341035

1035-
private getTab(editor: IEditorInput): HTMLElement {
1036+
private getTab(editor: IEditorInput): HTMLElement | undefined {
10361037
const editorIndex = this.group.getIndexOfEditor(editor);
10371038
if (editorIndex >= 0) {
10381039
return this.tabsContainer.children[editorIndex] as HTMLElement;
@@ -1193,12 +1194,12 @@ registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
11931194
const editorGroupHeaderTabsBackground = theme.getColor(EDITOR_GROUP_HEADER_TABS_BACKGROUND);
11941195
const editorDragAndDropBackground = theme.getColor(EDITOR_DRAG_AND_DROP_BACKGROUND);
11951196

1196-
let adjustedTabBackground: Color;
1197+
let adjustedTabBackground: Color | undefined;
11971198
if (editorGroupHeaderTabsBackground && editorBackgroundColor) {
11981199
adjustedTabBackground = editorGroupHeaderTabsBackground.flatten(editorBackgroundColor, editorBackgroundColor, workbenchBackground);
11991200
}
12001201

1201-
let adjustedTabDragBackground: Color;
1202+
let adjustedTabDragBackground: Color | undefined;
12021203
if (editorGroupHeaderTabsBackground && editorBackgroundColor && editorDragAndDropBackground && editorBackgroundColor) {
12031204
adjustedTabDragBackground = editorGroupHeaderTabsBackground.flatten(editorBackgroundColor, editorDragAndDropBackground, editorBackgroundColor, workbenchBackground);
12041205
}

src/vs/workbench/contrib/files/browser/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function getResourceForCommand(resource: URI | object, listService: IList
4141
}
4242
}
4343

44-
return toResource(editorService.activeEditor, { supportSideBySide: true });
44+
return editorService.activeEditor ? toResource(editorService.activeEditor, { supportSideBySide: true }) : null;
4545
}
4646

4747
export function getMultiSelectedResources(resource: URI | object, listService: IListService, editorService: IEditorService): Array<URI> {

src/vs/workbench/contrib/quickopen/browser/gotoLineHandler.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export class GotoLineAction extends QuickOpenAction {
4040
run(): Promise<void> {
4141

4242
let activeTextEditorWidget = this.editorService.activeTextEditorWidget;
43+
if (!activeTextEditorWidget) {
44+
return Promise.resolve();
45+
}
46+
4347
if (isDiffEditor(activeTextEditorWidget)) {
4448
activeTextEditorWidget = activeTextEditorWidget.getModifiedEditor();
4549
}
@@ -61,7 +65,7 @@ export class GotoLineAction extends QuickOpenAction {
6165

6266
if (restoreOptions) {
6367
Event.once(this._quickOpenService.onHide)(() => {
64-
activeTextEditorWidget.updateOptions(restoreOptions!);
68+
activeTextEditorWidget!.updateOptions(restoreOptions!);
6569
});
6670
}
6771

@@ -92,7 +96,7 @@ class GotoLineEntry extends EditorQuickOpenEntry {
9296
// Inform user about valid range if input is invalid
9397
const maxLineNumber = this.getMaxLineNumber();
9498

95-
if (this.invalidRange(maxLineNumber)) {
99+
if (this.editorService.activeTextEditorWidget && this.invalidRange(maxLineNumber)) {
96100
const position = this.editorService.activeTextEditorWidget.getPosition();
97101
if (position) {
98102
const currentLine = position.lineNumber;
@@ -115,6 +119,9 @@ class GotoLineEntry extends EditorQuickOpenEntry {
115119

116120
private getMaxLineNumber(): number {
117121
const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
122+
if (!activeTextEditorWidget) {
123+
return -1;
124+
}
118125

119126
let model = activeTextEditorWidget.getModel();
120127
if (model && (<IDiffEditorModel>model).modified && (<IDiffEditorModel>model).original) {
@@ -132,8 +139,8 @@ class GotoLineEntry extends EditorQuickOpenEntry {
132139
return this.runPreview();
133140
}
134141

135-
getInput(): IEditorInput {
136-
return this.editorService.activeEditor;
142+
getInput(): IEditorInput | null {
143+
return this.editorService.activeEditor || null;
137144
}
138145

139146
getOptions(pinned?: boolean): ITextEditorOptions {
@@ -153,7 +160,7 @@ class GotoLineEntry extends EditorQuickOpenEntry {
153160
// Check for sideBySide use
154161
const sideBySide = context.keymods.ctrlCmd;
155162
if (sideBySide) {
156-
this.editorService.openEditor(this.getInput(), this.getOptions(context.keymods.alt), SIDE_GROUP);
163+
this.editorService.openEditor(this.getInput()!, this.getOptions(context.keymods.alt), SIDE_GROUP);
157164
}
158165

159166
// Apply selection and focus
@@ -183,8 +190,8 @@ class GotoLineEntry extends EditorQuickOpenEntry {
183190
activeTextEditorWidget.revealRangeInCenter(range, ScrollType.Smooth);
184191

185192
// Decorate if possible
186-
if (types.isFunction(activeTextEditorWidget.changeDecorations)) {
187-
this.handler.decorateOutline(range, activeTextEditorWidget, this.editorService.activeControl.group!);
193+
if (this.editorService.activeControl && types.isFunction(activeTextEditorWidget.changeDecorations)) {
194+
this.handler.decorateOutline(range, activeTextEditorWidget, this.editorService.activeControl.group);
188195
}
189196
}
190197

@@ -236,7 +243,9 @@ export class GotoLineHandler extends QuickOpenHandler {
236243
// Remember view state to be able to restore on cancel
237244
if (!this.lastKnownEditorViewState) {
238245
const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
239-
this.lastKnownEditorViewState = activeTextEditorWidget.saveViewState();
246+
if (activeTextEditorWidget) {
247+
this.lastKnownEditorViewState = activeTextEditorWidget.saveViewState();
248+
}
240249
}
241250

242251
return Promise.resolve(new QuickOpenModel([new GotoLineEntry(searchValue, this.editorService, this)]));

src/vs/workbench/contrib/quickopen/browser/gotoSymbolHandler.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ class SymbolEntry extends EditorQuickOpenEntryGroup {
288288
return this.range;
289289
}
290290

291-
getInput(): IEditorInput {
292-
return this.editorService.activeEditor;
291+
getInput(): IEditorInput | null {
292+
return this.editorService.activeEditor || null;
293293
}
294294

295295
getOptions(pinned?: boolean): ITextEditorOptions {
@@ -312,7 +312,7 @@ class SymbolEntry extends EditorQuickOpenEntryGroup {
312312
// Check for sideBySide use
313313
const sideBySide = context.keymods.ctrlCmd;
314314
if (sideBySide) {
315-
this.editorService.openEditor(this.getInput(), this.getOptions(context.keymods.alt), SIDE_GROUP);
315+
this.editorService.openEditor(this.getInput()!, this.getOptions(context.keymods.alt), SIDE_GROUP);
316316
}
317317

318318
// Apply selection and focus
@@ -337,8 +337,8 @@ class SymbolEntry extends EditorQuickOpenEntryGroup {
337337
activeTextEditorWidget.revealRangeInCenter(range, ScrollType.Smooth);
338338

339339
// Decorate if possible
340-
if (types.isFunction(activeTextEditorWidget.changeDecorations)) {
341-
this.handler.decorateOutline(this.range, range, activeTextEditorWidget, this.editorService.activeControl.group!);
340+
if (this.editorService.activeControl && types.isFunction(activeTextEditorWidget.changeDecorations)) {
341+
this.handler.decorateOutline(this.range, range, activeTextEditorWidget, this.editorService.activeControl.group);
342342
}
343343
}
344344

@@ -401,7 +401,9 @@ export class GotoSymbolHandler extends QuickOpenHandler {
401401
// Remember view state to be able to restore on cancel
402402
if (!this.lastKnownEditorViewState) {
403403
const activeTextEditorWidget = this.editorService.activeTextEditorWidget;
404-
this.lastKnownEditorViewState = activeTextEditorWidget.saveViewState();
404+
if (activeTextEditorWidget) {
405+
this.lastKnownEditorViewState = activeTextEditorWidget.saveViewState();
406+
}
405407
}
406408

407409
// Resolve Outline Model

src/vs/workbench/services/history/browser/history.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class HistoryService extends Disposable implements IHistoryService {
217217
// Track the last edit location by tracking model content change events
218218
// Use a debouncer to make sure to capture the correct cursor position
219219
// after the model content has changed.
220-
this.activeEditorListeners.push(Event.debounce(activeTextEditorWidget.onDidChangeModelContent, (last, event) => event, 0)((event => this.rememberLastEditLocation(activeEditor, activeTextEditorWidget))));
220+
this.activeEditorListeners.push(Event.debounce(activeTextEditorWidget.onDidChangeModelContent, (last, event) => event, 0)((event => this.rememberLastEditLocation(activeEditor!, activeTextEditorWidget))));
221221
}
222222
}
223223

0 commit comments

Comments
 (0)