@difizen/libro-codemirror 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.
Files changed (126) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/es/auto-complete/closebrackets.d.ts +12 -0
  4. package/es/auto-complete/closebrackets.d.ts.map +1 -0
  5. package/es/auto-complete/closebrackets.js +408 -0
  6. package/es/auto-complete/completion.d.ts +57 -0
  7. package/es/auto-complete/completion.d.ts.map +1 -0
  8. package/es/auto-complete/completion.js +265 -0
  9. package/es/auto-complete/config.d.ts +22 -0
  10. package/es/auto-complete/config.d.ts.map +1 -0
  11. package/es/auto-complete/config.js +44 -0
  12. package/es/auto-complete/filter.d.ts +13 -0
  13. package/es/auto-complete/filter.d.ts.map +1 -0
  14. package/es/auto-complete/filter.js +191 -0
  15. package/es/auto-complete/index.d.ts +17 -0
  16. package/es/auto-complete/index.d.ts.map +1 -0
  17. package/es/auto-complete/index.js +107 -0
  18. package/es/auto-complete/snippet.d.ts +14 -0
  19. package/es/auto-complete/snippet.d.ts.map +1 -0
  20. package/es/auto-complete/snippet.js +447 -0
  21. package/es/auto-complete/state.d.ts +63 -0
  22. package/es/auto-complete/state.d.ts.map +1 -0
  23. package/es/auto-complete/state.js +452 -0
  24. package/es/auto-complete/theme.d.ts +6 -0
  25. package/es/auto-complete/theme.d.ts.map +1 -0
  26. package/es/auto-complete/theme.js +151 -0
  27. package/es/auto-complete/tooltip.d.ts +5 -0
  28. package/es/auto-complete/tooltip.d.ts.map +1 -0
  29. package/es/auto-complete/tooltip.js +365 -0
  30. package/es/auto-complete/view.d.ts +43 -0
  31. package/es/auto-complete/view.d.ts.map +1 -0
  32. package/es/auto-complete/view.js +372 -0
  33. package/es/auto-complete/word.d.ts +3 -0
  34. package/es/auto-complete/word.d.ts.map +1 -0
  35. package/es/auto-complete/word.js +119 -0
  36. package/es/completion.d.ts +6 -0
  37. package/es/completion.d.ts.map +1 -0
  38. package/es/completion.js +84 -0
  39. package/es/config.d.ts +184 -0
  40. package/es/config.d.ts.map +1 -0
  41. package/es/config.js +473 -0
  42. package/es/editor.d.ts +361 -0
  43. package/es/editor.d.ts.map +1 -0
  44. package/es/editor.js +1126 -0
  45. package/es/factory.d.ts +3 -0
  46. package/es/factory.d.ts.map +1 -0
  47. package/es/factory.js +12 -0
  48. package/es/hyperlink.d.ts +15 -0
  49. package/es/hyperlink.d.ts.map +1 -0
  50. package/es/hyperlink.js +120 -0
  51. package/es/indent.d.ts +8 -0
  52. package/es/indent.d.ts.map +1 -0
  53. package/es/indent.js +58 -0
  54. package/es/indentation-markers/config.d.ts +17 -0
  55. package/es/indentation-markers/config.d.ts.map +1 -0
  56. package/es/indentation-markers/config.js +10 -0
  57. package/es/indentation-markers/index.d.ts +3 -0
  58. package/es/indentation-markers/index.d.ts.map +1 -0
  59. package/es/indentation-markers/index.js +160 -0
  60. package/es/indentation-markers/map.d.ts +77 -0
  61. package/es/indentation-markers/map.d.ts.map +1 -0
  62. package/es/indentation-markers/map.js +265 -0
  63. package/es/indentation-markers/utils.d.ts +27 -0
  64. package/es/indentation-markers/utils.d.ts.map +1 -0
  65. package/es/indentation-markers/utils.js +91 -0
  66. package/es/index.d.ts +11 -0
  67. package/es/index.d.ts.map +1 -0
  68. package/es/index.js +10 -0
  69. package/es/libro-icon.d.ts +3 -0
  70. package/es/libro-icon.d.ts.map +1 -0
  71. package/es/libro-icon.js +2 -0
  72. package/es/mimetype.d.ts +22 -0
  73. package/es/mimetype.d.ts.map +1 -0
  74. package/es/mimetype.js +59 -0
  75. package/es/mode.d.ts +86 -0
  76. package/es/mode.d.ts.map +1 -0
  77. package/es/mode.js +284 -0
  78. package/es/monitor.d.ts +32 -0
  79. package/es/monitor.d.ts.map +1 -0
  80. package/es/monitor.js +129 -0
  81. package/es/python-lang.d.ts +3 -0
  82. package/es/python-lang.d.ts.map +1 -0
  83. package/es/python-lang.js +7 -0
  84. package/es/style/base.css +131 -0
  85. package/es/style/theme.css +12 -0
  86. package/es/style/variables.css +403 -0
  87. package/es/theme.d.ts +35 -0
  88. package/es/theme.d.ts.map +1 -0
  89. package/es/theme.js +225 -0
  90. package/es/tooltip.d.ts +10 -0
  91. package/es/tooltip.d.ts.map +1 -0
  92. package/es/tooltip.js +170 -0
  93. package/package.json +74 -0
  94. package/src/auto-complete/README.md +71 -0
  95. package/src/auto-complete/closebrackets.ts +423 -0
  96. package/src/auto-complete/completion.ts +345 -0
  97. package/src/auto-complete/config.ts +101 -0
  98. package/src/auto-complete/filter.ts +215 -0
  99. package/src/auto-complete/index.ts +112 -0
  100. package/src/auto-complete/snippet.ts +394 -0
  101. package/src/auto-complete/state.ts +472 -0
  102. package/src/auto-complete/theme.ts +126 -0
  103. package/src/auto-complete/tooltip.ts +386 -0
  104. package/src/auto-complete/view.ts +343 -0
  105. package/src/auto-complete/word.ts +118 -0
  106. package/src/completion.ts +61 -0
  107. package/src/config.ts +689 -0
  108. package/src/editor.ts +1078 -0
  109. package/src/factory.ts +10 -0
  110. package/src/hyperlink.ts +95 -0
  111. package/src/indent.ts +69 -0
  112. package/src/indentation-markers/config.ts +31 -0
  113. package/src/indentation-markers/index.ts +192 -0
  114. package/src/indentation-markers/map.ts +273 -0
  115. package/src/indentation-markers/utils.ts +84 -0
  116. package/src/index.ts +11 -0
  117. package/src/libro-icon.ts +4 -0
  118. package/src/mimetype.ts +49 -0
  119. package/src/mode.ts +269 -0
  120. package/src/monitor.ts +105 -0
  121. package/src/python-lang.ts +7 -0
  122. package/src/style/base.css +129 -0
  123. package/src/style/theme.css +12 -0
  124. package/src/style/variables.css +405 -0
  125. package/src/theme.ts +231 -0
  126. package/src/tooltip.ts +145 -0
