File size: 13,193 Bytes
a1b6014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands'
import { bracketMatching, indentOnInput, LanguageDescription } from '@codemirror/language'
import { languages } from '@codemirror/language-data'
import { Compartment, EditorState } from '@codemirror/state'
import { Decoration, drawSelection, EditorView, keymap, lineNumbers } from '@codemirror/view'
import { type RefObject, useEffect, useRef } from 'react'

import { tryFormatJson } from '@/lib/json-format'
import { cn } from '@/lib/utils'
import { useTheme } from '@/themes/context'

import { githubEditorTheme } from './code-editor-theme'

type FormatOutcome = { ok: true } | { ok: false; error: string }

function applyFormatJson(view: EditorView, onError?: (error: string) => void): FormatOutcome {
  const text = view.state.doc.toString()
  const result = tryFormatJson(text)

  if (!result.ok) {
    onError?.(result.error)

    return result
  }

  if (result.text !== text) {
    view.dispatch({ changes: { from: 0, insert: result.text, to: view.state.doc.length } })
  }

  return { ok: true }
}

/** Imperative surface for callers that drive selection from outside (e.g. a
 *  config list focusing its block in the document). */
export interface CodeEditorApi {
  formatJson: () => FormatOutcome
  setCursor: (pos: number) => void
}

interface CodeEditorProps {
  apiRef?: RefObject<CodeEditorApi | null>
  className?: string
  /** Read-only: block edits (e.g. while a save is in flight) without unmounting. */
  disabled?: boolean
  /** Mod-Shift-F + `apiRef.formatJson()`. In-memory JSON docs only. */
  formatJson?: boolean
  /**
   * Standalone chrome: rounded border on an outer shell. The CodeMirror surface
   * inside is identical to pane previews (no extra inset). Off by default.
   */
  framed?: boolean
  filePath: string
  /** Character range to wash with a subtle background (the "you are here" block). */
  highlight?: null | { from: number; to: number }
  // Read once at mount. To load a different file or discard edits, remount the
  // component (give it a new React `key`) rather than pushing a new value in.
  initialValue: string
  onCancel?: () => void
  onChange: (value: string) => void
  /** Button or Mod-Shift-F. */
  onFormatJsonError?: (error: string) => void
  /** Fires with the primary cursor offset whenever the selection moves. */
  onCursorChange?: (pos: number) => void
  onSave?: () => void
}

// Focus treatment for the active range: a subtle wash on its lines, and
// everything OUTSIDE dimmed — the document recedes so the block you're in
// reads as "you are here".
function blockHighlight(range: { from: number; to: number }) {
  return EditorView.decorations.compute([], state => {
    const clamp = (pos: number) => Math.max(0, Math.min(pos, state.doc.length))
    const active = Decoration.line({ class: 'cm-hermes-active-block' })
    // Inline style, not a theme class: theme rules are scoped per-extension
    // and line opacity must never lose that fight.
    const dimmed = Decoration.line({ attributes: { style: 'opacity:0.5;transition:opacity 120ms ease-out' } })
    const first = state.doc.lineAt(clamp(range.from)).number
    const last = state.doc.lineAt(clamp(range.to)).number
    const marks = []

    for (let n = 1; n <= state.doc.lines; n++) {
      marks.push((n >= first && n <= last ? active : dimmed).range(state.doc.line(n).from))
    }

    return Decoration.set(marks)
  })
}

function baseName(filePath: string): string {
  const cleaned = filePath.replace(/[\\/]+$/, '')

  return (
    cleaned
      .slice(cleaned.lastIndexOf('/') + 1)
      .split('\\')
      .pop() ?? cleaned
  )
}

// Mirror SourceView's geometry/typography 1:1 so toggling preview⇄edit never
// shifts the file. CM's base stylesheet targets some of these with two-class
// selectors (e.g. `.cm-lineNumbers .cm-gutterElement`) that out-specify a bare
// `.cm-gutterElement` rule, so we match that specificity to win. SourceView
// reference: font var(--font-mono)/0.7rem/400, 1.25rem rows, gutter w-9 + pr-2
// (muted/55), code 0.625rem line inset.
const MONO_FONT = 'var(--font-mono)'
const ROW_HEIGHT = '1.25rem'
const CODE_SIZE = '0.7rem'
const GUTTER_COLOR = 'color-mix(in oklab, var(--muted-foreground) 55%, transparent)'

