vimamsa 0.1.27 → 0.1.28

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.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +139 -89
  3. data/custom_example.rb +18 -6
  4. data/doc/TODO.md +327 -110
  5. data/doc/action_names_plan.md +442 -0
  6. data/doc/bundling_plan.md +2 -1
  7. data/doc/delta_syntax_plan.md +293 -0
  8. data/doc/demo_recording_plan.md +288 -0
  9. data/doc/design_goals.md +27 -0
  10. data/doc/intro_page_plan.md +266 -0
  11. data/doc/key_bindings_report_plan.md +45 -0
  12. data/doc/line_tree_plan.md +89 -0
  13. data/doc/missing_vim_bindings.md +34 -44
  14. data/doc/modules_settings_plan.md +1 -1
  15. data/doc/registries_plan.md +202 -0
  16. data/doc/startup_speed_plan.md +97 -0
  17. data/doc/treap_visualizer.html +585 -0
  18. data/doc/vim_compatibility_plan.md +186 -31
  19. data/doc/vimlike_vim_conflicts.md +236 -228
  20. data/doc/vm_snapshots_plan.md +4 -4
  21. data/doc/wysiwyg_plan.md +231 -0
  22. data/exe/run_tests.rb +8 -1
  23. data/exe/vimamsa +49 -6
  24. data/help/intro.txt +30 -0
  25. data/help/vim_intro.txt +31 -0
  26. data/img/demo.gif +0 -0
  27. data/img/demo.mp4 +0 -0
  28. data/lang/hyperplaintext.lang +1 -1
  29. data/lib/vimamsa/ack.rb +2 -0
  30. data/lib/vimamsa/actions.rb +381 -36
  31. data/lib/vimamsa/bookmarks.rb +8 -8
  32. data/lib/vimamsa/buffer.rb +83 -37
  33. data/lib/vimamsa/buffer_changetext.rb +58 -36
  34. data/lib/vimamsa/buffer_list.rb +2 -2
  35. data/lib/vimamsa/buffer_manager.rb +6 -6
  36. data/lib/vimamsa/cli_help.rb +75 -0
  37. data/lib/vimamsa/clipboard.rb +6 -3
  38. data/lib/vimamsa/conf.rb +9 -0
  39. data/lib/vimamsa/crash_handler.rb +2 -2
  40. data/lib/vimamsa/debug.rb +2 -2
  41. data/lib/vimamsa/demo_player.rb +458 -0
  42. data/lib/vimamsa/demo_recorder.rb +277 -0
  43. data/lib/vimamsa/diff_buffer.rb +1 -1
  44. data/lib/vimamsa/dot_repeat.rb +104 -0
  45. data/lib/vimamsa/easy_jump.rb +54 -3
  46. data/lib/vimamsa/editor.rb +89 -46
  47. data/lib/vimamsa/file_history.rb +1 -1
  48. data/lib/vimamsa/file_manager.rb +23 -23
  49. data/lib/vimamsa/form_input.rb +388 -0
  50. data/lib/vimamsa/gui.rb +83 -13
  51. data/lib/vimamsa/gui_command_line.rb +44 -5
  52. data/lib/vimamsa/gui_form_generator.rb +17 -6
  53. data/lib/vimamsa/gui_key_hint.rb +165 -0
  54. data/lib/vimamsa/gui_keylog_panel.rb +164 -2
  55. data/lib/vimamsa/gui_menu.rb +69 -50
  56. data/lib/vimamsa/gui_select_window.rb +77 -43
  57. data/lib/vimamsa/gui_settings.rb +9 -5
  58. data/lib/vimamsa/gui_sourceview.rb +15 -81
  59. data/lib/vimamsa/gui_theme.rb +11 -2
  60. data/lib/vimamsa/hook.rb +2 -0
  61. data/lib/vimamsa/intro_page.rb +368 -0
  62. data/lib/vimamsa/key_actions.rb +315 -225
  63. data/lib/vimamsa/key_binding_tree.rb +228 -61
  64. data/lib/vimamsa/key_bindings_notepad.rb +18 -16
  65. data/lib/vimamsa/key_bindings_report.rb +290 -67
  66. data/lib/vimamsa/key_bindings_vim.rb +47 -297
  67. data/lib/vimamsa/key_bindings_vimlike.rb +347 -339
  68. data/lib/vimamsa/key_names.rb +80 -0
  69. data/lib/vimamsa/line_tree.rb +367 -0
  70. data/lib/vimamsa/macro.rb +20 -3
  71. data/lib/vimamsa/rbvma.rb +15 -0
  72. data/lib/vimamsa/registers.rb +125 -0
  73. data/lib/vimamsa/run_ruby.rb +64 -0
  74. data/lib/vimamsa/search_replace.rb +4 -4
  75. data/lib/vimamsa/string_util.rb +17 -3
  76. data/lib/vimamsa/tests.rb +8 -8
  77. data/lib/vimamsa/text_objects.rb +11 -54
  78. data/lib/vimamsa/text_transforms.rb +4 -0
  79. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  80. data/lib/vimamsa/user_dirs.rb +83 -0
  81. data/lib/vimamsa/version.rb +1 -1
  82. data/lib/vimamsa/vim_core.rb +355 -0
  83. data/lib/vimamsa/vim_operators.rb +365 -0
  84. data/modules/calculator/calculator.rb +5 -5
  85. data/modules/color_highlight/color_highlight.rb +3 -3
  86. data/modules/dictation/dictation.rb +32 -32
  87. data/modules/ollama/ollama.rb +5 -5
  88. data/modules/terminal/terminal.rb +5 -5
  89. data/modules/tts/tts.rb +10 -10
  90. data/run_e2e_vm.sh +3 -3
  91. data/run_tests_vm.rb +138 -0
  92. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  93. data/scripts/demo/demo_files_steps.txt +52 -0
  94. data/scripts/demo/demo_steps.txt +9 -1
  95. data/scripts/demo/notes.txt +6 -0
  96. data/scripts/demo/record_demo.rb +60 -81
  97. data/scripts/demo/report.rb +7 -0
  98. data/scripts/demo/shapes.rb +1 -0
  99. data/scripts/demo/square/shapes.rb +29 -0
  100. data/scripts/demo/square/steps.txt +185 -0
  101. data/scripts/lib/perf_startup.rb +138 -0
  102. data/scripts/lib/script_helpers.rb +53 -0
  103. data/scripts/perf_compare.rb +34 -0
  104. data/scripts/profile_startup.rb +72 -0
  105. data/tests/test_action_ids.rb +232 -0
  106. data/tests/test_auto_format.rb +2 -2
  107. data/tests/test_autocomplete.rb +1 -1
  108. data/tests/test_basic_editing.rb +18 -18
  109. data/tests/test_bookmarks.rb +6 -6
  110. data/tests/test_bracket_match.rb +3 -0
  111. data/tests/test_cli_help.rb +64 -0
  112. data/tests/test_color_highlight.rb +2 -2
  113. data/tests/test_command_line.rb +1 -1
  114. data/tests/test_context_menu_file.rb +18 -0
  115. data/tests/test_copy_paste.rb +33 -33
  116. data/tests/test_custom_two_phase.rb +1 -1
  117. data/tests/test_demo_player.rb +301 -0
  118. data/tests/test_demo_recorder.rb +202 -0
  119. data/tests/test_dictation.rb +16 -16
  120. data/tests/test_dot_repeat.rb +88 -0
  121. data/tests/test_easy_jump.rb +93 -0
  122. data/tests/test_extract_file_links.rb +3 -3
  123. data/tests/test_file_panel.rb +2 -2
  124. data/tests/test_flash.rb +1 -1
  125. data/tests/test_fold.rb +1 -1
  126. data/tests/test_form_input.rb +203 -0
  127. data/tests/test_func_panel.rb +2 -2
  128. data/tests/test_gtk_sync.rb +7 -7
  129. data/tests/test_intro_page.rb +437 -0
  130. data/tests/test_key_categories.rb +163 -0
  131. data/tests/test_keylog_panel.rb +163 -3
  132. data/tests/test_line_ends.rb +51 -0
  133. data/tests/test_line_tree.rb +86 -0
  134. data/tests/test_lsp_clangd.rb +2 -2
  135. data/tests/test_macro.rb +2 -2
  136. data/tests/test_module_interface.rb +9 -9
  137. data/tests/test_notepad_bindings.rb +5 -5
  138. data/tests/test_perf_startup.rb +28 -0
  139. data/tests/test_random_edit.rb +27 -27
  140. data/tests/test_registers.rb +122 -0
  141. data/tests/test_run_ruby.rb +77 -0
  142. data/tests/test_startup_file.rb +1 -1
  143. data/tests/test_text_objects.rb +1 -1
  144. data/tests/test_text_transforms.rb +12 -0
  145. data/tests/test_tmp_user_dir.rb +79 -0
  146. data/tests/test_tts.rb +10 -10
  147. data/tests/test_undo.rb +34 -34
  148. data/tests/test_user_dirs.rb +122 -0
  149. data/tests/test_vim_bindings.rb +107 -6
  150. data/tests/test_vim_conflicts.rb +41 -4
  151. data/tests/test_vim_operators.rb +116 -0
  152. data/tests/test_window_layout.rb +83 -0
  153. data/vimamsa.gemspec +1 -1
  154. data/vm_guest_run.sh +22 -2
  155. metadata +58 -3
  156. data/run_tests_vm.sh +0 -130
