UNPKG

8.27 kBTypeScriptView Raw
1import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol';
2import URI from '../common/uri';
3import { ContributionProvider } from './contribution-provider';
4import { Event, Emitter } from './event';
5import { Disposable } from './disposable';
6import { MaybePromise } from './types';
7import { CancellationToken } from './cancellation';
8import { ApplicationError } from './application-error';
9import { ReadableStream, Readable } from './stream';
10import { SyncReferenceCollection, Reference } from './reference';
11export interface ResourceVersion {
12}
13export interface ResourceReadOptions {
14 encoding?: string;
15}
16export interface ResourceSaveOptions {
17 encoding?: string;
18 overwriteEncoding?: boolean;
19 version?: ResourceVersion;
20}
21export interface Resource extends Disposable {
22 readonly uri: URI;
23 /**
24 * Latest read version of this resource.
25 *
26 * Optional if a resource does not support versioning, check with `in` operator`.
27 * Undefined if a resource did not read content yet.
28 */
29 readonly version?: ResourceVersion | undefined;
30 /**
31 * Latest read encoding of this resource.
32 *
33 * Optional if a resource does not support encoding, check with `in` operator`.
34 * Undefined if a resource did not read content yet.
35 */
36 readonly encoding?: string | undefined;
37 readonly isReadonly?: boolean;
38 /**
39 * Reads latest content of this resource.
40 *
41 * If a resource supports versioning it updates version to latest.
42 * If a resource supports encoding it updates encoding to latest.
43 *
44 * @throws `ResourceError.NotFound` if a resource not found
45 */
46 readContents(options?: ResourceReadOptions): Promise<string>;
47 /**
48 * Stream latest content of this resource.
49 *
50 * If a resource supports versioning it updates version to latest.
51 * If a resource supports encoding it updates encoding to latest.
52 *
53 * @throws `ResourceError.NotFound` if a resource not found
54 */
55 readStream?(options?: ResourceReadOptions): Promise<ReadableStream<string>>;
56 /**
57 * Rewrites the complete content for this resource.
58 * If a resource does not exist it will be created.
59 *
60 * If a resource supports versioning clients can pass some version
61 * to check against it, if it is not provided latest version is used.
62 *
63 * It updates version and encoding to latest.
64 *
65 * @throws `ResourceError.OutOfSync` if latest resource version is out of sync with the given
66 */
67 saveContents?(content: string, options?: ResourceSaveOptions): Promise<void>;
68 /**
69 * Rewrites the complete content for this resource.
70 * If a resource does not exist it will be created.
71 *
72 * If a resource supports versioning clients can pass some version
73 * to check against it, if it is not provided latest version is used.
74 *
75 * It updates version and encoding to latest.
76 *
77 * @throws `ResourceError.OutOfSync` if latest resource version is out of sync with the given
78 */
79 saveStream?(content: Readable<string>, options?: ResourceSaveOptions): Promise<void>;
80 /**
81 * Applies incremental content changes to this resource.
82 *
83 * If a resource supports versioning clients can pass some version
84 * to check against it, if it is not provided latest version is used.
85 * It updates version to latest.
86 *
87 * @throws `ResourceError.NotFound` if a resource not found or was not read yet
88 * @throws `ResourceError.OutOfSync` if latest resource version is out of sync with the given
89 */
90 saveContentChanges?(changes: TextDocumentContentChangeEvent[], options?: ResourceSaveOptions): Promise<void>;
91 readonly onDidChangeContents?: Event<void>;
92 guessEncoding?(): Promise<string | undefined>;
93}
94export declare namespace Resource {
95 interface SaveContext {
96 contentLength: number;
97 content: string | Readable<string>;
98 changes?: TextDocumentContentChangeEvent[];
99 options?: ResourceSaveOptions;
100 }
101 function save(resource: Resource, context: SaveContext, token?: CancellationToken): Promise<void>;
102 function trySaveContentChanges(resource: Resource, context: SaveContext): Promise<boolean>;
103 function shouldSaveContent(resource: Resource, { contentLength, changes }: SaveContext): boolean;
104}
105export declare namespace ResourceError {
106 const NotFound: ApplicationError.Constructor<-40000, {
107 uri: URI;
108 }>;
109 const OutOfSync: ApplicationError.Constructor<-40001, {
110 uri: URI;
111 }>;
112}
113export declare const ResourceResolver: unique symbol;
114export interface ResourceResolver {
115 /**
116 * Reject if a resource cannot be provided.
117 */
118 resolve(uri: URI): MaybePromise<Resource>;
119}
120export declare const ResourceProvider: unique symbol;
121export declare type ResourceProvider = (uri: URI) => Promise<Resource>;
122export declare class DefaultResourceProvider {
123 protected readonly resolversProvider: ContributionProvider<ResourceResolver>;
124 constructor(resolversProvider: ContributionProvider<ResourceResolver>);
125 /**
126 * Reject if a resource cannot be provided.
127 */
128 get(uri: URI): Promise<Resource>;
129}
130export declare class MutableResource implements Resource {
131 readonly uri: URI;
132 private contents;
133 constructor(uri: URI);
134 dispose(): void;
135 readContents(): Promise<string>;
136 saveContents(contents: string): Promise<void>;
137 protected readonly onDidChangeContentsEmitter: Emitter<void>;
138 readonly onDidChangeContents: Event<void>;
139 protected fireDidChangeContents(): void;
140}
141export declare class ReferenceMutableResource implements Resource {
142 protected reference: Reference<MutableResource>;
143 constructor(reference: Reference<MutableResource>);
144 get uri(): URI;
145 get onDidChangeContents(): Event<void>;
146 dispose(): void;
147 readContents(): Promise<string>;
148 saveContents(contents: string): Promise<void>;
149}
150export declare class InMemoryResources implements ResourceResolver {
151 protected readonly resources: SyncReferenceCollection<string, MutableResource>;
152 add(uri: URI, contents: string): Resource;
153 update(uri: URI, contents: string): Resource;
154 resolve(uri: URI): Resource;
155 protected acquire(uri: string): ReferenceMutableResource;
156}
157export declare const MEMORY_TEXT = "mem-txt";
158/**
159 * Resource implementation for 'mem-txt' URI scheme where content is saved in URI query.
160 */
161export declare class InMemoryTextResource implements Resource {
162 readonly uri: URI;
163 constructor(uri: URI);
164 readContents(options?: {
165 encoding?: string | undefined;
166 } | undefined): Promise<string>;
167 dispose(): void;
168}
169/**
170 * ResourceResolver implementation for 'mem-txt' URI scheme.
171 */
172export declare class InMemoryTextResourceResolver implements ResourceResolver {
173 resolve(uri: URI): MaybePromise<Resource>;
174}
175export declare const UNTITLED_SCHEME = "untitled";
176export declare class UntitledResourceResolver implements ResourceResolver {
177 protected readonly resources: Map<string, UntitledResource>;
178 has(uri: URI): boolean;
179 resolve(uri: URI): Promise<UntitledResource>;
180 createUntitledResource(content?: string, extension?: string, uri?: URI): Promise<UntitledResource>;
181 createUntitledURI(extension?: string, parent?: URI): URI;
182}
183export declare class UntitledResource implements Resource {
184 private resources;
185 uri: URI;
186 private content?;
187 protected readonly onDidChangeContentsEmitter: Emitter<void>;
188 get onDidChangeContents(): Event<void>;
189 constructor(resources: Map<string, UntitledResource>, uri: URI, content?: string | undefined);
190 dispose(): void;
191 readContents(options?: {
192 encoding?: string | undefined;
193 } | undefined): Promise<string>;
194 saveContents(content: string, options?: {
195 encoding?: string;
196 overwriteEncoding?: boolean;
197 }): Promise<void>;
198 protected fireDidChangeContents(): void;
199 get version(): ResourceVersion | undefined;
200 get encoding(): string | undefined;
201}
202/**
203 * @deprecated Since 1.27.0. Please use `UntitledResourceResolver.createUntitledURI` instead.
204 */
205export declare function createUntitledURI(extension?: string, parent?: URI): URI;
206//# sourceMappingURL=resource.d.ts.map
\No newline at end of file