textModelResolverService.ts 7.2 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { URI } from 'vs/base/common/uri';
7
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
A
Alex Dima 已提交
8
import { ITextModel } from 'vs/editor/common/model';
9
import { IDisposable, toDisposable, IReference, ReferenceCollection } from 'vs/base/common/lifecycle';
B
Benjamin Pasero 已提交
10 11
import { IModelService } from 'vs/editor/common/services/modelService';
import { ResourceEditorModel } from 'vs/workbench/common/editor/resourceEditorModel';
B
Benjamin Pasero 已提交
12
import { ITextFileService, TextFileLoadReason } from 'vs/workbench/services/textfile/common/textfiles';
13
import * as network from 'vs/base/common/network';
M
Matt Bierner 已提交
14
import { ITextModelService, ITextModelContentProvider, ITextEditorModel, IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
15
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
J
Johannes Rieken 已提交
16
import { IFileService } from 'vs/platform/files/common/files';
17
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
18
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
19
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
B
Benjamin Pasero 已提交
20

J
Johannes Rieken 已提交
21
class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorModel>> {
B
Benjamin Pasero 已提交
22

23 24
	private readonly providers: { [scheme: string]: ITextModelContentProvider[] } = Object.create(null);
	private readonly modelsToDispose = new Set<string>();
B
Benjamin Pasero 已提交
25 26

	constructor(
27 28
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@ITextFileService private readonly textFileService: ITextFileService,
29 30
		@IFileService private readonly fileService: IFileService,
		@ITelemetryService private readonly telemetryService: ITelemetryService,
31
		@IModelService private readonly modelService: IModelService
B
Benjamin Pasero 已提交
32
	) {
33
		super();
B
Benjamin Pasero 已提交
34 35
	}

36
	async createReferencedObject(key: string, skipActivateProvider?: boolean): Promise<ITextEditorModel> {
37 38

		// Untrack as being disposed
J
Joao Moreno 已提交
39 40
		this.modelsToDispose.delete(key);

41
		// inMemory Schema: go through model service cache
42
		const resource = URI.parse(key);
43 44 45 46 47 48 49 50
		if (resource.scheme === network.Schemas.inMemory) {
			const cachedModel = this.modelService.getModel(resource);
			if (!cachedModel) {
				throw new Error(`Unable to resolve inMemory resource ${key}`);
			}

			return this.instantiationService.createInstance(ResourceEditorModel, resource);
		}
B
Benjamin Pasero 已提交
51

52 53 54 55 56 57
		// Untitled Schema: go through untitled text service
		if (resource.scheme === network.Schemas.untitled) {
			return this.textFileService.untitled.resolve({ untitledResource: resource });
		}

		// File or remote file: go through text file service
J
Johannes Rieken 已提交
58
		if (this.fileService.canHandleResource(resource)) {
B
Benjamin Pasero 已提交
59
			return this.textFileService.files.resolve(resource, { reason: TextFileLoadReason.REFERENCE });
J
Johannes Rieken 已提交
60
		}
61

B
Benjamin Pasero 已提交
62 63
		// Virtual documents
		if (this.providers[resource.scheme]) {
64 65 66
			await this.resolveTextModelContent(key);

			return this.instantiationService.createInstance(ResourceEditorModel, resource);
B
Benjamin Pasero 已提交
67 68
		}

69 70
		// Either unknown schema, or not yet registered, try to activate
		if (!skipActivateProvider) {
71 72 73
			await this.fileService.activateProvider(resource.scheme);

			return this.createReferencedObject(key, true);
B
Benjamin Pasero 已提交
74 75
		}

76
		throw new Error(`Unable to resolve resource ${key}`);
77
	}
78

J
Johannes Rieken 已提交
79
	destroyReferencedObject(key: string, modelPromise: Promise<ITextEditorModel>): void {
80 81

		// Track as being disposed
J
Joao Moreno 已提交
82 83
		this.modelsToDispose.add(key);

84
		modelPromise.then(model => {
85 86 87 88 89 90 91 92 93 94 95
			if (!this.modelsToDispose.has(key)) {
				return; // return if model has been aquired again meanwhile
			}

			const resource = URI.parse(key);
			if (resource.scheme === network.Schemas.untitled || resource.scheme === network.Schemas.inMemory) {
				// untitled and inMemory are bound to a different lifecycle
			} else if (model instanceof TextFileEditorModel) {
				this.textFileService.files.disposeModel(model);
			} else {
				model.dispose();
96
			}
J
Johannes Rieken 已提交
97 98
		}, err => {
			// ignore
99
		});
100 101
	}

B
Benjamin Pasero 已提交
102
	registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider): IDisposable {
103 104
		const registry = this.providers;
		const providers = registry[scheme] || (registry[scheme] = []);
B
Benjamin Pasero 已提交
105

106 107 108 109 110 111 112 113 114 115 116 117 118
		providers.unshift(provider);

		return toDisposable(() => {
			const array = registry[scheme];

			if (!array) {
				return;
			}

			const index = array.indexOf(provider);

			if (index === -1) {
				return;
B
Benjamin Pasero 已提交
119
			}
120 121 122 123 124 125 126

			array.splice(index, 1);

			if (array.length === 0) {
				delete registry[scheme];
			}
		});
B
Benjamin Pasero 已提交
127 128
	}

129 130 131 132
	hasTextModelContentProvider(scheme: string): boolean {
		return this.providers[scheme] !== undefined;
	}

133
	private async resolveTextModelContent(key: string): Promise<ITextModel> {
134
		const resource = URI.parse(key);
J
Joao Moreno 已提交
135
		const providers = this.providers[resource.scheme] || [];
B
Benjamin Pasero 已提交
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		if (resource.query || resource.fragment) {
			type TextModelResolverUri = {
				query: boolean;
				fragment: boolean;
			};
			type TextModelResolverUriMeta = {
				query: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
				fragment: { classification: 'SystemMetaData', purpose: 'FeatureInsight' };
			};
			this.telemetryService.publicLog2<TextModelResolverUri, TextModelResolverUriMeta>('textmodelresolveruri', {
				query: Boolean(resource.query),
				fragment: Boolean(resource.fragment)
			});
		}

152 153 154 155 156
		for (const provider of providers) {
			const value = await provider.provideTextContent(resource);
			if (value) {
				return value;
			}
157
		}
158 159

		throw new Error(`Unable to resolve text model content for resource ${key}`);
160 161 162
	}
}