const LAYOUT_THEME = EditorView.theme({
  '&': {
    WebkitFontSmoothing: 'antialiased',
    backgroundColor: 'transparent',
    height: '100%'
  },
  // CM's base theme ships `.cm-content { padding: 4px 0 }` (~5px top/bottom).
  // Zero it explicitly so pane + framed interiors match SourceView flush-top.
  '.cm-content': {
    fontFamily: MONO_FONT,
    fontSize: CODE_SIZE,
    fontWeight: '400',
    lineHeight: ROW_HEIGHT,
    padding: '0',
    paddingBottom: '0',
    paddingTop: '0'
  },
  '.cm-gutters': {
    backgroundColor: 'transparent',
    border: 'none',
    color: GUTTER_COLOR,
    fontFamily: MONO_FONT,
    fontSize: CODE_SIZE
  },
  // Two-class selector to beat CM's base `.cm-lineNumbers .cm-gutterElement`.
  '.cm-lineNumbers .cm-gutterElement': {
    boxSizing: 'border-box',
    fontVariantNumeric: 'tabular-nums',
    fontWeight: '400',
    lineHeight: ROW_HEIGHT,
    minWidth: '2.25rem',
    padding: '0 0.5rem 0 0',
    textAlign: 'right'
  },
  '.cm-line': {
    fontFamily: MONO_FONT,
    fontSize: CODE_SIZE,
    fontWeight: '400',
    lineHeight: ROW_HEIGHT,
    padding: '0 0.625rem'
  },
  '.cm-scroller': {
    fontFamily: MONO_FONT,
    fontSize: CODE_SIZE,
    lineHeight: ROW_HEIGHT,
    overflow: 'auto'
  },
  '.cm-hermes-active-block': {
    backgroundColor: 'color-mix(in srgb, var(--dt-foreground) 5%, transparent)'
  }
})

// Framed = prose editing (SOUL.md, skills, memories): no line-number gutter (it
// shoved text right and made the left inset dwarf the top), and zero the line's
// own horizontal padding so the host's uniform `p-2` is the ONLY inset — even
// breathing room on all four sides. Long lines wrap rather than scroll.
const FRAMED_THEME = EditorView.theme({
  '.cm-line': { padding: '0' }
})

