@@ -11,15 +11,15 @@ import { IModelService } from 'vs/editor/common/services/model';
11
11
import { TextResourceEditorModel } from 'vs/workbench/common/editor/textResourceEditorModel' ;
12
12
import { ITextFileService , TextFileResolveReason } from 'vs/workbench/services/textfile/common/textfiles' ;
13
13
import { Schemas } from 'vs/base/common/network' ;
14
- import { ITextModelService , ITextModelContentProvider , ITextEditorModel , IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService' ;
14
+ import { ITextModelService , ITextModelContentProvider , ITextEditorModel , IResolvedTextEditorModel , isResolvedTextEditorModel } from 'vs/editor/common/services/resolverService' ;
15
15
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel' ;
16
16
import { IFileService } from 'vs/platform/files/common/files' ;
17
17
import { registerSingleton } from 'vs/platform/instantiation/common/extensions' ;
18
18
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo' ;
19
19
import { ModelUndoRedoParticipant } from 'vs/editor/common/services/modelUndoRedoParticipant' ;
20
20
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity' ;
21
21
22
- class ResourceModelCollection extends ReferenceCollection < Promise < ITextEditorModel > > {
22
+ class ResourceModelCollection extends ReferenceCollection < Promise < IResolvedTextEditorModel > > {
23
23
24
24
private readonly providers = new Map < string , ITextModelContentProvider [ ] > ( ) ;
25
25
private readonly modelsToDispose = new Set < string > ( ) ;
@@ -33,11 +33,11 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
33
33
super ( ) ;
34
34
}
35
35
36
- createReferencedObject ( key : string ) : Promise < ITextEditorModel > {
36
+ createReferencedObject ( key : string ) : Promise < IResolvedTextEditorModel > {
37
37
return this . doCreateReferencedObject ( key ) ;
38
38
}
39
39
40
- private async doCreateReferencedObject ( key : string , skipActivateProvider ?: boolean ) : Promise < ITextEditorModel > {
40
+ private async doCreateReferencedObject ( key : string , skipActivateProvider ?: boolean ) : Promise < IResolvedTextEditorModel > {
41
41
42
42
// Untrack as being disposed
43
43
this . modelsToDispose . delete ( key ) ;
@@ -50,24 +50,36 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
50
50
throw new Error ( `Unable to resolve inMemory resource ${ key } ` ) ;
51
51
}
52
52
53
- return this . instantiationService . createInstance ( TextResourceEditorModel , resource ) ;
53
+ const model = this . instantiationService . createInstance ( TextResourceEditorModel , resource ) ;
54
+ if ( this . ensureResolvedModel ( model , key ) ) {
55
+ return model ;
56
+ }
54
57
}
55
58
56
59
// Untitled Schema: go through untitled text service
57
60
if ( resource . scheme === Schemas . untitled ) {
58
- return this . textFileService . untitled . resolve ( { untitledResource : resource } ) ;
61
+ const model = await this . textFileService . untitled . resolve ( { untitledResource : resource } ) ;
62
+ if ( this . ensureResolvedModel ( model , key ) ) {
63
+ return model ;
64
+ }
59
65
}
60
66
61
67
// File or remote file: go through text file service
62
68
if ( this . fileService . hasProvider ( resource ) ) {
63
- return this . textFileService . files . resolve ( resource , { reason : TextFileResolveReason . REFERENCE } ) ;
69
+ const model = await this . textFileService . files . resolve ( resource , { reason : TextFileResolveReason . REFERENCE } ) ;
70
+ if ( this . ensureResolvedModel ( model , key ) ) {
71
+ return model ;
72
+ }
64
73
}
65
74
66
75
// Virtual documents
67
76
if ( this . providers . has ( resource . scheme ) ) {
68
77
await this . resolveTextModelContent ( key ) ;
69
78
70
- return this . instantiationService . createInstance ( TextResourceEditorModel , resource ) ;
79
+ const model = this . instantiationService . createInstance ( TextResourceEditorModel , resource ) ;
80
+ if ( this . ensureResolvedModel ( model , key ) ) {
81
+ return model ;
82
+ }
71
83
}
72
84
73
85
// Either unknown schema, or not yet registered, try to activate
@@ -80,6 +92,14 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
80
92
throw new Error ( `Unable to resolve resource ${ key } ` ) ;
81
93
}
82
94
95
+ private ensureResolvedModel ( model : ITextEditorModel , key : string ) : model is IResolvedTextEditorModel {
96
+ if ( isResolvedTextEditorModel ( model ) ) {
97
+ return true ;
98
+ }
99
+
100
+ throw new Error ( `Unable to resolve resource ${ key } ` ) ;
101
+ }
102
+
83
103
destroyReferencedObject ( key : string , modelPromise : Promise < ITextEditorModel > ) : void {
84
104
85
105
// untitled and inMemory are bound to a different lifecycle
@@ -173,7 +193,7 @@ export class TextModelResolverService extends Disposable implements ITextModelSe
173
193
174
194
declare readonly _serviceBrand : undefined ;
175
195
176
- private readonly resourceModelCollection = this . instantiationService . createInstance ( ResourceModelCollection ) ;
196
+ private readonly resourceModelCollection : ResourceModelCollection & ReferenceCollection < Promise < IResolvedTextEditorModel > > /* TS Fail */ = this . instantiationService . createInstance ( ResourceModelCollection ) ;
177
197
private readonly asyncModelCollection = new AsyncReferenceCollection ( this . resourceModelCollection ) ;
178
198
179
199
constructor (
@@ -195,8 +215,7 @@ export class TextModelResolverService extends Disposable implements ITextModelSe
195
215
// with different resource forms (e.g. path casing on Windows)
196
216
resource = this . uriIdentityService . asCanonicalUri ( resource ) ;
197
217
198
- const result = await this . asyncModelCollection . acquire ( resource . toString ( ) ) ;
199
- return result as IReference < IResolvedTextEditorModel > ; // TODO@Ben : why is this cast here?
218
+ return await this . asyncModelCollection . acquire ( resource . toString ( ) ) ;
200
219
}
201
220
202
221
registerTextModelContentProvider ( scheme : string , provider : ITextModelContentProvider ) : IDisposable {
0 commit comments