@adecore/editor 0.0.1 → 0.17.0-beta.2

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 (107) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +57 -0
  3. package/dist/attribution.d.ts +28 -0
  4. package/dist/attribution.js +55 -0
  5. package/dist/code-block.d.ts +2 -0
  6. package/dist/code-block.js +89 -0
  7. package/dist/column-selection.d.ts +9 -0
  8. package/dist/column-selection.js +52 -0
  9. package/dist/controller.d.ts +64 -0
  10. package/dist/controller.js +772 -0
  11. package/dist/editor.css +813 -0
  12. package/dist/engine.d.ts +2 -0
  13. package/dist/engine.js +610 -0
  14. package/dist/fake.d.ts +192 -0
  15. package/dist/fake.js +663 -0
  16. package/dist/find.d.ts +30 -0
  17. package/dist/find.js +108 -0
  18. package/dist/index.d.ts +5 -0
  19. package/dist/index.js +4 -0
  20. package/dist/input.d.ts +9 -0
  21. package/dist/input.js +57 -0
  22. package/dist/keymap-table.d.ts +102 -0
  23. package/dist/keymap-table.js +130 -0
  24. package/dist/keymap.d.ts +36 -0
  25. package/dist/keymap.js +163 -0
  26. package/dist/layout.d.ts +167 -0
  27. package/dist/layout.js +583 -0
  28. package/dist/lens.d.ts +3 -0
  29. package/dist/lens.js +29 -0
  30. package/dist/listeners.d.ts +3 -0
  31. package/dist/listeners.js +12 -0
  32. package/dist/metrics.d.ts +8 -0
  33. package/dist/metrics.js +54 -0
  34. package/dist/modifier-gesture.d.ts +24 -0
  35. package/dist/modifier-gesture.js +98 -0
  36. package/dist/native-input.d.ts +26 -0
  37. package/dist/native-input.js +147 -0
  38. package/dist/offsets.d.ts +2 -0
  39. package/dist/offsets.js +14 -0
  40. package/dist/outline.d.ts +55 -0
  41. package/dist/outline.js +181 -0
  42. package/dist/overview.d.ts +17 -0
  43. package/dist/overview.js +55 -0
  44. package/dist/paint.d.ts +86 -0
  45. package/dist/paint.js +481 -0
  46. package/dist/php-html.d.ts +9 -0
  47. package/dist/php-html.js +36 -0
  48. package/dist/pointer.d.ts +49 -0
  49. package/dist/pointer.js +243 -0
  50. package/dist/scroll-animation.d.ts +3 -0
  51. package/dist/scroll-animation.js +45 -0
  52. package/dist/scroll.d.ts +28 -0
  53. package/dist/scroll.js +76 -0
  54. package/dist/semantic.d.ts +8 -0
  55. package/dist/semantic.js +47 -0
  56. package/dist/shiki.d.ts +7 -0
  57. package/dist/shiki.js +106 -0
  58. package/dist/smart-keys.d.ts +3 -0
  59. package/dist/smart-keys.js +14 -0
  60. package/dist/testing.d.ts +34 -0
  61. package/dist/testing.js +117 -0
  62. package/dist/theme-scopes.d.ts +9 -0
  63. package/dist/theme-scopes.js +61 -0
  64. package/dist/tokens.d.ts +28 -0
  65. package/dist/tokens.js +152 -0
  66. package/dist/tracked-range.d.ts +5 -0
  67. package/dist/tracked-range.js +20 -0
  68. package/dist/types.d.ts +344 -0
  69. package/dist/types.js +1 -0
  70. package/dist/view.d.ts +383 -0
  71. package/dist/view.js +2134 -0
  72. package/package.json +63 -3
  73. package/src/attribution.ts +85 -0
  74. package/src/code-block.ts +100 -0
  75. package/src/column-selection.ts +63 -0
  76. package/src/controller.ts +838 -0
  77. package/src/editor.css +813 -0
  78. package/src/engine.ts +764 -0
  79. package/src/fake.ts +838 -0
  80. package/src/find.ts +128 -0
  81. package/src/index.ts +69 -0
  82. package/src/input.ts +63 -0
  83. package/src/keymap-table.ts +163 -0
  84. package/src/keymap.ts +204 -0
  85. package/src/layout.ts +754 -0
  86. package/src/lens.ts +32 -0
  87. package/src/listeners.ts +15 -0
  88. package/src/metrics.ts +65 -0
  89. package/src/modifier-gesture.ts +115 -0
  90. package/src/native-input.ts +179 -0
  91. package/src/offsets.ts +16 -0
  92. package/src/outline.ts +248 -0
  93. package/src/overview.ts +80 -0
  94. package/src/paint.ts +608 -0
  95. package/src/php-html.ts +43 -0
  96. package/src/pointer.ts +326 -0
  97. package/src/scroll-animation.ts +48 -0
  98. package/src/scroll.ts +102 -0
  99. package/src/semantic.ts +54 -0
  100. package/src/shiki.ts +118 -0
  101. package/src/smart-keys.ts +17 -0
  102. package/src/testing.ts +157 -0
  103. package/src/theme-scopes.ts +75 -0
  104. package/src/tokens.ts +175 -0
  105. package/src/tracked-range.ts +22 -0
  106. package/src/types.ts +659 -0
  107. package/src/view.ts +2437 -0