// A deliberately small CodeMirror 6 surface for *spot edits* — not an IDE: line
// numbers, history, selection, bracket matching, syntax highlighting. No fold
// gutter, autocomplete, or active-line chrome, so it reads like the preview it
// replaces. It owns its own buffer; the parent tracks dirty via `onChange` and
// resets by remounting. ⌘/Ctrl+S and ⌘/Ctrl+Enter save; Esc cancels; the app's
// light/dark mode is followed live without losing the cursor.
export function CodeEditor({
  apiRef,
  className,
  disabled = false,
  formatJson = false,
  framed = false,
  filePath,
  highlight,
  initialValue,
  onCancel,
  onChange,
  onCursorChange,
  onFormatJsonError,
  onSave
}: CodeEditorProps) {
  const { resolvedMode } = useTheme()
  const hostRef = useRef<HTMLDivElement | null>(null)
  const viewRef = useRef<EditorView | null>(null)
  const languageConf = useRef(new Compartment())
  const themeConf = useRef(new Compartment())
  const highlightConf = useRef(new Compartment())
  const editableConf = useRef(new Compartment())
  const onCancelRef = useRef(onCancel)
  const onChangeRef = useRef(onChange)
  const onCursorChangeRef = useRef(onCursorChange)
  const onFormatJsonErrorRef = useRef(onFormatJsonError)
  const onSaveRef = useRef(onSave)
  const formatJsonRef = useRef(formatJson)
  onCancelRef.current = onCancel
  onChangeRef.current = onChange
  onCursorChangeRef.current = onCursorChange
  onFormatJsonErrorRef.current = onFormatJsonError
  onSaveRef.current = onSave
  formatJsonRef.current = formatJson

  // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
  useEffect(() => {
    const host = hostRef.current

    if (!host) {
      return
    }

    const isDark = resolvedMode === 'dark'

    const save = () => {
      onSaveRef.current?.()

      return true
    }

    const runFormatJson = () => {
      if (!formatJsonRef.current || !viewRef.current) {
        return false
      }

      applyFormatJson(viewRef.current, error => onFormatJsonErrorRef.current?.(error))

      return true
    }

    const state = EditorState.create({
      doc: initialValue,
      extensions: [
        // Gutter only outside framed mode — framed prose reads better flush.
        ...(framed ? [] : [lineNumbers()]),
        history(),
        drawSelection(),
        indentOnInput(),
        bracketMatching(),
        keymap.of([
          ...defaultKeymap,
          ...historyKeymap,
          indentWithTab,
          { key: 'Mod-s', preventDefault: true, run: save },
          { key: 'Mod-Enter', preventDefault: true, run: save },
          ...(formatJson ? [{ key: 'Mod-Shift-f', preventDefault: true, run: runFormatJson }] : []),
          {
            key: 'Escape',
            run: () => {
              if (!onCancelRef.current) {
                return false
              }

              onCancelRef.current()

              return true
            }
          }
        ]),
        languageConf.current.of([]),
        themeConf.current.of(githubEditorTheme(isDark)),
        highlightConf.current.of([]),
        editableConf.current.of(EditorState.readOnly.of(disabled)),
        EditorView.updateListener.of(update => {
          if (update.docChanged) {
            onChangeRef.current(update.state.doc.toString())
          }

          if (update.selectionSet || update.docChanged) {
            onCursorChangeRef.current?.(update.state.selection.main.head)
          }
        }),
        LAYOUT_THEME,
        // Standalone edits (SOUL.md, skills, memories) are prose, not code —
        // wrap long lines instead of scrolling horizontally, and drop the gutter
        // inset. Pane previews stay flush/scrolling to mirror their SourceView.
        ...(framed ? [EditorView.lineWrapping, FRAMED_THEME] : [])
      ]
    })

    const view = new EditorView({ parent: host, state })
    viewRef.current = view

    if (apiRef) {
      apiRef.current = {
        formatJson: () => {
          const view = viewRef.current

          if (!view || !formatJsonRef.current) {
            return { ok: false, error: 'JSON formatting is not enabled for this editor' }
          }

          return applyFormatJson(view)
        },
        setCursor: pos => {
          const clamped = Math.max(0, Math.min(pos, view.state.doc.length))
          view.dispatch({ scrollIntoView: true, selection: { anchor: clamped } })
          view.focus()
        }
      }
    }

    // Focus on mount so entering edit mode (button or double-click) lands the
    // caret in the buffer ready to type, no extra click required.
    view.focus()

    return () => {
      view.destroy()
      viewRef.current = null

      if (apiRef) {
        apiRef.current = null
      }
    }
    // Created once per mount; the parent remounts (via `key`) to load a new
    // file or discard. Theme/language are applied reactively below.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  // Load + apply syntax highlighting for the file's language (lazy per language).
  useEffect(() => {
    let cancelled = false
    const description = LanguageDescription.matchFilename(languages, baseName(filePath))

    if (!description) {
      viewRef.current?.dispatch({ effects: languageConf.current.reconfigure([]) })

      return
    }

    void description.load().then(support => {
      if (!cancelled && viewRef.current) {
        viewRef.current.dispatch({ effects: languageConf.current.reconfigure(support) })
      }
    })

    return () => {
      cancelled = true
    }
  }, [filePath])

  useEffect(() => {
    viewRef.current?.dispatch({
      effects: themeConf.current.reconfigure(githubEditorTheme(resolvedMode === 'dark'))
    })
  }, [resolvedMode])

  const highlightFrom = highlight?.from
  const highlightTo = highlight?.to

  useEffect(() => {
    viewRef.current?.dispatch({
      effects: highlightConf.current.reconfigure(
        highlightFrom !== undefined && highlightTo !== undefined
          ? blockHighlight({ from: highlightFrom, to: highlightTo })
          : []
      )
    })
  }, [highlightFrom, highlightTo])

  useEffect(() => {
    viewRef.current?.dispatch({ effects: editableConf.current.reconfigure(EditorState.readOnly.of(disabled)) })
  }, [disabled])

  if (!framed) {
    return <div className={cn('h-full min-h-0 overflow-hidden', className)} ref={hostRef} />
  }

  // Border on the shell only — inner body matches preview-file / DetailPane:
  // <div className="min-h-0 flex-1 overflow-hidden"><CodeEditor /></div>
  return (
    <div
      className={cn(
        'flex h-full min-h-0 flex-col overflow-hidden rounded-md border border-(--ui-stroke-tertiary)',
        className
      )}
    >
      {/* Padding lives on the CM *mount node* itself — outside CodeMirror's
          DOM entirely, so its `.cm-content { padding: 0 }` can't fight it. This
          is why every prior attempt (Tailwind on .cm-content, scroller padding)
          lost: they targeted CM-owned nodes. This div isn't one. */}
      <div className="min-h-0 flex-1 overflow-hidden p-2" ref={hostRef} />
    </div>
  )
}