@fusion.dev/helix-code-editor 0.247.0 → 0.248.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.
@@ -1,9 +1,9 @@
1
1
  import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
2
- import { bracketMatching, codeFolding, foldGutter, foldKeymap, indentOnInput, indentUnit, StreamLanguage, syntaxHighlighting, } from '@codemirror/language';
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, Facet, RangeSet, StateEffect, StateField, Transaction, } from '@codemirror/state';
6
- import { Decoration, drawSelection, EditorView, GutterMarker, gutterLineClass, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, placeholder, ViewPlugin, } from '@codemirror/view';
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,6 +50,92 @@ 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();
54
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))';
55
141
  const flashing = Facet.define({ combine: (values) => values.some(Boolean) });
@@ -190,38 +276,100 @@ const flashTheme = EditorView.theme({
190
276
  },
191
277
  },
192
278
  });
279
+ const drawn = (line) => `${line}[data-line]::before, ${line}[data-line]::after`;
193
280
  const helixTheme = EditorView.theme({
194
- '.cm-activeLine, .cm-activeLineGutter': {
281
+ [`.cm-activeLine, ${drawn('.cm-activeLine')}`]: {
195
282
  backgroundColor: 'color-mix(in oklab, var(--helix-color-foreground) 5%, transparent)',
196
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
+ },
197
287
  '.cm-content': {
198
288
  caretColor: 'var(--helix-color-primary)',
199
289
  paddingBlock: 'var(--helix-space-xs)',
200
290
  },
201
291
  '.cm-cursor': { borderLeftColor: 'var(--helix-color-primary)' },
202
- '.cm-foldGutter .cm-gutterElement': { paddingInline: 'var(--helix-space-2xs)' },
203
292
  '.cm-foldPlaceholder': {
204
293
  backgroundColor: 'var(--helix-color-surface-sunken)',
205
294
  border: 'none',
206
295
  color: 'var(--helix-color-foreground-muted)',
207
296
  },
208
- '.cm-gutterElement.cm-helix-problem-danger': {
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')]: {
209
353
  color: 'var(--helix-color-danger)',
210
354
  fontWeight: 'var(--helix-weight-semibold)',
211
355
  },
212
- '.cm-gutterElement.cm-helix-problem-info': { color: 'var(--helix-color-info)' },
213
- '.cm-gutterElement.cm-helix-problem-warning': {
356
+ [drawn('.cm-line.cm-helix-problem-info')]: { color: 'var(--helix-color-info)' },
357
+ [drawn('.cm-line.cm-helix-problem-warning')]: {
214
358
  color: 'var(--helix-color-warning)',
215
359
  fontWeight: 'var(--helix-weight-semibold)',
216
360
  },
217
- '.cm-gutters': {
218
- backgroundColor: 'transparent',
219
- border: 'none',
220
- color: 'var(--helix-color-foreground-muted)',
361
+ [drawn('.cm-lineWrapping > .cm-line')]: {
362
+ float: 'none',
363
+ insetBlock: '0',
364
+ margin: '0',
365
+ position: 'absolute',
221
366
  },
222
- '.cm-lineNumbers .cm-gutterElement': {
223
- minInlineSize: '3ch',
224
- paddingInline: 'var(--helix-space-sm) var(--helix-space-2xs)',
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))',
225
373
  },
226
374
  '.cm-lintRange-error': {
227
375
  backgroundImage: 'none',
@@ -260,8 +408,9 @@ const helixTheme = EditorView.theme({
260
408
  borderRadius: 'var(--helix-radii-sm)',
261
409
  },
262
410
  '&': { blockSize: '100%', color: 'var(--helix-color-foreground)', fontSize: 'inherit' },
263
- '&:not(.cm-focused) .cm-activeLine, &:not(.cm-focused) .cm-activeLineGutter': {
411
+ [`&:not(.cm-focused) .cm-activeLine, ${drawn('&:not(.cm-focused) .cm-activeLine')}`]: {
264
412
  backgroundColor: 'transparent',
413
+ boxShadow: 'none',
265
414
  },
266
415
  '&.cm-focused': { outline: 'none' },
267
416
  });
@@ -356,18 +505,8 @@ const lintOf = (state, length) => state.diagnostics.map((one) => ({
356
505
  severity: SEVERITY[one.tone ?? 'danger'],
357
506
  to: Math.min(Math.max(one.from, one.to), length),
358
507
  }));
359
- class ProblemLine extends GutterMarker {
360
- constructor(tone) {
361
- super();
362
- this.tone = tone;
363
- this.elementClass = `cm-helix-problem-${tone}`;
364
- }
365
- eq(other) {
366
- return other.tone === this.tone;
367
- }
368
- }
369
508
  const RANK = { danger: 2, info: 0, warning: 1 };
370
- const problemLines = (state, diagnostics) => {
509
+ const problemLines = (diagnostics) => EditorView.decorations.compute(['doc'], (state) => {
371
510
  const worst = new Map();
372
511
  for (const one of diagnostics) {
373
512
  const line = state.doc.lineAt(Math.min(Math.max(0, one.from), state.doc.length)).from;
@@ -377,11 +516,10 @@ const problemLines = (state, diagnostics) => {
377
516
  worst.set(line, tone);
378
517
  }
379
518
  }
380
- const marks = [...worst]
519
+ return RangeSet.of([...worst]
381
520
  .sort(([a], [b]) => a - b)
382
- .map(([from, tone]) => new ProblemLine(tone).range(from));
383
- return gutterLineClass.of(RangeSet.of(marks));
384
- };
521
+ .map(([from, tone]) => Decoration.line({ class: `cm-helix-problem-${tone}` }).range(from)));
522
+ });
385
523
  const queryOf = (query) => new SearchQuery({
386
524
  caseSensitive: query.caseSensitive,
387
525
  literal: !query.regex,
@@ -455,7 +593,7 @@ export const codeMirrorEngine = (options = {}) => ({
455
593
  state: EditorState.create({
456
594
  doc: state.value,
457
595
  extensions: [
458
- numbers.of(state.lineNumbers ? [lineNumbers(), highlightActiveLineGutter(), foldGutter()] : []),
596
+ numbers.of(state.lineNumbers ? numbering : []),
459
597
  remeasureOnFonts,
460
598
  history(),
461
599
  drawSelection(),
@@ -471,7 +609,7 @@ export const codeMirrorEngine = (options = {}) => ({
471
609
  return { dom, top: true };
472
610
  },
473
611
  }),
474
- problems.of(problemLines(EditorState.create({ doc: state.value }), state.diagnostics)),
612
+ problems.of(problemLines(state.diagnostics)),
475
613
  indentUnit.of(' '),
476
614
  EditorState.tabSize.of(2),
477
615
  keymap.of([...defaultKeymap, ...historyKeymap, ...foldKeymap, indentWithTab]),
@@ -565,7 +703,7 @@ export const codeMirrorEngine = (options = {}) => ({
565
703
  update: (next) => {
566
704
  const effects = [];
567
705
  if (next.lineNumbers !== current.lineNumbers) {
568
- effects.push(numbers.reconfigure(next.lineNumbers ? [lineNumbers(), highlightActiveLineGutter(), foldGutter()] : []));
706
+ effects.push(numbers.reconfigure(next.lineNumbers ? numbering : []));
569
707
  }
570
708
  if (next.readOnly !== current.readOnly) {
571
709
  effects.push(editable.reconfigure(EditorState.readOnly.of(next.readOnly)));
@@ -594,7 +732,7 @@ export const codeMirrorEngine = (options = {}) => ({
594
732
  if (next.diagnostics !== current.diagnostics || next.value !== doc) {
595
733
  view.dispatch(setDiagnostics(view.state, lintOf(next, view.state.doc.length)));
596
734
  view.dispatch({
597
- effects: problems.reconfigure(problemLines(view.state, next.diagnostics)),
735
+ effects: problems.reconfigure(problemLines(next.diagnostics)),
598
736
  });
599
737
  }
600
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.247.0",
4
+ "version": "0.248.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.247.0",
50
- "@fusion.dev/helix-sandbox": "0.247.0"
49
+ "@fusion.dev/helix-headless": "0.248.0",
50
+ "@fusion.dev/helix-sandbox": "0.248.0"
51
51
  },
52
52
  "devDependencies": {
53
- "@fusion.dev/helix-testing": "0.247.0",
53
+ "@fusion.dev/helix-testing": "0.248.0",
54
54
  "@types/bun": "^1.4.4"
55
55
  },
56
56
  "homepage": "https://fusion.dev",