@fusion.dev/helix-code-editor 0.246.0 → 0.247.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.
|
@@ -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 {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
|
|
2
|
-
import { bracketMatching, codeFolding,
|
|
2
|
+
import { bracketMatching, codeFolding, foldable, foldEffect, foldedRanges, foldKeymap, foldState, indentOnInput, indentUnit, language as languageFacet, StreamLanguage, syntaxHighlighting, syntaxTree, unfoldEffect, } 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 {
|
|
5
|
+
import { Annotation, Compartment, EditorState, Facet, RangeSet, RangeSetBuilder, StateEffect, StateField, Transaction, } from '@codemirror/state';
|
|
6
|
+
import { BlockType, Decoration, drawSelection, EditorView, highlightActiveLine, keymap, 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(),
|
|
@@ -50,39 +50,326 @@ const remeasureOnFonts = ViewPlugin.fromClass(class {
|
|
|
50
50
|
this.fonts?.removeEventListener('loadingdone', this.measure);
|
|
51
51
|
}
|
|
52
52
|
});
|
|
53
|
+
const foldOn = (state, from, to) => {
|
|
54
|
+
let found = null;
|
|
55
|
+
foldedRanges(state).between(from, to, (start, end) => {
|
|
56
|
+
if (!found || found.from > start) {
|
|
57
|
+
found = { from: start, to: end };
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return found;
|
|
61
|
+
};
|
|
62
|
+
const foldClass = (state, from, to) => foldOn(state, from, to) ? 'cm-helix-folded' : foldable(state, from, to) ? 'cm-helix-foldable' : '';
|
|
63
|
+
const numberedLines = ViewPlugin.fromClass(class {
|
|
64
|
+
constructor(view) {
|
|
65
|
+
this.decorations = this.number(view);
|
|
66
|
+
}
|
|
67
|
+
update(update) {
|
|
68
|
+
if (update.docChanged ||
|
|
69
|
+
update.viewportChanged ||
|
|
70
|
+
update.startState.facet(languageFacet) !== update.state.facet(languageFacet) ||
|
|
71
|
+
update.startState.field(foldState, false) !== update.state.field(foldState, false) ||
|
|
72
|
+
syntaxTree(update.startState) !== syntaxTree(update.state)) {
|
|
73
|
+
this.decorations = this.number(update.view);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
number(view) {
|
|
77
|
+
const builder = new RangeSetBuilder();
|
|
78
|
+
const { state } = view;
|
|
79
|
+
for (const block of view.viewportLineBlocks) {
|
|
80
|
+
const lines = Array.isArray(block.type)
|
|
81
|
+
? block.type.filter((inner) => inner.type === BlockType.Text)
|
|
82
|
+
: block.type === BlockType.Text
|
|
83
|
+
? [block]
|
|
84
|
+
: [];
|
|
85
|
+
for (const { from, to } of lines) {
|
|
86
|
+
const folds = foldClass(state, from, to);
|
|
87
|
+
builder.add(from, from, Decoration.line({
|
|
88
|
+
attributes: { 'data-line': String(state.doc.lineAt(from).number) },
|
|
89
|
+
...(folds ? { class: folds } : {}),
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return builder.finish();
|
|
94
|
+
}
|
|
95
|
+
}, {
|
|
96
|
+
decorations: (plugin) => plugin.decorations,
|
|
97
|
+
eventHandlers: {
|
|
98
|
+
mousedown(event, view) {
|
|
99
|
+
const line = event.target instanceof Element ? event.target.closest('.cm-line') : null;
|
|
100
|
+
if (event.button !== 0 ||
|
|
101
|
+
!(line instanceof HTMLElement) ||
|
|
102
|
+
!line.matches('.cm-helix-foldable, .cm-helix-folded')) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const marker = getComputedStyle(line, '::after');
|
|
106
|
+
const numbers = parseFloat(getComputedStyle(line, '::before').inlineSize) || 0;
|
|
107
|
+
const width = parseFloat(marker.inlineSize) || 0;
|
|
108
|
+
const start = Math.max(line.getBoundingClientRect().left - width, view.scrollDOM.getBoundingClientRect().left + numbers);
|
|
109
|
+
if (event.clientX < start || event.clientX >= start + width) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const at = view.posAtDOM(line, 0);
|
|
113
|
+
const { from, to } = view.state.doc.lineAt(at);
|
|
114
|
+
const folded = foldOn(view.state, from, to);
|
|
115
|
+
const range = folded ?? foldable(view.state, from, to);
|
|
116
|
+
if (!range) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
view.dispatch({ effects: (folded ? unfoldEffect : foldEffect).of(range) });
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
return true;
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
const LENGTHS = [
|
|
126
|
+
'--_code-editor-line-start: 6px',
|
|
127
|
+
'--_code-editor-numbers: max(round(down, 3ch, 1px / 64), calc(round(up, var(--_code-editor-digits) * 1ch, 1px / 64) + var(--helix-space-sm) + var(--helix-space-2xs)))',
|
|
128
|
+
'--_code-editor-folds: calc(round(up, 1ch + 2px, 1px / 64) + var(--helix-space-2xs) * 2)',
|
|
129
|
+
'--_code-editor-band: calc(var(--_code-editor-numbers) + var(--_code-editor-folds))',
|
|
130
|
+
].join('; ');
|
|
131
|
+
const widestNumber = EditorView.editorAttributes.compute(['doc'], (state) => {
|
|
132
|
+
const digits = String(state.doc.lines).length;
|
|
133
|
+
return {
|
|
134
|
+
class: 'cm-helix-numbered',
|
|
135
|
+
style: `--_code-editor-digits: ${digits}; --_code-editor-widest: "${'9'.repeat(digits)}"; ${LENGTHS}`,
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
const numbering = [numberedLines, widestNumber];
|
|
53
139
|
const given = Annotation.define();
|
|
140
|
+
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))';
|
|
141
|
+
const flashing = Facet.define({ combine: (values) => values.some(Boolean) });
|
|
142
|
+
const unflash = StateEffect.define();
|
|
143
|
+
const changedLines = (transaction) => {
|
|
144
|
+
const doc = transaction.state.doc;
|
|
145
|
+
const starts = new Set();
|
|
146
|
+
transaction.changes.iterChangedRanges((fromA, toA, fromB, toB) => {
|
|
147
|
+
if (fromB === toB) {
|
|
148
|
+
const removed = transaction.startState.sliceDoc(fromA, toA);
|
|
149
|
+
const line = doc.lineAt(fromB);
|
|
150
|
+
if ((removed.endsWith('\n') && line.from === fromB) ||
|
|
151
|
+
(removed.startsWith('\n') && line.to === fromB)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
let first = doc.lineAt(fromB);
|
|
156
|
+
let last = doc.lineAt(toB);
|
|
157
|
+
if (toB > fromB && first.to === fromB && first.number < last.number) {
|
|
158
|
+
first = doc.line(first.number + 1);
|
|
159
|
+
}
|
|
160
|
+
if (toB > fromB && last.from === toB && last.number > first.number) {
|
|
161
|
+
last = doc.line(last.number - 1);
|
|
162
|
+
}
|
|
163
|
+
for (let number = first.number; number <= last.number; number += 1) {
|
|
164
|
+
starts.add(doc.line(number).from);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
if (doc.lines > 1 && starts.size === doc.lines) {
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
return [...starts].sort((a, b) => a - b);
|
|
171
|
+
};
|
|
172
|
+
const flashedLines = StateField.define({
|
|
173
|
+
create: () => ({ lines: Decoration.none, round: 0 }),
|
|
174
|
+
provide: (field) => EditorView.decorations.from(field, (value) => value.lines),
|
|
175
|
+
update(value, transaction) {
|
|
176
|
+
let lines = value.lines.map(transaction.changes);
|
|
177
|
+
let round = value.round;
|
|
178
|
+
for (const effect of transaction.effects) {
|
|
179
|
+
if (effect.is(unflash)) {
|
|
180
|
+
lines = lines.update({ filter: (_from, _to, mark) => mark.spec.round !== effect.value });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const fromPage = transaction.annotation(given) === true ||
|
|
184
|
+
transaction.annotation(Transaction.userEvent) === undefined;
|
|
185
|
+
if (!transaction.docChanged || !fromPage || !transaction.state.facet(flashing)) {
|
|
186
|
+
return { lines, round };
|
|
187
|
+
}
|
|
188
|
+
const starts = changedLines(transaction);
|
|
189
|
+
if (starts.length === 0) {
|
|
190
|
+
return { lines, round };
|
|
191
|
+
}
|
|
192
|
+
round += 1;
|
|
193
|
+
const again = new Set();
|
|
194
|
+
lines.between(0, transaction.state.doc.length, (from, _to, mark) => {
|
|
195
|
+
if (starts.includes(from) && mark.spec.class === 'cm-helix-changed') {
|
|
196
|
+
again.add(from);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
lines = lines.update({
|
|
200
|
+
add: starts.map((from) => Decoration.line({
|
|
201
|
+
class: again.has(from) ? 'cm-helix-changed-again' : 'cm-helix-changed',
|
|
202
|
+
round,
|
|
203
|
+
}).range(from)),
|
|
204
|
+
filter: (from) => !starts.includes(from),
|
|
205
|
+
});
|
|
206
|
+
return { lines, round };
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
const timeOf = (view, name, standing) => {
|
|
210
|
+
const written = getComputedStyle(view.dom).getPropertyValue(name).trim();
|
|
211
|
+
const amount = Number.parseFloat(written);
|
|
212
|
+
if (!Number.isFinite(amount)) {
|
|
213
|
+
return standing;
|
|
214
|
+
}
|
|
215
|
+
return written.endsWith('ms') || !written.endsWith('s') ? amount : amount * 1000;
|
|
216
|
+
};
|
|
217
|
+
const unflashLater = ViewPlugin.fromClass(class {
|
|
218
|
+
constructor(view) {
|
|
219
|
+
this.timers = new Set();
|
|
220
|
+
this.view = view;
|
|
221
|
+
this.seen = view.state.field(flashedLines).round;
|
|
222
|
+
}
|
|
223
|
+
destroy() {
|
|
224
|
+
for (const timer of this.timers) {
|
|
225
|
+
clearTimeout(timer);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
update(update) {
|
|
229
|
+
const { round } = update.state.field(flashedLines);
|
|
230
|
+
if (round === this.seen) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.seen = round;
|
|
234
|
+
const reduced = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
235
|
+
const lasts = reduced
|
|
236
|
+
? 320 + timeOf(this.view, '--helix-duration-fade', 120)
|
|
237
|
+
: timeOf(this.view, '--helix-duration-slow', 320) * 3;
|
|
238
|
+
const timer = setTimeout(() => {
|
|
239
|
+
this.timers.delete(timer);
|
|
240
|
+
this.view.dispatch({ effects: unflash.of(round) });
|
|
241
|
+
}, lasts + 50);
|
|
242
|
+
this.timers.add(timer);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
const flashTheme = EditorView.theme({
|
|
246
|
+
'.cm-line.cm-helix-changed-again': { animationName: 'cm-helix-flash-again' },
|
|
247
|
+
'.cm-line.cm-helix-changed, .cm-line.cm-helix-changed-again': {
|
|
248
|
+
animationDuration: 'calc(var(--helix-duration-slow, 320ms) * 3)',
|
|
249
|
+
animationFillMode: 'both',
|
|
250
|
+
animationName: 'cm-helix-flash',
|
|
251
|
+
animationTimingFunction: 'linear',
|
|
252
|
+
},
|
|
253
|
+
'@keyframes cm-helix-flash': {
|
|
254
|
+
'0%, 33.333%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
|
|
255
|
+
'100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
|
|
256
|
+
},
|
|
257
|
+
'@keyframes cm-helix-flash-again': {
|
|
258
|
+
'0%, 33.333%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
|
|
259
|
+
'100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
|
|
260
|
+
},
|
|
261
|
+
'@keyframes cm-helix-flash-steady': {
|
|
262
|
+
'0%, 72.7%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
|
|
263
|
+
'100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
|
|
264
|
+
},
|
|
265
|
+
'@keyframes cm-helix-flash-steady-again': {
|
|
266
|
+
'0%, 72.7%': { boxShadow: `inset 0 0 0 100vmax ${WASH}` },
|
|
267
|
+
'100%': { boxShadow: 'inset 0 0 0 100vmax transparent' },
|
|
268
|
+
},
|
|
269
|
+
'@media (prefers-reduced-motion: reduce)': {
|
|
270
|
+
'.cm-content .cm-line.cm-helix-changed-again': {
|
|
271
|
+
animationName: 'cm-helix-flash-steady-again',
|
|
272
|
+
},
|
|
273
|
+
'.cm-content .cm-line.cm-helix-changed, .cm-content .cm-line.cm-helix-changed-again': {
|
|
274
|
+
animationDuration: 'calc(320ms + var(--helix-duration-fade, 120ms))',
|
|
275
|
+
animationName: 'cm-helix-flash-steady',
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
const drawn = (line) => `${line}[data-line]::before, ${line}[data-line]::after`;
|
|
54
280
|
const helixTheme = EditorView.theme({
|
|
55
|
-
|
|
281
|
+
[`.cm-activeLine, ${drawn('.cm-activeLine')}`]: {
|
|
56
282
|
backgroundColor: 'color-mix(in oklab, var(--helix-color-foreground) 5%, transparent)',
|
|
57
283
|
},
|
|
284
|
+
'.cm-activeLine[data-line]::before': {
|
|
285
|
+
boxShadow: 'calc(-1 * var(--_code-editor-numbers)) 0 0 0 color-mix(in oklab, var(--helix-color-foreground) 5%, transparent)',
|
|
286
|
+
},
|
|
58
287
|
'.cm-content': {
|
|
59
288
|
caretColor: 'var(--helix-color-primary)',
|
|
60
289
|
paddingBlock: 'var(--helix-space-xs)',
|
|
61
290
|
},
|
|
62
291
|
'.cm-cursor': { borderLeftColor: 'var(--helix-color-primary)' },
|
|
63
|
-
'.cm-foldGutter .cm-gutterElement': { paddingInline: 'var(--helix-space-2xs)' },
|
|
64
292
|
'.cm-foldPlaceholder': {
|
|
65
293
|
backgroundColor: 'var(--helix-color-surface-sunken)',
|
|
66
294
|
border: 'none',
|
|
67
295
|
color: 'var(--helix-color-foreground-muted)',
|
|
68
296
|
},
|
|
69
|
-
'
|
|
297
|
+
'&.cm-helix-numbered .cm-line': { paddingInlineStart: 'var(--_code-editor-line-start)' },
|
|
298
|
+
'&.cm-helix-numbered .cm-scroller::after': {
|
|
299
|
+
content: '"›\\A⌄"',
|
|
300
|
+
order: '-1',
|
|
301
|
+
paddingInline: 'calc(var(--helix-space-2xs) + 1px)',
|
|
302
|
+
whiteSpace: 'pre',
|
|
303
|
+
},
|
|
304
|
+
'&.cm-helix-numbered .cm-scroller::before': {
|
|
305
|
+
content: 'var(--_code-editor-widest, "9")',
|
|
306
|
+
minInlineSize: '3ch',
|
|
307
|
+
order: '-2',
|
|
308
|
+
paddingInline: 'var(--helix-space-sm) var(--helix-space-2xs)',
|
|
309
|
+
whiteSpace: 'nowrap',
|
|
310
|
+
},
|
|
311
|
+
'&.cm-helix-numbered .cm-scroller::before, &.cm-helix-numbered .cm-scroller::after': {
|
|
312
|
+
blockSize: '0',
|
|
313
|
+
boxSizing: 'border-box',
|
|
314
|
+
flexShrink: '0',
|
|
315
|
+
pointerEvents: 'none',
|
|
316
|
+
userSelect: 'none',
|
|
317
|
+
visibility: 'hidden',
|
|
318
|
+
},
|
|
319
|
+
[drawn('.cm-line')]: {
|
|
320
|
+
boxSizing: 'border-box',
|
|
321
|
+
color: 'var(--helix-color-foreground-muted)',
|
|
322
|
+
float: 'inline-start',
|
|
323
|
+
minBlockSize: '1lh',
|
|
324
|
+
position: 'sticky',
|
|
325
|
+
textIndent: '0',
|
|
326
|
+
userSelect: 'none',
|
|
327
|
+
whiteSpace: 'nowrap',
|
|
328
|
+
},
|
|
329
|
+
'.cm-line[data-line]::after': {
|
|
330
|
+
content: '""',
|
|
331
|
+
inlineSize: 'var(--_code-editor-folds)',
|
|
332
|
+
insetInlineStart: 'var(--_code-editor-numbers)',
|
|
333
|
+
marginInline: 'calc(-1 * (var(--_code-editor-folds) + var(--_code-editor-line-start))) var(--_code-editor-line-start)',
|
|
334
|
+
paddingInline: 'calc(var(--helix-space-2xs) + 1px)',
|
|
335
|
+
},
|
|
336
|
+
'.cm-line[data-line]::before': {
|
|
337
|
+
content: 'attr(data-line)',
|
|
338
|
+
inlineSize: 'var(--_code-editor-numbers)',
|
|
339
|
+
insetInlineStart: '0',
|
|
340
|
+
marginInline: 'calc(-1 * (var(--_code-editor-band) + var(--_code-editor-line-start))) calc(var(--_code-editor-folds) + var(--_code-editor-line-start))',
|
|
341
|
+
paddingInline: 'var(--helix-space-sm) var(--helix-space-2xs)',
|
|
342
|
+
pointerEvents: 'none',
|
|
343
|
+
textAlign: 'end',
|
|
344
|
+
},
|
|
345
|
+
'.cm-line[data-line].cm-helix-foldable::after': { content: '"⌄"', cursor: 'pointer' },
|
|
346
|
+
'.cm-line[data-line].cm-helix-folded::after': { content: '"›"', cursor: 'pointer' },
|
|
347
|
+
'@supports (content: "1" / "")': {
|
|
348
|
+
'.cm-content .cm-line[data-line]::before': { content: 'attr(data-line) / ""' },
|
|
349
|
+
'.cm-content .cm-line[data-line].cm-helix-foldable::after': { content: '"⌄" / ""' },
|
|
350
|
+
'.cm-content .cm-line[data-line].cm-helix-folded::after': { content: '"›" / ""' },
|
|
351
|
+
},
|
|
352
|
+
[drawn('.cm-line.cm-helix-problem-danger')]: {
|
|
70
353
|
color: 'var(--helix-color-danger)',
|
|
71
354
|
fontWeight: 'var(--helix-weight-semibold)',
|
|
72
355
|
},
|
|
73
|
-
'.cm-
|
|
74
|
-
'.cm-
|
|
356
|
+
[drawn('.cm-line.cm-helix-problem-info')]: { color: 'var(--helix-color-info)' },
|
|
357
|
+
[drawn('.cm-line.cm-helix-problem-warning')]: {
|
|
75
358
|
color: 'var(--helix-color-warning)',
|
|
76
359
|
fontWeight: 'var(--helix-weight-semibold)',
|
|
77
360
|
},
|
|
78
|
-
'.cm-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
361
|
+
[drawn('.cm-lineWrapping > .cm-line')]: {
|
|
362
|
+
float: 'none',
|
|
363
|
+
insetBlock: '0',
|
|
364
|
+
margin: '0',
|
|
365
|
+
position: 'absolute',
|
|
82
366
|
},
|
|
83
|
-
'.cm-
|
|
84
|
-
|
|
85
|
-
|
|
367
|
+
'.cm-lineWrapping > .cm-line[data-line]': { position: 'relative' },
|
|
368
|
+
'.cm-lineWrapping > .cm-line[data-line]::after': {
|
|
369
|
+
insetInlineStart: 'calc(-1 * var(--_code-editor-folds))',
|
|
370
|
+
},
|
|
371
|
+
'.cm-lineWrapping > .cm-line[data-line]::before': {
|
|
372
|
+
insetInlineStart: 'calc(-1 * var(--_code-editor-band))',
|
|
86
373
|
},
|
|
87
374
|
'.cm-lintRange-error': {
|
|
88
375
|
backgroundImage: 'none',
|
|
@@ -121,11 +408,96 @@ const helixTheme = EditorView.theme({
|
|
|
121
408
|
borderRadius: 'var(--helix-radii-sm)',
|
|
122
409
|
},
|
|
123
410
|
'&': { blockSize: '100%', color: 'var(--helix-color-foreground)', fontSize: 'inherit' },
|
|
124
|
-
|
|
411
|
+
[`&:not(.cm-focused) .cm-activeLine, ${drawn('&:not(.cm-focused) .cm-activeLine')}`]: {
|
|
125
412
|
backgroundColor: 'transparent',
|
|
413
|
+
boxShadow: 'none',
|
|
126
414
|
},
|
|
127
415
|
'&.cm-focused': { outline: 'none' },
|
|
128
416
|
});
|
|
417
|
+
const spanBetween = (from, to, at = 0) => {
|
|
418
|
+
const shorter = Math.min(from.length, to.length);
|
|
419
|
+
let start = 0;
|
|
420
|
+
while (start < shorter && from.charCodeAt(start) === to.charCodeAt(start)) {
|
|
421
|
+
start += 1;
|
|
422
|
+
}
|
|
423
|
+
let end = 0;
|
|
424
|
+
while (end < shorter - start &&
|
|
425
|
+
from.charCodeAt(from.length - 1 - end) === to.charCodeAt(to.length - 1 - end)) {
|
|
426
|
+
end += 1;
|
|
427
|
+
}
|
|
428
|
+
return { from: at + start, insert: to.slice(start, to.length - end), to: at + from.length - end };
|
|
429
|
+
};
|
|
430
|
+
const linesOf = (text) => text.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
431
|
+
const COMPARED = 1_000_000;
|
|
432
|
+
export const changeBetween = (from, to) => {
|
|
433
|
+
if (from === to) {
|
|
434
|
+
return [];
|
|
435
|
+
}
|
|
436
|
+
const before = linesOf(from);
|
|
437
|
+
const after = linesOf(to);
|
|
438
|
+
let head = 0;
|
|
439
|
+
while (head < before.length && head < after.length && before[head] === after[head]) {
|
|
440
|
+
head += 1;
|
|
441
|
+
}
|
|
442
|
+
let tail = 0;
|
|
443
|
+
while (tail < before.length - head &&
|
|
444
|
+
tail < after.length - head &&
|
|
445
|
+
before[before.length - 1 - tail] === after[after.length - 1 - tail]) {
|
|
446
|
+
tail += 1;
|
|
447
|
+
}
|
|
448
|
+
const old = before.slice(head, before.length - tail);
|
|
449
|
+
const now = after.slice(head, after.length - tail);
|
|
450
|
+
const start = before.slice(0, head).join('').length;
|
|
451
|
+
if (old.length * now.length > COMPARED) {
|
|
452
|
+
return [spanBetween(old.join(''), now.join(''), start)];
|
|
453
|
+
}
|
|
454
|
+
const shared = Array.from({ length: old.length + 1 }, () => new Array(now.length + 1).fill(0));
|
|
455
|
+
for (let i = old.length - 1; i >= 0; i -= 1) {
|
|
456
|
+
for (let j = now.length - 1; j >= 0; j -= 1) {
|
|
457
|
+
const row = shared[i];
|
|
458
|
+
row[j] =
|
|
459
|
+
old[i] === now[j]
|
|
460
|
+
? shared[i + 1][j + 1] + 1
|
|
461
|
+
: Math.max(shared[i + 1][j], row[j + 1]);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
const replacements = [];
|
|
465
|
+
let i = 0;
|
|
466
|
+
let j = 0;
|
|
467
|
+
let at = start;
|
|
468
|
+
let runFrom = at;
|
|
469
|
+
let removed = '';
|
|
470
|
+
let added = '';
|
|
471
|
+
const close = () => {
|
|
472
|
+
if (removed !== '' || added !== '') {
|
|
473
|
+
replacements.push(spanBetween(removed, added, runFrom));
|
|
474
|
+
}
|
|
475
|
+
removed = '';
|
|
476
|
+
added = '';
|
|
477
|
+
};
|
|
478
|
+
while (i < old.length || j < now.length) {
|
|
479
|
+
if (i < old.length && j < now.length && old[i] === now[j]) {
|
|
480
|
+
close();
|
|
481
|
+
at += old[i].length;
|
|
482
|
+
runFrom = at;
|
|
483
|
+
i += 1;
|
|
484
|
+
j += 1;
|
|
485
|
+
}
|
|
486
|
+
else if (j >= now.length ||
|
|
487
|
+
(i < old.length &&
|
|
488
|
+
shared[i + 1][j] >= shared[i][j + 1])) {
|
|
489
|
+
removed += old[i];
|
|
490
|
+
at += old[i].length;
|
|
491
|
+
i += 1;
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
added += now[j];
|
|
495
|
+
j += 1;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
close();
|
|
499
|
+
return replacements;
|
|
500
|
+
};
|
|
129
501
|
const SEVERITY = { danger: 'error', info: 'info', warning: 'warning' };
|
|
130
502
|
const lintOf = (state, length) => state.diagnostics.map((one) => ({
|
|
131
503
|
from: Math.min(Math.max(0, one.from), length),
|
|
@@ -133,18 +505,8 @@ const lintOf = (state, length) => state.diagnostics.map((one) => ({
|
|
|
133
505
|
severity: SEVERITY[one.tone ?? 'danger'],
|
|
134
506
|
to: Math.min(Math.max(one.from, one.to), length),
|
|
135
507
|
}));
|
|
136
|
-
class ProblemLine extends GutterMarker {
|
|
137
|
-
constructor(tone) {
|
|
138
|
-
super();
|
|
139
|
-
this.tone = tone;
|
|
140
|
-
this.elementClass = `cm-helix-problem-${tone}`;
|
|
141
|
-
}
|
|
142
|
-
eq(other) {
|
|
143
|
-
return other.tone === this.tone;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
508
|
const RANK = { danger: 2, info: 0, warning: 1 };
|
|
147
|
-
const problemLines = (
|
|
509
|
+
const problemLines = (diagnostics) => EditorView.decorations.compute(['doc'], (state) => {
|
|
148
510
|
const worst = new Map();
|
|
149
511
|
for (const one of diagnostics) {
|
|
150
512
|
const line = state.doc.lineAt(Math.min(Math.max(0, one.from), state.doc.length)).from;
|
|
@@ -154,11 +516,10 @@ const problemLines = (state, diagnostics) => {
|
|
|
154
516
|
worst.set(line, tone);
|
|
155
517
|
}
|
|
156
518
|
}
|
|
157
|
-
|
|
519
|
+
return RangeSet.of([...worst]
|
|
158
520
|
.sort(([a], [b]) => a - b)
|
|
159
|
-
.map(([from, tone]) =>
|
|
160
|
-
|
|
161
|
-
};
|
|
521
|
+
.map(([from, tone]) => Decoration.line({ class: `cm-helix-problem-${tone}` }).range(from)));
|
|
522
|
+
});
|
|
162
523
|
const queryOf = (query) => new SearchQuery({
|
|
163
524
|
caseSensitive: query.caseSensitive,
|
|
164
525
|
literal: !query.regex,
|
|
@@ -210,6 +571,7 @@ export const codeMirrorEngine = (options = {}) => ({
|
|
|
210
571
|
const words = new Compartment();
|
|
211
572
|
const hint = new Compartment();
|
|
212
573
|
const problems = new Compartment();
|
|
574
|
+
const flashes = new Compartment();
|
|
213
575
|
let current = state;
|
|
214
576
|
let asked = '';
|
|
215
577
|
const load = (name, view) => {
|
|
@@ -231,7 +593,7 @@ export const codeMirrorEngine = (options = {}) => ({
|
|
|
231
593
|
state: EditorState.create({
|
|
232
594
|
doc: state.value,
|
|
233
595
|
extensions: [
|
|
234
|
-
numbers.of(state.lineNumbers ?
|
|
596
|
+
numbers.of(state.lineNumbers ? numbering : []),
|
|
235
597
|
remeasureOnFonts,
|
|
236
598
|
history(),
|
|
237
599
|
drawSelection(),
|
|
@@ -247,12 +609,16 @@ export const codeMirrorEngine = (options = {}) => ({
|
|
|
247
609
|
return { dom, top: true };
|
|
248
610
|
},
|
|
249
611
|
}),
|
|
250
|
-
problems.of(problemLines(
|
|
612
|
+
problems.of(problemLines(state.diagnostics)),
|
|
251
613
|
indentUnit.of(' '),
|
|
252
614
|
EditorState.tabSize.of(2),
|
|
253
615
|
keymap.of([...defaultKeymap, ...historyKeymap, ...foldKeymap, indentWithTab]),
|
|
254
616
|
syntaxHighlighting(syntaxHighlighter, { fallback: true }),
|
|
255
617
|
helixTheme,
|
|
618
|
+
flashTheme,
|
|
619
|
+
flashedLines,
|
|
620
|
+
unflashLater,
|
|
621
|
+
flashes.of(flashing.of(state.flashChanges)),
|
|
256
622
|
language.of([]),
|
|
257
623
|
editable.of(EditorState.readOnly.of(state.readOnly)),
|
|
258
624
|
wrapping.of(state.wrap ? EditorView.lineWrapping : []),
|
|
@@ -337,7 +703,7 @@ export const codeMirrorEngine = (options = {}) => ({
|
|
|
337
703
|
update: (next) => {
|
|
338
704
|
const effects = [];
|
|
339
705
|
if (next.lineNumbers !== current.lineNumbers) {
|
|
340
|
-
effects.push(numbers.reconfigure(next.lineNumbers ?
|
|
706
|
+
effects.push(numbers.reconfigure(next.lineNumbers ? numbering : []));
|
|
341
707
|
}
|
|
342
708
|
if (next.readOnly !== current.readOnly) {
|
|
343
709
|
effects.push(editable.reconfigure(EditorState.readOnly.of(next.readOnly)));
|
|
@@ -354,18 +720,19 @@ export const codeMirrorEngine = (options = {}) => ({
|
|
|
354
720
|
if (next.placeholder !== current.placeholder) {
|
|
355
721
|
effects.push(hint.reconfigure(next.placeholder ? placeholder(next.placeholder) : []));
|
|
356
722
|
}
|
|
723
|
+
if (next.flashChanges !== current.flashChanges) {
|
|
724
|
+
effects.push(flashes.reconfigure(flashing.of(next.flashChanges)));
|
|
725
|
+
}
|
|
357
726
|
const doc = view.state.doc.toString();
|
|
358
727
|
view.dispatch({
|
|
359
728
|
annotations: given.of(true),
|
|
360
|
-
|
|
361
|
-
? {}
|
|
362
|
-
: { changes: { from: 0, insert: next.value, to: doc.length } }),
|
|
729
|
+
changes: changeBetween(doc, next.value),
|
|
363
730
|
effects,
|
|
364
731
|
});
|
|
365
732
|
if (next.diagnostics !== current.diagnostics || next.value !== doc) {
|
|
366
733
|
view.dispatch(setDiagnostics(view.state, lintOf(next, view.state.doc.length)));
|
|
367
734
|
view.dispatch({
|
|
368
|
-
effects: problems.reconfigure(problemLines(
|
|
735
|
+
effects: problems.reconfigure(problemLines(next.diagnostics)),
|
|
369
736
|
});
|
|
370
737
|
}
|
|
371
738
|
if (next.language !== current.language) {
|
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.
|
|
4
|
+
"version": "0.247.1",
|
|
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.
|
|
50
|
-
"@fusion.dev/helix-sandbox": "0.
|
|
49
|
+
"@fusion.dev/helix-headless": "0.247.1",
|
|
50
|
+
"@fusion.dev/helix-sandbox": "0.247.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@fusion.dev/helix-testing": "0.
|
|
53
|
+
"@fusion.dev/helix-testing": "0.247.1",
|
|
54
54
|
"@types/bun": "^1.4.4"
|
|
55
55
|
},
|
|
56
56
|
"homepage": "https://fusion.dev",
|