Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/editorCommon.ts
3292 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 { Event } from '../../base/common/event.js';
7
import { IMarkdownString } from '../../base/common/htmlContent.js';
8
import { IDisposable } from '../../base/common/lifecycle.js';
9
import { ThemeColor } from '../../base/common/themables.js';
10
import { URI, UriComponents } from '../../base/common/uri.js';
11
import { IEditorOptions } from './config/editorOptions.js';
12
import { IDimension } from './core/2d/dimension.js';
13
import { IPosition, Position } from './core/position.js';
14
import { IRange, Range } from './core/range.js';
15
import { ISelection, Selection } from './core/selection.js';
16
import { IModelDecoration, IModelDecorationsChangeAccessor, IModelDeltaDecoration, ITextModel, IValidEditOperation, OverviewRulerLane, TrackedRangeStickiness } from './model.js';
17
import { IModelDecorationsChangedEvent } from './textModelEvents.js';
18
import { ICommandMetadata } from '../../platform/commands/common/commands.js';
19
20
/**
21
* A builder and helper for edit operations for a command.
22
*/
23
export interface IEditOperationBuilder {
24
/**
25
* Add a new edit operation (a replace operation).
26
* @param range The range to replace (delete). May be empty to represent a simple insert.
27
* @param text The text to replace with. May be null to represent a simple delete.
28
*/
29
addEditOperation(range: IRange, text: string | null, forceMoveMarkers?: boolean): void;
30
31
/**
32
* Add a new edit operation (a replace operation).
33
* The inverse edits will be accessible in `ICursorStateComputerData.getInverseEditOperations()`
34
* @param range The range to replace (delete). May be empty to represent a simple insert.
35
* @param text The text to replace with. May be null to represent a simple delete.
36
*/
37
addTrackedEditOperation(range: IRange, text: string | null, forceMoveMarkers?: boolean): void;
38
39
/**
40
* Track `selection` when applying edit operations.
41
* A best effort will be made to not grow/expand the selection.
42
* An empty selection will clamp to a nearby character.
43
* @param selection The selection to track.
44
* @param trackPreviousOnEmpty If set, and the selection is empty, indicates whether the selection
45
* should clamp to the previous or the next character.
46
* @return A unique identifier.
47
*/
48
trackSelection(selection: Selection, trackPreviousOnEmpty?: boolean): string;
49
}
50
51
/**
52
* A helper for computing cursor state after a command.
53
*/
54
export interface ICursorStateComputerData {
55
/**
56
* Get the inverse edit operations of the added edit operations.
57
*/
58
getInverseEditOperations(): IValidEditOperation[];
59
/**
60
* Get a previously tracked selection.
61
* @param id The unique identifier returned by `trackSelection`.
62
* @return The selection.
63
*/
64
getTrackedSelection(id: string): Selection;
65
}
66
67
/**
68
* A command that modifies text / cursor state on a model.
69
*/
70
export interface ICommand {
71
72
/**
73
* Signal that this command is inserting automatic whitespace that should be trimmed if possible.
74
* @internal
75
*/
76
readonly insertsAutoWhitespace?: boolean;
77
78
/**
79
* Get the edit operations needed to execute this command.
80
* @param model The model the command will execute on.
81
* @param builder A helper to collect the needed edit operations and to track selections.
82
*/
83
getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void;
84
85
/**
86
* Compute the cursor state after the edit operations were applied.
87
* @param model The model the command has executed on.
88
* @param helper A helper to get inverse edit operations and to get previously tracked selections.
89
* @return The cursor state after the command executed.
90
*/
91
computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection;
92
}
93
94
/**
95
* A model for the diff editor.
96
*/
97
export interface IDiffEditorModel {
98
/**
99
* Original model.
100
*/
101
original: ITextModel;
102
/**
103
* Modified model.
104
*/
105
modified: ITextModel;
106
}
107
108
export interface IDiffEditorViewModel extends IDisposable {
109
readonly model: IDiffEditorModel;
110
111
waitForDiff(): Promise<void>;
112
}
113
114
/**
115
* An event describing that an editor has had its model reset (i.e. `editor.setModel()`).
116
*/
117
export interface IModelChangedEvent {
118
/**
119
* The `uri` of the previous model or null.
120
*/
121
readonly oldModelUrl: URI | null;
122
/**
123
* The `uri` of the new model or null.
124
*/
125
readonly newModelUrl: URI | null;
126
}
127
128
// --- view
129
130
export interface IScrollEvent {
131
readonly scrollTop: number;
132
readonly scrollLeft: number;
133
readonly scrollWidth: number;
134
readonly scrollHeight: number;
135
136
readonly scrollTopChanged: boolean;
137
readonly scrollLeftChanged: boolean;
138
readonly scrollWidthChanged: boolean;
139
readonly scrollHeightChanged: boolean;
140
}
141
142
export interface IContentSizeChangedEvent {
143
readonly contentWidth: number;
144
readonly contentHeight: number;
145
146
readonly contentWidthChanged: boolean;
147
readonly contentHeightChanged: boolean;
148
}
149
150
/**
151
* @internal
152
*/
153
export interface ITriggerEditorOperationEvent {
154
source: string | null | undefined;
155
handlerId: string;
156
payload: unknown;
157
}
158
159
export interface INewScrollPosition {
160
scrollLeft?: number;
161
scrollTop?: number;
162
}
163
164
export interface IEditorAction {
165
readonly id: string;
166
readonly label: string;
167
readonly alias: string;
168
readonly metadata: ICommandMetadata | undefined;
169
isSupported(): boolean;
170
run(args?: unknown): Promise<void>;
171
}
172
173
export type IEditorModel = ITextModel | IDiffEditorModel | IDiffEditorViewModel;
174
175
/**
176
* A (serializable) state of the cursors.
177
*/
178
export interface ICursorState {
179
inSelectionMode: boolean;
180
selectionStart: IPosition;
181
position: IPosition;
182
}
183
/**
184
* A (serializable) state of the view.
185
*/
186
export interface IViewState {
187
/** written by previous versions */
188
scrollTop?: number;
189
/** written by previous versions */
190
scrollTopWithoutViewZones?: number;
191
scrollLeft: number;
192
firstPosition: IPosition;
193
firstPositionDeltaTop: number;
194
}
195
/**
196
* A (serializable) state of the code editor.
197
*/
198
export interface ICodeEditorViewState {
199
cursorState: ICursorState[];
200
viewState: IViewState;
201
contributionsState: { [id: string]: any };
202
}
203
/**
204
* (Serializable) View state for the diff editor.
205
*/
206
export interface IDiffEditorViewState {
207
original: ICodeEditorViewState | null;
208
modified: ICodeEditorViewState | null;
209
modelState?: unknown;
210
}
211
/**
212
* An editor view state.
213
*/
214
export type IEditorViewState = ICodeEditorViewState | IDiffEditorViewState;
215
216
export const enum ScrollType {
217
Smooth = 0,
218
Immediate = 1,
219
}
220
221
/**
222
* An editor.
223
*/
224
export interface IEditor {
225
/**
226
* An event emitted when the editor has been disposed.
227
* @event
228
*/
229
onDidDispose(listener: () => void): IDisposable;
230
231
/**
232
* Dispose the editor.
233
*/
234
dispose(): void;
235
236
/**
237
* Get a unique id for this editor instance.
238
*/
239
getId(): string;
240
241
/**
242
* Get the editor type. Please see `EditorType`.
243
* This is to avoid an instanceof check
244
*/
245
getEditorType(): string;
246
247
/**
248
* Update the editor's options after the editor has been created.
249
*/
250
updateOptions(newOptions: IEditorOptions): void;
251
252
/**
253
* Indicates that the editor becomes visible.
254
* @internal
255
*/
256
onVisible(): void;
257
258
/**
259
* Indicates that the editor becomes hidden.
260
* @internal
261
*/
262
onHide(): void;
263
264
/**
265
* Instructs the editor to remeasure its container. This method should
266
* be called when the container of the editor gets resized.
267
*
268
* If a dimension is passed in, the passed in value will be used.
269
*
270
* By default, this will also render the editor immediately.
271
* If you prefer to delay rendering to the next animation frame, use postponeRendering == true.
272
*/
273
layout(dimension?: IDimension, postponeRendering?: boolean): void;
274
275
/**
276
* Brings browser focus to the editor text
277
*/
278
focus(): void;
279
280
/**
281
* Returns true if the text inside this editor is focused (i.e. cursor is blinking).
282
*/
283
hasTextFocus(): boolean;
284
285
/**
286
* Returns all actions associated with this editor.
287
*/
288
getSupportedActions(): IEditorAction[];
289
290
/**
291
* Saves current view state of the editor in a serializable object.
292
*/
293
saveViewState(): IEditorViewState | null;
294
295
/**
296
* Restores the view state of the editor from a serializable object generated by `saveViewState`.
297
*/
298
restoreViewState(state: IEditorViewState | null): void;
299
300
/**
301
* Given a position, returns a column number that takes tab-widths into account.
302
*/
303
getVisibleColumnFromPosition(position: IPosition): number;
304
305
/**
306
* Given a position, returns a column number that takes tab-widths into account.
307
* @internal
308
*/
309
getStatusbarColumn(position: IPosition): number;
310
311
/**
312
* Returns the primary position of the cursor.
313
*/
314
getPosition(): Position | null;
315
316
/**
317
* Set the primary position of the cursor. This will remove any secondary cursors.
318
* @param position New primary cursor's position
319
* @param source Source of the call that caused the position
320
*/
321
setPosition(position: IPosition, source?: string): void;
322
323
/**
324
* Scroll vertically as necessary and reveal a line.
325
*/
326
revealLine(lineNumber: number, scrollType?: ScrollType): void;
327
328
/**
329
* Scroll vertically as necessary and reveal a line centered vertically.
330
*/
331
revealLineInCenter(lineNumber: number, scrollType?: ScrollType): void;
332
333
/**
334
* Scroll vertically as necessary and reveal a line centered vertically only if it lies outside the viewport.
335
*/
336
revealLineInCenterIfOutsideViewport(lineNumber: number, scrollType?: ScrollType): void;
337
338
/**
339
* Scroll vertically as necessary and reveal a line close to the top of the viewport,
340
* optimized for viewing a code definition.
341
*/
342
revealLineNearTop(lineNumber: number, scrollType?: ScrollType): void;
343
344
/**
345
* Scroll vertically or horizontally as necessary and reveal a position.
346
*/
347
revealPosition(position: IPosition, scrollType?: ScrollType): void;
348
349
/**
350
* Scroll vertically or horizontally as necessary and reveal a position centered vertically.
351
*/
352
revealPositionInCenter(position: IPosition, scrollType?: ScrollType): void;
353
354
/**
355
* Scroll vertically or horizontally as necessary and reveal a position centered vertically only if it lies outside the viewport.
356
*/
357
revealPositionInCenterIfOutsideViewport(position: IPosition, scrollType?: ScrollType): void;
358
359
/**
360
* Scroll vertically or horizontally as necessary and reveal a position close to the top of the viewport,
361
* optimized for viewing a code definition.
362
*/
363
revealPositionNearTop(position: IPosition, scrollType?: ScrollType): void;
364
365
/**
366
* Returns the primary selection of the editor.
367
*/
368
getSelection(): Selection | null;
369
370
/**
371
* Returns all the selections of the editor.
372
*/
373
getSelections(): Selection[] | null;
374
375
/**
376
* Set the primary selection of the editor. This will remove any secondary cursors.
377
* @param selection The new selection
378
* @param source Source of the call that caused the selection
379
*/
380
setSelection(selection: IRange, source?: string): void;
381
/**
382
* Set the primary selection of the editor. This will remove any secondary cursors.
383
* @param selection The new selection
384
* @param source Source of the call that caused the selection
385
*/
386
setSelection(selection: Range, source?: string): void;
387
/**
388
* Set the primary selection of the editor. This will remove any secondary cursors.
389
* @param selection The new selection
390
* @param source Source of the call that caused the selection
391
*/
392
setSelection(selection: ISelection, source?: string): void;
393
/**
394
* Set the primary selection of the editor. This will remove any secondary cursors.
395
* @param selection The new selection
396
* @param source Source of the call that caused the selection
397
*/
398
setSelection(selection: Selection, source?: string): void;
399
400
/**
401
* Set the selections for all the cursors of the editor.
402
* Cursors will be removed or added, as necessary.
403
* @param selections The new selection
404
* @param source Source of the call that caused the selection
405
*/
406
setSelections(selections: readonly ISelection[], source?: string): void;
407
408
/**
409
* Scroll vertically as necessary and reveal lines.
410
*/
411
revealLines(startLineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
412
413
/**
414
* Scroll vertically as necessary and reveal lines centered vertically.
415
*/
416
revealLinesInCenter(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
417
418
/**
419
* Scroll vertically as necessary and reveal lines centered vertically only if it lies outside the viewport.
420
*/
421
revealLinesInCenterIfOutsideViewport(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
422
423
/**
424
* Scroll vertically as necessary and reveal lines close to the top of the viewport,
425
* optimized for viewing a code definition.
426
*/
427
revealLinesNearTop(lineNumber: number, endLineNumber: number, scrollType?: ScrollType): void;
428
429
/**
430
* Scroll vertically or horizontally as necessary and reveal a range.
431
*/
432
revealRange(range: IRange, scrollType?: ScrollType): void;
433
434
/**
435
* Scroll vertically or horizontally as necessary and reveal a range centered vertically.
436
*/
437
revealRangeInCenter(range: IRange, scrollType?: ScrollType): void;
438
439
/**
440
* Scroll vertically or horizontally as necessary and reveal a range at the top of the viewport.
441
*/
442
revealRangeAtTop(range: IRange, scrollType?: ScrollType): void;
443
444
/**
445
* Scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.
446
*/
447
revealRangeInCenterIfOutsideViewport(range: IRange, scrollType?: ScrollType): void;
448
449
/**
450
* Scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport,
451
* optimized for viewing a code definition.
452
*/
453
revealRangeNearTop(range: IRange, scrollType?: ScrollType): void;
454
455
/**
456
* Scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport,
457
* optimized for viewing a code definition. Only if it lies outside the viewport.
458
*/
459
revealRangeNearTopIfOutsideViewport(range: IRange, scrollType?: ScrollType): void;
460
461
/**
462
* Directly trigger a handler or an editor action.
463
* @param source The source of the call.
464
* @param handlerId The id of the handler or the id of a contribution.
465
* @param payload Extra data to be sent to the handler.
466
*/
467
trigger(source: string | null | undefined, handlerId: string, payload: any): void;
468
469
/**
470
* Gets the current model attached to this editor.
471
*/
472
getModel(): IEditorModel | null;
473
474
/**
475
* Sets the current model attached to this editor.
476
* If the previous model was created by the editor via the value key in the options
477
* literal object, it will be destroyed. Otherwise, if the previous model was set
478
* via setModel, or the model key in the options literal object, the previous model
479
* will not be destroyed.
480
* It is safe to call setModel(null) to simply detach the current model from the editor.
481
*/
482
setModel(model: IEditorModel | null): void;
483
484
/**
485
* Create a collection of decorations. All decorations added through this collection
486
* will get the ownerId of the editor (meaning they will not show up in other editors).
487
* These decorations will be automatically cleared when the editor's model changes.
488
*/
489
createDecorationsCollection(decorations?: IModelDeltaDecoration[]): IEditorDecorationsCollection;
490
491
/**
492
* Change the decorations. All decorations added through this changeAccessor
493
* will get the ownerId of the editor (meaning they will not show up in other
494
* editors).
495
* @see {@link ITextModel.changeDecorations}
496
* @internal
497
*/
498
changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any;
499
}
500
501
/**
502
* A diff editor.
503
*
504
* @internal
505
*/
506
export interface IDiffEditor extends IEditor {
507
508
/**
509
* Type the getModel() of IEditor.
510
*/
511
getModel(): IDiffEditorModel | null;
512
513
/**
514
* Get the `original` editor.
515
*/
516
getOriginalEditor(): IEditor;
517
518
/**
519
* Get the `modified` editor.
520
*/
521
getModifiedEditor(): IEditor;
522
}
523
524
/**
525
* @internal
526
*/
527
export interface ICompositeCodeEditor {
528
529
/**
530
* An event that signals that the active editor has changed
531
*/
532
readonly onDidChangeActiveEditor: Event<ICompositeCodeEditor>;
533
534
/**
535
* The active code editor iff any
536
*/
537
readonly activeCodeEditor: IEditor | undefined;
538
// readonly editors: readonly ICodeEditor[] maybe supported with uris
539
}
540
541
/**
542
* A collection of decorations
543
*/
544
export interface IEditorDecorationsCollection {
545
/**
546
* An event emitted when decorations change in the editor,
547
* but the change is not caused by us setting or clearing the collection.
548
*/
549
onDidChange: Event<IModelDecorationsChangedEvent>;
550
/**
551
* Get the decorations count.
552
*/
553
length: number;
554
/**
555
* Get the range for a decoration.
556
*/
557
getRange(index: number): Range | null;
558
/**
559
* Get all ranges for decorations.
560
*/
561
getRanges(): Range[];
562
/**
563
* Determine if a decoration is in this collection.
564
*/
565
has(decoration: IModelDecoration): boolean;
566
/**
567
* Replace all previous decorations with `newDecorations`.
568
*/
569
set(newDecorations: readonly IModelDeltaDecoration[]): string[];
570
/**
571
* Append `newDecorations` to this collection.
572
*/
573
append(newDecorations: readonly IModelDeltaDecoration[]): string[];
574
/**
575
* Remove all previous decorations.
576
*/
577
clear(): void;
578
}
579
580
/**
581
* An editor contribution that gets created every time a new editor gets created and gets disposed when the editor gets disposed.
582
*/
583
export interface IEditorContribution {
584
/**
585
* Dispose this contribution.
586
*/
587
dispose(): void;
588
/**
589
* Store view state.
590
*/
591
saveViewState?(): any;
592
/**
593
* Restore view state.
594
*/
595
restoreViewState?(state: any): void;
596
}
597
598
/**
599
* A diff editor contribution that gets created every time a new diffeditor gets created and gets disposed when the diff editor gets disposed.
600
* @internal
601
*/
602
export interface IDiffEditorContribution {
603
/**
604
* Dispose this contribution.
605
*/
606
dispose(): void;
607
}
608
609
/**
610
* @internal
611
*/
612
export function isThemeColor(o: any): o is ThemeColor {
613
return o && typeof o.id === 'string';
614
}
615
616
/**
617
* @internal
618
*/
619
export interface IThemeDecorationRenderOptions {
620
backgroundColor?: string | ThemeColor;
621
622
outline?: string;
623
outlineColor?: string | ThemeColor;
624
outlineStyle?: string;
625
outlineWidth?: string;
626
627
border?: string;
628
borderColor?: string | ThemeColor;
629
borderRadius?: string;
630
borderSpacing?: string;
631
borderStyle?: string;
632
borderWidth?: string;
633
634
fontStyle?: string;
635
fontWeight?: string;
636
fontFamily?: string;
637
fontSize?: string;
638
lineHeight?: number;
639
textDecoration?: string;
640
cursor?: string;
641
color?: string | ThemeColor;
642
opacity?: string;
643
letterSpacing?: string;
644
645
gutterIconPath?: UriComponents;
646
gutterIconSize?: string;
647
648
overviewRulerColor?: string | ThemeColor;
649
650
/**
651
* @deprecated
652
*/
653
before?: IContentDecorationRenderOptions;
654
/**
655
* @deprecated
656
*/
657
after?: IContentDecorationRenderOptions;
658
659
/**
660
* @deprecated
661
*/
662
beforeInjectedText?: IContentDecorationRenderOptions & { affectsLetterSpacing?: boolean };
663
/**
664
* @deprecated
665
*/
666
afterInjectedText?: IContentDecorationRenderOptions & { affectsLetterSpacing?: boolean };
667
}
668
669
/**
670
* @internal
671
*/
672
export interface IContentDecorationRenderOptions {
673
contentText?: string;
674
contentIconPath?: UriComponents;
675
676
border?: string;
677
borderColor?: string | ThemeColor;
678
borderRadius?: string;
679
fontStyle?: string;
680
fontWeight?: string;
681
fontSize?: string;
682
fontFamily?: string;
683
textDecoration?: string;
684
color?: string | ThemeColor;
685
backgroundColor?: string | ThemeColor;
686
opacity?: string;
687
verticalAlign?: string;
688
689
margin?: string;
690
padding?: string;
691
width?: string;
692
height?: string;
693
}
694
695
/**
696
* @internal
697
*/
698
export interface IDecorationRenderOptions extends IThemeDecorationRenderOptions {
699
isWholeLine?: boolean;
700
rangeBehavior?: TrackedRangeStickiness;
701
overviewRulerLane?: OverviewRulerLane;
702
703
light?: IThemeDecorationRenderOptions;
704
dark?: IThemeDecorationRenderOptions;
705
}
706
707
/**
708
* @internal
709
*/
710
export interface IThemeDecorationInstanceRenderOptions {
711
/**
712
* @deprecated
713
*/
714
before?: IContentDecorationRenderOptions;
715
/**
716
* @deprecated
717
*/
718
after?: IContentDecorationRenderOptions;
719
}
720
721
/**
722
* @internal
723
*/
724
export interface IDecorationInstanceRenderOptions extends IThemeDecorationInstanceRenderOptions {
725
light?: IThemeDecorationInstanceRenderOptions;
726
dark?: IThemeDecorationInstanceRenderOptions;
727
}
728
729
/**
730
* @internal
731
*/
732
export interface IDecorationOptions {
733
range: IRange;
734
hoverMessage?: IMarkdownString | IMarkdownString[];
735
renderOptions?: IDecorationInstanceRenderOptions;
736
}
737
738
/**
739
* The type of the `IEditor`.
740
*/
741
export const EditorType = {
742
ICodeEditor: 'vs.editor.ICodeEditor',
743
IDiffEditor: 'vs.editor.IDiffEditor'
744
};
745
746
/**
747
* Built-in commands.
748
* @internal
749
*/
750
export const enum Handler {
751
CompositionStart = 'compositionStart',
752
CompositionEnd = 'compositionEnd',
753
Type = 'type',
754
ReplacePreviousChar = 'replacePreviousChar',
755
CompositionType = 'compositionType',
756
Paste = 'paste',
757
Cut = 'cut',
758
}
759
760
/**
761
* @internal
762
*/
763
export interface TypePayload {
764
text: string;
765
}
766
767
/**
768
* @internal
769
*/
770
export interface ReplacePreviousCharPayload {
771
text: string;
772
replaceCharCnt: number;
773
}
774
775
/**
776
* @internal
777
*/
778
export interface CompositionTypePayload {
779
text: string;
780
replacePrevCharCnt: number;
781
replaceNextCharCnt: number;
782
positionDelta: number;
783
}
784
785