import { ViewPlugin, Decoration, EditorView, keymap } from '@codemirror/view'; import type { DecorationSet, ViewUpdate } from '@codemirror/view'; import { RangeSetBuilder } from '@codemirror/state'; import { indentUnit } from '@codemirror/language'; import { indentWithTab } from '@codemirror/commands'; import { python } from '@codemirror/lang-python'; /** * Visual decoration marks for indentation spaces. * Primary: The first space of every 4-space indent block (column 0, 4, 8, etc.) or tabs. * Secondary: Intermediate spaces within the indent block. */ const indentDotPrimary = Decoration.mark({ class: 'cm-indent-dot cm-indent-dot-primary' }); const indentDotSecondary = Decoration.mark({ class: 'cm-indent-dot cm-indent-dot-secondary' }); /** * Scans visible editor ranges and applies indentation dot decorations to leading whitespace. */ function buildIndentDecorations(view: EditorView): DecorationSet { const builder = new RangeSetBuilder(); for (const { from, to } of view.visibleRanges) { let pos = from; while (pos < to) { const line = view.state.doc.lineAt(pos); const text = line.text; let i = 0; while (i < text.length && (text[i] === ' ' || text[i] === '\t')) { const charFrom = line.from + i; const charTo = charFrom + 1; const isPrimary = i % 4 === 0 || text[i] === '\t'; builder.add(charFrom, charTo, isPrimary ? indentDotPrimary : indentDotSecondary); i++; } pos = line.to + 1; } } return builder.finish(); } /** * ViewPlugin that manages and updates indentation guide dots when document or viewport changes. */ export const indentDotsExtension = ViewPlugin.fromClass( class { decorations: DecorationSet; constructor(view: EditorView) { this.decorations = buildIndentDecorations(view); } update(update: ViewUpdate) { if (update.docChanged || update.viewportChanged) { this.decorations = buildIndentDecorations(update.view); } } }, { decorations: (v) => v.decorations, } ); /** * Cyber retro-arcade custom theme for the Daily Review Code Studio. */ export const codeStudioTheme = EditorView.theme({ '&': { backgroundColor: '#0c111d', color: '#f1f5f9', height: '100%', fontSize: '13px', fontFamily: 'var(--font-code, "JetBrains Mono", "Fira Code", monospace)', }, '.cm-scroller': { overflow: 'auto', fontFamily: 'inherit', lineHeight: '1.65', }, '.cm-content': { caretColor: '#4ade80', padding: '12px 0', }, '&.cm-focused .cm-cursor': { borderLeftColor: '#4ade80', borderLeftWidth: '2px', filter: 'drop-shadow(0 0 5px rgba(74, 222, 128, 0.8))', }, '&.cm-focused .cm-selectionBackground, .cm-selectionBackground, ::selection': { backgroundColor: 'rgba(56, 189, 248, 0.25) !important', }, '.cm-gutters': { backgroundColor: '#070a12', color: '#475569', borderRight: '2px solid #1e293b', paddingRight: '6px', userSelect: 'none', }, '.cm-activeLineGutter': { backgroundColor: '#131b2e', color: '#38bdf8', fontWeight: '700', }, '.cm-activeLine, &.cm-focused .cm-activeLine': { backgroundColor: '#0f172a !important', }, '.cm-line': { padding: '0 10px', }, // Indentation Dot glyphs '.cm-indent-dot': { position: 'relative', display: 'inline-block', }, '.cm-indent-dot::before': { position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', pointerEvents: 'none', userSelect: 'none', lineHeight: '0', fontFamily: 'monospace', }, '.cm-indent-dot-primary::before': { content: '"•"', color: '#38bdf8', fontSize: '9px', opacity: '0.85', }, '.cm-indent-dot-secondary::before': { content: '"·"', color: '#64748b', fontSize: '11px', opacity: '0.5', }, // Matching brackets '&.cm-focused .cm-matchingBracket, .cm-matchingBracket': { backgroundColor: 'rgba(74, 222, 128, 0.2)', outline: '1px solid #4ade80', borderRadius: '2px', color: '#86efac !important', }, // Placeholder styling '.cm-placeholder': { color: '#64748b', fontStyle: 'italic', }, // Custom scrollbar styling '.cm-scroller::-webkit-scrollbar': { width: '6px', height: '6px', }, '.cm-scroller::-webkit-scrollbar-track': { backgroundColor: '#070a12', }, '.cm-scroller::-webkit-scrollbar-thumb': { backgroundColor: '#1e293b', borderRadius: '3px', border: '1px solid #334155', }, '.cm-scroller::-webkit-scrollbar-thumb:hover': { backgroundColor: '#38bdf8', }, }); /** * Standardized CodeMirror extensions for CodeStudio: * - 4-space indentation for Python * - Tab key navigation for indent/dedent * - Indentation guide dots * - Cyber arcade editor theme * - Python language support * - Accessible ARIA label for screen readers */ export const codeStudioExtensions = [ python(), indentUnit.of(' '), keymap.of([indentWithTab]), indentDotsExtension, codeStudioTheme, EditorView.contentAttributes.of({ 'aria-label': 'Python Code Editor' }), ];