@fusion.dev/helix-code-editor 0.245.0 → 0.247.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.
@@ -4,6 +4,20 @@ import type { CodeEditorEngine } from '@fusion.dev/helix-headless/code-editor';
4
4
  export declare const languageName: (name: string) => string | undefined;
5
5
  /** Every name `language` takes, its aliases included. */
6
6
  export declare const languageNames: () => string[];
7
+ /** One replacement in a text: the span from `from` to `to`, and what goes there. */
8
+ interface Replacement {
9
+ from: number;
10
+ insert: string;
11
+ to: number;
12
+ }
13
+ /**
14
+ * The replacements that turn `from` into `to`, one for each run of lines that
15
+ * differs, each cut down to the characters that differ. **Only those spans are
16
+ * replaced**, so the selection and the lines between keep their places, and
17
+ * only the lines that changed light up: an agent's two edits far apart in a
18
+ * file are two changes and not every line between them.
19
+ */
20
+ export declare const changeBetween: (from: string, to: string) => Replacement[];
7
21
  /** What `codeMirrorEngine` takes beyond what the editor sets. */
8
22
  export interface CodeMirrorOptions {
9
23
  /** More CodeMirror extensions, such as completions or a language server client, added after Helix's own. */
@@ -23,3 +37,4 @@ export declare const codeMirrorEngine: (options?: CodeMirrorOptions) => CodeEdit
23
37
  export declare const connectCodeEditor: (editor: {
24
38
  engine: CodeEditorEngine | undefined;
25
39
  }, options?: CodeMirrorOptions) => (() => void);
40
+ export {};
@@ -2,8 +2,8 @@ import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirro
2
2
  import { bracketMatching, codeFolding, foldGutter, foldKeymap, indentOnInput, indentUnit, StreamLanguage, syntaxHighlighting, } from '@codemirror/language';
3
3
  import { setDiagnostics } from '@codemirror/lint';
4
4
  import { closeSearchPanel, highlightSelectionMatches, openSearchPanel, replaceAll, replaceNext, SearchQuery, search, searchPanelOpen, setSearchQuery, } from '@codemirror/search';
5
- import { Annotation, Compartment, EditorState, RangeSet } from '@codemirror/state';
6
- import { drawSelection, EditorView, GutterMarker, gutterLineClass, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, placeholder, ViewPlugin, } from '@codemirror/view';
5
+ import { Annotation, Compartment, EditorState, Facet, RangeSet, StateEffect, StateField, Transaction, } from '@codemirror/state';
6
+ import { Decoration, drawSelection, EditorView, GutterMarker, gutterLineClass, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, placeholder, ViewPlugin, } from '@codemirror/view';
7
7
  import { syntaxHighlighter, syntaxRules } from '@fusion.dev/helix-sandbox/highlight';
8
8
  const LANGUAGES = {
9
9
  css: async () => (await import('@codemirror/lang-css')).css(),
@@ -51,6 +51,145 @@ const remeasureOnFonts = ViewPlugin.fromClass(class {
51
51
  }
52
52
  });
53
53
  const given = Annotation.define();
54
+ const WASH = 'light-dark(color-mix(in oklab, var(--helix-color-info, oklch(0.48 0.12 245)) 14%, transparent), color-mix(in oklab, var(--helix-color-info, oklch(0.7 0.16 245)) 22%, transparent))';
55
+ const flashing = Facet.define({ combine: (values) => values.some(Boolean) });
56
+ const unflash = StateEffect.define();
57
+ const changedLines = (transaction) => {
58
+ const doc = transaction.state.doc;
59
+ const starts = new Set();
60
+ transaction.changes.iterChangedRanges((fromA, toA, fromB, toB) => {
61
+ if (fromB === toB) {
62
+ const removed = transaction.startState.sliceDoc(fromA, toA);
63
+ const line = doc.lineAt(fromB);
64
+ if ((removed.endsWith('\n') && line.from === fromB) ||
65
+ (removed.startsWith('\n') && line.to === fromB)) {
66
+ return;
67
+ }
68
+ }
69
+ let first = doc.lineAt(fromB);
70
+ let last = doc.lineAt(toB);
71
+ if (toB > fromB && first.to === fromB && first.number < last.number) {
72
+ first = doc.line(first.number + 1);
73
+ }
74
+ if (toB > fromB && last.from === toB && last.number > first.number) {
75
+ last = doc.line(last.number - 1);
76
+ }
77
+ for (let number = first.number; number <= last.number; number += 1) {
78
+ starts.add(doc.line(number).from);
79
+ }
80
+ });
81
+ if (doc.lines > 1 && starts.size === doc.lines) {
82
+ return [];
83
+ }
84
+ return [...starts].sort((a, b) => a - b);
85
+ };
86
+ const flashedLines = StateField.define({
87
+ create: () => ({ lines: Decoration.none, round: 0 }),
88
+ provide: (field) => EditorView.decorations.from(field, (value) => value.lines),
89
+ update(value, transaction) {
90
+ let lines = value.lines.map(transaction.changes);
91
+ let round = value.round;
92
+ for (const effect of transaction.effects) {
93
+ if (effect.is(unflash)) {
94
+ lines = lines.update({ filter: (_from, _to, mark) => mark.spec.round !== effect.value });
95
+ }
96
+ }
97
+ const fromPage = transaction.annotation(given) === true ||
98
+ transaction.annotation(Transaction.userEvent) === undefined;
99
+ if (!transaction.docChanged || !fromPage || !transaction.state.facet(flashing)) {
100
+ return { lines, round };
101
+ }
102
+ const starts = changedLines(transaction);
103
+ if (starts.length === 0) {
104
+ return { lines, round };
105
+ }
106
+ round += 1;
107
+ const again = new Set();
108
+ lines.between(0, transaction.state.doc.length, (from, _to, mark) => {
109
+ if (starts.includes(from) && mark.spec.class === 'cm-helix-changed') {
110
+ again.add(from);
111
+ }
112
+ });
113
+ lines = lines.update({
114
+ add: starts.map((from) => Decoration.line({
115
+ class: again.has(from) ? 'cm-helix-changed-again' : 'cm-helix-changed',
116
+ round,
117
+ }).range(from)),
118
+ filter: (from) => !starts.includes(from),
119
+ });
120
+ return { lines, round };
121
+ },
122
+ });
123
+ const timeOf = (view, name, standing) => {
124
+ const written = getComputedStyle(view.dom).getPropertyValue(name).trim();
125
+ const amount = Number.parseFloat(written);
126
+ if (!Number.isFinite(amount)) {
127
+ return standing;
128
+ }
129
+ return written.endsWith('ms') || !written.endsWith('s') ? amount : amount * 1000;
130
+ };
131
+ const unflashLater = ViewPlugin.fromClass(class {
132
+ constructor(view) {
133
+ this.timers = new Set();
134
+ this.view = view;
135
+ this.seen = view.state.field(flashedLines).round;
136
+ }
137
+ destroy() {
138
+ for (const timer of this.timers) {
139
+ clearTimeout(timer);
140
+ }
141
+ }
142
+ update(update) {
143
+ const { round } = update.state.field(flashedLines);
144
+ if (round === this.seen) {
145
+ return;
146
+ }
147
+ this.seen = round;
148
+ const reduced = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
149
+ const lasts = reduced
150
+ ? 320 + timeOf(this.view, '--helix-duration-fade', 120)
151
+ : timeOf(this.view, '--helix-duration-slow', 320) * 3;
152
+ const timer = setTimeout(() => {
153
+ this.timers.delete(timer);
154
+ this.view.dispatch({ effects: unflash.of(round) });
155
+ }, lasts + 50);
156
+ this.timers.add(timer);
157
+ }
158
+ });
159
+ const flashTheme = EditorView.theme({
160
+ '.cm-line.cm-helix-changed-again': { animationName: 'cm-helix-flash-again' },
161
+ '.cm-line.cm-helix-changed, .cm-line.cm-helix-changed-again': {
162
+ animationDuration: 'calc(var(--helix-duration-slow, 320ms) * 3)',
163
+ animationFillMode: 'both',
164
+ animationName: 'cm-helix-flash',
165
+ animationTimingFunction: 'linear',
166
+ },
167
+ '@keyframes cm-helix-flash': {
168
+ '0%, 33.333%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
169
+ '100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
170
+ },
171
+ '@keyframes cm-helix-flash-again': {
172
+ '0%, 33.333%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
173
+ '100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
174
+ },
175
+ '@keyframes cm-helix-flash-steady': {
176
+ '0%, 72.7%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
177
+ '100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
178
+ },
179
+ '@keyframes cm-helix-flash-steady-again': {
180
+ '0%, 72.7%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
181
+ '100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
182
+ },
183
+ '@media (prefers-reduced-motion: reduce)': {
184
+ '.cm-content .cm-line.cm-helix-changed-again': {
185
+ animationName: 'cm-helix-flash-steady-again',
186
+ },
187
+ '.cm-content .cm-line.cm-helix-changed, .cm-content .cm-line.cm-helix-changed-again': {
188
+ animationDuration: 'calc(320ms + var(--helix-duration-fade, 120ms))',
189
+ animationName: 'cm-helix-flash-steady',
190
+ },
191
+ },
192
+ });
54
193
  const helixTheme = EditorView.theme({
55
194
  '.cm-activeLine, .cm-activeLineGutter': {
56
195
  backgroundColor: 'color-mix(in oklab, var(--helix-color-foreground) 5%, transparent)',
@@ -126,6 +265,90 @@ const helixTheme = EditorView.theme({
126
265
  },
127
266
  '&.cm-focused': { outline: 'none' },
128
267
  });
268
+ const spanBetween = (from, to, at = 0) => {
269
+ const shorter = Math.min(from.length, to.length);
270
+ let start = 0;
271
+ while (start < shorter && from.charCodeAt(start) === to.charCodeAt(start)) {
272
+ start += 1;
273
+ }
274
+ let end = 0;
275
+ while (end < shorter - start &&
276
+ from.charCodeAt(from.length - 1 - end) === to.charCodeAt(to.length - 1 - end)) {
277
+ end += 1;
278
+ }
279
+ return { from: at + start, insert: to.slice(start, to.length - end), to: at + from.length - end };
280
+ };
281
+ const linesOf = (text) => text.match(/[^\n]*\n|[^\n]+$/g) ?? [];
282
+ const COMPARED = 1_000_000;
283
+ export const changeBetween = (from, to) => {
284
+ if (from === to) {
285
+ return [];
286
+ }
287
+ const before = linesOf(from);
288
+ const after = linesOf(to);
289
+ let head = 0;
290
+ while (head < before.length && head < after.length && before[head] === after[head]) {
291
+ head += 1;
292
+ }
293
+ let tail = 0;
294
+ while (tail < before.length - head &&
295
+ tail < after.length - head &&
296
+ before[before.length - 1 - tail] === after[after.length - 1 - tail]) {
297
+ tail += 1;
298
+ }
299
+ const old = before.slice(head, before.length - tail);
300
+ const now = after.slice(head, after.length - tail);
301
+ const start = before.slice(0, head).join('').length;
302
+ if (old.length * now.length > COMPARED) {
303
+ return [spanBetween(old.join(''), now.join(''), start)];
304
+ }
305
+ const shared = Array.from({ length: old.length + 1 }, () => new Array(now.length + 1).fill(0));
306
+ for (let i = old.length - 1; i >= 0; i -= 1) {
307
+ for (let j = now.length - 1; j >= 0; j -= 1) {
308
+ const row = shared[i];
309
+ row[j] =
310
+ old[i] === now[j]
311
+ ? shared[i + 1][j + 1] + 1
312
+ : Math.max(shared[i + 1][j], row[j + 1]);
313
+ }
314
+ }
315
+ const replacements = [];
316
+ let i = 0;
317
+ let j = 0;
318
+ let at = start;
319
+ let runFrom = at;
320
+ let removed = '';
321
+ let added = '';
322
+ const close = () => {
323
+ if (removed !== '' || added !== '') {
324
+ replacements.push(spanBetween(removed, added, runFrom));
325
+ }
326
+ removed = '';
327
+ added = '';
328
+ };
329
+ while (i < old.length || j < now.length) {
330
+ if (i < old.length && j < now.length && old[i] === now[j]) {
331
+ close();
332
+ at += old[i].length;
333
+ runFrom = at;
334
+ i += 1;
335
+ j += 1;
336
+ }
337
+ else if (j >= now.length ||
338
+ (i < old.length &&
339
+ shared[i + 1][j] >= shared[i][j + 1])) {
340
+ removed += old[i];
341
+ at += old[i].length;
342
+ i += 1;
343
+ }
344
+ else {
345
+ added += now[j];
346
+ j += 1;
347
+ }
348
+ }
349
+ close();
350
+ return replacements;
351
+ };
129
352
  const SEVERITY = { danger: 'error', info: 'info', warning: 'warning' };
130
353
  const lintOf = (state, length) => state.diagnostics.map((one) => ({
131
354
  from: Math.min(Math.max(0, one.from), length),
@@ -210,6 +433,7 @@ export const codeMirrorEngine = (options = {}) => ({
210
433
  const words = new Compartment();
211
434
  const hint = new Compartment();
212
435
  const problems = new Compartment();
436
+ const flashes = new Compartment();
213
437
  let current = state;
214
438
  let asked = '';
215
439
  const load = (name, view) => {
@@ -253,6 +477,10 @@ export const codeMirrorEngine = (options = {}) => ({
253
477
  keymap.of([...defaultKeymap, ...historyKeymap, ...foldKeymap, indentWithTab]),
254
478
  syntaxHighlighting(syntaxHighlighter, { fallback: true }),
255
479
  helixTheme,
480
+ flashTheme,
481
+ flashedLines,
482
+ unflashLater,
483
+ flashes.of(flashing.of(state.flashChanges)),
256
484
  language.of([]),
257
485
  editable.of(EditorState.readOnly.of(state.readOnly)),
258
486
  wrapping.of(state.wrap ? EditorView.lineWrapping : []),
@@ -354,12 +582,13 @@ export const codeMirrorEngine = (options = {}) => ({
354
582
  if (next.placeholder !== current.placeholder) {
355
583
  effects.push(hint.reconfigure(next.placeholder ? placeholder(next.placeholder) : []));
356
584
  }
585
+ if (next.flashChanges !== current.flashChanges) {
586
+ effects.push(flashes.reconfigure(flashing.of(next.flashChanges)));
587
+ }
357
588
  const doc = view.state.doc.toString();
358
589
  view.dispatch({
359
590
  annotations: given.of(true),
360
- ...(next.value === doc
361
- ? {}
362
- : { changes: { from: 0, insert: next.value, to: doc.length } }),
591
+ changes: changeBetween(doc, next.value),
363
592
  effects,
364
593
  });
365
594
  if (next.diagnostics !== current.diagnostics || next.value !== doc) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fusion.dev/helix-code-editor",
3
3
  "description": "Connects the Helix Code Editor to CodeMirror, themed with your tokens: highlighting, line numbers, folding, search, and diagnostics.",
4
- "version": "0.245.0",
4
+ "version": "0.247.0",
5
5
  "license": "MIT",
6
6
  "author": "Fusion Development Group",
7
7
  "repository": {
@@ -46,11 +46,11 @@
46
46
  "@codemirror/search": "^6.5.11",
47
47
  "@codemirror/state": "^6.7.6",
48
48
  "@codemirror/view": "^6.43.15",
49
- "@fusion.dev/helix-headless": "0.245.0",
50
- "@fusion.dev/helix-sandbox": "0.245.0"
49
+ "@fusion.dev/helix-headless": "0.247.0",
50
+ "@fusion.dev/helix-sandbox": "0.247.0"
51
51
  },
52
52
  "devDependencies": {
53
- "@fusion.dev/helix-testing": "0.245.0",
53
+ "@fusion.dev/helix-testing": "0.247.0",
54
54
  "@types/bun": "^1.4.4"
55
55
  },
56
56
  "homepage": "https://fusion.dev",