UNPKG

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