@adecore/editor 0.0.1 → 0.17.0-beta.1

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