Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/editor/common/editor.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { equals } from '../../../base/common/arrays.js';
7
import { IDisposable } from '../../../base/common/lifecycle.js';
8
import { URI } from '../../../base/common/uri.js';
9
import { IUriIdentityService } from '../../uriIdentity/common/uriIdentity.js';
10
import { IRectangle } from '../../window/common/window.js';
11
12
export interface IResolvableEditorModel extends IDisposable {
13
14
/**
15
* Resolves the model.
16
*/
17
resolve(): Promise<void>;
18
19
/**
20
* Find out if the editor model was resolved or not.
21
*/
22
isResolved(): boolean;
23
}
24
25
export function isResolvedEditorModel(model: IDisposable | undefined | null): model is IResolvableEditorModel {
26
const candidate = model as IResolvableEditorModel | undefined | null;
27
28
return typeof candidate?.resolve === 'function'
29
&& typeof candidate?.isResolved === 'function';
30
}
31
32
export interface IBaseUntypedEditorInput {
33
34
/**
35
* Optional options to use when opening the input.
36
*/
37
options?: IEditorOptions;
38
39
/**
40
* Label to show for the input.
41
*/
42
readonly label?: string;
43
44
/**
45
* Description to show for the input.
46
*/
47
readonly description?: string;
48
}
49
50
export interface IBaseResourceEditorInput extends IBaseUntypedEditorInput {
51
52
/**
53
* Hint to indicate that this input should be treated as a
54
* untitled file.
55
*
56
* Without this hint, the editor service will make a guess by
57
* looking at the scheme of the resource(s).
58
*
59
* Use `forceUntitled: true` when you pass in a `resource` that
60
* does not use the `untitled` scheme. The `resource` will then
61
* be used as associated path when saving the untitled file.
62
*/
63
readonly forceUntitled?: boolean;
64
}
65
66
export interface IBaseTextResourceEditorInput extends IBaseResourceEditorInput {
67
68
/**
69
* Optional options to use when opening the text input.
70
*/
71
options?: ITextEditorOptions;
72
73
/**
74
* The contents of the text input if known. If provided,
75
* the input will not attempt to load the contents from
76
* disk and may appear dirty.
77
*/
78
contents?: string;
79
80
/**
81
* The encoding of the text input if known.
82
*/
83
encoding?: string;
84
85
/**
86
* The identifier of the language id of the text input
87
* if known to use when displaying the contents.
88
*/
89
languageId?: string;
90
}
91
92
export interface IResourceEditorInput extends IBaseResourceEditorInput {
93
94
/**
95
* The resource URI of the resource to open.
96
*/
97
readonly resource: URI;
98
}
99
100
export interface ITextResourceEditorInput extends IResourceEditorInput, IBaseTextResourceEditorInput {
101
102
/**
103
* Optional options to use when opening the text input.
104
*/
105
options?: ITextEditorOptions;
106
}
107
108
/**
109
* This identifier allows to uniquely identify an editor with a
110
* resource, type and editor identifier.
111
*/
112
export interface IResourceEditorInputIdentifier {
113
114
/**
115
* The type of the editor.
116
*/
117
readonly typeId: string;
118
119
/**
120
* The identifier of the editor if provided.
121
*/
122
readonly editorId: string | undefined;
123
124
/**
125
* The resource URI of the editor.
126
*/
127
readonly resource: URI;
128
}
129
130
export enum EditorActivation {
131
132
/**
133
* Activate the editor after it opened. This will automatically restore
134
* the editor if it is minimized.
135
*/
136
ACTIVATE = 1,
137
138
/**
139
* Only restore the editor if it is minimized but do not activate it.
140
*
141
* Note: will only work in combination with the `preserveFocus: true` option.
142
* Otherwise, if focus moves into the editor, it will activate and restore
143
* automatically.
144
*/
145
RESTORE,
146
147
/**
148
* Preserve the current active editor.
149
*
150
* Note: will only work in combination with the `preserveFocus: true` option.
151
* Otherwise, if focus moves into the editor, it will activate and restore
152
* automatically.
153
*/
154
PRESERVE
155
}
156
157
export enum EditorResolution {
158
159
/**
160
* Displays a picker and allows the user to decide which editor to use.
161
*/
162
PICK,
163
164
/**
165
* Only exclusive editors are considered.
166
*/
167
EXCLUSIVE_ONLY
168
}
169
170
export enum EditorOpenSource {
171
172
/**
173
* Default: the editor is opening via a programmatic call
174
* to the editor service API.
175
*/
176
API,
177
178
/**
179
* Indicates that a user action triggered the opening, e.g.
180
* via mouse or keyboard use.
181
*/
182
USER
183
}
184
185
export interface IEditorOptions {
186
187
/**
188
* Tells the editor to not receive keyboard focus when the editor is being opened.
189
*
190
* Will also not activate the group the editor opens in unless the group is already
191
* the active one. This behaviour can be overridden via the `activation` option.
192
*/
193
preserveFocus?: boolean;
194
195
/**
196
* This option is only relevant if an editor is opened into a group that is not active
197
* already and allows to control if the inactive group should become active, restored
198
* or preserved.
199
*
200
* By default, the editor group will become active unless `preserveFocus` or `inactive`
201
* is specified.
202
*/
203
activation?: EditorActivation;
204
205
/**
206
* Tells the editor to reload the editor input in the editor even if it is identical to the one
207
* already showing. By default, the editor will not reload the input if it is identical to the
208
* one showing.
209
*/
210
forceReload?: boolean;
211
212
/**
213
* Will reveal the editor if it is already opened and visible in any of the opened editor groups.
214
*
215
* Note that this option is just a hint that might be ignored if the user wants to open an editor explicitly
216
* to the side of another one or into a specific editor group.
217
*/
218
revealIfVisible?: boolean;
219
220
/**
221
* Will reveal the editor if it is already opened (even when not visible) in any of the opened editor groups.
222
*
223
* Note that this option is just a hint that might be ignored if the user wants to open an editor explicitly
224
* to the side of another one or into a specific editor group.
225
*/
226
revealIfOpened?: boolean;
227
228
/**
229
* An editor that is pinned remains in the editor stack even when another editor is being opened.
230
* An editor that is not pinned will always get replaced by another editor that is not pinned.
231
*/
232
pinned?: boolean;
233
234
/**
235
* An editor that is sticky moves to the beginning of the editors list within the group and will remain
236
* there unless explicitly closed. Operations such as "Close All" will not close sticky editors.
237
*/
238
sticky?: boolean;
239
240
/**
241
* The index in the document stack where to insert the editor into when opening.
242
*/
243
index?: number;
244
245
/**
246
* An active editor that is opened will show its contents directly. Set to true to open an editor
247
* in the background without loading its contents.
248
*
249
* Will also not activate the group the editor opens in unless the group is already
250
* the active one. This behaviour can be overridden via the `activation` option.
251
*/
252
inactive?: boolean;
253
254
/**
255
* In case of an error opening the editor, will not present this error to the user (e.g. by showing
256
* a generic placeholder in the editor area). So it is up to the caller to provide error information
257
* in that case.
258
*
259
* By default, an error when opening an editor will result in a placeholder editor that shows the error.
260
* In certain cases a modal dialog may be presented to ask the user for further action.
261
*/
262
ignoreError?: boolean;
263
264
/**
265
* Allows to override the editor that should be used to display the input:
266
* - `undefined`: let the editor decide for itself
267
* - `string`: specific override by id
268
* - `EditorResolution`: specific override handling
269
*/
270
override?: string | EditorResolution;
271
272
/**
273
* A optional hint to signal in which context the editor opens.
274
*
275
* If configured to be `EditorOpenSource.USER`, this hint can be
276
* used in various places to control the experience. For example,
277
* if the editor to open fails with an error, a notification could
278
* inform about this in a modal dialog. If the editor opened through
279
* some background task, the notification would show in the background,
280
* not as a modal dialog.
281
*/
282
source?: EditorOpenSource;
283
284
/**
285
* An optional property to signal that certain view state should be
286
* applied when opening the editor.
287
*/
288
viewState?: object;
289
290
/**
291
* A transient editor will attempt to appear as preview and certain components
292
* (such as history tracking) may decide to ignore the editor when it becomes
293
* active.
294
* This option is meant to be used only when the editor is used for a short
295
* period of time, for example when opening a preview of the editor from a
296
* picker control in the background while navigating through results of the picker.
297
*
298
* Note: an editor that is already opened in a group that is not transient, will
299
* not turn transient.
300
*/
301
transient?: boolean;
302
303
/**
304
* Options that only apply when `AUX_WINDOW_GROUP` is used for opening.
305
*/
306
auxiliary?: {
307
308
/**
309
* Define the bounds of the editor window.
310
*/
311
bounds?: Partial<IRectangle>;
312
313
/**
314
* Show editor compact, hiding unnecessary elements.
315
*/
316
compact?: boolean;
317
318
/**
319
* Show the editor always on top of other windows.
320
*/
321
alwaysOnTop?: boolean;
322
};
323
}
324
325
export interface ITextEditorSelection {
326
readonly startLineNumber: number;
327
readonly startColumn: number;
328
readonly endLineNumber?: number;
329
readonly endColumn?: number;
330
}
331
332
export const enum TextEditorSelectionRevealType {
333
/**
334
* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically.
335
*/
336
Center = 0,
337
338
/**
339
* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.
340
*/
341
CenterIfOutsideViewport = 1,
342
343
/**
344
* Option to scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport, but not quite at the top.
345
*/
346
NearTop = 2,
347
348
/**
349
* Option to scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport, but not quite at the top.
350
* Only if it lies outside the viewport
351
*/
352
NearTopIfOutsideViewport = 3,
353
}
354
355
export const enum TextEditorSelectionSource {
356
357
/**
358
* Programmatic source indicates a selection change that
359
* was not triggered by the user via keyboard or mouse
360
* but through text editor APIs.
361
*/
362
PROGRAMMATIC = 'api',
363
364
/**
365
* Navigation source indicates a selection change that
366
* was caused via some command or UI component such as
367
* an outline tree.
368
*/
369
NAVIGATION = 'code.navigation',
370
371
/**
372
* Jump source indicates a selection change that
373
* was caused from within the text editor to another
374
* location in the same or different text editor such
375
* as "Go to definition".
376
*/
377
JUMP = 'code.jump'
378
}
379
380
export interface ITextEditorOptions extends IEditorOptions {
381
382
/**
383
* Text editor selection.
384
*/
385
selection?: ITextEditorSelection;
386
387
/**
388
* Option to control the text editor selection reveal type.
389
* Defaults to TextEditorSelectionRevealType.Center
390
*/
391
selectionRevealType?: TextEditorSelectionRevealType;
392
393
/**
394
* Source of the call that caused the selection.
395
*/
396
selectionSource?: TextEditorSelectionSource | string;
397
}
398
399
export type ITextEditorChange = [
400
originalStartLineNumber: number,
401
originalEndLineNumberExclusive: number,
402
modifiedStartLineNumber: number,
403
modifiedEndLineNumberExclusive: number
404
];
405
406
export interface ITextEditorDiffInformation {
407
readonly documentVersion: number;
408
readonly original: URI | undefined;
409
readonly modified: URI;
410
readonly changes: readonly ITextEditorChange[];
411
}
412
413
export function isTextEditorDiffInformationEqual(
414
uriIdentityService: IUriIdentityService,
415
diff1: ITextEditorDiffInformation | undefined,
416
diff2: ITextEditorDiffInformation | undefined): boolean {
417
return diff1?.documentVersion === diff2?.documentVersion &&
418
uriIdentityService.extUri.isEqual(diff1?.original, diff2?.original) &&
419
uriIdentityService.extUri.isEqual(diff1?.modified, diff2?.modified) &&
420
equals<ITextEditorChange>(diff1?.changes, diff2?.changes, (a, b) => {
421
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
422
});
423
}
424
425