package/src/config.ts ADDED
@@ -0,0 +1,689 @@
1
+ import { defaultKeymap, historyKeymap, history } from '@codemirror/commands';
2
+ import { pythonLanguage } from '@codemirror/lang-python';
3
+ import {
4
+ bracketMatching,
5
+ defaultHighlightStyle,
6
+ foldGutter,
7
+ foldKeymap,
8
+ indentOnInput,
9
+ indentUnit,
10
+ syntaxHighlighting,
11
+ } from '@codemirror/language';
12
+ import type { LanguageSupport } from '@codemirror/language';
13
+ import { lintKeymap } from '@codemirror/lint';
14
+ import { highlightSelectionMatches } from '@codemirror/search';
15
+ import type { Extension, Facet } from '@codemirror/state';
16
+ import { Compartment, EditorState, StateEffect } from '@codemirror/state';
17
+ import type { KeyBinding } from '@codemirror/view';
18
+ import {
19
+ crosshairCursor,
20
+ rectangularSelection,
21
+ dropCursor,
22
+ drawSelection,
23
+ highlightSpecialChars,
24
+ highlightActiveLineGutter,
25
+ EditorView,
26
+ highlightActiveLine,
27
+ keymap,
28
+ lineNumbers,
29
+ placeholder,
30
+ } from '@codemirror/view';
31
+ import type { IEditorConfig } from '@difizen/libro-code-editor';
32
+
33
+ import {
34
+ closeBrackets,
35
+ closeBracketsKeymap,
36
+ autocompletion,
37
+ completionKeymap,
38
+ } from './auto-complete/index.js';
39
+ import { kernelCompletions } from './completion.js';
40
+ import type { IOptions } from './editor.js';
41
+ import { hyperLink } from './hyperlink.js';
42
+ import { indentationMarkers } from './indentation-markers/index.js';
43
+ import { FoldIcon, UnFoldIcon } from './libro-icon.js';
44
+ import { ensure } from './mode.js';
45
+ import { getTheme, defaultTheme } from './theme.js';
46
+ import { tabTooltip, tooltipKeymap } from './tooltip.js';
47
+
48
+ /**
49
+ * The configuration options for a codemirror editor.
50
+ */
51
+ export interface CodeMirrorConfig extends IEditorConfig {
52
+ /**
53
+ * The mode to use.
54
+ */
55
+ mode?: string;
56
+
57
+ /**
58
+ * content mimetype
59
+ */
60
+ mimetype?: string;
61
+
62
+ /**
63
+ * The theme to style the editor with. see editortheme.ts for an example
64
+ * of how to design a theme for CodeMirror 6.
65
+ */
66
+ theme?: string;
67
+
68
+ // FIXME-TRANS: Handle theme localizable names
69
+ // themeDisplayName?: string
70
+
71
+ /**
72
+ * Whether to use the context-sensitive indentation that the mode provides
73
+ * (or just indent the same as the line before).
74
+ */
75
+ smartIndent?: boolean;
76
+
77
+ /**
78
+ * Configures whether the editor should re-indent the current line when a
79
+ * character is typed that might change its proper indentation
80
+ * (only works if the mode supports indentation).
81
+ */
82
+ electricChars?: boolean;
83
+
84
+ /**
85
+ * Configures the keymap to use. The default is "default", which is the
86
+ * only keymap defined in codemirror.js itself.
87
+ * Extra keymaps are found in the CodeMirror keymap directory.
88
+ */
89
+ keyMap?: string;
90
+
91
+ /**
92
+ * Can be used to specify extra keybindings for the editor, alongside the
93
+ * ones defined by keyMap. Should be either null, or a valid keymap value.
94
+ */
95
+ extraKeys?: KeyBinding[] | null;
96
+
97
+ /**
98
+ * Can be used to add extra gutters (beyond or instead of the line number
99
+ * gutter).
100
+ * Should be an array of CSS class names, each of which defines a width
101
+ * (and optionally a background),
102
+ * and which will be used to draw the background of the gutters.
103
+ * May include the CodeMirror-linenumbers class, in order to explicitly
104
+ * set the position of the line number gutter
105
+ * (it will default to be to the right of all other gutters).
106
+ * These class names are the keys passed to setGutterMarker.
107
+ */
108
+ gutters?: string[];
109
+
110
+ /**
111
+ * Determines whether the gutter scrolls along with the content
112
+ * horizontally (false)
113
+ * or whether it stays fixed during horizontal scrolling (true,
114
+ * the default).
115
+ */
116
+ fixedGutter?: boolean;
117
+
118
+ /**
119
+ * Whether the cursor should be drawn when a selection is active.
120
+ */
121
+ showCursorWhenSelecting?: boolean;
122
+
123
+ /**
124
+ * When fixedGutter is on, and there is a horizontal scrollbar, by default
125
+ * the gutter will be visible to the left of this scrollbar. If this
126
+ * option is set to true, it will be covered by an element with class
127
+ * CodeMirror-gutter-filler.
128
+ */
129
+ coverGutterNextToScrollbar?: boolean;
130
+
131
+ /**
132
+ * Controls whether drag-and-drop is enabled.
133
+ */
134
+ dragDrop?: boolean;
135
+
136
+ /**
137
+ * Explicitly set the line separator for the editor.
138
+ * By default (value null), the document will be split on CRLFs as well as
139
+ * lone CRs and LFs, and a single LF will be used as line separator in all
140
+ * output (such as getValue). When a specific string is given, lines will
141
+ * only be split on that string, and output will, by default, use that
142
+ * same separator.
143
+ */
144
+ lineSeparator?: string | null;
145
+
146
+ /**
147
+ * Chooses a scrollbar implementation. The default is "native", showing
148
+ * native scrollbars. The core library also provides the "null" style,
149
+ * which completely hides the scrollbars. Addons can implement additional
150
+ * scrollbar models.
151
+ */
152
+ scrollbarStyle?: string;
153
+
154
+ /**
155
+ * When enabled, which is the default, doing copy or cut when there is no
156
+ * selection will copy or cut the whole lines that have cursors on them.
157
+ */
158
+ lineWiseCopyCut?: boolean;
159
+
160
+ /**
161
+ * Whether to scroll past the end of the buffer.
162
+ */
163
+ scrollPastEnd?: boolean;
164
+
165
+ /**
166
+ * Whether to give the wrapper of the line that contains the cursor the class
167
+ * cm-activeLine.
168
+ */
169
+ styleActiveLine?: boolean;
170
+
171
+ /**
172
+ * Whether to causes the selected text to be marked with the CSS class
173
+ * CodeMirror-selectedtext. Useful to change the colour of the selection
174
+ * (in addition to the background).
175
+ */
176
+ styleSelectedText?: boolean;
177
+
178
+ /**
179
+ * Defines the mouse cursor appearance when hovering over the selection.
180
+ * It can be set to a string, like "pointer", or to true,
181
+ * in which case the "default" (arrow) cursor will be used.
182
+ */
183
+ selectionPointer?: boolean | string;
184
+
185
+ //
186
+ highlightActiveLineGutter?: boolean;
187
+ highlightSpecialChars?: boolean;
188
+ history?: boolean;
189
+ drawSelection?: boolean;
190
+ dropCursor?: boolean;
191
+ allowMultipleSelections?: boolean;
192
+ autocompletion?: boolean;
193
+ rectangularSelection?: boolean;
194
+ crosshairCursor?: boolean;
195
+ highlightSelectionMatches?: boolean;
196
+ foldGutter?: boolean;
197
+ syntaxHighlighting?: boolean;
198
+ /**
199
+ * 是否从kernel获取completion
200
+ */
201
+ jupyterKernelCompletion?: boolean;
202
+ /**
203
+ * 是否从kernel获取tooltip
204
+ */
205
+ jupyterKernelTooltip?: boolean;
206
+ indentationMarkers?: boolean;
207
+ hyperLink?: boolean;
208
+ /**
209
+ * 是否开启tab触发completion和tooltip
210
+ */
211
+ tabEditorFunction?: boolean;
212
+
213
+ lspCompletion?: boolean;
214
+
215
+ lspTooltip?: boolean;
216
+
217
+ lspLint?: boolean;
218
+
219
+ placeholder?: HTMLElement | string;
220
+ }
221
+ /**
222
+ * Extension builder (we use the same trick as in CM Extension
223
+ * to emulate empty interface)
224
+ */
225
+ interface IExtensionBuilder {
226
+ extensionBuilder: IExtensionBuilder;
227
+ }
228
+
229
+ /**
230
+ * Builder for extensions from value of type T
231
+ */
232
+ abstract class ExtensionBuilder<T> implements IExtensionBuilder {
233
+ abstract of(value: T): Extension;
234
+
235
+ get extensionBuilder(): IExtensionBuilder {
236
+ return this;
237
+ }
238
+ }
239
+ /**
240
+ * Extension builder that simply forwards a value as an extension.
241
+ */
242
+ class ExtensionForwarder<T extends Extension> extends ExtensionBuilder<T> {
243
+ of(value: T): Extension {
244
+ return value;
245
+ }
246
+ }
247
+ /**
248
+ * Extension builder that builds an extension from a facet.
249
+ */
250
+ class FacetWrapper<T, U> extends ExtensionBuilder<T> {
251
+ constructor(facet: Facet<T, U>) {
252
+ super();
253
+ this._facet = facet;
254
+ }
255
+
256
+ of(value: T): Extension {
257
+ return this._facet.of(value);
258
+ }
259
+
260
+ private _facet: Facet<T, U>;
261
+ }
262
+ /**
263
+ * Extension builder that provides an extension depending
264
+ * on a boolean value.
265
+ */
266
+ class ConditionalExtension extends ExtensionBuilder<boolean> {
267
+ constructor(truthy: Extension, falsy: Extension = []) {
268
+ super();
269
+ this._truthy = truthy;
270
+ this._falsy = falsy;
271
+ }
272
+
273
+ of(value: boolean): Extension {
274
+ return value ? this._truthy : this._falsy;
275
+ }
276
+
277
+ private _truthy: Extension;
278
+ private _falsy: Extension;
279
+ }
280
+
281
+ /**
282
+ * Extension builds that provides an extension depending on
283
+ * conditional function operating on a value.
284
+ */
285
+ class GenConditionalExtension<T> extends ExtensionBuilder<T> {
286
+ constructor(fn: (a: T) => boolean, truthy: Extension, falsy: Extension = []) {
287
+ super();
288
+ this._fn = fn;
289
+ this._builder = new ConditionalExtension(truthy, falsy);
290
+ }
291
+
292
+ of(value: T): Extension {
293
+ return this._builder.of(this._fn(value));
294
+ }
295
+
296
+ private _fn: (a: T) => boolean;
297
+ private _builder: ConditionalExtension;
298
+ }
299
+
300
+ /**
301
+ * Builds an extension in a compartment that can
302
+ * be reconfigured.
303
+ */
304
+ interface IConfigurableBuilder {
305
+ of: <T>(value: T) => Extension;
306
+ reconfigure: <T>(value: T) => StateEffect<unknown>;
307
+ }
308
+
309
+ /**
310
+ * IConfigurableBuilder implementation *
311
+ */
312
+ class ConfigurableBuilder implements IConfigurableBuilder {
313
+ constructor(builder: IExtensionBuilder) {
314
+ this._compartment = new Compartment();
315
+ this._builder = builder;
316
+ }
317
+
318
+ of<T>(value: T): Extension {
319
+ return this._compartment.of((this._builder as ExtensionBuilder<T>).of(value));
320
+ }
321
+
322
+ reconfigure<T>(value: T): StateEffect<unknown> {
323
+ return this._compartment.reconfigure(
324
+ (this._builder as ExtensionBuilder<T>).of(value),
325
+ );
326
+ }
327
+
328
+ private _compartment: Compartment;
329
+ private _builder: IExtensionBuilder;
330
+ }
331
+
332
+ /*
333
+ * Specific builder for themes. Provide a default theme and
334
+ * allows to register new themes.
335
+ */
336
+ class ThemeBuilder implements IConfigurableBuilder {
337
+ constructor() {
338
+ this._compartment = new Compartment();
339
+ }
340
+
341
+ of<T>(value: T): Extension {
342
+ const v = value as unknown as string;
343
+ return this._compartment.of(getTheme(v));
344
+ }
345
+
346
+ reconfigure<T>(value: T): StateEffect<unknown> {
347
+ const v = value as unknown as string;
348
+ return this._compartment.reconfigure(getTheme(v));
349
+ }
350
+
351
+ private _compartment: Compartment;
352
+ }
353
+
354
+ /*
355
+ * Specific builder for themes. Provide a default theme and
356
+ * allows to register new themes.
357
+ */
358
+ class PlaceHolderBuilder implements IConfigurableBuilder {
359
+ constructor() {
360
+ this._compartment = new Compartment();
361
+ }
362
+
363
+ of<T>(value: T): Extension {
364
+ const v = value as unknown as HTMLElement | string;
365
+ return this._compartment.of(placeholder(v));
366
+ }
367
+
368
+ reconfigure<T>(value: T): StateEffect<unknown> {
369
+ const v = value as unknown as HTMLElement | string;
370
+ return this._compartment.reconfigure(placeholder(v));
371
+ }
372
+
373
+ private _compartment: Compartment;
374
+ }
375
+
376
+ /**
377
+ * Creates a ConfigurableBuilder based on an ExtensionForwarder.
378
+ */
379
+ function createForwarderBuilder<T extends Extension>(): IConfigurableBuilder {
380
+ return new ConfigurableBuilder(new ExtensionForwarder<T>());
381
+ }
382
+
383
+ /**
384
+ * Creates a ConfigurableBuilder based on a Facet.
385
+ */
386
+ function createConfigurableBuilder<T, U>(facet: Facet<T, U>): IConfigurableBuilder {
387
+ return new ConfigurableBuilder(new FacetWrapper<T, U>(facet));
388
+ }
389
+
390
+ /**
391
+ * Creates a ConditionalBuilder from two extensions.
392
+ */
393
+ function createConditionalBuilder(
394
+ truthy: Extension,
395
+ falsy: Extension = [],
396
+ ): IConfigurableBuilder {
397
+ return new ConfigurableBuilder(new ConditionalExtension(truthy, falsy));
398
+ }
399
+
400
+ /**
401
+ * Creates a ConditionalBuilder based on two extensions and a
402
+ * conditional function.
403
+ */
404
+ function createGenConditionalBuilder<T>(
405
+ fn: (value: T) => boolean,
406
+ truthy: Extension,
407
+ falsy: Extension = [],
408
+ ): IConfigurableBuilder {
409
+ return new ConfigurableBuilder(new GenConditionalExtension<T>(fn, truthy, falsy));
410
+ }
411
+
412
+ /**
413
+ * Creates a theme builder.
414
+ */
415
+ function createThemeBuilder() {
416
+ return new ThemeBuilder();
417
+ }
418
+
419
+ /**
420
+ * Creates a theme builder.
421
+ */
422
+ function createPlaceHolderBuilder() {
423
+ return new PlaceHolderBuilder();
424
+ }
425
+
426
+ /**
427
+ * Editor configuration: provides APIs to get and reconfigure CodeMirror 6
428
+ * extensions from option names. Also allows to register new themes and
429
+ * inject ne extensions in already configured editor instances.
430
+ */
431
+ export class EditorConfiguration {
432
+ constructor(options: IOptions) {
433
+ // Order does not matter
434
+ this._configurableBuilderMap = new Map<string, IConfigurableBuilder>([
435
+ ['tabSize', createConfigurableBuilder(EditorState.tabSize)],
436
+ ['readOnly', createConfigurableBuilder(EditorState.readOnly)],
437
+ ['editable', createConfigurableBuilder(EditorView.editable)],
438
+ ['keymap', createConfigurableBuilder(keymap)],
439
+ ['indentUnit', createConfigurableBuilder(indentUnit)],
440
+ ['smartIndent', createConditionalBuilder(indentOnInput())],
441
+ ['autoClosingBrackets', createConditionalBuilder(closeBrackets())],
442
+ ['matchBrackets', createConditionalBuilder(bracketMatching())],
443
+ ['styleActiveLine', createConditionalBuilder(highlightActiveLine())],
444
+ ['lineNumbers', createConditionalBuilder(lineNumbers())],
445
+ [
446
+ 'lineWrap',
447
+ createGenConditionalBuilder(
448
+ (a: string) => a !== 'off',
449
+ EditorView.lineWrapping,
450
+ ),
451
+ ],
452
+ ['language', createForwarderBuilder<LanguageSupport>()],
453
+ ['theme', createThemeBuilder()],
454
+
455
+ // addtional
456
+ [
457
+ 'highlightActiveLineGutter',
458
+ createConditionalBuilder(highlightActiveLineGutter()),
459
+ ],
460
+ ['highlightSpecialChars', createConditionalBuilder(highlightSpecialChars())],
461
+ ['history', createConditionalBuilder(history())],
462
+ ['drawSelection', createConditionalBuilder(drawSelection())],
463
+ ['dropCursor', createConditionalBuilder(dropCursor())],
464
+ [
465
+ 'allowMultipleSelections',
466
+ createConfigurableBuilder(EditorState.allowMultipleSelections),
467
+ ],
468
+ [
469
+ 'autocompletion',
470
+ createConditionalBuilder(
471
+ autocompletion({ activateOnTyping: options.config?.lspCompletion === true }),
472
+ ),
473
+ ],
474
+ ['rectangularSelection', createConditionalBuilder(rectangularSelection())],
475
+ ['crosshairCursor', createConditionalBuilder(crosshairCursor())],
476
+ [
477
+ 'highlightSelectionMatches',
478
+ createConditionalBuilder(highlightSelectionMatches()),
479
+ ],
480
+ [
481
+ 'foldGutter',
482
+ createConditionalBuilder(
483
+ foldGutter({
484
+ markerDOM: (open) => {
485
+ if (open) {
486
+ const iconElement = document.createElement('div');
487
+ iconElement.innerHTML = FoldIcon;
488
+ iconElement.style.display = 'flex';
489
+ iconElement.style.cursor = 'pointer';
490
+ return iconElement;
491
+ } else {
492
+ const iconElement = document.createElement('div');
493
+ iconElement.innerHTML = UnFoldIcon;
494
+ iconElement.style.cursor = 'pointer';
495
+ return iconElement;
496
+ }
497
+ },
498
+ }),
499
+ ),
500
+ ],
501
+ [
502
+ 'syntaxHighlighting',
503
+ createConditionalBuilder(
504
+ syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
505
+ ),
506
+ ],
507
+ [
508
+ 'jupyterKernelCompletion',
509
+ createConditionalBuilder(
510
+ pythonLanguage.data.of({
511
+ autocomplete: kernelCompletions(options['completionProvider']),
512
+ }),
513
+ ),
514
+ ],
515
+ [
516
+ 'jupyterKernelTooltip',
517
+ createConditionalBuilder(tabTooltip(options['tooltipProvider'])),
518
+ ],
519
+ ['indentationMarkers', createConditionalBuilder(indentationMarkers())],
520
+ ['hyperLink', createConditionalBuilder(hyperLink)],
521
+ ['placeholder', createPlaceHolderBuilder()],
522
+ ]);
523
+ this._themeOverloaderSpec = { '&': {}, '.cm-line': {} };
524
+ this._themeOverloader = new Compartment();
525
+ }
526
+
527
+ /**
528
+ * Reconfigures the extension mapped with key with the provided value.
529
+ */
530
+ reconfigureExtension<T>(view: EditorView, key: string, value: T): void {
531
+ const builder = this.get(key);
532
+ if (builder) {
533
+ view.dispatch({
534
+ effects: builder.reconfigure(value),
535
+ });
536
+ } else {
537
+ const config: Record<string, any> = {};
538
+ config[key] = value;
539
+ const themeOverload = this.updateThemeOverload(config);
540
+ view.dispatch({
541
+ effects: this._themeOverloader.reconfigure(themeOverload),
542
+ });
543
+ }
544
+ }
545
+
546
+ /**
547
+ * Reconfigures all the extensions mapped with the options from the
548
+ * provided partial configuration.
549
+ */
550
+ reconfigureExtensions(view: EditorView, config: Partial<CodeMirrorConfig>): void {
551
+ const eff = [];
552
+ for (const [key, value] of Object.entries(config)) {
553
+ const builder = this.get(key);
554
+ if (builder) {
555
+ eff.push(builder.reconfigure(value));
556
+ }
557
+ }
558
+
559
+ const themeOverload = this.updateThemeOverload(config);
560
+ eff.push(this._themeOverloader.reconfigure(themeOverload));
561
+
562
+ view.dispatch({
563
+ effects: eff,
564
+ });
565
+ }
566
+
567
+ /**
568
+ * Appends extensions to the top-level configuration of the
569
+ * editor.
570
+ */
571
+ injectExtension(view: EditorView, ext: Extension): void {
572
+ view.dispatch({
573
+ effects: StateEffect.appendConfig.of(ext),
574
+ });
575
+ }
576
+
577
+ /**
578
+ * Returns the list of initial extensions of an editor
579
+ * based on the provided configuration.
580
+ */
581
+ getInitialExtensions(config: CodeMirrorConfig): Extension[] {
582
+ const keys = Object.keys(config).filter(
583
+ (v) => v !== 'insertSpaces' && v !== 'extraKeys',
584
+ );
585
+ const extensions = [];
586
+ for (const k of keys) {
587
+ const builder = this.get(k);
588
+ if (builder) {
589
+ const value = config[k as keyof CodeMirrorConfig];
590
+ extensions.push(builder.of(value));
591
+ }
592
+ }
593
+ const libroDefaultKeymap = defaultKeymap.filter((item) => item.key !== 'Mod-Enter');
594
+
595
+ if (!config.theme) {
596
+ extensions.push(defaultTheme());
597
+ }
598
+
599
+ const builder = this.get('keymap');
600
+
601
+ const initialKeymap = [
602
+ ...libroDefaultKeymap,
603
+ ...closeBracketsKeymap,
604
+ // ...searchKeymap,
605
+ ...historyKeymap,
606
+ ...foldKeymap,
607
+ ...completionKeymap,
608
+ ...lintKeymap,
609
+ ...tooltipKeymap,
610
+ ];
611
+
612
+ const keymapExt = builder!.of(
613
+ config.extraKeys ? [initialKeymap, ...config.extraKeys] : [...initialKeymap],
614
+ );
615
+ const indentBuilder = this.get('indentUnit');
616
+ const insertExt = indentBuilder!.of(
617
+ config.insertSpaces ? ' '.repeat(config.tabSize) : '\t',
618
+ );
619
+
620
+ const themeOverload = this.updateThemeOverload(config);
621
+ extensions.push(this._themeOverloader.of(themeOverload), insertExt, keymapExt);
622
+
623
+ ensure(config?.mimetype ?? 'text/x-python')
624
+ .then((spec) => {
625
+ if (spec) {
626
+ extensions.push(this.get('language')?.of(spec.support!));
627
+ }
628
+ return undefined;
629
+ })
630
+ .catch((error) => {
631
+ console.error('Could not load language parser:');
632
+ console.error(error);
633
+ });
634
+
635
+ return extensions;
636
+ }
637
+
638
+ private updateThemeOverload(
639
+ config: Partial<CodeMirrorConfig> | Record<string, any>,
640
+ ): Extension {
641
+ const { fontFamily, fontSize, lineHeight, lineWrap, wordWrapColumn } = config;
642
+
643
+ let needUpdate = false;
644
+
645
+ const parentStyle: Record<string, any> = {};
646
+ if (fontSize) {
647
+ parentStyle['fontSize'] = fontSize + 'px';
648
+ needUpdate = true;
649
+ }
650
+ if (fontFamily) {
651
+ parentStyle['fontFamily'] = fontFamily;
652
+ needUpdate = true;
653
+ }
654
+ if (lineHeight) {
655
+ parentStyle['lineHeight'] = lineHeight.toString();
656
+ needUpdate = true;
657
+ }
658
+
659
+ const lineStyle: Record<string, any> = {};
660
+ if (lineWrap === 'wordWrapColumn') {
661
+ lineStyle['width'] = wordWrapColumn + 'ch';
662
+ needUpdate = true;
663
+ } else if (lineWrap === 'bounded') {
664
+ lineStyle['maxWidth'] = wordWrapColumn + 'ch';
665
+ needUpdate = true;
666
+ }
667
+
668
+ if (needUpdate) {
669
+ const newThemeSpec = {
670
+ '&': parentStyle,
671
+ '.cm-line': lineStyle,
672
+ };
673
+ this._themeOverloaderSpec = {
674
+ ...this._themeOverloaderSpec,
675
+ ...newThemeSpec,
676
+ };
677
+ }
678
+
679
+ return EditorView.theme(this._themeOverloaderSpec);
680
+ }
681
+
682
+ private get(key: string): IConfigurableBuilder | undefined {
683
+ return this._configurableBuilderMap.get(key);
684
+ }
685
+
686
+ private _configurableBuilderMap: Map<string, IConfigurableBuilder>;
687
+ private _themeOverloaderSpec: Record<string, Record<string, string>>;
688
+ private _themeOverloader: Compartment;
689
+ }