Skip to content

Commit ad040a2

Browse files
committed
Strict null check history
1 parent 67e52a2 commit ad040a2

6 files changed

Lines changed: 35 additions & 28 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@
765765
"./vs/workbench/services/hash/common/hashService.ts",
766766
"./vs/workbench/services/hash/node/hashService.ts",
767767
"./vs/workbench/services/history/common/history.ts",
768+
"./vs/workbench/services/history/electron-browser/history.ts",
768769
"./vs/workbench/services/issue/common/issue.ts",
769770
"./vs/workbench/services/issue/electron-browser/workbenchIssueService.ts",
770771
"./vs/workbench/services/jsonschemas/common/jsonValidationExtensionPoint.ts",

src/vs/workbench/common/editor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ export interface IEncodingSupport {
510510
*/
511511
export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
512512

513+
getResource(): URI;
514+
513515
/**
514516
* Sets the preferred encodingt to use for this input.
515517
*/

src/vs/workbench/parts/terminal/electron-browser/terminalProcessManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class TerminalProcessManager implements ITerminalProcessManager {
112112
this.initialCwd = terminalEnvironment.getCwd(shellLaunchConfig, activeWorkspaceRootUri, this._configHelper.config.cwd);
113113

114114
// Resolve env vars from config and shell
115-
const lastActiveWorkspaceRoot = this._workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri);
115+
const lastActiveWorkspaceRoot = activeWorkspaceRootUri ? this._workspaceContextService.getWorkspaceFolder(activeWorkspaceRootUri) : null;
116116
const platformKey = platform.isWindows ? 'windows' : (platform.isMacintosh ? 'osx' : 'linux');
117117
const envFromConfig = terminalEnvironment.resolveConfigurationVariables(this._configurationResolverService, { ...this._configHelper.config.env[platformKey] }, lastActiveWorkspaceRoot);
118118
const envFromShell = terminalEnvironment.resolveConfigurationVariables(this._configurationResolverService, { ...shellLaunchConfig.env }, lastActiveWorkspaceRoot);

src/vs/workbench/services/dialogs/electron-browser/dialogService.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ export class FileDialogService implements IFileDialogService {
164164
) { }
165165

166166
public defaultFilePath(schemeFilter: string): URI | undefined {
167-
let candidate: URI;
168-
169167
// Check for last active file first...
170-
candidate = this.historyService.getLastActiveFile(schemeFilter);
168+
let candidate = this.historyService.getLastActiveFile(schemeFilter);
171169

172170
// ...then for last active file root
173171
if (!candidate) {
@@ -178,10 +176,8 @@ export class FileDialogService implements IFileDialogService {
178176
}
179177

180178
public defaultFolderPath(schemeFilter: string): URI | undefined {
181-
let candidate: URI;
182-
183179
// Check for last active file root first...
184-
candidate = this.historyService.getLastActiveWorkspaceRoot(schemeFilter);
180+
let candidate = this.historyService.getLastActiveWorkspaceRoot(schemeFilter);
185181

186182
// ...then for last active file
187183
if (!candidate) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ export interface IHistoryService {
7171
*
7272
* @param schemeFilter filter to restrict roots by scheme.
7373
*/
74-
getLastActiveWorkspaceRoot(schemeFilter?: string): URI;
74+
getLastActiveWorkspaceRoot(schemeFilter?: string): URI | undefined;
7575

7676
/**
7777
* Looking at the editor history, returns the resource of the last file that was opened.
7878
*
7979
* @param schemeFilter filter to restrict roots by scheme.
8080
*/
81-
getLastActiveFile(schemeFilter: string): URI;
81+
getLastActiveFile(schemeFilter: string): URI | undefined;
8282
}

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export class TextEditorState {
3939

4040
private static readonly EDITOR_SELECTION_THRESHOLD = 10; // number of lines to move in editor to justify for new state
4141

42-
private textEditorSelection: ITextEditorSelection;
42+
private textEditorSelection?: ITextEditorSelection;
4343

44-
constructor(private _editorInput: IEditorInput, private _selection: Selection) {
44+
constructor(private _editorInput: IEditorInput, private _selection: Selection | null) {
4545
this.textEditorSelection = Selection.isISelection(_selection) ? {
4646
startLineNumber: _selection.startLineNumber,
4747
startColumn: _selection.startColumn
@@ -52,7 +52,7 @@ export class TextEditorState {
5252
return this._editorInput;
5353
}
5454

55-
get selection(): ITextEditorSelection {
55+
get selection(): ITextEditorSelection | undefined {
5656
return this.textEditorSelection;
5757
}
5858

@@ -105,7 +105,7 @@ export class HistoryService extends Disposable implements IHistoryService {
105105
private static readonly MAX_RECENTLY_CLOSED_EDITORS = 20;
106106

107107
private activeEditorListeners: IDisposable[];
108-
private lastActiveEditor: IEditorIdentifier;
108+
private lastActiveEditor?: IEditorIdentifier;
109109

110110
private editorHistoryListeners: Map<EditorInput, IDisposable[]> = new Map();
111111
private editorStackListeners: Map<EditorInput, IDisposable[]> = new Map();
@@ -114,7 +114,7 @@ export class HistoryService extends Disposable implements IHistoryService {
114114
private index: number;
115115
private lastIndex: number;
116116
private navigatingInStack: boolean;
117-
private currentTextEditorState: TextEditorState;
117+
private currentTextEditorState: TextEditorState | null;
118118

119119
private lastEditLocation: IStackEntry;
120120

@@ -156,7 +156,7 @@ export class HistoryService extends Disposable implements IHistoryService {
156156
this.loaded = false;
157157
this.resourceFilter = this._register(instantiationService.createInstance(
158158
ResourceGlobMatcher,
159-
(root: URI) => this.getExcludes(root),
159+
(root?: URI) => this.getExcludes(root),
160160
(event: IConfigurationChangeEvent) => event.affectsConfiguration(FILES_EXCLUDE_CONFIG) || event.affectsConfiguration('search.exclude')
161161
));
162162

@@ -166,7 +166,7 @@ export class HistoryService extends Disposable implements IHistoryService {
166166
private getExcludes(root?: URI): IExpression {
167167
const scope = root ? { resource: root } : undefined;
168168

169-
return getExcludes(this.configurationService.getValue<ISearchConfiguration>(scope));
169+
return getExcludes(scope ? this.configurationService.getValue<ISearchConfiguration>(scope) : this.configurationService.getValue<ISearchConfiguration>())!;
170170
}
171171

172172
private registerListeners(): void {
@@ -192,7 +192,7 @@ export class HistoryService extends Disposable implements IHistoryService {
192192
}
193193

194194
// Remember as last active editor (can be undefined if none opened)
195-
this.lastActiveEditor = activeControl ? { editor: activeControl.input, groupId: activeControl.group.id } : undefined;
195+
this.lastActiveEditor = activeControl && activeControl.input && activeControl.group ? { editor: activeControl.input, groupId: activeControl.group.id } : undefined;
196196

197197
// Dispose old listeners
198198
dispose(this.activeEditorListeners);
@@ -254,7 +254,7 @@ export class HistoryService extends Disposable implements IHistoryService {
254254
if (!event.replaced) {
255255
const resource = event.editor ? event.editor.getResource() : undefined;
256256
const supportsReopen = resource && this.fileService.canHandleResource(resource); // we only support file'ish things to reopen
257-
if (supportsReopen) {
257+
if (resource && supportsReopen) {
258258

259259
// Remove all inputs matching and add as last recently closed
260260
this.removeFromRecentlyClosedFiles(event.editor);
@@ -452,7 +452,7 @@ export class HistoryService extends Disposable implements IHistoryService {
452452

453453
// Respect max entries setting
454454
if (this.history.length > HistoryService.MAX_HISTORY_ITEMS) {
455-
this.clearOnEditorDispose(this.history.pop(), this.editorHistoryListeners);
455+
this.clearOnEditorDispose(this.history.pop()!, this.editorHistoryListeners);
456456
}
457457

458458
// Remove this from the history unless the history input is a resource
@@ -537,15 +537,15 @@ export class HistoryService extends Disposable implements IHistoryService {
537537
});
538538
}
539539

540-
private handleEditorEventInStack(control: IBaseEditor, event?: ICursorPositionChangedEvent): void {
540+
private handleEditorEventInStack(control: IBaseEditor | undefined, event?: ICursorPositionChangedEvent): void {
541541
const codeEditor = control ? getCodeEditor(control.getControl()) : undefined;
542542

543543
// treat editor changes that happen as part of stack navigation specially
544544
// we do not want to add a new stack entry as a matter of navigating the
545545
// stack but we need to keep our currentTextEditorState up to date with
546546
// the navigtion that occurs.
547547
if (this.navigatingInStack) {
548-
if (codeEditor && control.input) {
548+
if (codeEditor && control && control.input) {
549549
this.currentTextEditorState = new TextEditorState(control.input, codeEditor.getSelection());
550550
} else {
551551
this.currentTextEditorState = null; // we navigated to a non text editor
@@ -556,7 +556,7 @@ export class HistoryService extends Disposable implements IHistoryService {
556556
else {
557557

558558
// navigation inside text editor
559-
if (codeEditor && control.input) {
559+
if (codeEditor && control && control.input) {
560560
this.handleTextEditorEvent(control, codeEditor, event);
561561
}
562562

@@ -572,6 +572,10 @@ export class HistoryService extends Disposable implements IHistoryService {
572572
}
573573

574574
private handleTextEditorEvent(editor: IBaseEditor, editorControl: IEditor, event?: ICursorPositionChangedEvent): void {
575+
if (!editor.input) {
576+
return;
577+
}
578+
575579
const stateCandidate = new TextEditorState(editor.input, editorControl.getSelection());
576580

577581
// Add to stack if we dont have a current state or this new state justifies a push
@@ -589,6 +593,10 @@ export class HistoryService extends Disposable implements IHistoryService {
589593
}
590594

591595
private handleNonTextEditorEvent(editor: IBaseEditor): void {
596+
if (!editor.input) {
597+
return;
598+
}
599+
592600
const currentStack = this.stack[this.index];
593601
if (currentStack && this.matches(editor.input, currentStack.input)) {
594602
return; // do not push same editor input again
@@ -656,7 +664,7 @@ export class HistoryService extends Disposable implements IHistoryService {
656664

657665
// Check for limit
658666
if (this.stack.length > HistoryService.MAX_STACK_ITEMS) {
659-
removedEntries.push(this.stack.shift()); // remove first
667+
removedEntries.push(this.stack.shift()!); // remove first
660668
if (this.lastIndex >= 0) {
661669
this.lastIndex--;
662670
}
@@ -691,7 +699,7 @@ export class HistoryService extends Disposable implements IHistoryService {
691699
return true;
692700
}
693701

694-
if ((!selectionA && selectionB) || (selectionA && !selectionB)) {
702+
if (!selectionA || !selectionB) {
695703
return false;
696704
}
697705

@@ -859,7 +867,7 @@ export class HistoryService extends Disposable implements IHistoryService {
859867
}));
860868
}
861869

862-
private safeLoadHistoryEntry(registry: IEditorInputFactoryRegistry, entry: ISerializedEditorHistoryEntry): IEditorInput | IResourceInput {
870+
private safeLoadHistoryEntry(registry: IEditorInputFactoryRegistry, entry: ISerializedEditorHistoryEntry): IEditorInput | IResourceInput | undefined {
863871
const serializedEditorHistoryEntry = entry as ISerializedEditorHistoryEntry;
864872

865873
// File resource: via URI.revive()
@@ -884,7 +892,7 @@ export class HistoryService extends Disposable implements IHistoryService {
884892
return undefined;
885893
}
886894

887-
getLastActiveWorkspaceRoot(schemeFilter?: string): URI {
895+
getLastActiveWorkspaceRoot(schemeFilter?: string): URI | undefined {
888896

889897
// No Folder: return early
890898
const folders = this.contextService.getWorkspace().folders;
@@ -931,10 +939,10 @@ export class HistoryService extends Disposable implements IHistoryService {
931939
return undefined;
932940
}
933941

934-
getLastActiveFile(schemeFilter: string): URI {
942+
getLastActiveFile(schemeFilter: string): URI | undefined {
935943
const history = this.getHistory();
936944
for (const input of history) {
937-
let resource: URI;
945+
let resource: URI | null;
938946
if (input instanceof EditorInput) {
939947
resource = toResource(input, { filter: schemeFilter });
940948
} else {

0 commit comments

Comments
 (0)