Skip to content

Commit 7d0267f

Browse files
authored
Make createInstance stricter (microsoft#83472)
Part of microsoft#81574 Makes the `createInstance` function signature more strict. Previously it inferred the argument types based on the actual argument values passed to `createInstance`. Instead, we now infer them from the constructor signature itself
1 parent 149376d commit 7d0267f

24 files changed

Lines changed: 50 additions & 41 deletions

File tree

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ import { activeContrastBorder, contrastBorder, registerColor } from 'vs/platform
3131
import { ITheme, IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
3232
import { PeekViewWidget, IPeekViewService } from './peekViewWidget';
3333
import { FileReferences, OneReference, ReferencesModel } from './referencesModel';
34-
import { ITreeRenderer, IAsyncDataSource } from 'vs/base/browser/ui/tree/tree';
3534
import { IAsyncDataTreeOptions } from 'vs/base/browser/ui/tree/asyncDataTree';
36-
import { IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
3735
import { FuzzyScore } from 'vs/base/common/filters';
3836
import { SplitView, Sizing } from 'vs/base/browser/ui/splitview/splitview';
3937

@@ -311,7 +309,7 @@ export class ReferenceWidget extends PeekViewWidget {
311309
keyboardNavigationLabelProvider: this._instantiationService.createInstance(StringRepresentationProvider),
312310
identityProvider: new IdentityProvider()
313311
};
314-
this._tree = this._instantiationService.createInstance<string, HTMLElement, IListVirtualDelegate<TreeElement>, ITreeRenderer<any, FuzzyScore, any>[], IAsyncDataSource<ReferencesModel | FileReferences, TreeElement>, IAsyncDataTreeOptions<TreeElement, FuzzyScore>, WorkbenchAsyncDataTree<ReferencesModel | FileReferences, TreeElement, FuzzyScore>>(
312+
this._tree = this._instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<ReferencesModel | FileReferences, TreeElement, FuzzyScore>>(
315313
WorkbenchAsyncDataTree,
316314
'ReferencesWidget',
317315
this._treeContainer,

src/vs/platform/instantiation/common/instantiation.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ export interface ServicesAccessor {
6767

6868
export const IInstantiationService = createDecorator<IInstantiationService>('instantiationService');
6969

70+
/**
71+
* Given a list of arguments as a tuple, attempt to extract the leading, non-service arguments
72+
* to their own tuple.
73+
*/
74+
type GetLeadingNonServiceArgs<Args> =
75+
Args extends [...BrandedService[]] ? []
76+
: Args extends [infer A1, ...BrandedService[]] ? [A1]
77+
: Args extends [infer A1, infer A2, ...BrandedService[]] ? [A1, A2]
78+
: Args extends [infer A1, infer A2, infer A3, ...BrandedService[]] ? [A1, A2, A3]
79+
: Args extends [infer A1, infer A2, infer A3, infer A4, ...BrandedService[]] ? [A1, A2, A3, A4]
80+
: Args extends [infer A1, infer A2, infer A3, infer A4, infer A5, ...BrandedService[]] ? [A1, A2, A3, A4, A5]
81+
: Args extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, ...BrandedService[]] ? [A1, A2, A3, A4, A5, A6]
82+
: Args extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7, ...BrandedService[]] ? [A1, A2, A3, A4, A5, A6, A7]
83+
: Args extends [infer A1, infer A2, infer A3, infer A4, infer A5, infer A6, infer A7, infer A8, ...BrandedService[]] ? [A1, A2, A3, A4, A5, A6, A7, A8]
84+
: never;
85+
7086
export interface IInstantiationService {
7187

7288
_serviceBrand: undefined;
@@ -85,15 +101,7 @@ export interface IInstantiationService {
85101
createInstance<A1, A2, A3, A4, A5, A6, A7, T>(descriptor: descriptors.SyncDescriptor7<A1, A2, A3, A4, A5, A6, A7, T>, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7): T;
86102
createInstance<A1, A2, A3, A4, A5, A6, A7, A8, T>(descriptor: descriptors.SyncDescriptor8<A1, A2, A3, A4, A5, A6, A7, A8, T>, a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8): T;
87103

88-
createInstance<T>(ctor: IConstructorSignature0<T>): T;
89-
createInstance<A1, T>(ctor: IConstructorSignature1<A1, T>, first: A1): T;
90-
createInstance<A1, A2, T>(ctor: IConstructorSignature2<A1, A2, T>, first: A1, second: A2): T;
91-
createInstance<A1, A2, A3, T>(ctor: IConstructorSignature3<A1, A2, A3, T>, first: A1, second: A2, third: A3): T;
92-
createInstance<A1, A2, A3, A4, T>(ctor: IConstructorSignature4<A1, A2, A3, A4, T>, first: A1, second: A2, third: A3, fourth: A4): T;
93-
createInstance<A1, A2, A3, A4, A5, T>(ctor: IConstructorSignature5<A1, A2, A3, A4, A5, T>, first: A1, second: A2, third: A3, fourth: A4, fifth: A5): T;
94-
createInstance<A1, A2, A3, A4, A5, A6, T>(ctor: IConstructorSignature6<A1, A2, A3, A4, A5, A6, T>, first: A1, second: A2, third: A3, fourth: A4, fifth: A5, sixth: A6): T;
95-
createInstance<A1, A2, A3, A4, A5, A6, A7, T>(ctor: IConstructorSignature7<A1, A2, A3, A4, A5, A6, A7, T>, first: A1, second: A2, third: A3, fourth: A4, fifth: A5, sixth: A6, seventh: A7): T;
96-
createInstance<A1, A2, A3, A4, A5, A6, A7, A8, T>(ctor: IConstructorSignature8<A1, A2, A3, A4, A5, A6, A7, A8, T>, first: A1, second: A2, third: A3, fourth: A4, fifth: A5, sixth: A6, seventh: A7, eigth: A8): T;
104+
createInstance<Ctor extends new (...args: any) => any, R extends InstanceType<Ctor>>(t: Ctor, ...args: GetLeadingNonServiceArgs<ConstructorParameters<Ctor>>): R;
97105

98106
/**
99107
*

src/vs/workbench/browser/parts/compositePart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export abstract class CompositePart<T extends Composite> extends Part {
168168
// Instantiate composite from registry otherwise
169169
const compositeDescriptor = this.registry.getComposite(id);
170170
if (compositeDescriptor) {
171-
const compositeProgressIndicator = this.instantiationService.createInstance(CompositeProgressIndicator, this.progressBar, compositeDescriptor.id, !!isActive);
171+
const compositeProgressIndicator = this.instantiationService.createInstance(CompositeProgressIndicator, assertIsDefined(this.progressBar), compositeDescriptor.id, !!isActive);
172172
const compositeInstantiationService = this.instantiationService.createChild(new ServiceCollection(
173173
[IEditorProgressService, compositeProgressIndicator] // provide the editor progress service for any editors instantiated within the composite
174174
));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export class BreadcrumbsFilePicker extends BreadcrumbsPicker {
374374
const labels = this._instantiationService.createInstance(ResourceLabels, DEFAULT_LABELS_CONTAINER /* TODO@Jo visibility propagation */);
375375
this._disposables.add(labels);
376376

377-
return this._instantiationService.createInstance(WorkbenchAsyncDataTree, 'BreadcrumbsFilePicker', container, new FileVirtualDelegate(), [this._instantiationService.createInstance(FileRenderer, labels)], this._instantiationService.createInstance(FileDataSource), {
377+
return this._instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<IWorkspace | URI, IWorkspaceFolder | IFileStat, FuzzyScore>>(WorkbenchAsyncDataTree, 'BreadcrumbsFilePicker', container, new FileVirtualDelegate(), [this._instantiationService.createInstance(FileRenderer, labels)], this._instantiationService.createInstance(FileDataSource), {
378378
multipleSelectionSupport: false,
379379
sorter: new FileSorter(),
380380
filter: this._instantiationService.createInstance(FileFilter),
@@ -438,7 +438,7 @@ export class BreadcrumbsOutlinePicker extends BreadcrumbsPicker {
438438
}
439439

440440
protected _createTree(container: HTMLElement) {
441-
return this._instantiationService.createInstance(
441+
return this._instantiationService.createInstance<typeof WorkbenchDataTree, WorkbenchDataTree<OutlineModel, any, FuzzyScore>>(
442442
WorkbenchDataTree,
443443
'BreadcrumbsOutlinePicker',
444444
container,

src/vs/workbench/browser/parts/notifications/notificationsList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class NotificationsList extends Themable {
7373
const renderer = this.instantiationService.createInstance(NotificationRenderer, actionRunner);
7474

7575
// List
76-
const list = this.list = this._register(this.instantiationService.createInstance(
76+
const list = this.list = this._register(this.instantiationService.createInstance<typeof WorkbenchList, WorkbenchList<INotificationViewItem>>(
7777
WorkbenchList,
7878
'NotificationsList',
7979
this.listContainer,

src/vs/workbench/contrib/callHierarchy/browser/callHierarchyPeek.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export class CallHierarchyTreePeekWidget extends PeekViewWidget {
201201
ariaLabel: localize('tree.aria', "Call Hierarchy"),
202202
expandOnlyOnTwistieClick: true,
203203
};
204-
this._tree = <any>this._instantiationService.createInstance(
204+
this._tree = this._instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<callHTree.CallHierarchyRoot, callHTree.Call, FuzzyScore>>(
205205
WorkbenchAsyncDataTree,
206206
'CallHierarchyPeek',
207207
treeContainer,

src/vs/workbench/contrib/debug/browser/callStackView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class CallStackView extends ViewletPanel {
122122
const treeContainer = renderViewTree(container);
123123

124124
this.dataSource = new CallStackDataSource(this.debugService);
125-
this.tree = this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'CallStackView', treeContainer, new CallStackDelegate(), [
125+
this.tree = this.instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<CallStackItem | IDebugModel, CallStackItem, FuzzyScore>>(WorkbenchAsyncDataTree, 'CallStackView', treeContainer, new CallStackDelegate(), [
126126
new SessionsRenderer(this.instantiationService),
127127
new ThreadsRenderer(this.instantiationService),
128128
this.instantiationService.createInstance(StackFramesRenderer),

src/vs/workbench/contrib/debug/browser/debugHover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class DebugHoverWidget implements IContentWidget {
7373
this.treeContainer.setAttribute('role', 'tree');
7474
const dataSource = new DebugHoverDataSource();
7575

76-
this.tree = this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'DebugHover', this.treeContainer, new DebugHoverDelegate(), [this.instantiationService.createInstance(VariablesRenderer)],
76+
this.tree = this.instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<IExpression, IExpression, any>>(WorkbenchAsyncDataTree, 'DebugHover', this.treeContainer, new DebugHoverDelegate(), [this.instantiationService.createInstance(VariablesRenderer)],
7777
dataSource, {
7878
ariaLabel: nls.localize('treeAriaLabel', "Debug Hover"),
7979
accessibilityProvider: new DebugHoverAccessibilityProvider(),

src/vs/workbench/contrib/debug/browser/loadedScriptsView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ export class LoadedScriptsView extends ViewletPanel {
427427
this.treeLabels = this.instantiationService.createInstance(ResourceLabels, { onDidChangeVisibility: this.onDidChangeBodyVisibility });
428428
this._register(this.treeLabels);
429429

430-
this.tree = this.instantiationService.createInstance(WorkbenchAsyncDataTree, 'LoadedScriptsView', this.treeContainer, new LoadedScriptsDelegate(),
430+
this.tree = this.instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<LoadedScriptsItem, LoadedScriptsItem, FuzzyScore>>(WorkbenchAsyncDataTree, 'LoadedScriptsView', this.treeContainer, new LoadedScriptsDelegate(),
431431
[new LoadedScriptsRenderer(this.treeLabels)],
432432
new LoadedScriptsDataSource(),
433433
{

src/vs/workbench/contrib/debug/browser/repl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export class Repl extends Panel implements IPrivateReplService, IHistoryNavigati
406406
const wordWrap = this.configurationService.getValue<IDebugConfiguration>('debug').console.wordWrap;
407407
dom.toggleClass(treeContainer, 'word-wrap', wordWrap);
408408
const linkDetector = this.instantiationService.createInstance(LinkDetector);
409-
this.tree = this.instantiationService.createInstance(
409+
this.tree = this.instantiationService.createInstance<typeof WorkbenchAsyncDataTree, WorkbenchAsyncDataTree<IDebugSession, IReplElement, FuzzyScore>>(
410410
WorkbenchAsyncDataTree,
411411
'DebugRepl',
412412
treeContainer,

0 commit comments

Comments
 (0)