-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
resource.ts
430 lines (377 loc) · 14.9 KB
/
resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { injectable, inject, named } from 'inversify';
import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol';
import URI from '../common/uri';
import { ContributionProvider } from './contribution-provider';
import { Event, Emitter } from './event';
import { Disposable } from './disposable';
import { MaybePromise } from './types';
import { CancellationToken } from './cancellation';
import { ApplicationError } from './application-error';
import { ReadableStream, Readable } from './stream';
import { SyncReferenceCollection, Reference } from './reference';
import { MarkdownString } from './markdown-rendering';
export interface ResourceVersion {
}
export interface ResourceReadOptions {
encoding?: string
}
export interface ResourceSaveOptions {
encoding?: string
overwriteEncoding?: boolean
version?: ResourceVersion
}
export interface Resource extends Disposable {
readonly uri: URI;
/**
* Latest read version of this resource.
*
* Optional if a resource does not support versioning, check with `in` operator`.
* Undefined if a resource did not read content yet.
*/
readonly version?: ResourceVersion | undefined;
/**
* Latest read encoding of this resource.
*
* Optional if a resource does not support encoding, check with `in` operator`.
* Undefined if a resource did not read content yet.
*/
readonly encoding?: string | undefined;
readonly onDidChangeReadOnly?: Event<boolean | MarkdownString>;
readonly readOnly?: boolean | MarkdownString;
/**
* Reads latest content of this resource.
*
* If a resource supports versioning it updates version to latest.
* If a resource supports encoding it updates encoding to latest.
*
* @throws `ResourceError.NotFound` if a resource not found
*/
readContents(options?: ResourceReadOptions): Promise<string>;
/**
* Stream latest content of this resource.
*
* If a resource supports versioning it updates version to latest.
* If a resource supports encoding it updates encoding to latest.
*
* @throws `ResourceError.NotFound` if a resource not found
*/
readStream?(options?: ResourceReadOptions): Promise<ReadableStream<string>>;
/**
* Rewrites the complete content for this resource.
* If a resource does not exist it will be created.
*
* If a resource supports versioning clients can pass some version
* to check against it, if it is not provided latest version is used.
*
* It updates version and encoding to latest.
*
* @throws `ResourceError.OutOfSync` if latest resource version is out of sync with the given
*/
saveContents?(content: string, options?: ResourceSaveOptions): Promise<void>;
/**
* Rewrites the complete content for this resource.
* If a resource does not exist it will be created.
*
* If a resource supports versioning clients can pass some version
* to check against it, if it is not provided latest version is used.
*
* It updates version and encoding to latest.
*
* @throws `ResourceError.OutOfSync` if latest resource version is out of sync with the given
*/
saveStream?(content: Readable<string>, options?: ResourceSaveOptions): Promise<void>;
/**
* Applies incremental content changes to this resource.
*
* If a resource supports versioning clients can pass some version
* to check against it, if it is not provided latest version is used.
* It updates version to latest.
*
* @throws `ResourceError.NotFound` if a resource not found or was not read yet
* @throws `ResourceError.OutOfSync` if latest resource version is out of sync with the given
*/
saveContentChanges?(changes: TextDocumentContentChangeEvent[], options?: ResourceSaveOptions): Promise<void>;
readonly onDidChangeContents?: Event<void>;
guessEncoding?(): Promise<string | undefined>
}
export namespace Resource {
export interface SaveContext {
contentLength: number
content: string | Readable<string>
changes?: TextDocumentContentChangeEvent[]
options?: ResourceSaveOptions
}
export async function save(resource: Resource, context: SaveContext, token?: CancellationToken): Promise<void> {
if (!resource.saveContents) {
return;
}
if (await trySaveContentChanges(resource, context)) {
return;
}
if (token && token.isCancellationRequested) {
return;
}
if (typeof context.content !== 'string' && resource.saveStream) {
await resource.saveStream(context.content, context.options);
} else {
const content = typeof context.content === 'string' ? context.content : Readable.toString(context.content);
await resource.saveContents(content, context.options);
}
}
export async function trySaveContentChanges(resource: Resource, context: SaveContext): Promise<boolean> {
if (!context.changes || !resource.saveContentChanges || shouldSaveContent(resource, context)) {
return false;
}
try {
await resource.saveContentChanges(context.changes, context.options);
return true;
} catch (e) {
if (!ResourceError.NotFound.is(e) && !ResourceError.OutOfSync.is(e)) {
console.error(`Failed to apply incremental changes to '${resource.uri.toString()}':`, e);
}
return false;
}
}
export function shouldSaveContent(resource: Resource, { contentLength, changes }: SaveContext): boolean {
if (!changes || (resource.saveStream && contentLength > 32 * 1024 * 1024)) {
return true;
}
let contentChangesLength = 0;
for (const change of changes) {
contentChangesLength += JSON.stringify(change).length;
if (contentChangesLength > contentLength) {
return true;
}
}
return contentChangesLength > contentLength;
}
}
export namespace ResourceError {
export const NotFound = ApplicationError.declare(-40000, (raw: ApplicationError.Literal<{ uri: URI }>) => raw);
export const OutOfSync = ApplicationError.declare(-40001, (raw: ApplicationError.Literal<{ uri: URI }>) => raw);
}
export const ResourceResolver = Symbol('ResourceResolver');
export interface ResourceResolver {
/**
* Reject if a resource cannot be provided.
*/
resolve(uri: URI): MaybePromise<Resource>;
}
export const ResourceProvider = Symbol('ResourceProvider');
export type ResourceProvider = (uri: URI) => Promise<Resource>;
@injectable()
export class DefaultResourceProvider {
constructor(
@inject(ContributionProvider) @named(ResourceResolver)
protected readonly resolversProvider: ContributionProvider<ResourceResolver>
) { }
/**
* Reject if a resource cannot be provided.
*/
async get(uri: URI): Promise<Resource> {
const resolvers = this.resolversProvider.getContributions();
for (const resolver of resolvers) {
try {
return await resolver.resolve(uri);
} catch (err) {
// no-op
}
}
return Promise.reject(new Error(`A resource provider for '${uri.toString()}' is not registered.`));
}
}
export class MutableResource implements Resource {
private contents: string = '';
constructor(readonly uri: URI) {
}
dispose(): void { }
async readContents(): Promise<string> {
return this.contents;
}
async saveContents(contents: string): Promise<void> {
this.contents = contents;
this.fireDidChangeContents();
}
protected readonly onDidChangeContentsEmitter = new Emitter<void>();
readonly onDidChangeContents = this.onDidChangeContentsEmitter.event;
protected fireDidChangeContents(): void {
this.onDidChangeContentsEmitter.fire(undefined);
}
}
export class ReferenceMutableResource implements Resource {
constructor(protected reference: Reference<MutableResource>) { }
get uri(): URI {
return this.reference.object.uri;
}
get onDidChangeContents(): Event<void> {
return this.reference.object.onDidChangeContents;
}
dispose(): void {
this.reference.dispose();
}
readContents(): Promise<string> {
return this.reference.object.readContents();
}
saveContents(contents: string): Promise<void> {
return this.reference.object.saveContents(contents);
}
}
@injectable()
export class InMemoryResources implements ResourceResolver {
protected readonly resources = new SyncReferenceCollection<string, MutableResource>(uri => new MutableResource(new URI(uri)));
add(uri: URI, contents: string): Resource {
const resourceUri = uri.toString();
if (this.resources.has(resourceUri)) {
throw new Error(`Cannot add already existing in-memory resource '${resourceUri}'`);
}
const resource = this.acquire(resourceUri);
resource.saveContents(contents);
return resource;
}
update(uri: URI, contents: string): Resource {
const resourceUri = uri.toString();
const resource = this.resources.get(resourceUri);
if (!resource) {
throw new Error(`Cannot update non-existed in-memory resource '${resourceUri}'`);
}
resource.saveContents(contents);
return resource;
}
resolve(uri: URI): Resource {
const uriString = uri.toString();
if (!this.resources.has(uriString)) {
throw new Error(`In memory '${uriString}' resource does not exist.`);
}
return this.acquire(uriString);
}
protected acquire(uri: string): ReferenceMutableResource {
const reference = this.resources.acquire(uri);
return new ReferenceMutableResource(reference);
}
}
export const MEMORY_TEXT = 'mem-txt';
/**
* Resource implementation for 'mem-txt' URI scheme where content is saved in URI query.
*/
export class InMemoryTextResource implements Resource {
constructor(readonly uri: URI) { }
async readContents(options?: { encoding?: string | undefined; } | undefined): Promise<string> {
return this.uri.query;
}
dispose(): void { }
}
/**
* ResourceResolver implementation for 'mem-txt' URI scheme.
*/
@injectable()
export class InMemoryTextResourceResolver implements ResourceResolver {
resolve(uri: URI): MaybePromise<Resource> {
if (uri.scheme !== MEMORY_TEXT) {
throw new Error(`Expected a URI with ${MEMORY_TEXT} scheme. Was: ${uri}.`);
}
return new InMemoryTextResource(uri);
}
}
export const UNTITLED_SCHEME = 'untitled';
let untitledResourceSequenceIndex = 0;
@injectable()
export class UntitledResourceResolver implements ResourceResolver {
protected readonly resources = new Map<string, UntitledResource>();
has(uri: URI): boolean {
if (uri.scheme !== UNTITLED_SCHEME) {
throw new Error('The given uri is not untitled file uri: ' + uri);
} else {
return this.resources.has(uri.toString());
}
}
async resolve(uri: URI): Promise<UntitledResource> {
if (uri.scheme !== UNTITLED_SCHEME) {
throw new Error('The given uri is not untitled file uri: ' + uri);
} else {
const untitledResource = this.resources.get(uri.toString());
if (!untitledResource) {
return this.createUntitledResource('', '', uri);
} else {
return untitledResource;
}
}
}
async createUntitledResource(content?: string, extension?: string, uri?: URI): Promise<UntitledResource> {
if (!uri) {
uri = this.createUntitledURI(extension);
}
return new UntitledResource(this.resources, uri, content);
}
createUntitledURI(extension?: string, parent?: URI): URI {
let counter = 1; // vscode starts at 1
let untitledUri;
do {
const name = `Untitled-${counter}${extension ?? ''}`;
if (parent) {
untitledUri = parent.resolve(name).withScheme(UNTITLED_SCHEME);
}
untitledUri = new URI().resolve(name).withScheme(UNTITLED_SCHEME);
counter++;
} while (this.has(untitledUri));
return untitledUri;
}
}
export class UntitledResource implements Resource {
protected readonly onDidChangeContentsEmitter = new Emitter<void>();
get onDidChangeContents(): Event<void> {
return this.onDidChangeContentsEmitter.event;
}
constructor(private resources: Map<string, UntitledResource>, public uri: URI, private content?: string) {
this.resources.set(this.uri.toString(), this);
}
dispose(): void {
this.resources.delete(this.uri.toString());
this.onDidChangeContentsEmitter.dispose();
}
async readContents(options?: { encoding?: string | undefined; } | undefined): Promise<string> {
if (this.content) {
return this.content;
} else {
return '';
}
}
async saveContents(content: string, options?: { encoding?: string, overwriteEncoding?: boolean }): Promise<void> {
// This function must exist to ensure readOnly is false for the Monaco editor.
// However it should not be called because saving 'untitled' is always processed as 'Save As'.
throw Error('Untitled resources cannot be saved.');
}
protected fireDidChangeContents(): void {
this.onDidChangeContentsEmitter.fire(undefined);
}
get version(): ResourceVersion | undefined {
return undefined;
}
get encoding(): string | undefined {
return undefined;
}
}
/**
* @deprecated Since 1.27.0. Please use `UntitledResourceResolver.createUntitledURI` instead.
*/
export function createUntitledURI(extension?: string, parent?: URI): URI {
const name = `Untitled-${untitledResourceSequenceIndex++}${extension ?? ''}`;
if (parent) {
return parent.resolve(name).withScheme(UNTITLED_SCHEME);
}
return new URI().resolve(name).withScheme(UNTITLED_SCHEME);
}