163
export class TextModelResolverService implements ITextModelService {
164

165
	_serviceBrand: undefined;
J
Joao Moreno 已提交
166

167
	private readonly resourceModelCollection = this.instantiationService.createInstance(ResourceModelCollection);
168 169

	constructor(
170
		@IInstantiationService private readonly instantiationService: IInstantiationService,
171 172
		@IFileService private readonly fileService: IFileService,
		@IUriIdentityService private readonly uriIdentService: IUriIdentityService,
173 174 175
	) {
	}

176
	async createModelReference(resource: URI): Promise<IReference<IResolvedTextEditorModel>> {
177 178 179 180

		const canonicalResource = this.uriIdentService.asCanonicalUri(resource);

		const ref = this.resourceModelCollection.acquire(canonicalResource.toString());
181

182 183
		try {
			const model = await ref.object;
184

185 186 187 188
			return {
				object: model as IResolvedTextEditorModel,
				dispose: () => ref.dispose()
			};
189 190 191 192 193
		} catch (error) {
			ref.dispose();

			throw error;
		}
194 195
	}

B
Benjamin Pasero 已提交
196
	registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider): IDisposable {
197
		return this.resourceModelCollection.registerTextModelContentProvider(scheme, provider);
B
Benjamin Pasero 已提交
198
	}
199

200 201 202
	canHandleResource(resource: URI): boolean {
		if (this.fileService.canHandleResource(resource) || resource.scheme === network.Schemas.untitled || resource.scheme === network.Schemas.inMemory) {
			return true; // we handle file://, untitled:// and inMemory:// automatically
203 204
		}

205
		return this.resourceModelCollection.hasTextModelContentProvider(resource.scheme);
206
	}
207
}
208

209
registerSingleton(ITextModelService, TextModelResolverService, true);