Skip to content

Commit 4212b7e

Browse files
committed
fix: hideImportedModules for nested imports
1 parent 707b254 commit 4212b7e

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ function compile(
931931
isrc = reader(imports[i]);
932932
}
933933
targ =
934-
`/*___wenyan_import_${imports[i]}_start___*/` +
934+
`/*___wenyan_module_${imports[i]}_start___*/` +
935935
mwrapper(
936936
imports[i],
937937
compile(lang, isrc, {
@@ -942,7 +942,7 @@ function compile(
942942
lib
943943
})
944944
) +
945-
`/*___wenyan_import_${imports[i]}_end___*/` +
945+
`/*___wenyan_module_${imports[i]}_end___*/` +
946946
targ;
947947
}
948948

tools/make_ide.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,36 @@ for (var i = 0; i < files.length; i++) {
1818
var lib = utils.loadlib();
1919

2020
function main() {
21+
function hideImportedModules(source) {
22+
const markerRegex = /\/\*___wenyan_module_([\s\S]+?)_(start|end)___\*\//g;
23+
const matches = [];
24+
25+
var match;
26+
while ((match = markerRegex.exec(source))) {
27+
if (!match) break;
28+
29+
if (matches.length) {
30+
const prev = matches[matches.length - 1];
31+
if (prev[2] !== "end" && prev[1] !== match[1]) continue; // ignore nested imports
32+
}
33+
34+
matches.push(match);
35+
}
36+
37+
for (const match of matches) {
38+
if (match[2] === "start") continue;
39+
40+
source = source.replace(
41+
new RegExp(
42+
`\\/\\*___wenyan_module_${match[1]}_start___\\*\\/[\\s\\S]*\\/\\*___wenyan_module_${match[1]}_end___\\*\\/`
43+
),
44+
`/* module ${match[1]} is hidden */\n`
45+
);
46+
}
47+
48+
return source;
49+
}
50+
2151
var ed = newEditor(prgms["mandelbrot"]);
2252
// var ln = newLineNo(ed);
2353

@@ -92,14 +122,7 @@ function main() {
92122
reader: x => prgms[x]
93123
});
94124

95-
var showcode = code;
96-
97-
if (hidestd.checked) {
98-
showcode = showcode.replace(
99-
/\/\*___wenyan_import_([\s\S]+?)_start___\*\/([\s\S]*?)\/\*___wenyan_import_([\s\S]+?)_end___\*\//g,
100-
"/* module $1 is hidden */\n"
101-
);
102-
}
125+
var showcode = hidestd.checked ? hideImportedModules(code) : code;
103126

104127
document.getElementById("js").innerText = js_beautify(showcode);
105128
hljs.highlightBlock(document.getElementById("js"));

0 commit comments

Comments
 (0)