@@ -0,0 +1,437 @@
1
+ # The introduction page and the Vim summary (intro_page.rb, help/*.txt):
2
+ # ⟦run:…⟧ links and their buttons, {key:…} placeholders, read-only help
3
+ # buffers, the demo list above the key log, and when the page is shown at
4
+ # start (doc/intro_page_plan.md)
5
+ require "rbconfig"
6
+ require "tmpdir"
7
+
8
+ class TestIntroPage < VmaTest
9
+ REPO = File.expand_path("..", __dir__)
10
+
11
+ def setup_counter
12
+ $intro_test_runs = 0
13
+ reg_act("help.zz_intro_test", proc { $intro_test_runs += 1 }, "test")
14
+ reg_act("edit.zz_intro_test", proc { $intro_test_runs += 1 }, "test")
15
+ end
16
+
17
+ def close_help_pages
18
+ vma.buffers.list.select { |b| b.help_page }.each { |b| vma.buffers.close_buffer(b.id) }
19
+ end
20
+
21
+ def stop_demo
22
+ p = DemoPlayer.current
23
+ return if p.nil?
24
+ p.stop
25
+ (p.bufs || []).each { |b| vma.buffers.close_buffer(b.id) if vma.buffers.list.any? { |x| x.equal?(b) } }
26
+ drain_idle
27
+ end
28
+
29
+ # ── Links ────────────────────────────────────────────────────────────────
30
+
31
+ def test_link_at
32
+ s = "abc ⟦run:help.x|Two words⟧ d ⟦help:intro⟧\n"
33
+ assert_eq "⟦run:help.x|Two words⟧", hpt_link_at(s, s.index("words"))
34
+ assert_eq "⟦help:intro⟧", hpt_link_at(s, s.index("intro"))
35
+ assert_eq nil, hpt_link_at(s, 1)
36
+ end
37
+
38
+ def test_enter_runs_allowed_action
39
+ setup_counter
40
+ create_new_buffer("x ⟦run:help.zz_intro_test|Run it now⟧\n", "linktest")
41
+ buf.set_pos(buf.index("it now"))
42
+ buf.handle_line_action
43
+ assert_eq 1, $intro_test_runs
44
+ ensure
45
+ vma.actions.unregister("help.zz_intro_test") rescue nil
46
+ vma.actions.unregister("edit.zz_intro_test") rescue nil
47
+ end
48
+
49
+ # Any .txt file can hold a ⟦run:…⟧ link: only allowed actions run
50
+ def test_disallowed_action_not_run
51
+ setup_counter
52
+ create_new_buffer("⟦run:edit.zz_intro_test|Do it⟧\n", "linktest")
53
+ buf.set_pos(3)
54
+ buf.handle_line_action
55
+ assert_eq 0, $intro_test_runs
56
+ assert !hpt_run_allowed?("app.quit")
57
+ assert hpt_run_allowed?("help.demo")
58
+ assert hpt_run_allowed?("app.demo")
59
+ ensure
60
+ vma.actions.unregister("help.zz_intro_test") rescue nil
61
+ vma.actions.unregister("edit.zz_intro_test") rescue nil
62
+ end
63
+
64
+ def test_help_link_opens_vim_intro
65
+ create_new_buffer("see ⟦help:vim_intro⟧\n", "linktest")
66
+ buf.set_pos(6)
67
+ buf.handle_line_action
68
+ assert_eq "vim_intro", vma.buf.help_page
69
+ ensure
70
+ close_help_pages
71
+ end
72
+
73
+ # ── The page ─────────────────────────────────────────────────────────────
74
+
75
+ def test_intro_page_filled_in
76
+ b = show_help_page("intro")
77
+ assert_eq "intro", b.help_page
78
+ assert b.read_only
79
+ assert !b.include?("{"), "placeholder left: #{b.lines.grep(/\{/).first}"
80
+ assert !b.include?("(no key for"), "unbound key: #{b.lines.grep(/no key for/).first}"
81
+ DemoPlayer::DEMOS.each_value { |d| assert b.include?("⟦run:#{d[:action]}|▶ #{d[:title]}⟧"), d[:title] }
82
+ assert b.include?("⦁ctrl!⦁ or ⦁i⦁"), "mode.insert keys: #{b.lines.grep(/Insert mode/).first}"
83
+ ensure
84
+ close_help_pages
85
+ end
86
+
87
+ # The links to the other help pages are buttons too, and open them
88
+ def test_help_page_links_are_buttons
89
+ b = show_help_page("intro")
90
+ drain_idle
91
+ %w[help.vim_intro help.keys].each { |act|
92
+ assert b.help_buttons.any? { |a, _, _| a == act }, "no button for #{act}"
93
+ }
94
+ btn = b.help_buttons.find { |a, _, _| a == "help.vim_intro" }[1]
95
+ btn.signal_emit("clicked")
96
+ assert_eq "vim_intro", vma.buf.help_page
97
+ ensure
98
+ close_help_pages
99
+ end
100
+
101
+ # On a read-only page the ⦁ marks are hidden, the text between them is
102
+ # not, and the cursor steps over a hidden mark
103
+ def test_marks_hidden_on_help_page
104
+ b = show_help_page("intro")
105
+ drain_idle
106
+ # the first marked key on the page (e.g. ⦁ctrl!⦁ from {key:...})
107
+ m = b.match(/⦁[^⦁\n]+⦁/)
108
+ assert m, "no ⦁ marks on the page"
109
+ i = m.begin(0)
110
+ assert hidden?(b, i), "opening mark shown"
111
+ assert hidden?(b, m.end(0) - 1), "closing mark shown"
112
+ assert !hidden?(b, i + 1), "key text hidden"
113
+ b.set_pos(i - 1)
114
+ hpt_skip_hidden_marks(b)
115
+ keys "l"
116
+ assert_eq i + 1, b.pos, "cursor on the hidden opening mark"
117
+ keys "h"
118
+ assert_eq i - 1, b.pos, "cursor back on the hidden opening mark"
119
+ # an editable buffer shows them
120
+ e = create_new_buffer("a ⦁key⦁ b\n", "marktest")
121
+ gui_set_file_lang(e.id, "hyperplaintext")
122
+ hpt_hide_marks(e)
123
+ assert !hidden?(e, 2), "mark hidden in an editable buffer"
124
+ ensure
125
+ close_help_pages
126
+ end
127
+
128
+ # The keys come from the bindings, so the page works in notepad too
129
+ # (which never leaves Insert mode, so it has no I:mode.prev)
130
+ def test_keys_resolve_in_notepad
131
+ kbd = load_scheme_kbd(["vimlike", "notepad"])
132
+ specs = HELP_PAGES.flat_map { |n| File.read(File.join(HELP_DIR, "#{n}.txt")).scan(/\{key:([^}]+)\}/).flatten }
133
+ assert specs.size >= 8
134
+ (specs - ["I:mode.prev"]).each { |s|
135
+ t = help_key_text(s, kbd)
136
+ assert !t.include?("no key for"), "#{s}: #{t}"
137
+ }
138
+ end
139
+
140
+ def test_opening_twice_keeps_one_buffer
141
+ a = show_help_page("intro")
142
+ create_new_buffer("other\n", "other")
143
+ b = show_help_page("intro")
144
+ assert a.equal?(b)
145
+ assert vma.buf.equal?(a)
146
+ assert_eq 1, vma.buffers.list.count { |x| x.help_page == "intro" }
147
+ ensure
148
+ close_help_pages
149
+ end
150
+
151
+ def test_read_only
152
+ b = show_help_page("intro")
153
+ text = b.to_s.dup
154
+ keys "x d d"
155
+ keys "i a b c esc"
156
+ assert_eq text, b.to_s
157
+ ensure
158
+ vma.kbd.set_mode(:command)
159
+ close_help_pages
160
+ end
161
+
162
+ def test_command_line_and_menu
163
+ vma.gui.cmd_line.execute("intro")
164
+ assert_eq "intro", vma.buf.help_page
165
+ assert_eq "Help ▸ Introduction", vma.gui.menu.path_for("help.intro")
166
+ assert_eq "Help ▸ Demos ▸ Easy jump", vma.gui.menu.path_for("help.demo.easyjump")
167
+ ensure
168
+ close_help_pages
169
+ end
170
+
171
+ # ── Buttons and demos ────────────────────────────────────────────────────
172
+
173
+ # One button per ⟦run:…⟧ line; the GTK buffer keeps the Buffer's length
174
+ def test_buttons_added
175
+ b = show_help_page("intro")
176
+ drain_idle
177
+ n = b.lines.count { |l| l.include?("⟦run:") }
178
+ assert_eq n, b.help_buttons.size
179
+ assert_eq DemoPlayer::DEMOS.size + 4, n
180
+ assert_eq b.size, b.view.buffer.char_count
181
+ ensure
182
+ close_help_pages
183
+ end
184
+
185
+ def hidden?(b, pos)
186
+ tag = b.view.buffer.tag_table.lookup("hpt_hidden")
187
+ !tag.nil? && b.view.buffer.get_iter_at(offset: pos).has_tag?(tag)
188
+ end
189
+
190
+ # On a read-only page the buttons replace the link text everywhere; the
191
+ # cursor moves off the hidden text onto the button, where enter runs it
192
+ def test_buttons_replace_links_on_help_page
193
+ setup_counter
194
+ b = show_help_page("intro")
195
+ drain_idle
196
+ from = b.index("⟦run:help.demo|")
197
+ assert_eq HPT_BUTTON_SLOT, b[from - 1]
198
+ assert !hidden?(b, from - 1), "the button's place hidden"
199
+ assert hidden?(b, from), "link text shown"
200
+ assert hidden?(b, from + 5)
201
+ btn = b.help_buttons[0][1]
202
+ assert btn.visible?, "button hidden"
203
+ assert_eq "pointer", btn.cursor&.name, "mouse pointer over the button"
204
+ # the cursor on the link's line: still hidden (read-only)
205
+ b.set_pos(from + 3)
206
+ hpt_update_run_buttons(b)
207
+ assert hidden?(b, from), "link shown with the cursor on its line"
208
+ assert_eq from - 1, b.pos, "cursor not moved onto the button"
209
+ ensure
210
+ vma.actions.unregister("help.zz_intro_test") rescue nil
211
+ vma.actions.unregister("edit.zz_intro_test") rescue nil
212
+ close_help_pages
213
+ end
214
+
215
+ # The cursor treats a button as one char: w and l from the button go past
216
+ # its (hidden) link, b and h come back onto it, w from before it stops on
217
+ # it; and many w's in a row move on without getting stuck
218
+ def test_cursor_treats_button_as_one_char
219
+ b = show_help_page("intro")
220
+ drain_idle
221
+ at = b.index("#{HPT_BUTTON_SLOT}⟦run:help.demo|")
222
+ to = b.index("⟧", at) + 1
223
+ b.set_pos(at)
224
+ hpt_skip_button(b)
225
+ keys "l"
226
+ assert_eq to, b.pos, "l from the button"
227
+ keys "h"
228
+ assert_eq at, b.pos, "h back onto the button"
229
+ keys "w"
230
+ assert_eq b.index(/\S/, to), b.pos, "w from the button: the next word after the link"
231
+ keys "b"
232
+ assert_eq at, b.pos, "b back onto the button"
233
+ # w from the end of the line above stops on the button
234
+ b.set_pos(at - 2)
235
+ hpt_skip_button(b)
236
+ keys "w"
237
+ assert_eq at, b.pos, "w onto the button"
238
+ last = b.pos
239
+ 40.times {
240
+ keys "w"
241
+ assert b.pos > last || b.pos >= b.size - 2, "w stuck at #{b.pos}: #{b[b.pos, 20].inspect}"
242
+ last = b.pos
243
+ break if b.pos >= b.size - 2
244
+ }
245
+ ensure
246
+ close_help_pages
247
+ end
248
+
249
+ # In a buffer that can be edited, the link text shows instead of the
250
+ # button while the cursor is on its line
251
+ def test_link_code_shown_on_cursor_line_when_editable
252
+ setup_counter
253
+ b = create_new_buffer("first line\nx#{HPT_BUTTON_SLOT}⟦run:help.zz_intro_test|Go⟧ y\nlast\n", "linktest")
254
+ help_add_run_buttons(b)
255
+ btn = b.help_buttons[0][1]
256
+ from = b.index("⟦")
257
+ b.set_pos(0)
258
+ hpt_update_run_buttons(b)
259
+ assert hidden?(b, from)
260
+ assert btn.visible?
261
+ keys "j"
262
+ assert !hidden?(b, from), "link code hidden with the cursor on its line"
263
+ assert !btn.visible?, "button shown with the cursor on its line"
264
+ keys "j"
265
+ assert hidden?(b, from)
266
+ assert btn.visible?
267
+ assert_eq b.size, b.view.buffer.char_count
268
+ btn.signal_emit("clicked")
269
+ assert_eq 1, $intro_test_runs
270
+ ensure
271
+ vma.actions.unregister("help.zz_intro_test") rescue nil
272
+ vma.actions.unregister("edit.zz_intro_test") rescue nil
273
+ end
274
+
275
+ # Enter on the button position (before the hidden link) runs the link
276
+ def test_enter_on_button_position
277
+ setup_counter
278
+ b = create_new_buffer("x ⟦run:help.zz_intro_test|Go⟧\n", "linktest")
279
+ b.set_pos(1)
280
+ b.handle_line_action
281
+ assert_eq 1, $intro_test_runs
282
+ ensure
283
+ vma.actions.unregister("help.zz_intro_test") rescue nil
284
+ vma.actions.unregister("edit.zz_intro_test") rescue nil
285
+ end
286
+
287
+ # A demo started from the page goes back to the page when it stops
288
+ def test_button_starts_demo_and_returns
289
+ intro = show_help_page("intro")
290
+ drain_idle
291
+ _, btn = intro.help_buttons.find { |a, _| a == "help.demo.easyjump" }
292
+ btn.signal_emit("clicked")
293
+ drain_idle
294
+ p = DemoPlayer.current
295
+ assert p.running?, "demo not started"
296
+ assert_eq :easyjump, p.name
297
+ assert !vma.buf.equal?(intro)
298
+ p.stop("Demo stopped")
299
+ drain_idle
300
+ assert vma.buf.equal?(intro), "not back on the intro page"
301
+ ensure
302
+ stop_demo
303
+ close_help_pages
304
+ end
305
+
306
+ # The demo list above the key log switches demos, keeping the speed
307
+ def test_demo_list_switches
308
+ DemoPlayer.toggle(:basic)
309
+ a = DemoPlayer.current
310
+ a.faster
311
+ rate = a.rate
312
+ list = vma.gui.keylog_panel.demo_list
313
+ assert_eq 0, list.selected
314
+ list.selected = DemoPlayer::DEMOS.keys.index(:easyjump)
315
+ # the switch runs in an idle callback
316
+ t = Time.now
317
+ until !DemoPlayer.current.equal?(a) || Time.now - t > 5
318
+ drain_idle
319
+ sleep 0.01
320
+ end
321
+ b = DemoPlayer.current
322
+ assert !a.running?, "the basic demo still runs"
323
+ assert b.running?
324
+ assert_eq :easyjump, b.name
325
+ assert_eq rate, b.rate
326
+ assert_eq DemoPlayer::DEMOS.keys.index(:easyjump), list.selected
327
+ ensure
328
+ [a, b].compact.each { |x| x.stop; (x.bufs || []).each { |y| vma.buffers.close_buffer(y.id) if vma.buffers.list.any? { |z| z.equal?(y) } } }
329
+ drain_idle
330
+ end
331
+
332
+ def test_demo_duration
333
+ DemoPlayer::DEMOS.each_key { |n|
334
+ d = DemoPlayer.duration(n)
335
+ assert d > 5 && d < 600, "#{n}: #{d} s"
336
+ assert DemoPlayer.duration_text(n).match?(/\A\d+ (s|min)\z/)
337
+ }
338
+ end
339
+
340
+ # ── Vim summary ──────────────────────────────────────────────────────────
341
+
342
+ # Each row of the "keys that do something else" table matches the keymap
343
+ def test_vim_key_table_matches_bindings
344
+ all = vma.kbd.bindings
345
+ VIM_INTRO_KEYS.each { |mode, keys, _vim, action|
346
+ b = all.find { |x| x[:mode] == mode && x[:keys].join(" ") == keys && x[:template].nil? }
347
+ assert b, "#{mode} #{keys}: not bound"
348
+ assert_eq action, b[:action].to_s, "#{mode} #{keys}"
349
+ }
350
+ page = show_help_page("vim_intro")
351
+ VIM_INTRO_KEYS.each { |_, keys, vim, _| assert page.lines.any? { |l| l.include?(keys) && l.include?(vim) }, keys }
352
+ ensure
353
+ close_help_pages
354
+ end
355
+
356
+ # ── At start ─────────────────────────────────────────────────────────────
357
+
358
+ def test_intro_on_startup_rules
359
+ was = cnf.intro.show_on_startup?
360
+ sf = cnf.startup_file!
361
+ cnf.intro.show_on_startup = true
362
+ cnf.startup_file = false
363
+ rule = lambda { |argv: [], forced: false, test_mode: false, session: []|
364
+ intro_on_startup?(argv: argv, forced: forced, test_mode: test_mode, session: session)
365
+ }
366
+ assert rule.(), "fresh start"
367
+ assert rule.(forced: true, test_mode: true, session: ["x"]), "--intro"
368
+ assert !rule.(test_mode: true), "test mode"
369
+ assert !rule.(argv: [File.join(REPO, "README.md")]), "file argument"
370
+ assert !rule.(argv: [REPO]), "dir argument"
371
+ assert !rule.(session: ["/x"]), "session to restore"
372
+ cnf.startup_file = "/nonexistent/startup.txt"
373
+ assert rule.(), "startup file that doesn't exist"
374
+ cnf.startup_file = File.join(REPO, "README.md")
375
+ assert !rule.(), "startup file opened"
376
+ cnf.startup_file = false
377
+ cnf.intro.show_on_startup = false
378
+ assert !rule.(), "setting off"
379
+ ensure
380
+ cnf.intro.show_on_startup = was
381
+ cnf.startup_file = sf
382
+ end
383
+
384
+ # Real starts: shown with a fresh config dir, not the second time, not
385
+ # with a file to open; --intro shows it anyway
386
+ def test_shown_on_first_start
387
+ skip "needs a display" unless ENV["DISPLAY"] || ENV["WAYLAND_DISPLAY"]
388
+ Dir.mktmpdir("vma-intro") { |tmp|
389
+ ud = File.join(tmp, "user")
390
+ assert_eq "intro", start_and_report(ud)
391
+ assert_eq "nil", start_and_report(ud)
392
+ assert_eq "intro", start_and_report(ud, "--intro")
393
+ assert_eq "nil", start_and_report(File.join(tmp, "user2"), File.join(REPO, "README.md"))
394
+ assert File.read(File.join(ud, "settings.rb")).include?("cnf.intro.show_on_startup = false")
395
+ }
396
+ end
397
+
398
+ # Starts the editor with user dir ud; returns the help page shown once it
399
+ # is up ("nil" for none)
400
+ def start_and_report(ud, *args)
401
+ FileUtils.mkdir_p(ud)
402
+ out = File.join(ud, "shown.txt")
403
+ FileUtils.rm_f(out)
404
+ # custom.rb as on a real first start (from custom_example.rb), plus a
405
+ # report of the page shown
406
+ File.write(File.join(ud, "custom.rb"), File.read(File.join(REPO, "custom_example.rb")) + <<~RUBY)
407
+ cnf.lsp.enabled = false
408
+ def hook_custom_after_init
409
+ GLib::Idle.add {
410
+ File.write(#{out.inspect}, vma.buf.help_page.inspect.delete('"'))
411
+ GLib::Timeout.add(200) { vma.shutdown; false }
412
+ false
413
+ }
414
+ end
415
+ RUBY
416
+ pid = Process.spawn(RbConfig.ruby, File.join(REPO, "exe/vimamsa"), "--user-dir", ud, *args,
417
+ out: File::NULL, err: File.join(ud, "log.txt"))
418
+ t = Time.now
419
+ until File.exist?(out) && File.size(out) > 0
420
+ if Process.waitpid(pid, Process::WNOHANG)
421
+ pid = nil
422
+ break
423
+ end
424
+ if Time.now - t > 60
425
+ break
426
+ end
427
+ sleep 0.05
428
+ end
429
+ if pid
430
+ t = Time.now
431
+ sleep 0.05 while Process.waitpid(pid, Process::WNOHANG).nil? && Time.now - t < 20
432
+ Process.kill("KILL", pid) rescue nil
433
+ Process.wait(pid) rescue nil
434
+ end
435
+ File.exist?(out) ? File.read(out) : "no report (log: #{File.read(File.join(ud, "log.txt"))[-500..]})"
436
+ end
437
+ end
@@ -0,0 +1,163 @@
1
+ # Structured binding records (KeyBindingTree#bindings) and the categories of
2
+ # the key bindings page (doc/key_bindings_report_plan.md steps 1-2).
3
+ class TestKeyCategories < VmaTest
4
+ def find(kbd, mode, keys)
5
+ kbd.bindings.find { |b| b[:mode] == mode && b[:keys].join(" ") == keys }
6
+ end
7
+
8
+ def test_binding_records
9
+ b = find(vma.kbd, "C", "d i <char>")
10
+ assert !b.nil?, "text object binding missing"
11
+ assert_eq "C <op> <textobj>", b[:template]
12
+ assert_eq({ op: :delete, textobj: :inner }, b[:args])
13
+ u = find(vma.kbd, "C", "u")
14
+ assert_eq "undo.last", u[:action]
15
+ assert_eq "Undo the last action", u[:desc] # dotted ids show their description
16
+ g = find(vma.kbd, "C", "G(vma.kbd.next_command_count!=nil)")
17
+ assert !g.nil?, "condition missing from keys"
18
+ # an action on a node that also has longer bindings below it
19
+ assert !find(vma.kbd, "C", "z").nil?, "C z (browse mode) not listed"
20
+ end
21
+
22
+ # Every binding of every keymap has a category, so nothing lands in a
23
+ # catch-all section. A new binding without one fails here: give it a named
24
+ # action, or put it in an add_keys block tagged "cat:<id>".
25
+ def test_every_binding_has_a_category
26
+ [["vimlike"], ["vim"], ["vimlike", "notepad"]].each { |scheme|
27
+ kbd = load_scheme_kbd(scheme)
28
+ bad = kbd.bindings.select { |b| key_binding_category(b, kbd).nil? }
29
+ assert bad.empty?, "#{scheme.join("+")}: no category for " +
30
+ bad.map { |b| "#{b[:mode]} #{b[:keys].join(" ")} => #{b[:action].inspect}" }.inspect
31
+ }
32
+ end
33
+
34
+ # The running tree also has the bindings of modules, minor modes and
35
+ # custom.rb (other tests leave ad-hoc bindings in it, so only those are checked)
36
+ def test_live_bindings_have_known_categories
37
+ ids = KEY_CATEGORIES.map(&:first)
38
+ vma.kbd.bindings.each { |b|
39
+ from_context = b[:mode].match?(/\A\p{Ll}/) || b[:keywords].any? { |k| k == "custom" || k.start_with?("module:") }
40
+ next if !from_context
41
+ c = key_binding_category(b)
42
+ assert !c.nil?, "no category: #{b[:mode]} #{b[:keys].join(" ")} => #{b[:action].inspect}"
43
+ assert ids.include?(c) || c.start_with?("module:", "mode:"), "unknown category #{c.inspect}"
44
+ }
45
+ end
46
+
47
+ def test_categories_from_namespace_mode_and_context
48
+ kbd = KeyBindingTree.new(actions: ActionList.new)
49
+ kbd.add_mode("C", :command)
50
+ kbd.add_minor_mode("fexp", :file_exp, :command)
51
+ kbd.bindkey("C , Q 1", "move.word.next")
52
+ kbd.bindkey("fexp , Q 2", "move.word.next")
53
+ kbd.with_keywords(["module:foo"]) { kbd.bindkey("C , Q 3", "foo_bar()") }
54
+ kbd.bindkey("C , Q 4", "foo_bar()", keywords: "cat:edit")
55
+ kbd.bindkey("C , Q 5", "foo_bar()")
56
+ cat = ->(k) { key_binding_category(find(kbd, k.split[0], k.split[1..].join(" ")), kbd) }
57
+ assert_eq "motion", cat.("C , Q 1")
58
+ assert_eq "mode:fexp", cat.("fexp , Q 2")
59
+ assert_eq "module:foo", cat.("C , Q 3")
60
+ assert_eq "edit", cat.("C , Q 4")
61
+ assert_eq nil, cat.("C , Q 5")
62
+ assert kbd.extra_keywords.empty?, "with_keywords leaked"
63
+ end
64
+
65
+ def test_page_sections
66
+ show_key_bindings
67
+ s = vma.buf.to_s
68
+ begin
69
+ heads = s.scan(/^◼ (.+)$/).flatten
70
+ assert heads.include?("Moving the cursor") && heads.include?("Text objects"), heads.inspect
71
+ assert heads.index("Moving the cursor") < heads.index("Text objects")
72
+ assert !heads.include?("Others"), heads.inspect
73
+ # bindings other tests left behind may be uncategorized: last, if at all
74
+ assert !heads.include?("Uncategorized") || heads.last == "Uncategorized", heads.inspect
75
+ assert s.include?("Undo the last action"), "descriptions missing"
76
+ ensure
77
+ vma.buffers.close_buffer(vma.buf.id)
78
+ end
79
+ end
80
+
81
+ # Step 3: rows merged across modes and keys, grouped by modes, conditions
82
+ # as text, definition order
83
+ def test_section_layout
84
+ kbd = KeyBindingTree.new(actions: ActionList.new)
85
+ kbd.add_mode("C", :command)
86
+ kbd.add_mode("V", :visual, :visual)
87
+ kbd.bindkey("VC , Q 2", "move.word.next")
88
+ kbd.bindkey("VC , Q 1", "move.word.next")
89
+ kbd.bindkey("C , Q 3(vma.kbd.next_command_count!=nil)", "move.word.next")
90
+ kbd.bindkey("C , Q 4(foo_bar)", "move.word.prev")
91
+ w = ", Q 2 / , Q 1".size
92
+ assert_eq ["◼◼ Command, Visual",
93
+ " , Q 2 / , Q 1 move.word.next",
94
+ "◼◼ Command",
95
+ " #{", Q 3".ljust(w)} move.word.next (with a count)",
96
+ " #{", Q 4".ljust(w)} move.word.prev (if foo_bar)"],
97
+ key_bindings_section(kbd.bindings)
98
+ end
99
+
100
+ def test_page_layout
101
+ s = key_bindings_page(vma.kbd, scheme: "vimlike")
102
+ assert s.start_with?("❙Key bindings — scheme: vimlike❙"), s[0, 80]
103
+ heads = s.scan(/^◼ (.+)$/).flatten
104
+ assert_eq "Basics", heads[0]
105
+ basics = s[/^◼ Basics\n(.*?)\n\n/m, 1]
106
+ assert basics.include?(vma.actions["undo.last"].method_name), basics
107
+ assert s.include?("(with a count)"), "condition text missing"
108
+ assert !s.include?("(vma.kbd.next_command_count"), "raw condition shown"
109
+ motion = s[/^◼ Moving the cursor\n(.*?)\n\n/m, 1]
110
+ e, b = %w[move.word.end move.word.prev].map { |id| motion.index(vma.actions[id].method_name) }
111
+ assert e && b && e < b, "not in definition order:\n#{motion}"
112
+ assert motion.include?("◼◼ Command, Visual"), motion
113
+ # templates: one line each, and the key sets listed
114
+ assert s.include?("<op> <motion>"), "template line missing"
115
+ assert !s.match?(/^ +c w /), "template expansions listed one by one"
116
+ assert s.include?("◼ Operators, motions and text objects"), "key sets missing"
117
+ end
118
+
119
+ # doc/action_names_plan.md step 6: every binding of every keymap runs a
120
+ # named action with a description, not Ruby code. A new Ruby-string
121
+ # binding fails here: register an action (reg_act) and bind its id.
122
+ # Readchar mode ("R <char>") is internal and not named.
123
+ def test_every_binding_has_a_description
124
+ [["vimlike"], ["vim"], ["vimlike", "notepad"]].each { |scheme|
125
+ kbd = load_scheme_kbd(scheme)
126
+ bad = kbd.bindings.reject { |b|
127
+ next true if b[:mode] == "R"
128
+ act = b[:action]
129
+ next false if act.class == Proc
130
+ r = [kbd.actions, vma.actions].map { |l| l[act] }.find { |x| x && !x.opt&.[](:code) }
131
+ r && !r.method_name.to_s.strip.empty? && r.method_name.to_s != act.to_s
132
+ }
133
+ assert bad.empty?, "#{scheme.join("+")}: no description for " +
134
+ bad.map { |b| "#{b[:mode]} #{b[:keys].join(" ")} => #{b[:action].inspect}" }.inspect
135
+ }
136
+ end
137
+
138
+ # A named action bound to a <char> key gets the typed character
139
+ def test_char_argument
140
+ buf.set_content("abc\n")
141
+ buf.set_pos(0)
142
+ keys("f c") # jump.char.next
143
+ assert_pos 0, 2
144
+ keys("r x") # edit.replace.char
145
+ assert_buf "abx\n"
146
+ keys("m q") # mark.set
147
+ keys("0")
148
+ keys("' q") # mark.jump
149
+ assert_pos 0, 2
150
+ keys("i z esc") # edit.insert.char
151
+ assert_buf "abzx\n"
152
+ end
153
+
154
+ # The character reaches the action also when a macro replays it
155
+ def test_char_argument_in_macro
156
+ buf.set_content("abc\n")
157
+ buf.set_pos(0)
158
+ keys("q w r y q") # macro.record w, edit.replace.char y, macro.record.stop
159
+ assert_buf "ybc\n"
160
+ keys("l @ w") # macro.run w
161
+ assert_buf "yyc\n"
162
+ end
163
+ end