-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
210 lines (189 loc) · 7.47 KB
/
editor.js
File metadata and controls
210 lines (189 loc) · 7.47 KB
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
/*
* SPDX-FileCopyrightText: 2026 John Samuel <johnsamuelwrites@gmail.com>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
"use strict";
import { EXAMPLES, EXAMPLES_MEDIUM, EXAMPLES_ADVANCED } from './examples.js';
/* ── DOM refs used by the editor module ─────────────────────────── */
const $lang = document.getElementById('lang-select');
const $exampleSel = document.getElementById('example-select');
const $badge = document.getElementById('lang-badge');
const $exampleLink = document.getElementById('complete-features-link');
/* ── CodeMirror instance ─────────────────────────────────────────── */
const currentTheme = document.documentElement.getAttribute('data-theme');
export const editor = CodeMirror.fromTextArea(
document.getElementById('code-area'),
{
mode: 'python',
theme: currentTheme === 'dark' ? 'dracula' : 'eclipse',
lineNumbers: true,
indentUnit: 4, tabSize: 4, indentWithTabs: false,
lineWrapping: false, autofocus: true,
// extraKeys (Ctrl-Enter / Cmd-Enter) are wired in main.js to avoid
// a circular import with runtime.js.
}
);
editor.getWrapperElement().setAttribute('role', 'textbox');
editor.getWrapperElement().setAttribute('aria-multiline', 'true');
editor.getWrapperElement().setAttribute('aria-labelledby', 'editor-pane-title');
editor.getInputField().setAttribute('aria-label', 'Source code editor');
export function resizeEditor() {
const w = document.getElementById('editor-wrapper');
editor.setSize('100%', w.clientHeight);
}
window.addEventListener('resize', resizeEditor);
setTimeout(resizeEditor, 60);
/* ── Example loading ─────────────────────────────────────────────── */
export function loadExample(lang, level) {
const map = level === 'advanced' ? EXAMPLES_ADVANCED
: level === 'medium' ? EXAMPLES_MEDIUM
: EXAMPLES;
editor.setValue(map[lang] || map.en);
}
export function loadLang(lang) {
const level = $exampleSel ? $exampleSel.value : 'basics';
loadExample(lang, level);
$badge.textContent = lang;
const isRtl = lang === 'ar';
const wrapper = editor.getWrapperElement();
wrapper.style.direction = 'ltr';
wrapper.classList.toggle('editor-rtl', isRtl);
editor.setOption('direction', isRtl ? 'rtl' : 'ltr');
editor.setOption('lineWrapping', isRtl);
applyEditorHighlighting(lang);
updateCompleteFeaturesLink(lang);
editor.refresh();
}
function updateCompleteFeaturesLink(lang) {
if (!$exampleLink) return;
const langCode = lang || 'en';
$exampleLink.href = `https://github.com/johnsamuelwrites/multilingual/blob/main/examples/complete_features_${langCode}.ml`;
$exampleLink.textContent = `📘 Complete features (${langCode}) ✨`;
}
/* ── Keyword highlighting ────────────────────────────────────────── */
export let languageKeywords = new Map();
let unicodeWordRegex = null;
try {
unicodeWordRegex = new RegExp('[\\p{L}\\p{M}\\p{N}_]', 'u');
} catch (_) {
unicodeWordRegex = null;
}
function isWordChar(ch) {
if (!ch) return false;
if (unicodeWordRegex) return unicodeWordRegex.test(ch);
return /[A-Za-z0-9_]/.test(ch) || (ch.codePointAt(0) > 127 && !/\s/.test(ch));
}
function normalizeKeywordEntry(value) {
if (Array.isArray(value)) return value;
return typeof value === 'string' ? [value] : [];
}
function extractKeywordsForLanguage(keywordSpec, lang) {
const collected = new Set();
const categories = keywordSpec && keywordSpec.categories ? keywordSpec.categories : {};
Object.values(categories).forEach(group => {
Object.values(group).forEach(mapping => {
normalizeKeywordEntry(mapping[lang]).forEach(word => {
if (typeof word === 'string') {
const trimmed = word.trim();
if (trimmed) collected.add(trimmed);
}
});
});
});
return [...collected].sort((a, b) => b.length - a.length);
}
async function fetchKeywordSpec() {
const candidates = [
'./assets/keywords.json',
'../assets/keywords.json',
'assets/keywords.json',
'../multilingualprogramming/resources/usm/keywords.json',
'./multilingualprogramming/resources/usm/keywords.json',
'https://raw.githubusercontent.com/johnsamuelwrites/multilingual/main/multilingualprogramming/resources/usm/keywords.json',
];
for (const url of candidates) {
try {
const resp = await fetch(url, { cache: 'no-cache' });
if (!resp.ok) continue;
return await resp.json();
} catch (_) { /* try next candidate */ }
}
return null;
}
function applyKeywordOnlyMode(lang, keywords) {
const modeName = `ml-keywords-${lang}`;
if (!CodeMirror.modes[modeName]) {
const keywordSet = new Set(keywords);
const isDigit = (ch) => !!ch && ch >= '0' && ch <= '9';
const isOperator = (ch) => !!ch && /[+\-*/%=<>!&|^~:,.[\](){}]/.test(ch);
CodeMirror.defineMode(modeName, () => ({
token(stream) {
const ch = stream.peek();
if (!ch) return null;
if (/\s/.test(ch)) { stream.next(); return null; }
if (ch === '#') { stream.skipToEnd(); return 'comment'; }
if (ch === '"' || ch === "'") {
const quote = stream.next();
let escaped = false;
while (!stream.eol()) {
const c = stream.next();
if (escaped) { escaped = false; continue; }
if (c === '\\') { escaped = true; continue; }
if (c === quote) break;
}
return 'string';
}
if (isDigit(ch)) { stream.eatWhile(/[0-9._]/); return 'number'; }
if (isWordChar(ch)) {
let word = '';
while (!stream.eol() && isWordChar(stream.peek())) word += stream.next();
return keywordSet.has(word) ? 'keyword' : null;
}
if (isOperator(ch)) { stream.next(); return 'operator'; }
stream.next();
return null;
},
}));
}
editor.setOption('mode', modeName);
editor.refresh();
}
export function applyEditorHighlighting(lang) {
const keywords = languageKeywords.get(lang);
if (!keywords || !keywords.length || lang === 'en') {
editor.setOption('mode', 'python');
editor.refresh();
return;
}
applyKeywordOnlyMode(lang, keywords);
}
export async function initLanguageKeywords() {
const spec = await fetchKeywordSpec();
if (!spec) {
console.warn('Could not load keywords.json; using default python highlighting.');
return;
}
($lang ? Array.from($lang.options).map(opt => opt.value) : []).forEach(code => {
languageKeywords.set(code, extractKeywordsForLanguage(spec, code));
});
}
/* ── Error line highlighting ─────────────────────────────────────── */
export function clearErrorLines() {
for (let i = 0; i < editor.lineCount(); i++) {
editor.removeLineClass(i, 'background', 'error-line');
}
}
export function highlightErrorLine(errText) {
clearErrorLines();
const m = errText.match(/line[:\s]+(\d+)/i)
|| errText.match(/at line (\d+)/i)
|| errText.match(/:(\d+):/);
if (!m) return;
const lineNum = parseInt(m[1], 10) - 1;
if (lineNum >= 0 && lineNum < editor.lineCount()) {
editor.addLineClass(lineNum, 'background', 'error-line');
editor.scrollIntoView({ line: lineNum, ch: 0 }, 80);
}
}
editor.on('change', clearErrorLines);