@difizen/libro-code-editor 0.0.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,667 @@
1
+ import type { JSONObject } from '@difizen/libro-common';
2
+ import type { Disposable, Event } from '@difizen/mana-app';
3
+ import type {
4
+ DidChangeConfigurationParams,
5
+ ServerCapabilities,
6
+ } from 'vscode-languageserver-protocol';
7
+
8
+ import type { IModel } from './model.js';
9
+
10
+ /**
11
+ * Code editor accessor.
12
+ */
13
+ export interface ILSPEditor {
14
+ /**
15
+ * CodeEditor getter.
16
+ *
17
+ * It will return `null` if the editor is not yet instantiated;
18
+ * e.g. to support windowed notebook.
19
+ */
20
+ getEditor(): IEditor | null;
21
+
22
+ /**
23
+ * Promise getter that resolved when the editor is instantiated.
24
+ */
25
+ ready(): Promise<IEditor>;
26
+
27
+ /**
28
+ * Reveal the code editor in viewport.
29
+ *
30
+ * ### Notes
31
+ * The promise will resolve when the editor is instantiated and in
32
+ * the viewport.
33
+ */
34
+ reveal(): Promise<IEditor>;
35
+ }
36
+
37
+ /**
38
+ * A zero-based position in the editor.
39
+ */
40
+ export interface IPosition extends JSONObject {
41
+ /**
42
+ * The cursor line number.
43
+ */
44
+ readonly line: number;
45
+
46
+ /**
47
+ * The cursor column number.
48
+ */
49
+ readonly column: number;
50
+ }
51
+ /**
52
+ * An interface describing editor state coordinates.
53
+ */
54
+ export type ICoordinate = DOMRectReadOnly;
55
+ /**
56
+ * A range.
57
+ */
58
+ export interface IRange {
59
+ /**
60
+ * The position of the first character in the current range.
61
+ *
62
+ * #### Notes
63
+ * If this position is greater than [end] then the range is considered
64
+ * to be backward.
65
+ */
66
+ readonly start: IPosition;
67
+
68
+ /**
69
+ * The position of the last character in the current range.
70
+ *
71
+ * #### Notes
72
+ * If this position is less than [start] then the range is considered
73
+ * to be backward.
74
+ */
75
+ readonly end: IPosition;
76
+ }
77
+
78
+ /**
79
+ * A selection style.
80
+ */
81
+ export interface IEditorSelectionStyle {
82
+ /**
83
+ * A class name added to a selection.
84
+ */
85
+ className: string;
86
+
87
+ /**
88
+ * A display name added to a selection.
89
+ */
90
+ displayName: string;
91
+
92
+ /**
93
+ * A color for UI elements.
94
+ */
95
+ color: string;
96
+ }
97
+
98
+ /**
99
+ * The default selection style.
100
+ */
101
+ export const defaultSelectionStyle: IEditorSelectionStyle = {
102
+ className: '',
103
+ displayName: '',
104
+ color: 'black',
105
+ };
106
+
107
+ /**
108
+ * A text selection.
109
+ */
110
+ export interface ITextSelection extends IRange {
111
+ /**
112
+ * The uuid of the text selection owner.
113
+ */
114
+ readonly uuid: string;
115
+
116
+ /**
117
+ * The style of this selection.
118
+ */
119
+ readonly style: IEditorSelectionStyle;
120
+ }
121
+
122
+ /**
123
+ * An interface for a text token, such as a word, keyword, or variable.
124
+ */
125
+ export interface IToken {
126
+ /**
127
+ * The value of the token.
128
+ */
129
+ value: string;
130
+
131
+ /**
132
+ * The offset of the token in the code editor.
133
+ */
134
+ offset: number;
135
+
136
+ /**
137
+ * An optional type for the token.
138
+ */
139
+ type?: string;
140
+ }
141
+
142
+ /**
143
+ * A selection owner.
144
+ */
145
+ export interface ISelectionOwner {
146
+ /**
147
+ * The uuid of this selection owner.
148
+ */
149
+ uuid: string;
150
+
151
+ /**
152
+ * Returns the primary position of the cursor, never `null`.
153
+ */
154
+ getCursorPosition: () => IPosition;
155
+
156
+ /**
157
+ * Set the primary position of the cursor.
158
+ *
159
+ * @param position - The new primary position.
160
+ *
161
+ * #### Notes
162
+ * This will remove any secondary cursors.
163
+ */
164
+ setCursorPosition: (position: IPosition) => void;
165
+
166
+ /**
167
+ * Returns the primary selection, never `null`.
168
+ */
169
+ getSelection: () => IRange;
170
+
171
+ /**
172
+ * Set the primary selection.
173
+ *
174
+ * @param selection - The desired selection range.
175
+ *
176
+ * #### Notes
177
+ * This will remove any secondary cursors.
178
+ */
179
+ setSelection: (selection: IRange) => void;
180
+
181
+ /**
182
+ * Gets the selections for all the cursors, never `null` or empty.
183
+ */
184
+ getSelections: () => IRange[];
185
+
186
+ /**
187
+ * Sets the selections for all the cursors.
188
+ *
189
+ * @param selections - The new selections.
190
+ *
191
+ * #### Notes
192
+ * Cursors will be removed or added, as necessary.
193
+ * Passing an empty array resets a cursor position to the start of a
194
+ * document.
195
+ */
196
+ setSelections: (selections: IRange[]) => void;
197
+ }
198
+
199
+ /**
200
+ * A keydown handler type.
201
+ *
202
+ * #### Notes
203
+ * Return `true` to prevent the default handling of the event by the
204
+ * editor.
205
+ */
206
+ export type KeydownHandler = (instance: IEditor, event: KeyboardEvent) => boolean;
207
+
208
+ /**
209
+ * The location of requested edges.
210
+ */
211
+ export type EdgeLocation = 'top' | 'topLine' | 'bottom';
212
+ /**
213
+ * A widget that provides a code editor.
214
+ */
215
+ export interface IEditor extends ISelectionOwner, Disposable {
216
+ /**
217
+ * A signal emitted when either the top or bottom edge is requested.
218
+ */
219
+ // readonly edgeRequested: ISignal<IEditor, EdgeLocation>;
220
+
221
+ // /**
222
+ // * The default selection style for the editor.
223
+ // */
224
+ // selectionStyle: IEditorSelectionStyle;
225
+
226
+ /**
227
+ * The DOM node that hosts the editor.
228
+ */
229
+ readonly host: HTMLElement;
230
+
231
+ /**
232
+ * The model used by the editor.
233
+ */
234
+ readonly model: IModel;
235
+
236
+ // /**
237
+ // * The height of a line in the editor in pixels.
238
+ // */
239
+ // readonly lineHeight: number;
240
+
241
+ // /**
242
+ // * The widget of a character in the editor in pixels.
243
+ // */
244
+ // readonly charWidth: number;
245
+
246
+ /**
247
+ * Get the number of lines in the editor.
248
+ */
249
+ readonly lineCount: number;
250
+
251
+ /**
252
+ * Get a config option for the editor.
253
+ */
254
+ getOption: <K extends keyof IEditorConfig>(option: K) => IEditorConfig[K];
255
+
256
+ /**
257
+ * Set a config option for the editor.
258
+ */
259
+ setOption: <K extends keyof IEditorConfig>(
260
+ option: K,
261
+ value: IEditorConfig[K],
262
+ ) => void;
263
+
264
+ // /**
265
+ // * Set config options for the editor.
266
+ // */
267
+ // setOptions: (options: Partial<IEditorConfig>) => void;
268
+
269
+ /**
270
+ * Returns the content for the given line number.
271
+ *
272
+ * @param line - The line of interest.
273
+ *
274
+ * @returns The value of the line.
275
+ *
276
+ * #### Notes
277
+ * Lines are 0-based, and accessing a line out of range returns
278
+ * `undefined`.
279
+ */
280
+ getLine: (line: number) => string | undefined;
281
+
282
+ /**
283
+ * Find an offset for the given position.
284
+ *
285
+ * @param position - The position of interest.
286
+ *
287
+ * @returns The offset at the position, clamped to the extent of the
288
+ * editor contents.
289
+ */
290
+ getOffsetAt: (position: IPosition) => number;
291
+
292
+ // /**
293
+ // * Find a position for the given offset.
294
+ // *
295
+ // * @param offset - The offset of interest.
296
+ // *
297
+ // * @returns The position at the offset, clamped to the extent of the
298
+ // * editor contents.
299
+ // */
300
+ // getPositionAt: (offset: number) => IPosition | undefined;
301
+
302
+ /**
303
+ * Undo one edit (if any undo events are stored).
304
+ */
305
+ undo: () => void;
306
+
307
+ /**
308
+ * Redo one undone edit.
309
+ */
310
+ redo: () => void;
311
+
312
+ // /**
313
+ // * Clear the undo history.
314
+ // */
315
+ // clearHistory: () => void;
316
+
317
+ /**
318
+ * Brings browser focus to this editor text.
319
+ */
320
+ focus: () => void;
321
+
322
+ /**
323
+ * Test whether the editor has keyboard focus.
324
+ */
325
+ hasFocus: () => boolean;
326
+
327
+ /**
328
+ * Explicitly blur the editor.
329
+ */
330
+ blur: () => void;
331
+
332
+ /**
333
+ * Resize the editor to fit its host node.
334
+ */
335
+ resizeToFit: () => void;
336
+
337
+ /**
338
+ * Get the cursor position given window coordinates.
339
+ *
340
+ * @param coordinate - The desired coordinate.
341
+ *
342
+ * @returns The position of the coordinates, or null if not
343
+ * contained in the editor.
344
+ */
345
+ getPositionForCoordinate: (coordinate: ICoordinate) => IPosition | null;
346
+
347
+ onModalChange: Event<boolean>;
348
+ }
349
+
350
+ /**
351
+ * A factory used to create a code editor.
352
+ */
353
+ export type CodeEditorFactory = (options: IEditorOptions) => IEditor;
354
+
355
+ /**
356
+ * The configuration options for an editor.
357
+ */
358
+ export interface IEditorConfig {
359
+ /**
360
+ * Half-period in milliseconds used for cursor blinking.
361
+ * By setting this to zero, blinking can be disabled.
362
+ * A negative value hides the cursor entirely.
363
+ */
364
+ cursorBlinkRate: number;
365
+
366
+ /**
367
+ * User preferred font family for text editors.
368
+ */
369
+ fontFamily: string | null;
370
+
371
+ /**
372
+ * User preferred size in pixel of the font used in text editors.
373
+ */
374
+ fontSize: number | null;
375
+
376
+ /**
377
+ * User preferred text line height, as a multiplier of font size.
378
+ */
379
+ lineHeight: number | null;
380
+
381
+ /**
382
+ * Whether line numbers should be displayed.
383
+ */
384
+ lineNumbers: boolean;
385
+
386
+ /**
387
+ * Control the line wrapping of the editor. Possible values are:
388
+ * - "off", lines will never wrap.
389
+ * - "on", lines will wrap at the viewport border.
390
+ * - "wordWrapColumn", lines will wrap at `wordWrapColumn`.
391
+ * - "bounded", lines will wrap at minimum between viewport width and wordWrapColumn.
392
+ */
393
+ lineWrap: 'off' | 'on' | 'wordWrapColumn' | 'bounded';
394
+
395
+ /**
396
+ * Whether the editor content is read-only.
397
+ */
398
+ readOnly: boolean;
399
+
400
+ /**
401
+ * whther the editor view is editable.
402
+ */
403
+ editable: boolean;
404
+
405
+ /**
406
+ * The number of spaces a tab is equal to.
407
+ */
408
+ tabSize: number;
409
+
410
+ /**
411
+ * Whether to insert spaces when pressing Tab.
412
+ */
413
+ insertSpaces: boolean;
414
+
415
+ /**
416
+ * Whether to highlight matching brackets when one of them is selected.
417
+ */
418
+ matchBrackets: boolean;
419
+
420
+ /**
421
+ * Whether to automatically close brackets after opening them.
422
+ */
423
+ autoClosingBrackets: boolean;
424
+
425
+ /**
426
+ * Whether the editor should handle paste events.
427
+ */
428
+ handlePaste?: boolean;
429
+
430
+ /**
431
+ * The column where to break text line.
432
+ */
433
+ wordWrapColumn: number;
434
+
435
+ /**
436
+ * Column index at which rulers should be added.
437
+ */
438
+ rulers: number[];
439
+
440
+ /**
441
+ * Whether to allow code folding
442
+ */
443
+ codeFolding: boolean;
444
+
445
+ /**
446
+ * Whether to highlight trailing whitespace
447
+ */
448
+ showTrailingSpace: boolean;
449
+
450
+ foldGutter?: boolean;
451
+
452
+ styleActiveLine?: boolean;
453
+
454
+ highlightActiveLineGutter?: boolean;
455
+
456
+ placeholder?: HTMLElement | string;
457
+ }
458
+
459
+ /**
460
+ * The default configuration options for an editor.
461
+ */
462
+ export const defaultConfig: IEditorConfig = {
463
+ // Order matters as gutters will be sorted by the configuration order
464
+ autoClosingBrackets: true,
465
+ cursorBlinkRate: 530,
466
+ fontFamily: null,
467
+ fontSize: null,
468
+ handlePaste: true,
469
+ insertSpaces: true,
470
+ lineHeight: null,
471
+ lineNumbers: true,
472
+ lineWrap: 'on',
473
+ matchBrackets: true,
474
+ readOnly: false,
475
+ editable: true,
476
+ tabSize: 4,
477
+ rulers: [],
478
+ showTrailingSpace: false,
479
+ wordWrapColumn: 80,
480
+ codeFolding: false,
481
+ foldGutter: true,
482
+ styleActiveLine: false,
483
+ highlightActiveLineGutter: false,
484
+ };
485
+
486
+ export type TooltipProviderOption = { cursorPosition: number };
487
+
488
+ export type TooltipProvider = (option: TooltipProviderOption) => Promise<string | null>;
489
+
490
+ export type CompletionProviderOption = { cursorPosition: number };
491
+ export type CompletionReply = {
492
+ matches: string[];
493
+ cursor_start: number;
494
+ cursor_end: number;
495
+ metadata: JSONObject;
496
+ };
497
+ export type CompletionProvider = (
498
+ option: CompletionProviderOption,
499
+ ) => Promise<CompletionReply>;
500
+
501
+ export type LSPProviderResult = {
502
+ virtualDocument: IVirtualDocument;
503
+ lspConnection: ILspConnection;
504
+ editor: ILSPEditor;
505
+ };
506
+
507
+ export type LSPProvider = () => Promise<LSPProviderResult>;
508
+
509
+ export interface Position {
510
+ /**
511
+ * Line number
512
+ */
513
+ line: number;
514
+
515
+ /**
516
+ * Position of character in line
517
+ */
518
+ ch: number;
519
+ }
520
+
521
+ /**
522
+ * is_* attributes are there only to enforce strict interface type checking
523
+ */
524
+ export interface ISourcePosition extends Position {
525
+ isSource: true;
526
+ }
527
+
528
+ export interface IEditorPosition extends Position {
529
+ isEditor: true;
530
+ }
531
+
532
+ export interface IVirtualPosition extends Position {
533
+ isVirtual: true;
534
+ }
535
+
536
+ export interface IRootPosition extends ISourcePosition {
537
+ isRoot: true;
538
+ }
539
+ export interface IVirtualDocument {
540
+ uri: string;
541
+
542
+ /**
543
+ * Get the corresponding editor of the virtual line.
544
+ */
545
+ getEditorAtVirtualLine: (pos: IVirtualPosition) => ILSPEditor;
546
+ transformEditorToVirtual: (
547
+ editor: ILSPEditor,
548
+ position: IEditorPosition,
549
+ ) => IVirtualPosition | null;
550
+ ttransformVirtualToEditor: (
551
+ virtualPosition: IVirtualPosition,
552
+ ) => IEditorPosition | null;
553
+ }
554
+
555
+ export interface IDocumentInfo {
556
+ /**
557
+ * URI of the virtual document.
558
+ */
559
+ uri: string;
560
+
561
+ /**
562
+ * Version of the virtual document.
563
+ */
564
+ version: number;
565
+
566
+ /**
567
+ * Text content of the document.
568
+ */
569
+ text: string;
570
+
571
+ /**
572
+ * Language id of the document.
573
+ */
574
+ languageId: string;
575
+ }
576
+ export interface ILspConnection {
577
+ /**
578
+ * Is the language server is connected?
579
+ */
580
+ isConnected: boolean;
581
+ /**
582
+ * Is the language server is initialized?
583
+ */
584
+ isInitialized: boolean;
585
+
586
+ /**
587
+ * Is the language server is connected and initialized?
588
+ */
589
+ isReady: boolean;
590
+
591
+ /**
592
+ * Initialize a connection over a web socket that speaks the LSP protocol
593
+ */
594
+ connect(socket: WebSocket): void;
595
+
596
+ /**
597
+ * Close the connection
598
+ */
599
+ close(): void;
600
+
601
+ // This should support every method from https://microsoft.github.io/language-server-protocol/specification
602
+ /**
603
+ * The initialize request tells the server which options the client supports
604
+ */
605
+ sendInitialize(): void;
606
+ /**
607
+ * Inform the server that the document was opened
608
+ */
609
+ sendOpen(documentInfo: IDocumentInfo): void;
610
+
611
+ /**
612
+ * Sends the full text of the document to the server
613
+ */
614
+ sendChange(documentInfo: IDocumentInfo): void;
615
+
616
+ /**
617
+ * Send save notification to the server.
618
+ */
619
+ sendSaved(documentInfo: IDocumentInfo): void;
620
+
621
+ /**
622
+ * Send configuration change to the server.
623
+ */
624
+ sendConfigurationChange(settings: DidChangeConfigurationParams): void;
625
+
626
+ provides(provider: keyof ServerCapabilities): boolean;
627
+ }
628
+
629
+ /**
630
+ * The options used to initialize an editor.
631
+ */
632
+ export interface IEditorOptions {
633
+ /**
634
+ * The host widget used by the editor.
635
+ */
636
+ host: HTMLElement;
637
+
638
+ /**
639
+ * The model used by the editor.
640
+ */
641
+ model: IModel;
642
+
643
+ /**
644
+ * The desired uuid for the editor.
645
+ */
646
+ uuid?: string | undefined;
647
+
648
+ /**
649
+ * The default selection style for the editor.
650
+ */
651
+ selectionStyle?: Partial<IEditorSelectionStyle> | undefined;
652
+
653
+ /**
654
+ * The configuration options for the editor.
655
+ */
656
+ config?: Partial<IEditorConfig> | undefined;
657
+
658
+ // /**
659
+ // * The configuration options for the editor.
660
+ // */
661
+ // translator?: ITranslator;
662
+
663
+ //
664
+ tooltipProvider?: TooltipProvider | undefined;
665
+ completionProvider?: CompletionProvider | undefined;
666
+ lspProvider?: LSPProvider | undefined;
667
+ }