vimamsa 0.1.27 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/appimage.yml +17 -10
- data/Dockerfile +2 -2
- data/README.md +162 -90
- data/custom_example.rb +33 -15
- data/doc/TODO.md +334 -110
- data/doc/action_names_plan.md +442 -0
- data/doc/bundling_plan.md +2 -1
- data/doc/delta_syntax_plan.md +293 -0
- data/doc/demo_recording_plan.md +288 -0
- data/doc/design_goals.md +27 -0
- data/doc/intro_page_plan.md +266 -0
- data/doc/key_bindings_report_plan.md +45 -0
- data/doc/line_tree_plan.md +89 -0
- data/doc/missing_vim_bindings.md +34 -44
- data/doc/modules_settings_plan.md +1 -1
- data/doc/registries_plan.md +202 -0
- data/doc/startup_speed_plan.md +97 -0
- data/doc/test_commands.md +232 -0
- data/doc/treap_visualizer.html +585 -0
- data/doc/vim_compatibility_plan.md +186 -31
- data/doc/vimlike_vim_conflicts.md +236 -228
- data/doc/vm_snapshots_plan.md +4 -4
- data/doc/wysiwyg_plan.md +231 -0
- data/docker_cmd.sh +1 -1
- data/exe/run_tests.rb +85 -3
- data/exe/vimamsa +49 -6
- data/help/intro.txt +30 -0
- data/help/vim_intro.txt +31 -0
- data/img/demo.gif +0 -0
- data/img/demo.mp4 +0 -0
- data/lang/hyperplaintext.lang +1 -1
- data/lib/vimamsa/ack.rb +2 -0
- data/lib/vimamsa/actions.rb +381 -36
- data/lib/vimamsa/bookmarks.rb +8 -8
- data/lib/vimamsa/buffer.rb +84 -38
- data/lib/vimamsa/buffer_changetext.rb +58 -36
- data/lib/vimamsa/buffer_list.rb +2 -2
- data/lib/vimamsa/buffer_manager.rb +6 -6
- data/lib/vimamsa/cli_help.rb +75 -0
- data/lib/vimamsa/clipboard.rb +6 -3
- data/lib/vimamsa/conf.rb +9 -0
- data/lib/vimamsa/crash_handler.rb +2 -2
- data/lib/vimamsa/debug.rb +2 -2
- data/lib/vimamsa/demo_player.rb +494 -0
- data/lib/vimamsa/demo_recorder.rb +277 -0
- data/lib/vimamsa/diff_buffer.rb +1 -1
- data/lib/vimamsa/dot_repeat.rb +104 -0
- data/lib/vimamsa/easy_jump.rb +54 -3
- data/lib/vimamsa/editor.rb +136 -65
- data/lib/vimamsa/file_history.rb +1 -1
- data/lib/vimamsa/file_manager.rb +23 -23
- data/lib/vimamsa/form_input.rb +388 -0
- data/lib/vimamsa/gui.rb +89 -13
- data/lib/vimamsa/gui_command_line.rb +44 -5
- data/lib/vimamsa/gui_form_generator.rb +17 -6
- data/lib/vimamsa/gui_func_panel.rb +1 -1
- data/lib/vimamsa/gui_key_hint.rb +165 -0
- data/lib/vimamsa/gui_keylog_panel.rb +164 -2
- data/lib/vimamsa/gui_menu.rb +69 -50
- data/lib/vimamsa/gui_select_window.rb +77 -43
- data/lib/vimamsa/gui_settings.rb +9 -5
- data/lib/vimamsa/gui_sourceview.rb +28 -81
- data/lib/vimamsa/gui_theme.rb +11 -2
- data/lib/vimamsa/hook.rb +2 -0
- data/lib/vimamsa/intro_page.rb +367 -0
- data/lib/vimamsa/key_actions.rb +315 -225
- data/lib/vimamsa/key_binding_tree.rb +228 -61
- data/lib/vimamsa/key_bindings_notepad.rb +18 -16
- data/lib/vimamsa/key_bindings_report.rb +290 -67
- data/lib/vimamsa/key_bindings_vim.rb +43 -297
- data/lib/vimamsa/key_bindings_vim_core.rb +356 -0
- data/lib/vimamsa/key_bindings_vimlike.rb +347 -339
- data/lib/vimamsa/key_names.rb +80 -0
- data/lib/vimamsa/langservp.rb +62 -7
- data/lib/vimamsa/line_tree.rb +367 -0
- data/lib/vimamsa/macro.rb +20 -3
- data/lib/vimamsa/rbvma.rb +15 -0
- data/lib/vimamsa/registers.rb +125 -0
- data/lib/vimamsa/run_ruby.rb +64 -0
- data/lib/vimamsa/search_replace.rb +4 -4
- data/lib/vimamsa/string_util.rb +17 -3
- data/lib/vimamsa/tests.rb +8 -8
- data/lib/vimamsa/text_objects.rb +11 -54
- data/lib/vimamsa/text_transforms.rb +4 -0
- data/lib/vimamsa/tmp_user_dir.rb +39 -0
- data/lib/vimamsa/user_dirs.rb +83 -0
- data/lib/vimamsa/version.rb +1 -1
- data/lib/vimamsa/vim_operators.rb +365 -0
- data/modules/calculator/calculator.rb +5 -5
- data/modules/color_highlight/color_highlight.rb +3 -3
- data/modules/dictation/dictation.rb +32 -32
- data/modules/ollama/ollama.rb +5 -5
- data/modules/terminal/terminal.rb +5 -5
- data/modules/tts/tts.rb +10 -10
- data/run_e2e_vm.sh +3 -3
- data/run_tests_vm.rb +163 -0
- data/scripts/appimage/AppRun +7 -0
- data/scripts/appimage/build_in_container.sh +11 -1
- data/scripts/appimage/smoke_test.sh +1 -1
- data/scripts/demo/demo_easyjump_steps.txt +60 -0
- data/scripts/demo/demo_files_steps.txt +52 -0
- data/scripts/demo/demo_steps.txt +9 -1
- data/scripts/demo/notes.txt +6 -0
- data/scripts/demo/record_demo.rb +60 -81
- data/scripts/demo/report.rb +7 -0
- data/scripts/demo/shapes.rb +1 -0
- data/scripts/demo/square/shapes.rb +29 -0
- data/scripts/demo/square/steps.txt +185 -0
- data/scripts/lib/perf_startup.rb +138 -0
- data/scripts/lib/script_helpers.rb +53 -0
- data/scripts/perf_compare.rb +34 -0
- data/scripts/profile_startup.rb +72 -0
- data/tests/test_action_ids.rb +232 -0
- data/tests/test_auto_format.rb +2 -2
- data/tests/test_autocomplete.rb +1 -1
- data/tests/test_basic_editing.rb +18 -18
- data/tests/test_bookmarks.rb +6 -6
- data/tests/test_bracket_match.rb +3 -0
- data/tests/test_cli_help.rb +64 -0
- data/tests/test_color_highlight.rb +2 -2
- data/tests/test_command_line.rb +1 -1
- data/tests/test_context_menu_file.rb +18 -0
- data/tests/test_copy_paste.rb +33 -33
- data/tests/test_custom_two_phase.rb +1 -1
- data/tests/test_demo_player.rb +355 -0
- data/tests/test_demo_recorder.rb +202 -0
- data/tests/test_dictation.rb +16 -16
- data/tests/test_dot_repeat.rb +88 -0
- data/tests/test_easy_jump.rb +93 -0
- data/tests/test_extract_file_links.rb +3 -3
- data/tests/test_file_panel.rb +2 -2
- data/tests/test_flash.rb +1 -1
- data/tests/test_fold.rb +1 -1
- data/tests/test_form_input.rb +203 -0
- data/tests/test_func_panel.rb +2 -2
- data/tests/test_gtk_sync.rb +7 -7
- data/tests/test_intro_page.rb +437 -0
- data/tests/test_key_categories.rb +163 -0
- data/tests/test_keylog_panel.rb +163 -3
- data/tests/test_line_ends.rb +51 -0
- data/tests/test_line_tree.rb +86 -0
- data/tests/test_lsp_clangd.rb +2 -2
- data/tests/test_lsp_root.rb +43 -0
- data/tests/test_macro.rb +2 -2
- data/tests/test_module_interface.rb +9 -9
- data/tests/test_notepad_bindings.rb +5 -5
- data/tests/test_perf_startup.rb +28 -0
- data/tests/test_random_edit.rb +27 -27
- data/tests/test_registers.rb +131 -0
- data/tests/test_run_ruby.rb +77 -0
- data/tests/test_startup_file.rb +45 -4
- data/tests/test_text_objects.rb +1 -1
- data/tests/test_text_transforms.rb +12 -0
- data/tests/test_tmp_user_dir.rb +79 -0
- data/tests/test_tts.rb +10 -10
- data/tests/test_undo.rb +34 -34
- data/tests/test_user_dirs.rb +122 -0
- data/tests/test_vim_bindings.rb +107 -6
- data/tests/test_vim_conflicts.rb +41 -4
- data/tests/test_vim_operators.rb +116 -0
- data/tests/test_window_layout.rb +83 -0
- data/vimamsa.gemspec +1 -1
- data/vm_guest_run.sh +75 -28
- metadata +60 -4
- data/run_tests.rb +0 -89
- data/run_tests_vm.sh +0 -130
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>Treap of Lines</title>
|
|
7
|
+
<style>
|
|
8
|
+
/* Standalone, no external resources. Top: operations and the lines. Middle: the tree. Bottom: code and call stack. */
|
|
9
|
+
:root {
|
|
10
|
+
--bg: #eef1f5; --panel: #fff; --ink: #17212d; --muted: #5b6778; --line: #d6dce5;
|
|
11
|
+
--accent: #0c7380; --chg: #b25806; --prio: #7b3bd8; --code-bg: #f6f8fb; --hot: #dff1f3;
|
|
12
|
+
--mono: ui-monospace, "SFMono-Regular", "DejaVu Sans Mono", Menlo, Consolas, monospace;
|
|
13
|
+
--sans: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
|
|
14
|
+
color-scheme: light;
|
|
15
|
+
}
|
|
16
|
+
@media (prefers-color-scheme: dark) {
|
|
17
|
+
:root:not([data-theme="light"]) {
|
|
18
|
+
--bg: #0e131a; --panel: #151c26; --ink: #e5eaf1; --muted: #98a4b5; --line: #2a3443;
|
|
19
|
+
--accent: #3cc3cd; --chg: #f2a64f; --prio: #b994ff; --code-bg: #10171f; --hot: #163a40; color-scheme: dark;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
:root[data-theme="dark"] {
|
|
23
|
+
--bg: #0e131a; --panel: #151c26; --ink: #e5eaf1; --muted: #98a4b5; --line: #2a3443;
|
|
24
|
+
--accent: #3cc3cd; --chg: #f2a64f; --prio: #b994ff; --code-bg: #10171f; --hot: #163a40; color-scheme: dark;
|
|
25
|
+
}
|
|
26
|
+
* { box-sizing: border-box; }
|
|
27
|
+
body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.45 var(--sans);
|
|
28
|
+
padding: 18px max(16px, env(safe-area-inset-right, 0px)) 36px max(16px, env(safe-area-inset-left, 0px)); }
|
|
29
|
+
.wrap { max-width: 1200px; margin: 0 auto; display: grid; gap: 14px; }
|
|
30
|
+
h1 { font-size: 22px; margin: 0; }
|
|
31
|
+
header p { margin: 4px 0 0; color: var(--muted); max-width: 90ch; }
|
|
32
|
+
code { font-family: var(--mono); font-size: 0.92em; }
|
|
33
|
+
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; min-width: 0; }
|
|
34
|
+
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 0 8px; }
|
|
35
|
+
.top { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; }
|
|
36
|
+
.bottom { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 14px; align-items: start; }
|
|
37
|
+
@media (max-width: 860px) { .top, .bottom { grid-template-columns: minmax(0, 1fr); } }
|
|
38
|
+
|
|
39
|
+
.ops { display: grid; gap: 8px; }
|
|
40
|
+
.row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
|
|
41
|
+
input { font: 12.5px var(--mono); color: var(--ink); background: var(--code-bg); border: 1px solid var(--line); border-radius: 5px; padding: 4px 6px; width: 64px; }
|
|
42
|
+
button { font: 600 12.5px var(--sans); color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 5px; padding: 4px 10px; cursor: pointer; }
|
|
43
|
+
button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
|
|
44
|
+
button.primary { background: var(--accent); border-color: var(--accent); color: var(--panel); }
|
|
45
|
+
button.primary:hover:not(:disabled) { color: var(--panel); filter: brightness(1.08); }
|
|
46
|
+
button:disabled { opacity: 0.45; cursor: not-allowed; }
|
|
47
|
+
button code { font-weight: 600; }
|
|
48
|
+
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
|
|
49
|
+
.hint { color: var(--muted); font-size: 12px; }
|
|
50
|
+
.err { color: var(--chg); font-size: 12.5px; min-height: 1.3em; }
|
|
51
|
+
|
|
52
|
+
table.lines { border-collapse: collapse; width: 100%; font: 12.5px var(--mono); font-variant-numeric: tabular-nums; }
|
|
53
|
+
table.lines th { text-align: right; font: 600 11px var(--sans); color: var(--muted); padding: 2px 8px; }
|
|
54
|
+
table.lines td { text-align: right; padding: 1px 8px; border-top: 1px solid var(--line); }
|
|
55
|
+
table.lines td.tx, table.lines th.tx { text-align: left; width: 100%; white-space: pre; }
|
|
56
|
+
table.lines tr.sel td { background: var(--hot); }
|
|
57
|
+
table.lines tr { cursor: pointer; }
|
|
58
|
+
|
|
59
|
+
.treebox { overflow: auto; border: 1px solid var(--line); border-radius: 6px; background: var(--code-bg); }
|
|
60
|
+
svg { display: block; }
|
|
61
|
+
.edge { stroke: var(--muted); stroke-width: 1.4; opacity: 0.75; }
|
|
62
|
+
.nbox { fill: var(--panel); stroke: var(--line); stroke-width: 1.4; }
|
|
63
|
+
.nid { fill: var(--ink); font: 700 11px var(--sans); }
|
|
64
|
+
.nprio { fill: var(--prio); font: 700 10.5px var(--mono); }
|
|
65
|
+
.ntxt { fill: var(--ink); font: 10.5px var(--mono); }
|
|
66
|
+
.nstat { fill: var(--muted); font: 10px var(--mono); }
|
|
67
|
+
.nstat .c { fill: var(--chg); font-weight: 700; }
|
|
68
|
+
.plabel { fill: var(--accent); font: 700 11px var(--mono); }
|
|
69
|
+
.tag rect { fill: var(--accent); } .tag.ret rect { fill: var(--muted); }
|
|
70
|
+
.tag text { fill: var(--panel); font: 700 10px var(--mono); }
|
|
71
|
+
g.ref .nbox { stroke: var(--accent); stroke-width: 2.4; }
|
|
72
|
+
g.changed .nbox { fill: color-mix(in srgb, var(--chg) 15%, var(--panel)); }
|
|
73
|
+
|
|
74
|
+
.controls { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 10px; }
|
|
75
|
+
.stepno { margin-left: auto; color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
|
|
76
|
+
.note { margin-top: 8px; min-height: 2.6em; font-size: 13.5px; }
|
|
77
|
+
.note b { font-family: var(--mono); color: var(--accent); }
|
|
78
|
+
|
|
79
|
+
.fns { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 8px; }
|
|
80
|
+
.fn { font: 12px var(--mono); padding: 1px 7px; border-radius: 4px; border: 1px solid var(--line); background: var(--code-bg); cursor: pointer; color: var(--ink); }
|
|
81
|
+
.fn .n { color: var(--muted); font-size: 11px; margin-left: 4px; }
|
|
82
|
+
.fn.on { border-color: var(--accent); background: var(--hot); }
|
|
83
|
+
.fn.run { background: var(--accent); border-color: var(--accent); color: var(--panel); }
|
|
84
|
+
.fn.run .n { color: var(--panel); }
|
|
85
|
+
.fn.view { outline: 2px dashed var(--prio); outline-offset: 1px; }
|
|
86
|
+
pre.code { margin: 0; font: 12.5px/1.55 var(--mono); background: var(--code-bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 0; overflow-x: auto; }
|
|
87
|
+
pre.code .ln { display: grid; grid-template-columns: 26px 1fr; white-space: pre; border-left: 3px solid transparent; padding-right: 10px; }
|
|
88
|
+
pre.code .no { color: var(--muted); text-align: right; padding-right: 8px; user-select: none; }
|
|
89
|
+
pre.code .ln.hot { background: var(--hot); border-left-color: var(--accent); }
|
|
90
|
+
.kw { color: var(--prio); } .cm { color: var(--muted); } .iv { color: var(--chg); }
|
|
91
|
+
|
|
92
|
+
.stack { display: grid; gap: 6px; }
|
|
93
|
+
.box { border: 1px solid var(--line); border-radius: 6px; background: var(--code-bg); }
|
|
94
|
+
.box .h { display: flex; justify-content: space-between; gap: 8px; padding: 4px 8px; font: 12px var(--mono); cursor: pointer; }
|
|
95
|
+
.box .h .l { color: var(--muted); }
|
|
96
|
+
.box.run { border-color: var(--accent); } .box.run .h { background: var(--hot); font-weight: 700; }
|
|
97
|
+
.box.ret { border-style: dashed; border-color: var(--muted); } .box.ret.now { border-style: solid; border-color: var(--prio); }
|
|
98
|
+
.box.ret .h { cursor: default; font: 600 11px var(--sans); text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
|
|
99
|
+
.vars { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 1px 10px; padding: 3px 8px 6px; font: 12px var(--mono); }
|
|
100
|
+
.vars .k { color: var(--muted); }
|
|
101
|
+
.vars .chg { color: var(--chg); font-weight: 700; }
|
|
102
|
+
.nref { color: var(--accent); font-weight: 700; }
|
|
103
|
+
.muted { color: var(--muted); }
|
|
104
|
+
</style>
|
|
105
|
+
</head>
|
|
106
|
+
<body>
|
|
107
|
+
<div class="wrap">
|
|
108
|
+
<header>
|
|
109
|
+
<h1>Treap of Lines</h1>
|
|
110
|
+
<p>Each node is one line. It stores only the line's length (<code>len</code>, with its <code>\n</code>), a random priority (<code>p</code>: a parent's is always higher), and two totals for its subtree: lines (<code>cnt</code>) and characters (<code>Σ</code>). Line numbers aren't stored. Pick an operation and step through the Ruby code line by line.</p>
|
|
111
|
+
</header>
|
|
112
|
+
|
|
113
|
+
<div class="top">
|
|
114
|
+
<section class="panel">
|
|
115
|
+
<h2>Operations</h2>
|
|
116
|
+
<div class="ops">
|
|
117
|
+
<div class="row"><label for="in-i">line i</label><input type="number" id="in-i" min="0" value="3"><span class="hint">or click a line on the right</span></div>
|
|
118
|
+
<div class="row"><button id="b-start" class="primary">Where does line i start? <code>line_start(i)</code></button></div>
|
|
119
|
+
<div class="row"><label for="in-d">chars</label><input type="number" id="in-d" value="3"><button id="b-len">Make line i longer <code>add_len(i, d)</code></button></div>
|
|
120
|
+
<div class="row"><label for="in-len">length</label><input type="number" id="in-len" min="1" value="6"><button id="b-ins">Insert a line before i <code>insert_line(i, len)</code></button></div>
|
|
121
|
+
<div class="row"><button id="b-del">Delete line i <code>delete_line(i)</code></button><button id="b-reset">New random tree</button></div>
|
|
122
|
+
<div class="err" id="err" role="status"></div>
|
|
123
|
+
</div>
|
|
124
|
+
</section>
|
|
125
|
+
<section class="panel">
|
|
126
|
+
<h2>Lines (as the tree says at this step)</h2>
|
|
127
|
+
<table class="lines"><thead><tr><th>line</th><th>start</th><th>len</th><th class="tx">text</th></tr></thead><tbody id="lines"></tbody></table>
|
|
128
|
+
</section>
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
<section class="panel">
|
|
132
|
+
<h2>Tree</h2>
|
|
133
|
+
<div class="treebox"><svg id="svg" role="img" aria-label="The treap at this step"></svg></div>
|
|
134
|
+
<div class="note" id="note"></div>
|
|
135
|
+
<div class="controls">
|
|
136
|
+
<button id="c-first" title="First step (Home)">⏮</button>
|
|
137
|
+
<button id="c-back" title="Back (←)">◀ Back</button>
|
|
138
|
+
<button id="c-step" class="primary" title="Next line (→)">Step ▶</button>
|
|
139
|
+
<button id="c-over" title="Next line in this function (↓)">Step over</button>
|
|
140
|
+
<button id="c-out" title="Until this function returns (↑)">Step out</button>
|
|
141
|
+
<button id="c-play">Play</button>
|
|
142
|
+
<button id="c-last" title="Last step (End)">⏭</button>
|
|
143
|
+
<span class="stepno" id="stepno"></span>
|
|
144
|
+
</div>
|
|
145
|
+
</section>
|
|
146
|
+
|
|
147
|
+
<div class="bottom">
|
|
148
|
+
<section class="panel">
|
|
149
|
+
<h2>Functions</h2>
|
|
150
|
+
<div class="fns" id="fns"></div>
|
|
151
|
+
<pre class="code" id="code"></pre>
|
|
152
|
+
</section>
|
|
153
|
+
<section class="panel">
|
|
154
|
+
<h2>Call stack</h2>
|
|
155
|
+
<div class="stack" id="stack"></div>
|
|
156
|
+
</section>
|
|
157
|
+
</div>
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
<script>
|
|
161
|
+
(() => {
|
|
162
|
+
"use strict";
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------- the Ruby code (simplified from lib/vimamsa/line_tree.rb)
|
|
165
|
+
const SRC = {
|
|
166
|
+
line_start: `def line_start(i) # where line i starts
|
|
167
|
+
pos = 0
|
|
168
|
+
n = @root
|
|
169
|
+
while n
|
|
170
|
+
lc = cnt(n.left)
|
|
171
|
+
if i < lc # line i is on the left
|
|
172
|
+
n = n.left
|
|
173
|
+
elsif i == lc # this node is line i
|
|
174
|
+
return pos + sum(n.left)
|
|
175
|
+
else # skip left subtree + this line
|
|
176
|
+
pos += sum(n.left) + n.len
|
|
177
|
+
i -= lc + 1
|
|
178
|
+
n = n.right
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end`,
|
|
182
|
+
add_len: `def add_len(n, i, d) # line i got d chars longer
|
|
183
|
+
lc = cnt(n.left)
|
|
184
|
+
if i < lc
|
|
185
|
+
add_len(n.left, i, d)
|
|
186
|
+
elsif i > lc
|
|
187
|
+
add_len(n.right, i - lc - 1, d)
|
|
188
|
+
else
|
|
189
|
+
n.len += d # this node is line i
|
|
190
|
+
end
|
|
191
|
+
n.sum += d # every node on the path
|
|
192
|
+
end`,
|
|
193
|
+
insert_line: `def insert_line(i, len) # new line before line i
|
|
194
|
+
l, r = split(@root, i)
|
|
195
|
+
n = Node.new(len) # random priority
|
|
196
|
+
@root = merge(merge(l, n), r)
|
|
197
|
+
end`,
|
|
198
|
+
delete_line: `def delete_line(i)
|
|
199
|
+
l, rest = split(@root, i)
|
|
200
|
+
gone, r = split(rest, 1)
|
|
201
|
+
@root = merge(l, r)
|
|
202
|
+
end`,
|
|
203
|
+
split: `def split(n, i) # => [lines 0...i, lines i..]
|
|
204
|
+
return [nil, nil] if n.nil?
|
|
205
|
+
if i <= cnt(n.left) # the cut is on the left
|
|
206
|
+
l, r = split(n.left, i)
|
|
207
|
+
n.left = r
|
|
208
|
+
update(n)
|
|
209
|
+
[l, n]
|
|
210
|
+
else # the cut is on the right
|
|
211
|
+
l, r = split(n.right, i - cnt(n.left) - 1)
|
|
212
|
+
n.right = l
|
|
213
|
+
update(n)
|
|
214
|
+
[n, r]
|
|
215
|
+
end
|
|
216
|
+
end`,
|
|
217
|
+
merge: `def merge(a, b) # all of a comes before all of b
|
|
218
|
+
return a || b if a.nil? || b.nil?
|
|
219
|
+
if a.prio > b.prio # higher priority stays on top
|
|
220
|
+
a.right = merge(a.right, b)
|
|
221
|
+
update(a)
|
|
222
|
+
a
|
|
223
|
+
else
|
|
224
|
+
b.left = merge(a, b.left)
|
|
225
|
+
update(b)
|
|
226
|
+
b
|
|
227
|
+
end
|
|
228
|
+
end`,
|
|
229
|
+
update: `def update(n) # recompute the subtree totals
|
|
230
|
+
n.cnt = 1 + cnt(n.left) + cnt(n.right)
|
|
231
|
+
n.sum = n.len + sum(n.left) + sum(n.right)
|
|
232
|
+
end`
|
|
233
|
+
};
|
|
234
|
+
const RET_NAMES = { split: ["l", "r"] };
|
|
235
|
+
|
|
236
|
+
// ---------------------------------------------------------------- tree
|
|
237
|
+
let nid = 0;
|
|
238
|
+
const usedP = new Set();
|
|
239
|
+
function prio() { for (;;) { const p = 10 + Math.floor(Math.random() * 90); if (!usedP.has(p) || usedP.size > 85) { usedP.add(p); return p; } } }
|
|
240
|
+
class Node { constructor(len, txt) { this.id = ++nid; this.len = len; this.prio = prio(); this.left = this.right = null; this.cnt = 1; this.sum = len; this.txt = txt; } }
|
|
241
|
+
const cnt = n => n ? n.cnt : 0, sum = n => n ? n.sum : 0;
|
|
242
|
+
let ROOT = null;
|
|
243
|
+
const fixup = n => { n.cnt = 1 + cnt(n.left) + cnt(n.right); n.sum = n.len + sum(n.left) + sum(n.right); };
|
|
244
|
+
function buildQuiet(lines) { // plain O(k) build, not traced
|
|
245
|
+
const st = [];
|
|
246
|
+
for (const t of lines) {
|
|
247
|
+
const n = new Node([...t].length + 1, t); let last = null;
|
|
248
|
+
while (st.length && st[st.length - 1].prio < n.prio) { last = st.pop(); fixup(last); }
|
|
249
|
+
n.left = last; if (st.length) st[st.length - 1].right = n; st.push(n);
|
|
250
|
+
}
|
|
251
|
+
for (let k = st.length - 1; k >= 0; k--) fixup(st[k]);
|
|
252
|
+
return st[0] || null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------- tracing: every yield is one Ruby line
|
|
256
|
+
let STACK = [], STEPS = [], COUNTS = {}, LASTRET = null, fid = 0, prevNodes = {}, TITLE = "";
|
|
257
|
+
function enter(fn, vars) { const f = { fn, line: 0, vars: { ...vars }, args: Object.entries(vars), id: ++fid }; STACK.push(f); COUNTS[fn] = (COUNTS[fn] || 0) + 1; return f; }
|
|
258
|
+
function at(f, line, upd, note) { f.line = line; const ch = upd ? Object.keys(upd) : []; if (upd) Object.assign(f.vars, upd); return snap(note, ch); }
|
|
259
|
+
function leave(f, val, has) { STACK.pop(); LASTRET = { fn: f.fn, args: f.args, val, has, step: STEPS.length }; }
|
|
260
|
+
function* ret(f, line, val, note) { yield at(f, line, { "⇒ return": val }, note); leave(f, val, true); return val; }
|
|
261
|
+
|
|
262
|
+
const ser = v => v instanceof Node ? { node: v.id } : Array.isArray(v) ? { arr: v.map(ser) } : v == null ? { nil: 1 } : { val: v };
|
|
263
|
+
function snap(note, changed) {
|
|
264
|
+
// trees to draw: @root, plus every node held by a variable or the last return value that @root can't reach
|
|
265
|
+
const cands = [], seen = new Set();
|
|
266
|
+
const add = n => { if (n && !seen.has(n.id)) { seen.add(n.id); cands.push(n); } };
|
|
267
|
+
const walkV = v => { if (v instanceof Node) add(v); else if (Array.isArray(v)) v.forEach(walkV); };
|
|
268
|
+
add(ROOT);
|
|
269
|
+
if (LASTRET && LASTRET.has) walkV(LASTRET.val);
|
|
270
|
+
STACK.forEach(f => Object.values(f.vars).forEach(walkV));
|
|
271
|
+
const nodes = {}, inner = new Set();
|
|
272
|
+
const visit = (n, top) => { if (!n) return; if (!top) inner.add(n.id); if (nodes[n.id]) return;
|
|
273
|
+
nodes[n.id] = { id: n.id, len: n.len, prio: n.prio, cnt: n.cnt, sum: n.sum, txt: n.txt, left: n.left && n.left.id, right: n.right && n.right.id };
|
|
274
|
+
visit(n.left, false); visit(n.right, false); };
|
|
275
|
+
cands.forEach(c => visit(c, true));
|
|
276
|
+
const roots = cands.filter(c => !inner.has(c.id)).map(c => c.id);
|
|
277
|
+
const labels = {}; roots.forEach(id => labels[id] = []);
|
|
278
|
+
if (ROOT && labels[ROOT.id]) labels[ROOT.id].push("@root");
|
|
279
|
+
STACK.forEach((f, k) => Object.entries(f.vars).forEach(([name, v]) => {
|
|
280
|
+
const pre = k === STACK.length - 1 ? "" : f.fn + ".";
|
|
281
|
+
const w = (x, nm) => { if (x instanceof Node && labels[x.id] && !labels[x.id].includes(pre + nm)) labels[x.id].push(pre + nm); else if (Array.isArray(x)) x.forEach((y, i) => w(y, `${nm}[${i}]`)); };
|
|
282
|
+
w(v, name);
|
|
283
|
+
}));
|
|
284
|
+
const chg = {};
|
|
285
|
+
for (const id in nodes) { const a = nodes[id], b = prevNodes[id]; if (!b) continue;
|
|
286
|
+
const c = ["len", "cnt", "sum"].filter(k => a[k] !== b[k]); if (a.left !== b.left || a.right !== b.right) c.push("link"); if (c.length) chg[id] = c; }
|
|
287
|
+
prevNodes = nodes;
|
|
288
|
+
const s = { nodes, roots, labels, chg, note: note || "", counts: { ...COUNTS },
|
|
289
|
+
stack: STACK.map((f, k) => ({ fn: f.fn, line: f.line, id: f.id, vars: Object.entries(f.vars).map(([a, v]) => [a, ser(v)]), changed: k === STACK.length - 1 ? changed : [] })),
|
|
290
|
+
last: LASTRET && { fn: LASTRET.fn, args: LASTRET.args.map(([a, v]) => [a, ser(v)]), has: LASTRET.has, val: LASTRET.has ? ser(LASTRET.val) : null, step: LASTRET.step } };
|
|
291
|
+
STEPS.push(s);
|
|
292
|
+
return s;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ---------------------------------------------------------------- the traced functions
|
|
296
|
+
function* update(n) {
|
|
297
|
+
const f = enter("update", { n });
|
|
298
|
+
n.cnt = 1 + cnt(n.left) + cnt(n.right);
|
|
299
|
+
yield at(f, 1, null, `n.cnt = 1 + ${cnt(n.left)} + ${cnt(n.right)} = ${n.cnt}`);
|
|
300
|
+
n.sum = n.len + sum(n.left) + sum(n.right);
|
|
301
|
+
yield at(f, 2, null, `n.sum = ${n.len} + ${sum(n.left)} + ${sum(n.right)} = ${n.sum}`);
|
|
302
|
+
leave(f);
|
|
303
|
+
}
|
|
304
|
+
function* split(n, i) {
|
|
305
|
+
const f = enter("split", { n, i });
|
|
306
|
+
if (!n) return yield* ret(f, 1, [null, null], "An empty subtree: return [nil, nil]. Now the calls return one by one.");
|
|
307
|
+
yield at(f, 1, null, "n isn't nil.");
|
|
308
|
+
const lc = cnt(n.left);
|
|
309
|
+
yield at(f, 2, null, `i <= cnt(n.left) → ${i} <= ${lc} → ${i <= lc}`);
|
|
310
|
+
if (i <= lc) {
|
|
311
|
+
yield at(f, 3, null, `The cut is in the left subtree. Split it at ${i}.`);
|
|
312
|
+
const [l, r] = yield* split(n.left, i);
|
|
313
|
+
yield at(f, 3, { l, r }, "The call returned l and r.");
|
|
314
|
+
n.left = r; yield at(f, 4, null, `N${n.id} keeps r (the lines after the cut) as its left child.`);
|
|
315
|
+
yield* update(n); yield at(f, 5, null, "update(n) returned.");
|
|
316
|
+
return yield* ret(f, 6, [l, n], `Return [l, N${n.id}].`);
|
|
317
|
+
}
|
|
318
|
+
yield at(f, 7, null, "The cut is in the right subtree.");
|
|
319
|
+
yield at(f, 8, null, `Split the right subtree at ${i} - ${lc} - 1 = ${i - lc - 1}.`);
|
|
320
|
+
const [l, r] = yield* split(n.right, i - lc - 1);
|
|
321
|
+
yield at(f, 8, { l, r }, "The call returned l and r.");
|
|
322
|
+
n.right = l; yield at(f, 9, null, `N${n.id} keeps l (the lines before the cut) as its right child.`);
|
|
323
|
+
yield* update(n); yield at(f, 10, null, "update(n) returned.");
|
|
324
|
+
return yield* ret(f, 11, [n, r], `Return [N${n.id}, r].`);
|
|
325
|
+
}
|
|
326
|
+
function* merge(a, b) {
|
|
327
|
+
const f = enter("merge", { a, b });
|
|
328
|
+
if (!a || !b) return yield* ret(f, 1, a || b, `One side is nil: return the other${a || b ? ` (N${(a || b).id})` : ""}.`);
|
|
329
|
+
yield at(f, 1, null, "Neither side is nil.");
|
|
330
|
+
yield at(f, 2, null, `a.prio > b.prio → ${a.prio} > ${b.prio} → ${a.prio > b.prio}`);
|
|
331
|
+
if (a.prio > b.prio) {
|
|
332
|
+
yield at(f, 3, null, `N${a.id} stays on top. Merge its right subtree with b.`);
|
|
333
|
+
const m = yield* merge(a.right, b); a.right = m;
|
|
334
|
+
yield at(f, 3, null, `a.right = the merged tree${m ? ` (N${m.id})` : ""}.`);
|
|
335
|
+
yield* update(a); yield at(f, 4, null, "update(a) returned.");
|
|
336
|
+
return yield* ret(f, 5, a, `Return N${a.id}.`);
|
|
337
|
+
}
|
|
338
|
+
yield at(f, 6, null, "b has the higher priority.");
|
|
339
|
+
yield at(f, 7, null, `N${b.id} stays on top. Merge a with its left subtree.`);
|
|
340
|
+
const m = yield* merge(a, b.left); b.left = m;
|
|
341
|
+
yield at(f, 7, null, `b.left = the merged tree${m ? ` (N${m.id})` : ""}.`);
|
|
342
|
+
yield* update(b); yield at(f, 8, null, "update(b) returned.");
|
|
343
|
+
return yield* ret(f, 9, b, `Return N${b.id}.`);
|
|
344
|
+
}
|
|
345
|
+
function* line_start(i0) {
|
|
346
|
+
const f = enter("line_start", { i: i0 });
|
|
347
|
+
let i = i0, pos = 0; yield at(f, 1, { pos });
|
|
348
|
+
let n = ROOT; yield at(f, 2, { n }, `Start at the root, N${n.id}.`);
|
|
349
|
+
for (;;) {
|
|
350
|
+
yield at(f, 3, null, n ? "n isn't nil." : "n is nil.");
|
|
351
|
+
if (!n) break;
|
|
352
|
+
const lc = cnt(n.left); yield at(f, 4, { lc }, `N${n.id}'s left subtree has ${lc} line(s).`);
|
|
353
|
+
yield at(f, 5, null, `i < lc → ${i} < ${lc} → ${i < lc}`);
|
|
354
|
+
if (i < lc) { n = n.left; yield at(f, 6, { n }, "Go left."); continue; }
|
|
355
|
+
yield at(f, 7, null, `i == lc → ${i} == ${lc} → ${i === lc}`);
|
|
356
|
+
if (i === lc) return yield* ret(f, 8, pos + sum(n.left), `N${n.id} is line ${i0}. It starts at ${pos} + ${sum(n.left)} = ${pos + sum(n.left)}.`);
|
|
357
|
+
pos += sum(n.left) + n.len; yield at(f, 10, { pos }, `Skip the left subtree (Σ ${sum(n.left)}) and N${n.id} (len ${n.len}).`);
|
|
358
|
+
i -= lc + 1; yield at(f, 11, { i }, `Line ${i0} is line ${i} of the right subtree.`);
|
|
359
|
+
n = n.right; yield at(f, 12, { n }, "Go right.");
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function* add_len(n, i, d) {
|
|
363
|
+
const f = enter("add_len", { n, i, d });
|
|
364
|
+
const lc = cnt(n.left); yield at(f, 1, { lc });
|
|
365
|
+
yield at(f, 2, null, `i < lc → ${i} < ${lc} → ${i < lc}`);
|
|
366
|
+
if (i < lc) { yield at(f, 3, null, "Line i is on the left."); yield* add_len(n.left, i, d); yield at(f, 3, null, "returned."); }
|
|
367
|
+
else {
|
|
368
|
+
yield at(f, 4, null, `i > lc → ${i} > ${lc} → ${i > lc}`);
|
|
369
|
+
if (i > lc) { yield at(f, 5, null, `Line i is on the right, as its line ${i - lc - 1}.`); yield* add_len(n.right, i - lc - 1, d); yield at(f, 5, null, "returned."); }
|
|
370
|
+
else { n.len += d; yield at(f, 7, null, `N${n.id} is the line: len += ${d} → ${n.len}.`); }
|
|
371
|
+
}
|
|
372
|
+
n.sum += d; yield at(f, 9, null, `N${n.id}.sum += ${d} → ${n.sum}. Line counts don't change.`);
|
|
373
|
+
leave(f);
|
|
374
|
+
}
|
|
375
|
+
function* insert_line(i, len) {
|
|
376
|
+
const f = enter("insert_line", { i, len });
|
|
377
|
+
yield at(f, 1, null, `Cut the tree before line ${i}.`);
|
|
378
|
+
const [l, r] = yield* split(ROOT, i);
|
|
379
|
+
yield at(f, 1, { l, r }, `l has lines 0…${i - 1}, r has line ${i} and after.`);
|
|
380
|
+
const n = new Node(len, "(new)");
|
|
381
|
+
yield at(f, 2, { n }, `A new node N${n.id} with len ${len} and random priority ${n.prio}.`);
|
|
382
|
+
yield at(f, 3, null, "First merge l with the new node…");
|
|
383
|
+
const ln = yield* merge(l, n);
|
|
384
|
+
yield at(f, 3, null, "…then merge the result with r.");
|
|
385
|
+
const all = yield* merge(ln, r);
|
|
386
|
+
ROOT = all;
|
|
387
|
+
yield at(f, 3, null, `@root is N${all.id}. The lines after the new one got new numbers without being touched.`);
|
|
388
|
+
leave(f);
|
|
389
|
+
}
|
|
390
|
+
function* delete_line(i) {
|
|
391
|
+
const f = enter("delete_line", { i });
|
|
392
|
+
yield at(f, 1, null, `Cut before line ${i}.`);
|
|
393
|
+
const [l, rest] = yield* split(ROOT, i);
|
|
394
|
+
yield at(f, 1, { l, rest });
|
|
395
|
+
yield at(f, 2, null, "Cut the first line off rest.");
|
|
396
|
+
const [gone, r] = yield* split(rest, 1);
|
|
397
|
+
yield at(f, 2, { gone, r }, `gone holds just the deleted line (N${gone.id}).`);
|
|
398
|
+
yield at(f, 3, null, "Merge the two outer parts.");
|
|
399
|
+
const all = yield* merge(l, r);
|
|
400
|
+
ROOT = all;
|
|
401
|
+
yield at(f, 3, null, `@root is N${all.id}. gone is dropped when this method returns.`);
|
|
402
|
+
leave(f);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function run(title, gen) {
|
|
406
|
+
STACK = []; STEPS = []; COUNTS = {}; LASTRET = null; fid = 0; prevNodes = {}; TITLE = title;
|
|
407
|
+
const g = gen(); let r = g.next(); while (!r.done) r = g.next();
|
|
408
|
+
snap(`Done: ${title}${r.value !== undefined ? " → " + r.value : ""}.`, []);
|
|
409
|
+
viewFn = null; viewFrame = null; stop(); show(0);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// ---------------------------------------------------------------- rendering
|
|
413
|
+
const $ = id => document.getElementById(id);
|
|
414
|
+
const esc = s => String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
415
|
+
let cur = 0, viewFn = null, viewFrame = null, timer = null;
|
|
416
|
+
const NW = 92, NH = 58, SW = 100, LH = 86, PAD = 12, TOP = 34;
|
|
417
|
+
|
|
418
|
+
function inorderIds(s, id, out) { if (id == null) return out; inorderIds(s, s.nodes[id].left, out); out.push(id); inorderIds(s, s.nodes[id].right, out); return out; }
|
|
419
|
+
|
|
420
|
+
function drawTree(s) {
|
|
421
|
+
const P = {}, edges = [], lbl = [];
|
|
422
|
+
let slot = 0, maxD = 0;
|
|
423
|
+
s.roots.forEach((rid, t) => {
|
|
424
|
+
const start = slot;
|
|
425
|
+
const w = (id, d) => { if (id == null) return; const n = s.nodes[id]; w(n.left, d + 1); P[id] = { x: PAD + slot * SW, y: TOP + d * LH }; slot++; maxD = Math.max(maxD, d); w(n.right, d + 1);
|
|
426
|
+
if (n.left != null) edges.push([id, n.left]); if (n.right != null) edges.push([id, n.right]); };
|
|
427
|
+
w(rid, 0);
|
|
428
|
+
lbl.push({ x: PAD + ((start + slot - 1) / 2) * SW + NW / 2, t: s.labels[rid].join(", ") });
|
|
429
|
+
if (t < s.roots.length - 1) slot += 0.5;
|
|
430
|
+
});
|
|
431
|
+
const W = Math.max(280, PAD * 2 + slot * SW - (SW - NW)), H = TOP + (maxD + 1) * LH - (LH - NH) + 12;
|
|
432
|
+
const tags = {};
|
|
433
|
+
const top = s.stack[s.stack.length - 1];
|
|
434
|
+
if (top) top.vars.forEach(([k, v]) => { const w = (x, nm) => { if (x.node) (tags[x.node] ||= []).push([nm, ""]); else if (x.arr) x.arr.forEach((y, i) => w(y, `${nm}[${i}]`)); }; w(v, k); });
|
|
435
|
+
if (s.last && s.last.has) retParts(s.last).forEach(([k, v]) => { if (v.node) (tags[v.node] ||= []).push(["⇐" + k, "ret"]); });
|
|
436
|
+
let h = lbl.map(l => `<text class="plabel" x="${l.x}" y="14" text-anchor="middle">${esc(l.t)}</text>`).join("");
|
|
437
|
+
edges.forEach(([a, b]) => { h += `<line class="edge" x1="${P[a].x + NW / 2}" y1="${P[a].y + NH}" x2="${P[b].x + NW / 2}" y2="${P[b].y}"/>`; });
|
|
438
|
+
for (const id in P) {
|
|
439
|
+
const n = s.nodes[id], p = P[id], c = s.chg[id] || [];
|
|
440
|
+
const v = (key, label, val) => c.includes(key) ? `<tspan class="c">${label} ${val}</tspan>` : `${label} ${val}`;
|
|
441
|
+
h += `<g class="${tags[id] ? "ref " : ""}${c.length ? "changed" : ""}" transform="translate(${p.x},${p.y})">`;
|
|
442
|
+
h += `<rect class="nbox" width="${NW}" height="${NH}" rx="6"/>`;
|
|
443
|
+
h += `<text class="nid" x="7" y="15">N${n.id}</text><text class="nprio" x="${NW - 7}" y="15" text-anchor="end">p ${n.prio}</text>`;
|
|
444
|
+
h += `<text class="ntxt" x="7" y="32">${esc([...n.txt].slice(0, 12).join("") + ([...n.txt].length > 12 ? "…" : ""))}</text>`;
|
|
445
|
+
h += `<text class="nstat" x="7" y="49">${v("len", "len", n.len)} ${v("cnt", "cnt", n.cnt)} ${v("sum", "Σ", n.sum)}</text>`;
|
|
446
|
+
h += `<title>N${n.id}: len ${n.len}, priority ${n.prio}; its subtree has ${n.cnt} line(s), ${n.sum} chars</title>`;
|
|
447
|
+
let tx = 0;
|
|
448
|
+
(tags[id] || []).forEach(([t, cl]) => { const w = 8 + 6.4 * [...t].length; h += `<g class="tag ${cl}" transform="translate(${tx},-12)"><rect width="${w}" height="13" rx="3"/><text x="4" y="10">${esc(t)}</text></g>`; tx += w + 3; });
|
|
449
|
+
h += `</g>`;
|
|
450
|
+
}
|
|
451
|
+
const svg = $("svg");
|
|
452
|
+
svg.setAttribute("width", W); svg.setAttribute("height", H); svg.setAttribute("viewBox", `0 0 ${W} ${H}`);
|
|
453
|
+
svg.innerHTML = h;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function drawLines(s) {
|
|
457
|
+
const rid = Object.keys(s.labels).find(id => s.labels[id].includes("@root"));
|
|
458
|
+
const ids = rid ? inorderIds(s, +rid, []) : [];
|
|
459
|
+
let p = 0, h = "";
|
|
460
|
+
const sel = +$("in-i").value;
|
|
461
|
+
ids.forEach((id, k) => { const n = s.nodes[id];
|
|
462
|
+
h += `<tr data-i="${k}"${k === sel ? ' class="sel"' : ""}><td>${k}</td><td>${p}</td><td>${n.len}</td><td class="tx">${esc(n.txt)}</td></tr>`; p += n.len; });
|
|
463
|
+
if (s.roots.length > 1) h += `<tr><td colspan="4" class="tx muted" style="text-align:left">(only the lines @root reaches right now)</td></tr>`;
|
|
464
|
+
$("lines").innerHTML = h;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const fmt = (v, s) => v.nil ? `<span class="muted">nil</span>` : v.node ? `<span class="nref">N${v.node}</span>` : v.arr ? "[" + v.arr.map(x => fmt(x, s)).join(", ") + "]" : esc(v.val);
|
|
468
|
+
function retParts(l) {
|
|
469
|
+
const nm = RET_NAMES[l.fn];
|
|
470
|
+
if (l.val.arr && nm) return l.val.arr.map((x, i) => [nm[i], x]);
|
|
471
|
+
return [["value", l.val]];
|
|
472
|
+
}
|
|
473
|
+
function drawStack(s) {
|
|
474
|
+
let h = "";
|
|
475
|
+
const l = s.last;
|
|
476
|
+
if (l) {
|
|
477
|
+
const ago = STEPS.indexOf(s) - l.step;
|
|
478
|
+
h += `<div class="box ret${ago === 0 ? " now" : ""}"><div class="h">Last return${ago === 0 ? " (just now)" : ` (${ago} step${ago === 1 ? "" : "s"} ago)`}</div><div class="vars">`;
|
|
479
|
+
h += `<span class="k">call</span><span>${esc(l.fn)}(${l.args.map(([k, v]) => `${esc(k)}=${fmt(v, s)}`).join(", ")})</span>`;
|
|
480
|
+
if (l.has) retParts(l).forEach(([k, v]) => { h += `<span class="k">⇐ ${esc(k)}</span><span>${fmt(v, s)}</span>`; });
|
|
481
|
+
else h += `<span class="k">⇐</span><span class="muted">nothing</span>`;
|
|
482
|
+
h += `</div></div>`;
|
|
483
|
+
}
|
|
484
|
+
if (!s.stack.length) h += `<div class="muted">No function is running.</div>`;
|
|
485
|
+
for (let k = s.stack.length - 1; k >= 0; k--) {
|
|
486
|
+
const f = s.stack[k], run = k === s.stack.length - 1, open = run || viewFrame === f.id;
|
|
487
|
+
h += `<div class="box${run ? " run" : ""}"><div class="h" data-frame="${f.id}" role="button" tabindex="0"><span>${esc(f.fn)}</span><span class="l">line ${f.line + 1}</span></div>`;
|
|
488
|
+
if (open) h += `<div class="vars">${f.vars.map(([a, v]) => `<span class="k">${esc(a)}</span><span class="${f.changed.includes(a) ? "chg" : ""}">${fmt(v, s)}</span>`).join("")}</div>`;
|
|
489
|
+
h += `</div>`;
|
|
490
|
+
}
|
|
491
|
+
$("stack").innerHTML = h;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function drawCode(s) {
|
|
495
|
+
let fn = null, line = -1;
|
|
496
|
+
const fr = viewFrame != null && s.stack.find(f => f.id === viewFrame);
|
|
497
|
+
if (fr) { fn = fr.fn; line = fr.line; }
|
|
498
|
+
else if (viewFn) fn = viewFn;
|
|
499
|
+
else if (s.stack.length) { const t = s.stack[s.stack.length - 1]; fn = t.fn; line = t.line; }
|
|
500
|
+
fn ||= "line_start";
|
|
501
|
+
const run = s.stack.length ? s.stack[s.stack.length - 1].fn : null, on = new Set(s.stack.map(f => f.fn));
|
|
502
|
+
$("fns").innerHTML = Object.keys(SRC).map(k => `<button class="fn${k === run ? " run" : on.has(k) ? " on" : ""}${k === fn && (viewFn || fr) ? " view" : ""}" data-fn="${k}">${k}<span class="n">${s.counts[k] || 0}</span></button>`).join("");
|
|
503
|
+
$("code").innerHTML = SRC[fn].split("\n").map((t, i) => {
|
|
504
|
+
const m = t.match(/^(.*?)(\s+#\s.*)?$/);
|
|
505
|
+
const c = esc(m[1]).replace(/\b(def|end|if|elsif|else|while|return|nil)\b/g, '<span class="kw">$1</span>').replace(/(@\w+)/g, '<span class="iv">$1</span>');
|
|
506
|
+
return `<span class="ln${i === line ? " hot" : ""}"><span class="no">${i + 1}</span><span>${c}${m[2] ? `<span class="cm">${esc(m[2])}</span>` : ""}</span></span>`;
|
|
507
|
+
}).join("");
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function show(k) {
|
|
511
|
+
cur = Math.max(0, Math.min(k, STEPS.length - 1));
|
|
512
|
+
const s = STEPS[cur];
|
|
513
|
+
if (viewFrame != null && !s.stack.some(f => f.id === viewFrame)) viewFrame = null;
|
|
514
|
+
drawTree(s); drawLines(s); drawStack(s); drawCode(s);
|
|
515
|
+
const t = s.stack[s.stack.length - 1];
|
|
516
|
+
$("note").innerHTML = (t ? `<b>${esc(t.fn)}, line ${t.line + 1}:</b> ` : "") + esc(s.note);
|
|
517
|
+
$("stepno").textContent = `${TITLE} · step ${cur + 1} of ${STEPS.length}`;
|
|
518
|
+
$("c-first").disabled = $("c-back").disabled = cur === 0;
|
|
519
|
+
$("c-step").disabled = $("c-over").disabled = $("c-out").disabled = $("c-last").disabled = cur === STEPS.length - 1;
|
|
520
|
+
}
|
|
521
|
+
const depth = k => STEPS[k].stack.length;
|
|
522
|
+
const over = () => { const d = depth(cur); for (let k = cur + 1; k < STEPS.length; k++) if (depth(k) <= d) return show(k); show(STEPS.length - 1); };
|
|
523
|
+
const out = () => { const d = depth(cur); for (let k = cur + 1; k < STEPS.length; k++) if (depth(k) < d) return show(k); show(STEPS.length - 1); };
|
|
524
|
+
function play() { if (cur >= STEPS.length - 1) show(0); $("c-play").textContent = "Pause"; const tick = () => { if (cur < STEPS.length - 1) { show(cur + 1); timer = setTimeout(tick, 450); } else stop(); }; timer = setTimeout(tick, 450); }
|
|
525
|
+
function stop() { clearTimeout(timer); timer = null; $("c-play").textContent = "Play"; }
|
|
526
|
+
|
|
527
|
+
// ---------------------------------------------------------------- operations
|
|
528
|
+
const lineCount = () => cnt(ROOT);
|
|
529
|
+
function getI(max) {
|
|
530
|
+
const i = parseInt($("in-i").value, 10);
|
|
531
|
+
if (!(i >= 0 && i <= max)) { $("err").textContent = `Line i must be between 0 and ${max}.`; return null; }
|
|
532
|
+
$("err").textContent = ""; return i;
|
|
533
|
+
}
|
|
534
|
+
$("b-start").onclick = () => { const i = getI(lineCount() - 1); if (i != null) run(`line_start(${i})`, () => line_start(i)); };
|
|
535
|
+
$("b-len").onclick = () => {
|
|
536
|
+
const i = getI(lineCount() - 1); if (i == null) return;
|
|
537
|
+
const d = parseInt($("in-d").value, 10) || 0;
|
|
538
|
+
let len = 0; (function w(n, j) { if (!n) return; const lc = cnt(n.left); if (j < lc) w(n.left, j); else if (j > lc) w(n.right, j - lc - 1); else len = n.len; })(ROOT, i);
|
|
539
|
+
if (len + d < 1) { $("err").textContent = "A line keeps at least its \\n (length 1)."; return; }
|
|
540
|
+
run(`add_len(@root, ${i}, ${d})`, () => add_len(ROOT, i, d));
|
|
541
|
+
};
|
|
542
|
+
$("b-ins").onclick = () => {
|
|
543
|
+
const i = getI(lineCount()); if (i == null) return;
|
|
544
|
+
const len = parseInt($("in-len").value, 10);
|
|
545
|
+
if (!(len >= 1)) { $("err").textContent = "The length must be at least 1 (the \\n)."; return; }
|
|
546
|
+
run(`insert_line(${i}, ${len})`, () => insert_line(i, len));
|
|
547
|
+
};
|
|
548
|
+
$("b-del").onclick = () => {
|
|
549
|
+
if (lineCount() <= 1) { $("err").textContent = "Keep at least one line."; return; }
|
|
550
|
+
const i = getI(lineCount() - 1); if (i != null) run(`delete_line(${i})`, () => delete_line(i));
|
|
551
|
+
};
|
|
552
|
+
const SAMPLE = ["# a treap of lines", "def greet(name)", " puts \"Hi, #{name}\"", "end", "", "greet(\"Vimamsa\")", "greet(\"world\")"];
|
|
553
|
+
function reset() {
|
|
554
|
+
nid = 0; usedP.clear(); ROOT = buildQuiet(SAMPLE);
|
|
555
|
+
STACK = []; STEPS = []; COUNTS = {}; LASTRET = null; prevNodes = {}; TITLE = "new tree";
|
|
556
|
+
snap(`A random treap of ${SAMPLE.length} lines. The order of the lines is fixed; the shape comes from the random priorities.`, []);
|
|
557
|
+
viewFn = null; viewFrame = null; stop(); show(0);
|
|
558
|
+
}
|
|
559
|
+
$("b-reset").onclick = reset;
|
|
560
|
+
$("in-i").oninput = () => show(cur);
|
|
561
|
+
$("lines").onclick = e => { const r = e.target.closest("tr[data-i]"); if (r) { $("in-i").value = r.dataset.i; show(cur); } };
|
|
562
|
+
|
|
563
|
+
$("c-first").onclick = () => { stop(); show(0); };
|
|
564
|
+
$("c-back").onclick = () => { stop(); show(cur - 1); };
|
|
565
|
+
$("c-step").onclick = () => { stop(); show(cur + 1); };
|
|
566
|
+
$("c-over").onclick = () => { stop(); over(); };
|
|
567
|
+
$("c-out").onclick = () => { stop(); out(); };
|
|
568
|
+
$("c-last").onclick = () => { stop(); show(STEPS.length - 1); };
|
|
569
|
+
$("c-play").onclick = () => timer ? stop() : play();
|
|
570
|
+
$("fns").onclick = e => { const b = e.target.closest(".fn"); if (!b) return; viewFrame = null; viewFn = viewFn === b.dataset.fn ? null : b.dataset.fn; show(cur); };
|
|
571
|
+
const pick = e => { const h = e.target.closest("[data-frame]"); if (!h) return; const id = +h.dataset.frame; const s = STEPS[cur];
|
|
572
|
+
viewFn = null; viewFrame = (viewFrame === id || s.stack[s.stack.length - 1].id === id) ? null : id; show(cur); };
|
|
573
|
+
$("stack").onclick = pick;
|
|
574
|
+
$("stack").onkeydown = e => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); pick(e); } };
|
|
575
|
+
document.addEventListener("keydown", e => {
|
|
576
|
+
if (e.target.closest && e.target.closest("input")) return;
|
|
577
|
+
const m = { ArrowRight: () => show(cur + 1), ArrowLeft: () => show(cur - 1), ArrowDown: over, ArrowUp: out, Home: () => show(0), End: () => show(STEPS.length - 1) };
|
|
578
|
+
if (m[e.key]) { e.preventDefault(); stop(); m[e.key](); }
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
reset();
|
|
582
|
+
})();
|
|
583
|
+
</script>
|
|
584
|
+
</body>
|
|
585
|
+
</html>
|