@@ -0,0 +1,2 @@
1
+ import type { EditorEngine, SmartEditorEngineOptions } from './types.ts';
2
+ export declare function createSmartEditorEngine(options: SmartEditorEngineOptions): EditorEngine;
package/dist/engine.js ADDED
@@ -0,0 +1,610 @@
1
+ import { changedSpans, DocumentModel, replacementText } from '@adecore/editor-core';
2
+ import { InputController } from './controller.js';
3
+ import { resolveSmartKeys } from './smart-keys.js';
4
+ import { emit, subscribe } from './listeners.js';
5
+ import { fillLens, lensSignature } from './lens.js';
6
+ import { EditorView } from './view.js';
7
+ const DEFAULT_TAB_SIZE = 4;
8
+ const DEFAULT_LABEL = 'Code editor';
9
+ const DEFAULT_WIDGET_OWNER = 'default';
10
+ class SmartEditor {
11
+ model;
12
+ settings;
13
+ view;
14
+ controller;
15
+ changes = new Set();
16
+ textChanges = new Set();
17
+ carets = new Set();
18
+ caretOffset = 0;
19
+ saves = new Set();
20
+ blurs = new Set();
21
+ subscription;
22
+ language;
23
+ theme;
24
+ revision;
25
+ lensRows = new Map();
26
+ lensRevisions = new Map();
27
+ tokenizerRequest = 0;
28
+ settingText = false;
29
+ disposed = false;
30
+ engine;
31
+ constructor(engine, element, options) {
32
+ this.engine = engine;
33
+ this.model = new DocumentModel(options.text);
34
+ this.language = options.language;
35
+ this.theme = options.theme;
36
+ this.settings = {
37
+ language: options.language,
38
+ tabSize: options.indentation?.tabSize ?? DEFAULT_TAB_SIZE,
39
+ insertSpaces: options.indentation?.insertSpaces ?? true,
40
+ readOnly: options.readOnly ?? false,
41
+ readOnlyReason: options.readOnlyReason,
42
+ wrap: options.wrap ?? false,
43
+ smartKeys: resolveSmartKeys(options.smartKeys),
44
+ messages: options.messages ?? {},
45
+ label: options.label ?? DEFAULT_LABEL,
46
+ guides: options.guides !== false,
47
+ whitespace: options.whitespace === true,
48
+ rightMargin: options.rightMargin ?? null,
49
+ foldOutline: options.foldOutline ?? 'hover'
50
+ };
51
+ this.view = new EditorView(element, this.model, this.settings);
52
+ this.controller = new InputController(this.view, {
53
+ handBack: engine.handBack ?? [],
54
+ keymap: engine.keymap,
55
+ apple: engine.apple ?? false,
56
+ save: () => emit(this.saves),
57
+ blur: () => emit(this.blurs)
58
+ });
59
+ this.revision = this.model.getRevision();
60
+ this.caretOffset = this.model.getPrimary().head;
61
+ this.subscription = this.model.subscribe((snapshot) => {
62
+ this.caretMoved(snapshot.selections.at(-1).head);
63
+ if (snapshot.revision !== this.revision) {
64
+ this.revision = snapshot.revision;
65
+ if (snapshot.contentEdits !== undefined && snapshot.contentEdits.length > 0) {
66
+ this.announceTextChange(snapshot.contentEdits, snapshot.source ?? 'external');
67
+ }
68
+ if (!this.settingText) {
69
+ emit(this.changes);
70
+ }
71
+ }
72
+ });
73
+ this.loadTokenizer();
74
+ if (options.folds !== undefined || options.foldDefaults !== undefined) {
75
+ this.view.restoreFolds(options.folds ?? null, options.foldDefaults);
76
+ }
77
+ this.placeCursor(options);
78
+ }
79
+ /* Where the editor opens: on a line and column, scrolled to a position, or with that line in view. */
80
+ placeCursor(options) {
81
+ if (options.line !== undefined) {
82
+ const line = this.clampLine(options.line);
83
+ const bounds = this.model.getLine(line);
84
+ const offset = Math.min(bounds.end, bounds.start + Math.max(0, (options.column ?? 1) - 1));
85
+ this.model.setSelections([{ anchor: offset, head: offset }]);
86
+ if (options.scrollTop === undefined) {
87
+ // A file opens where it is told to, without travelling there.
88
+ this.view.revealOffset(offset, 'center', false);
89
+ }
90
+ }
91
+ if (options.scrollTop !== undefined) {
92
+ this.view.render();
93
+ this.view.viewport.scrollTop = options.scrollTop;
94
+ this.view.render();
95
+ }
96
+ }
97
+ loadTokenizer() {
98
+ const request = ++this.tokenizerRequest;
99
+ void this.engine
100
+ .scopeColors?.(this.theme)
101
+ .catch(() => null)
102
+ .then((colors) => {
103
+ if (!this.disposed && request === this.tokenizerRequest) {
104
+ this.view.setScopeColors(colors ?? null);
105
+ }
106
+ });
107
+ void this.engine
108
+ .tokenizer(this.language, this.theme, this.model.getText())
109
+ .catch(() => null)
110
+ .then((tokenizer) => {
111
+ if (!this.disposed && request === this.tokenizerRequest) {
112
+ this.view.setTokenizer(tokenizer);
113
+ }
114
+ });
115
+ }
116
+ getText() {
117
+ return this.model.getText();
118
+ }
119
+ setText(text) {
120
+ const spans = changedSpans(this.model.getText(), text);
121
+ if (spans.length === 0) {
122
+ return;
123
+ }
124
+ this.settingText = true;
125
+ try {
126
+ // Edits and not a reset, which would drop the undo history and put the cursor on line one.
127
+ this.model.applyEdits(spans.map((span) => ({ from: span.start, to: span.end, text: span.text })), { source: 'external' });
128
+ }
129
+ finally {
130
+ this.settingText = false;
131
+ }
132
+ }
133
+ onChange(listener) {
134
+ return subscribe(this.changes, listener);
135
+ }
136
+ onTextChange(listener) {
137
+ this.textChanges.add(listener);
138
+ return () => {
139
+ this.textChanges.delete(listener);
140
+ };
141
+ }
142
+ announceTextChange(edits, source) {
143
+ const change = {
144
+ source,
145
+ changes: edits.map((edit) => ({
146
+ range: { start: { line: edit.start.line, character: edit.start.column }, end: { line: edit.end.line, character: edit.end.column } },
147
+ text: edit.text
148
+ }))
149
+ };
150
+ for (const listener of [...this.textChanges]) {
151
+ listener(change);
152
+ }
153
+ }
154
+ caretMoved(head) {
155
+ if (head === this.caretOffset) {
156
+ return;
157
+ }
158
+ this.caretOffset = head;
159
+ const position = this.positionAt(head);
160
+ for (const listener of [...this.carets]) {
161
+ listener(position);
162
+ }
163
+ }
164
+ getCaret() {
165
+ return this.positionAt(this.model.getPrimary().head);
166
+ }
167
+ getScrollTop() {
168
+ return this.view.scrollPlace.y;
169
+ }
170
+ getSelection() {
171
+ const { anchor, head } = this.model.getPrimary();
172
+ return { start: this.positionAt(Math.min(anchor, head)), end: this.positionAt(Math.max(anchor, head)) };
173
+ }
174
+ getSelections() {
175
+ return this.model.getSelections().map(({ anchor, head }) => ({
176
+ start: this.positionAt(Math.min(anchor, head)),
177
+ end: this.positionAt(Math.max(anchor, head))
178
+ }));
179
+ }
180
+ getIndentation() {
181
+ return { tabSize: this.settings.tabSize, insertSpaces: this.settings.insertSpaces };
182
+ }
183
+ setCaret(position, reveal = 'relative') {
184
+ const offset = this.offsetAt(position);
185
+ this.model.setSelections([{ anchor: offset, head: offset }]);
186
+ this.view.revealOffset(offset, reveal);
187
+ }
188
+ setSelection(range, reveal = 'relative') {
189
+ const head = this.offsetAt(range.end);
190
+ this.model.setSelections([{ anchor: this.offsetAt(range.start), head }]);
191
+ this.view.revealOffset(head, reveal);
192
+ }
193
+ setSelections(ranges, reveal = 'relative') {
194
+ const last = ranges.at(-1);
195
+ if (last === undefined) {
196
+ return;
197
+ }
198
+ this.model.setSelections(ranges.map((range) => ({ anchor: this.offsetAt(range.start), head: this.offsetAt(range.end) })));
199
+ this.view.revealOffset(this.offsetAt(last.end), reveal);
200
+ }
201
+ onCaret(listener) {
202
+ this.carets.add(listener);
203
+ return () => {
204
+ this.carets.delete(listener);
205
+ };
206
+ }
207
+ onHover(listener) {
208
+ return this.view.onHover((offset) => {
209
+ const rect = offset === null ? null : this.view.clientRectOf(offset);
210
+ listener(offset === null || rect === null ? null : { position: this.positionAt(offset), rect });
211
+ });
212
+ }
213
+ rectAt(position) {
214
+ return this.view.clientRectOf(this.offsetAt(position));
215
+ }
216
+ onViewChange(listener) {
217
+ return this.view.onViewChange(listener);
218
+ }
219
+ onClick(handler) {
220
+ return this.controller.onClick(handler);
221
+ }
222
+ onContextMenu(listener) {
223
+ return this.controller.onContextMenu(listener);
224
+ }
225
+ onKeyDown(handler) {
226
+ return this.controller.onKeyDown(handler);
227
+ }
228
+ setSelectionRanges(provider) {
229
+ this.controller.setSelectionRanges(provider);
230
+ }
231
+ applyEdits(edits) {
232
+ if (this.settings.readOnly || edits.length === 0) {
233
+ return false;
234
+ }
235
+ try {
236
+ return this.model.applyEdits(edits.map((edit) => ({ from: this.offsetAt(edit.range.start), to: this.offsetAt(edit.range.end), text: edit.text })), { source: 'command' });
237
+ }
238
+ catch {
239
+ return false;
240
+ }
241
+ }
242
+ setMarkers(markers) {
243
+ this.view.setMarkers(markers.map((marker) => ({
244
+ severity: marker.severity,
245
+ from: this.offsetAt(marker.range.start),
246
+ to: this.offsetAt(marker.range.end),
247
+ unnecessary: marker.unnecessary === true,
248
+ deprecated: marker.deprecated === true,
249
+ ...(marker.message === undefined ? {} : { message: marker.message })
250
+ })));
251
+ }
252
+ setInlayHints(hints) {
253
+ this.view.setInlays(hints.map((hint, index) => ({ id: `hint-${index}`, at: this.offsetAt(hint.position), text: hint.label })));
254
+ }
255
+ setGhostText(ghost) {
256
+ this.view.setGhost(ghost === null ? null : { at: this.offsetAt(ghost.position), text: ghost.text, accessory: ghost.accessory });
257
+ }
258
+ setSemanticTokens(tokens) {
259
+ this.view.setSemanticTokens(tokens === null
260
+ ? null
261
+ : tokens.map((token) => {
262
+ const from = this.offsetAt({ line: token.line, character: token.character });
263
+ return { from, to: from + token.length, scopes: token.scopes };
264
+ }));
265
+ }
266
+ setHighlights(highlights) {
267
+ this.view.setOccurrences(highlights.map((highlight) => ({ from: this.offsetAt(highlight.range.start), to: this.offsetAt(highlight.range.end), kind: highlight.kind })));
268
+ }
269
+ setLink(range) {
270
+ this.view.setLink(range === null ? null : { from: this.offsetAt(range.start), to: this.offsetAt(range.end) });
271
+ }
272
+ getVisibleRange() {
273
+ const { first, last } = this.view.visibleLines();
274
+ return { start: { line: first, character: 0 }, end: this.positionAt(this.model.getLine(last).end) };
275
+ }
276
+ positionAt(offset) {
277
+ const { line, column } = this.model.positionAt(offset);
278
+ return { line, character: column };
279
+ }
280
+ textInRange(range) {
281
+ return this.model.slice(this.offsetAt(range.start), this.offsetAt(range.end));
282
+ }
283
+ offsetAt(position) {
284
+ return this.model.offsetAt({ line: Math.max(0, position.line), column: Math.max(0, position.character) });
285
+ }
286
+ onSave(listener) {
287
+ return subscribe(this.saves, listener);
288
+ }
289
+ onBlur(listener) {
290
+ return subscribe(this.blurs, listener);
291
+ }
292
+ revealLine(line, reveal = 'center') {
293
+ const bounds = this.model.getLine(this.clampLine(line));
294
+ this.model.setSelections([{ anchor: bounds.start, head: bounds.start }]);
295
+ this.view.revealOffset(bounds.start, reveal);
296
+ }
297
+ find(query) {
298
+ this.view.setFind(query, true);
299
+ }
300
+ findStep(direction) {
301
+ this.view.stepFind(direction);
302
+ }
303
+ replace(replacement, replaceOptions = {}) {
304
+ const { find } = this.view;
305
+ const mark = find.currentMark;
306
+ const query = find.activeQuery;
307
+ if (mark === null || query === null || this.blockedByReadOnly()) {
308
+ return false;
309
+ }
310
+ try {
311
+ const options = { caseSensitive: query.caseSensitive, wholeWord: query.wholeWord, regex: query.regex, from: mark.from, to: mark.to, maxResults: 1 };
312
+ const match = this.model.find(query.text, options)[0];
313
+ if (match === undefined) {
314
+ return false;
315
+ }
316
+ // The next match is looked for after what was written, or a replacement that holds the query would find itself again.
317
+ find.pendingAnchor = mark.from + replacementText(this.model.getText(), match, replacement, false, replaceOptions.preserveCase).length;
318
+ if (!this.model.replace(match, replacement, { preserveCase: replaceOptions.preserveCase })) {
319
+ find.pendingAnchor = undefined;
320
+ return false;
321
+ }
322
+ }
323
+ catch {
324
+ find.pendingAnchor = undefined;
325
+ return false;
326
+ }
327
+ this.view.revealFind();
328
+ this.view.render();
329
+ return true;
330
+ }
331
+ replaceAll(replacement, options = {}) {
332
+ const query = this.view.find.activeQuery;
333
+ if (query === null || this.blockedByReadOnly()) {
334
+ return 0;
335
+ }
336
+ try {
337
+ return this.model.replaceAll(query.text, replacement, {
338
+ caseSensitive: query.caseSensitive,
339
+ wholeWord: query.wholeWord,
340
+ regex: query.regex,
341
+ preserveCase: options.preserveCase,
342
+ ...this.view.find.scope
343
+ });
344
+ }
345
+ catch {
346
+ return 0;
347
+ }
348
+ }
349
+ setReplacePreview(replacement, options = {}) {
350
+ this.view.setReplacePreview(replacement, options.preserveCase === true);
351
+ }
352
+ selectFindMatches() {
353
+ const { find } = this.view;
354
+ const marks = find.matches;
355
+ if (marks.length === 0) {
356
+ return 0;
357
+ }
358
+ const current = find.current ?? marks.length - 1;
359
+ const ordered = [...marks.slice(0, current), ...marks.slice(current + 1), marks[current]];
360
+ this.view.setFind(null, false);
361
+ for (const mark of ordered) {
362
+ this.view.ensureVisible(mark.from);
363
+ }
364
+ this.model.setSelections(ordered.map((mark) => ({ anchor: mark.from, head: mark.to })));
365
+ this.view.revealCaret();
366
+ return ordered.length;
367
+ }
368
+ findFromCursor(query, direction) {
369
+ if (query.text === '') {
370
+ return false;
371
+ }
372
+ let matches;
373
+ try {
374
+ matches = this.model.find(query.text, { caseSensitive: query.caseSensitive, wholeWord: query.wholeWord, regex: query.regex });
375
+ }
376
+ catch {
377
+ return false;
378
+ }
379
+ if (matches.length === 0) {
380
+ return false;
381
+ }
382
+ const { anchor, head } = this.model.getPrimary();
383
+ const from = Math.min(anchor, head);
384
+ const to = Math.max(anchor, head);
385
+ const target = direction === 1 ? (matches.find((match) => match.from >= to) ?? matches[0]) : (matches.findLast((match) => match.to <= from) ?? matches.at(-1));
386
+ this.view.ensureVisible(target.from);
387
+ this.model.setSelections([direction === 1 ? { anchor: target.from, head: target.to } : { anchor: target.to, head: target.from }]);
388
+ this.view.revealCaret(target.from > head ? 'centerDown' : 'centerUp');
389
+ return true;
390
+ }
391
+ blockedByReadOnly() {
392
+ if (!this.settings.readOnly) {
393
+ return false;
394
+ }
395
+ if (this.settings.readOnlyReason) {
396
+ this.view.notify(this.settings.readOnlyReason);
397
+ }
398
+ return true;
399
+ }
400
+ onFind(listener) {
401
+ return this.view.onFind(listener);
402
+ }
403
+ endFind() {
404
+ const mark = this.view.find.currentMark;
405
+ this.view.setFind(null, false);
406
+ if (mark) {
407
+ this.view.ensureVisible(mark.from);
408
+ this.model.setSelections([{ anchor: mark.from, head: mark.to }]);
409
+ this.view.revealCaret();
410
+ }
411
+ }
412
+ setWrap(wrap) {
413
+ this.settings.wrap = wrap;
414
+ if (wrap) {
415
+ this.view.viewport.scrollLeft = 0;
416
+ }
417
+ this.view.applySettings();
418
+ }
419
+ setChangeMarks(marks) {
420
+ this.view.setChangeMarks(marks);
421
+ }
422
+ setAttributionMarks(marks) {
423
+ this.view.setAttributionMarks(marks);
424
+ }
425
+ setLineHighlights(highlights) {
426
+ this.view.setLineHighlights(highlights);
427
+ }
428
+ renderCode(container, text, options = {}) {
429
+ this.view.renderCode(container, text, options);
430
+ }
431
+ setRemoteCursors(cursors) {
432
+ this.view.setRemoteCarets(cursors.map((cursor) => ({ id: cursor.id, name: cursor.name, color: cursor.color, at: this.offsetAt(cursor.position) })));
433
+ }
434
+ onAttributionHover(listener) {
435
+ return this.view.onAttributionHover(listener);
436
+ }
437
+ setWidgets(widgets, owner = DEFAULT_WIDGET_OWNER) {
438
+ this.view.setBlockWidgets(owner, widgets.map((widget) => ({
439
+ id: `widget:${owner}:${widget.id}`,
440
+ at: this.offsetAt({ line: widget.line, character: 0 }),
441
+ placement: widget.placement ?? 'below',
442
+ height: widget.height,
443
+ render: (container) => {
444
+ container.classList.add('se-widget');
445
+ widget.render(container);
446
+ }
447
+ })));
448
+ }
449
+ setLineActions(actions, owner = DEFAULT_WIDGET_OWNER) {
450
+ this.view.setLineActions(owner, actions.map((action) => ({
451
+ id: action.id,
452
+ at: this.offsetAt({ line: action.line, character: 0 }),
453
+ render: (container) => action.render(container)
454
+ })));
455
+ }
456
+ trackRange(range) {
457
+ const tracked = this.view.trackRange(this.offsetAt(range.start), this.offsetAt(range.end));
458
+ return {
459
+ get: () => {
460
+ const offsets = tracked.get();
461
+ return offsets === null ? null : { start: this.positionAt(offsets.from), end: this.positionAt(offsets.to) };
462
+ },
463
+ dispose: () => tracked.dispose()
464
+ };
465
+ }
466
+ setCodeVision(rows) {
467
+ const current = new Map(rows.map((row) => [row.id, row]));
468
+ const revisions = new Map();
469
+ for (const row of rows) {
470
+ const signature = lensSignature(row.entries);
471
+ const before = this.lensRevisions.get(row.id);
472
+ revisions.set(row.id, {
473
+ signature,
474
+ revision: before === undefined || before.signature === signature ? (before?.revision ?? 0) : before.revision + 1
475
+ });
476
+ }
477
+ this.lensRows = current;
478
+ this.lensRevisions = revisions;
479
+ this.view.setLenses(rows.map((row) => ({
480
+ id: `lens:${row.id}`,
481
+ at: this.indentOffsetOf(row.line),
482
+ placement: 'above',
483
+ lens: true,
484
+ revision: revisions.get(row.id).revision,
485
+ render: (container) => fillLens(container, row.entries, (id) => this.lensRows.get(row.id)?.entries.find((entry) => entry.id === id))
486
+ })));
487
+ }
488
+ /* Where the text of a line begins after its indentation, so a row sitting there stays with the declaration when something is typed in front of it. */
489
+ indentOffsetOf(line) {
490
+ const { start, text } = this.model.getLine(Math.max(0, Math.min(line, this.model.getLineCount() - 1)));
491
+ return start + (/^[ \t]*/.exec(text)?.[0].length ?? 0);
492
+ }
493
+ setGutterAction(action) {
494
+ this.view.setGutterAction(action);
495
+ }
496
+ onGutterAction(listener) {
497
+ return this.view.onGutterAction(listener);
498
+ }
499
+ setGutterMarkers(markers, owner = 'default') {
500
+ this.view.setGutterMarkers(owner, markers);
501
+ }
502
+ onGutterMarker(listener) {
503
+ return this.view.onGutterMarker(listener);
504
+ }
505
+ setBlocks(blocks) {
506
+ this.view.setBlocks(blocks);
507
+ }
508
+ onScope(listener) {
509
+ return this.view.onScope(listener);
510
+ }
511
+ setGuides(guides) {
512
+ this.settings.guides = guides;
513
+ this.view.applySettings();
514
+ }
515
+ setWhitespace(whitespace) {
516
+ this.settings.whitespace = whitespace;
517
+ this.view.applySettings();
518
+ }
519
+ setRightMargin(column) {
520
+ this.settings.rightMargin = column;
521
+ this.view.applySettings();
522
+ }
523
+ setFoldOutline(outline) {
524
+ this.settings.foldOutline = outline;
525
+ this.view.applySettings();
526
+ }
527
+ setLabel(label) {
528
+ this.settings.label = label;
529
+ this.view.applySettings();
530
+ }
531
+ setFoldHints(hints) {
532
+ this.view.setFoldHints(hints === null
533
+ ? null
534
+ : {
535
+ symbols: (hints.symbols ?? []).map((symbol) => ({
536
+ from: this.offsetAt(symbol.range.start),
537
+ to: this.offsetAt(symbol.range.end),
538
+ body: symbol.body
539
+ })),
540
+ ranges: (hints.ranges ?? []).map((range) => ({
541
+ from: this.model.getLine(Math.min(range.startLine, this.model.getLineCount() - 1)).start,
542
+ to: this.model.getLine(Math.min(range.endLine, this.model.getLineCount() - 1)).start,
543
+ ...(range.kind === undefined ? {} : { kind: range.kind })
544
+ }))
545
+ });
546
+ }
547
+ setSmartKeys(keys) {
548
+ this.settings.smartKeys = { ...this.settings.smartKeys, ...keys };
549
+ }
550
+ setIndentation(indentation) {
551
+ if (indentation.tabSize === this.settings.tabSize && indentation.insertSpaces === this.settings.insertSpaces) {
552
+ return;
553
+ }
554
+ this.settings.tabSize = indentation.tabSize;
555
+ this.settings.insertSpaces = indentation.insertSpaces;
556
+ this.view.applySettings();
557
+ }
558
+ setTheme(theme) {
559
+ if (theme !== this.theme) {
560
+ this.theme = theme;
561
+ this.loadTokenizer();
562
+ }
563
+ }
564
+ refreshFont() {
565
+ this.view.refreshFont();
566
+ }
567
+ setReadOnly(readOnly, reason) {
568
+ this.settings.readOnly = readOnly;
569
+ this.settings.readOnlyReason = reason;
570
+ this.view.applySettings();
571
+ }
572
+ runCommand(command) {
573
+ return this.controller.command(command);
574
+ }
575
+ getFolds() {
576
+ return this.view.getFolds();
577
+ }
578
+ focus() {
579
+ this.view.focus();
580
+ }
581
+ dispose() {
582
+ if (this.disposed) {
583
+ return;
584
+ }
585
+ this.disposed = true;
586
+ this.changes.clear();
587
+ this.textChanges.clear();
588
+ this.carets.clear();
589
+ this.saves.clear();
590
+ this.blurs.clear();
591
+ this.subscription.dispose();
592
+ this.controller.dispose();
593
+ this.view.dispose();
594
+ }
595
+ clampLine(line) {
596
+ return Math.min(Math.max(1, Math.trunc(line) || 1), this.model.getLineCount()) - 1;
597
+ }
598
+ }
599
+ class SmartEditorEngine {
600
+ options;
601
+ constructor(options) {
602
+ this.options = options;
603
+ }
604
+ mount(element, options) {
605
+ return new SmartEditor(this.options, element, options);
606
+ }
607
+ }
608
+ export function createSmartEditorEngine(options) {
609
+ return new SmartEditorEngine(options);
610
+ }