Spaces:
Running
Running
File size: 5,142 Bytes
b606b67 b53e6f3 b606b67 b53e6f3 b606b67 b53e6f3 b606b67 | 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 | 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<Decoration>();
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' }),
];
|