vimamsa 0.1.26 โ†’ 0.1.27

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 (170) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/appimage.yml +93 -0
  3. data/.gitignore +4 -1
  4. data/Dockerfile +7 -4
  5. data/README.md +62 -16
  6. data/custom_example.rb +43 -13
  7. data/doc/TODO.md +461 -0
  8. data/doc/bundling_plan.md +569 -0
  9. data/doc/key_bindings_report_plan.md +168 -0
  10. data/doc/missing_vim_bindings.md +71 -0
  11. data/doc/modules_settings_plan.md +433 -0
  12. data/doc/refactor_dedup_plan.md +117 -0
  13. data/doc/unused_code_plan.md +120 -0
  14. data/doc/vim_compatibility_plan.md +296 -0
  15. data/doc/vimlike_vim_conflicts.md +254 -0
  16. data/doc/vm_snapshots_plan.md +146 -0
  17. data/exe/run_tests.rb +117 -3
  18. data/exe/vimamsa +24 -4
  19. data/fonts/OFL-Oxanium.txt +93 -0
  20. data/fonts/Oxanium[wght].ttf +0 -0
  21. data/icon.png +0 -0
  22. data/img/demo.gif +0 -0
  23. data/lang/hyperplaintext.lang +6 -0
  24. data/lib/vimamsa/ack.rb +11 -1
  25. data/lib/vimamsa/actions.rb +4 -1
  26. data/lib/vimamsa/appimage_env.rb +35 -0
  27. data/lib/vimamsa/audio.rb +2 -5
  28. data/lib/vimamsa/bookmarks.rb +302 -0
  29. data/lib/vimamsa/buffer.rb +593 -82
  30. data/lib/vimamsa/buffer_changetext.rb +102 -42
  31. data/lib/vimamsa/buffer_cursor.rb +7 -0
  32. data/lib/vimamsa/buffer_list.rb +4 -0
  33. data/lib/vimamsa/buffer_manager.rb +4 -1
  34. data/lib/vimamsa/bundled_fonts.rb +32 -0
  35. data/lib/vimamsa/clipboard.rb +3 -0
  36. data/lib/vimamsa/conf.rb +59 -3
  37. data/lib/vimamsa/constants.rb +3 -0
  38. data/lib/vimamsa/crash_handler.rb +149 -0
  39. data/lib/vimamsa/debug.rb +4 -1
  40. data/lib/vimamsa/desktop_install.rb +96 -0
  41. data/lib/vimamsa/diff_buffer.rb +39 -10
  42. data/lib/vimamsa/easy_jump.rb +3 -0
  43. data/lib/vimamsa/editor.rb +263 -57
  44. data/lib/vimamsa/encrypt.rb +78 -29
  45. data/lib/vimamsa/file_finder.rb +4 -19
  46. data/lib/vimamsa/file_history.rb +3 -0
  47. data/lib/vimamsa/file_manager.rb +30 -15
  48. data/lib/vimamsa/fold.rb +226 -0
  49. data/lib/vimamsa/gui.rb +520 -106
  50. data/lib/vimamsa/gui_command_line.rb +208 -0
  51. data/lib/vimamsa/gui_dialog.rb +94 -42
  52. data/lib/vimamsa/gui_file_panel.rb +356 -25
  53. data/lib/vimamsa/gui_form_generator.rb +59 -35
  54. data/lib/vimamsa/gui_func_panel.rb +67 -22
  55. data/lib/vimamsa/gui_image.rb +3 -0
  56. data/lib/vimamsa/gui_keylog_panel.rb +246 -0
  57. data/lib/vimamsa/gui_menu.rb +23 -11
  58. data/lib/vimamsa/gui_select_window.rb +19 -48
  59. data/lib/vimamsa/gui_settings.rb +78 -37
  60. data/lib/vimamsa/gui_sourceview.rb +273 -19
  61. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  62. data/lib/vimamsa/gui_text.rb +63 -2
  63. data/lib/vimamsa/gui_theme.rb +606 -0
  64. data/lib/vimamsa/hook.rb +3 -0
  65. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  66. data/lib/vimamsa/key_actions.rb +112 -13
  67. data/lib/vimamsa/key_binding_tree.rb +242 -215
  68. data/lib/vimamsa/key_bindings_notepad.rb +43 -0
  69. data/lib/vimamsa/key_bindings_report.rb +261 -0
  70. data/lib/vimamsa/key_bindings_vim.rb +354 -0
  71. data/lib/vimamsa/key_bindings_vimlike.rb +65 -9
  72. data/lib/vimamsa/langservp.rb +271 -54
  73. data/lib/vimamsa/macro.rb +3 -0
  74. data/lib/vimamsa/main.rb +14 -8
  75. data/lib/vimamsa/rbvma.rb +26 -1
  76. data/lib/vimamsa/search.rb +3 -0
  77. data/lib/vimamsa/search_replace.rb +11 -10
  78. data/lib/vimamsa/string_util.rb +120 -40
  79. data/lib/vimamsa/terminal.rb +3 -0
  80. data/lib/vimamsa/test_framework.rb +28 -2
  81. data/lib/vimamsa/tests.rb +3 -0
  82. data/lib/vimamsa/text_objects.rb +353 -0
  83. data/lib/vimamsa/text_transforms.rb +14 -0
  84. data/lib/vimamsa/util.rb +46 -7
  85. data/lib/vimamsa/version.rb +1 -1
  86. data/lib/vimamsa.rb +0 -1
  87. data/modules/color_highlight/color_highlight.rb +118 -0
  88. data/modules/color_highlight/color_highlight_info.rb +3 -0
  89. data/modules/dictation/dictation.rb +1054 -0
  90. data/modules/dictation/dictation_info.rb +3 -0
  91. data/modules/dictation/whisper_worker.py +273 -0
  92. data/modules/ollama/ollama.rb +107 -0
  93. data/modules/ollama/ollama_info.rb +3 -0
  94. data/modules/tts/tts.rb +119 -0
  95. data/modules/tts/tts_info.rb +3 -0
  96. data/run_e2e_vm.sh +28 -0
  97. data/run_tests_vm.sh +130 -0
  98. data/scripts/appimage/AppRun +54 -0
  99. data/scripts/appimage/Dockerfile +18 -0
  100. data/scripts/appimage/build.sh +19 -0
  101. data/scripts/appimage/build_in_container.sh +192 -0
  102. data/scripts/appimage/smoke_test.sh +27 -0
  103. data/scripts/demo/demo_steps.txt +59 -0
  104. data/scripts/demo/record_demo.rb +177 -0
  105. data/scripts/demo/shapes.rb +28 -0
  106. data/styles/134272-molokai.xml +3 -0
  107. data/styles/molokai_cyber.xml +48 -0
  108. data/styles/molokai_edit.xml +1 -0
  109. data/tests/support/virtual_keyboard.rb +254 -0
  110. data/tests/support/virtual_mic.rb +67 -0
  111. data/tests/test_ack.rb +22 -0
  112. data/tests/test_auto_format.rb +122 -0
  113. data/tests/test_autocomplete.rb +211 -0
  114. data/tests/test_autosave.rb +145 -0
  115. data/tests/test_basic_editing.rb +33 -3
  116. data/tests/test_bookmarks.rb +206 -0
  117. data/tests/test_bracket_match.rb +85 -0
  118. data/tests/test_bundling.rb +92 -0
  119. data/tests/test_color_highlight.rb +92 -0
  120. data/tests/test_command_line.rb +79 -0
  121. data/tests/test_command_line_file.rb +27 -0
  122. data/tests/test_context_menu_file.rb +129 -0
  123. data/tests/test_copy_paste.rb +80 -0
  124. data/tests/test_crash_handler.rb +39 -0
  125. data/tests/test_custom_two_phase.rb +92 -0
  126. data/tests/test_dialog_callback_errors.rb +44 -0
  127. data/tests/test_dictation.rb +294 -0
  128. data/tests/test_diff_buffer_name.rb +51 -0
  129. data/tests/test_double_click.rb +41 -0
  130. data/tests/test_drag_selection.rb +125 -0
  131. data/tests/test_e2e_cmdline.rb +116 -0
  132. data/tests/test_e2e_dictation.rb +98 -0
  133. data/tests/test_e2e_replace_char.rb +58 -0
  134. data/tests/test_extract_file_links.rb +55 -0
  135. data/tests/test_file_format.rb +217 -0
  136. data/tests/test_file_manager.rb +72 -0
  137. data/tests/test_file_panel.rb +269 -0
  138. data/tests/test_flash.rb +78 -0
  139. data/tests/test_fold.rb +111 -0
  140. data/tests/test_font_refresh.rb +56 -0
  141. data/tests/test_func_panel.rb +68 -0
  142. data/tests/test_grep_apply.rb +46 -0
  143. data/tests/test_gtk_sync.rb +126 -0
  144. data/tests/test_hpt_highlight_visible.rb +75 -0
  145. data/tests/test_hpt_link_highlight.rb +82 -0
  146. data/tests/test_jump_to_file.rb +41 -0
  147. data/tests/test_key_bindings.rb +208 -0
  148. data/tests/test_keylog_panel.rb +101 -0
  149. data/tests/test_lsp_clangd.rb +281 -0
  150. data/tests/test_lsp_jump.rb +87 -0
  151. data/tests/test_lsp_position_encoding.rb +49 -0
  152. data/tests/test_lsp_symbols.rb +68 -0
  153. data/tests/test_macro.rb +137 -0
  154. data/tests/test_markdown_highlight.rb +57 -0
  155. data/tests/test_mode_badge.rb +41 -0
  156. data/tests/test_notepad_bindings.rb +67 -0
  157. data/tests/test_popup_queue.rb +51 -0
  158. data/tests/test_random_edit.rb +109 -0
  159. data/tests/test_revert.rb +127 -0
  160. data/tests/test_startup_file.rb +58 -0
  161. data/tests/test_status_area.rb +50 -0
  162. data/tests/test_text_objects.rb +163 -0
  163. data/tests/test_text_transforms.rb +329 -0
  164. data/tests/test_tts.rb +68 -0
  165. data/tests/test_vim_bindings.rb +252 -0
  166. data/tests/test_vim_conflicts.rb +63 -0
  167. data/tests/test_window_layout.rb +219 -0
  168. data/vimamsa.gemspec +3 -1
  169. data/vm_guest_run.sh +109 -0
  170. metadata +112 -10
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  vma.kbd.add_mode("C", :command)
2
4
  vma.kbd.add_mode("I", :insert, :insert)
3
5
  vma.kbd.add_mode("V", :visual, :visual)
@@ -21,6 +23,7 @@ cnf.mode.browse.cursor.background = "#f803fc"
21
23
  cnf.mode.insert.cursor.background = "#ffffff"
22
24
  cnf.mode.inactive.cursor.background = "#777777"
23
25
 
26
+
24
27
  def _insert_move(op)
25
28
  if op == :pagedown
26
29
  vma.gui.page_down
@@ -47,7 +50,9 @@ add_keys "intro", {
47
50
  "VCX right" => "buf.move(FORWARD_CHAR)",
48
51
  "VCX left" => "buf.move(BACKWARD_CHAR)",
49
52
  "C , b" => :start_buf_manager,
50
- "VC l" => "buf.move(FORWARD_CHAR)",
53
+ "C , M" => :bookmark_add,
54
+ "C , '" => :bookmark_list,
55
+ "VC l" => :e_move_forward_char,
51
56
  "VC , , s" => :search_actions,
52
57
  "C , n b" => :buf_new,
53
58
  "C y y" => :copy_cur_line,
@@ -66,10 +71,15 @@ add_keys "intro", {
66
71
  "VC b" => :jump_prev_word_start,
67
72
  "VC w" => :jump_next_word_start,
68
73
  "V esc" => "buf.end_visual_mode",
69
- "V ctrl!" => "buf.end_visual_mode",
74
+ "V ctrl!" => "buf.end_visual_mode",
75
+ "V tab" => "buf.indent_selection",
76
+ "V shift-tab" => "buf.unindent_selection",
70
77
 
71
78
  "C ctrl!" => :insert_mode,
72
79
  "C i" => :insert_mode,
80
+ # Guarded esc must be defined before the unguarded one: on multiple
81
+ # matches the first-registered state with an action wins
82
+ # "I esc(vma.buf.view.autocp_active)" => "vma.buf.view.hide_completions",
73
83
  "I esc || I ctrl!" => :prev_mode,
74
84
  "IX alt-b" => :jump_prev_word_start,
75
85
  "IX alt-f" => :jump_next_word_start,
@@ -87,6 +97,7 @@ add_keys "intro", {
87
97
  "I ctrl-y" => :redo,
88
98
  "CI backspace" => :insert_backspace,
89
99
  "CI ctrl-o" => :open_file_dialog,
100
+ "C g f" => :open_file_under_cursor,
90
101
  "I ctrl-l" => :find_in_buffer,
91
102
  "I ctrl-g" => :find_next,
92
103
 
@@ -109,6 +120,11 @@ add_keys "core", {
109
120
  # Buffer handling
110
121
  # "C B" => "bufs.switch",
111
122
  "C tab" => "bufs.switch_to_last_buf",
123
+ # prev/next file in file panel order (no-op while the panel is hidden)
124
+ "C alt-h" => :file_panel_prev_file,
125
+ "C alt-l" => :file_panel_next_file,
126
+ # label the file panel rows, jump to a file by typing its label
127
+ "C g s" => :file_panel_easy_jump,
112
128
  # 'C , s'=> 'gui_select_buffer',
113
129
  "C , r v b" => :buf_revert,
114
130
  "C , c b" => "bufs.close_current_buffer",
@@ -119,11 +135,17 @@ add_keys "core", {
119
135
  "VCI pagedown" => :page_down,
120
136
  "VCI pageup" => :page_up,
121
137
 
122
- "I down(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select_next",
138
+ # "I down(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select_next",
123
139
  "I tab(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select_next",
124
- "I up(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select_previous",
140
+ # "I up(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select_previous",
125
141
  "I shift-tab(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select_previous",
126
- "I enter(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select",
142
+ "I shift!(vma.buf.view.autocp_active)" => "vma.buf.view.autocp_select",
143
+
144
+ # Typing a character. Reached both from a key press and from an input method
145
+ # commit (AltGr, dead keys, compose โ€” those produce no key press at all, see
146
+ # VSourceView#register_buffer_signals), so insertion lives here rather than in
147
+ # the GUI layer.
148
+ "I <char>" => "buf.insert_txt(<char>)",
127
149
 
128
150
  "I tab" => "buf.insert_tab",
129
151
  "I shift-tab" => "buf.unindent",
@@ -153,14 +175,15 @@ add_keys "core", {
153
175
  "VC /[1-9]/" => "vma.kbd.set_next_command_count(<char>)",
154
176
  # 'VC number=/[0-9]/+ g'=> 'jump_to_line(<number>)',
155
177
  # 'VC X=/[0-9]/+ * Y=/[0-9]/+ '=> 'x_times_y(<X>,<Y>)',
156
- "VC 0(vma.kbd.next_command_count!=nil)" => "set_next_command_count(<char>)",
178
+ "VC 0(vma.kbd.next_command_count!=nil)" => "vma.kbd.set_next_command_count(<char>)",
157
179
  "VC 0(vma.kbd.next_command_count==nil)" => "buf.jump(BEGINNING_OF_LINE)",
158
180
  # 'C 0'=> 'buf.jump(BEGINNING_OF_LINE)',
159
181
  "VC ^" => "buf.jump(BEGINNING_OF_LINE)",
160
182
  # 'VC z z' => 'center_on_current_line',
161
183
  "VC *" => "buf.jump_to_next_instance_of_word",
162
184
 
163
- "C , e" => "invoke_command", # Currently eval
185
+ "C , e" => :start_cmd_line,
186
+ "C :" => :start_cmd_line,
164
187
 
165
188
  "VC /" => :find_in_buffer,
166
189
 
@@ -193,6 +216,7 @@ add_keys "core", {
193
216
  "C y O" => "buf.copy(:to_line_end)",
194
217
  "C y 0" => "buf.copy(:to_line_start)",
195
218
  "C y e" => "buf.copy(:to_word_end)", # TODO
219
+ "C y ' <char>" => "buf.copy(:to_mark,<char>)",
196
220
  #### Deleting
197
221
  # "C x" => "buf.delete(CURRENT_CHAR_FORWARD)",
198
222
  # 'C d k'=> 'delete_line(BACKWARD)', #TODO
@@ -202,8 +226,12 @@ add_keys "core", {
202
226
  "C d O" => "buf.delete2(:to_line_end)",
203
227
  "C d $" => "buf.delete2(:to_line_end)",
204
228
  # 'C d e'=> 'buf.delete_to_next_word_end',
205
- "C d <num> e" => "delete_next_word",
229
+ "C d <num> e" => "buf.delete2(:to_word_end)", # d 3 e: repeated by the count
206
230
  "C d ' <char>" => "buf.delete2(:to_mark,<char>)",
231
+ "C d f <char>" => "buf.delete2(:to_char,<char>)",
232
+ "C d F <char>" => "buf.delete2(:to_char_backward,<char>)",
233
+ "C d f space" => "buf.delete2(:to_char,' ')",
234
+ "C d F space" => "buf.delete2(:to_char_backward,' ')",
207
235
  "C r <char>" => "buf.replace_with_char(<char>)", # TODO
208
236
  "C r space" => "buf.replace_with_char(' ')", # TODO
209
237
  "C , l b" => "load_buffer_list",
@@ -225,6 +253,22 @@ add_keys "core", {
225
253
  "V g c" => :selection_capitalize,
226
254
  "V g s" => :selection_swapcase,
227
255
  "V g r" => :selection_reverse,
256
+ "V g i" => :selection_remove_indent,
257
+ "V g I" => :selection_remove_common_indent,
258
+ "V U" => :selection_upcase,
259
+ "V u" => :selection_downcase,
260
+ "V ~" => :selection_swapcase,
261
+ "C ~" => :vim_toggle_case,
262
+ "C g U w" => :word_upcase,
263
+ "C g u w" => :word_downcase,
264
+ "C g ~ w" => :word_swapcase,
265
+ "C g U U" => :line_upcase,
266
+ "C g u u" => :line_downcase,
267
+ "C g ~ ~" => :line_swapcase,
268
+ "V g w" => :remove_extra_spaces,
269
+ "C g w" => :remove_extra_spaces,
270
+ "C g i" => :selection_remove_indent,
271
+ "C g I" => :selection_remove_common_indent,
228
272
 
229
273
  "VC j" => :forward_line,
230
274
  "VC k" => :backward_line,
@@ -299,6 +343,8 @@ add_keys "core", {
299
343
  "C z " => :start_browse_mode,
300
344
  "B h" => :history_switch_backwards,
301
345
  "B l" => :history_switch_forwards,
346
+ "B p" => :file_panel_prev_file,
347
+ "B n" => :file_panel_next_file,
302
348
  "B z" => "center_on_current_line();call_action(:exit_browse_mode)",
303
349
  "B enter || B return || B esc || B j || B ctrl!" => :exit_browse_mode,
304
350
  "B s" => :page_up,
@@ -318,6 +364,8 @@ add_keys "core", {
318
364
  "VC , r r" => :gui_search_replace,
319
365
  "C , w" => :toggle_active_window,
320
366
  "C , , w" => :toggle_two_column,
367
+ "C , ; a c" => :toggle_autocomplete,
368
+ "C , R" => :restart_editor,
321
369
 
322
370
  "VC s" => :easy_jump,
323
371
  "I alt-s" => :easy_jump,
@@ -342,6 +390,7 @@ add_keys "core", {
342
390
  add_keys "hyperplaintext", {
343
391
  "V , t b" => :set_style_bold,
344
392
  "V , t l" => :set_style_link,
393
+ "V , t i" => :set_style_italic,
345
394
  "V , t c" => :clear_formats,
346
395
  "C , t h" => :set_line_style_heading,
347
396
  "C , t 1" => :set_line_style_h1,
@@ -350,6 +399,7 @@ add_keys "hyperplaintext", {
350
399
  "C , t 4" => :set_line_style_h4,
351
400
  "C , t b" => :set_line_style_bold,
352
401
  "C , t t" => :set_line_style_title,
402
+ "C , t i" => :set_line_style_italic,
353
403
  "C , t c" => :clear_line_styles,
354
404
  }
355
405
 
@@ -358,11 +408,13 @@ bindkey ["VCB M", "B m"], :run_last_macro
358
408
  add_keys "experimental", {
359
409
  "C ` k" => :lsp_debug,
360
410
  "C ` j" => :lsp_jump_to_definition,
411
+ "C ` r" => :lsp_restart,
361
412
  "C , u s" => :audio_stop,
362
413
 
363
414
  "C , t r" => "run_tests()",
364
415
  # "CV , R" => "restart_application", #TODO: does not work
365
- "I ctrl-h" => :show_autocomplete, #TODO: does not work
416
+ "I ctrl-h" => :autocp_manual_trigger,
417
+ "I ctrl-space" => :autocp_manual_trigger,
366
418
  "C , d m" => :kbd_dump_state,
367
419
  "C , ; ." => :increment_word,
368
420
  "C , d d" => "debug_dump_deltas",
@@ -392,3 +444,7 @@ bindkey "C , i p", "generate_password_to_buf(15)"
392
444
  # default_keys.each { |key, value|
393
445
  # bindkey(key, value)
394
446
  # }
447
+
448
+ # Vim text objects (diw, ci", ya(, vip, ...), see text_objects.rb
449
+ bind_vim_text_objects
450
+ end # module Vimamsa
@@ -1,11 +1,43 @@
1
1
  require "language_server-protocol"
2
+
3
+ module Vimamsa
2
4
  LSP = LanguageServer::Protocol
3
5
 
4
6
  class LangSrv
5
7
  @@languages = {}
6
8
  attr_accessor :error
9
+ attr_reader :completion_supported, :position_encoding
10
+
11
+ # LSP CompletionItemKind (1..25) => short tag for the popup
12
+ COMPLETION_KINDS = {
13
+ 1 => "txt", 2 => "mthd", 3 => "fn", 4 => "ctor", 5 => "fld",
14
+ 6 => "var", 7 => "cls", 8 => "ifc", 9 => "mod", 10 => "prop",
15
+ 11 => "unit", 12 => "val", 13 => "enum", 14 => "kw", 15 => "snip",
16
+ 16 => "col", 17 => "file", 18 => "ref", 19 => "dir", 20 => "emem",
17
+ 21 => "cons", 22 => "strt", 23 => "evt", 24 => "op", 25 => "tprm",
18
+ }.freeze
19
+
20
+ # GtkSourceView language ids for header files => the language their LSP
21
+ # server is configured for, so headers share the server of their sources.
22
+ LSP_LANG = { "chdr" => "c", "cpphdr" => "cpp" }.freeze
23
+
24
+ # Stop the server for lang (if running), start a new one and reattach every
25
+ # open buffer of that language (also ones whose server failed to start).
26
+ # Picks up configuration changes the server only reads at startup / file
27
+ # open, e.g. a new compile_flags.txt for clangd. Returns the new server or nil.
28
+ def self.restart(lang)
29
+ lang = LSP_LANG.fetch(lang, lang)
30
+ @@languages.delete(lang)&.shutdown
31
+ vma.buffers.list.each do |b|
32
+ next if b.fname.nil? || b.lang.nil?
33
+ next unless LSP_LANG.fetch(b.lang, b.lang) == lang
34
+ b.init_lsp
35
+ end
36
+ @@languages[lang]
37
+ end
7
38
 
8
39
  def self.get(lang)
40
+ lang = LSP_LANG.fetch(lang, lang)
9
41
  if @@languages[lang].nil?
10
42
  @@languages[lang] = LangSrv.new(lang)
11
43
  @@languages[lang] = nil if @@languages[lang].error
@@ -24,7 +56,7 @@ class LangSrv
24
56
  @lang = lang
25
57
 
26
58
  lspconf = nil
27
- ret = cnf.lsp.server?.find { |k, v| v[:languages].include?(@lang) }
59
+ ret = (cnf.lsp.server? || {}).find { |k, v| v[:languages].include?(@lang) }
28
60
  lspconf = ret[1] unless ret.nil?
29
61
 
30
62
  if !lspconf.nil?
@@ -47,6 +79,9 @@ class LangSrv
47
79
  else
48
80
  return nil
49
81
  end
82
+ @name = lspconf[:name] || @lang
83
+ # file path => latest diagnostics the server published for it
84
+ @diagnostics = {}
50
85
  @writer = LSP::Transport::Io::Writer.new(@io)
51
86
  @reader = LSP::Transport::Io::Reader.new(@io)
52
87
  @id = 0
@@ -60,32 +95,58 @@ class LangSrv
60
95
 
61
96
  pid = Process.pid
62
97
 
63
- if lspconf[:name] == "phpactor"
64
- initp = LSP::Interface::InitializeParams.new(
65
- process_id: pid,
66
- root_uri: lspconf[:rooturi],
67
- workspace_folders: wf,
68
- capabilities: { 'workspace': { 'workspaceFolders': true } },
69
- )
70
- else
71
- initp = LSP::Interface::InitializeParams.new(
72
- process_id: pid,
73
- root_uri: "null",
74
- workspace_folders: wf,
75
- capabilities: { 'workspace': { 'workspaceFolders': true } },
76
- )
77
- end
98
+ root_uri = lspconf[:rooturi] || (wf.empty? ? nil : wf.first.uri)
99
+ initp = LSP::Interface::InitializeParams.new(
100
+ process_id: pid,
101
+ root_uri: root_uri,
102
+ workspace_folders: wf.empty? ? nil : wf,
103
+ capabilities: {
104
+ 'workspace' => { 'workspaceFolders' => true },
105
+ 'textDocument' => {
106
+ 'definition' => { 'linkSupport' => true },
107
+ 'completion' => { 'completionItem' => { 'snippetSupport' => false } },
108
+ },
109
+ },
110
+ )
78
111
  @resp = {}
112
+ @pending = {} # id => callback, marshaled to the GTK thread on response
113
+ @pending_mutex = Mutex.new
114
+ init_id = new_id
79
115
 
80
- @writer.write(id: new_id, params: initp, method: "initialize")
116
+ @writer.write(id: init_id, params: initp, method: "initialize")
81
117
 
82
118
  @lst = Thread.new {
83
119
  @reader.read do |r|
120
+ if r[:method] == "textDocument/publishDiagnostics"
121
+ path = uri_to_path(r.dig(:params, :uri)) rescue nil
122
+ @diagnostics[path] = r.dig(:params, :diagnostics) || [] if path
123
+ next
124
+ end
84
125
  @resp[r[:id]] = r
126
+ cb = @pending_mutex.synchronize { @pending.delete(r[:id]) }
127
+ # GTK is single-threaded; callbacks must run on the main loop
128
+ GLib::Idle.add { cb.call(r); false } if cb
85
129
  pp r
86
- # exit
87
130
  end
88
131
  }
132
+
133
+ # LSP spec: wait for initialize result, then send initialized notification.
134
+ # Servers such as ruby-lsp will not respond to any request until this is done.
135
+ init_result = wait_for_response(init_id)
136
+ if init_result.nil?
137
+ @error = true
138
+ return
139
+ end
140
+ caps = init_result.dig(:result, :capabilities) || {}
141
+ # Units of Position.character: the server's choice (LSP 3.17
142
+ # positionEncoding, or clangd's older offsetEncoding), else the LSP default.
143
+ @position_encoding = caps[:positionEncoding] || init_result.dig(:result, :offsetEncoding) || "utf-16"
144
+ @completion_supported = !!(caps[:completionProvider] || caps["completionProvider"])
145
+ unless caps[:definitionProvider] || caps["definitionProvider"]
146
+ message("Warning: LSP server #{lspconf[:name]} did not advertise definitionProvider โ€” " \
147
+ "jump to definition will not work. Try running with 'bundle exec'.")
148
+ end
149
+ @writer.write(method: "initialized", params: {})
89
150
  @error = false
90
151
  end
91
152
 
@@ -97,11 +158,15 @@ class LangSrv
97
158
  # delta[2]: number of chars affected
98
159
  # delta[3]: text to add in case of insert
99
160
 
161
+ # Called before the buffer is changed, so line_text sees the old text the
162
+ # positions refer to.
163
+ sc = lsp_character(fpath, delta[4][0], delta[4][1])
100
164
  changes = nil
101
165
  if delta[1] == INSERT
102
- changes = [{ 'rangeLength': 0, 'range': { 'start': { 'line': delta[4][0], 'character': delta[4][1] }, 'end': { 'line': delta[4][0], 'character': delta[4][1] } }, 'text': delta[3] }]
166
+ changes = [{ 'rangeLength': 0, 'range': { 'start': { 'line': delta[4][0], 'character': sc }, 'end': { 'line': delta[4][0], 'character': sc } }, 'text': delta[3] }]
103
167
  elsif delta[1] == DELETE
104
- changes = [{ 'rangeLength': delta[2], 'range': { 'start': { 'line': delta[4][0], 'character': delta[4][1] }, 'end': { 'line': delta[5][0], 'character': delta[5][1] } }, 'text': "" }]
168
+ ec = lsp_character(fpath, delta[5][0], delta[5][1])
169
+ changes = [{ 'rangeLength': delta[2], 'range': { 'start': { 'line': delta[4][0], 'character': sc }, 'end': { 'line': delta[5][0], 'character': ec } }, 'text': "" }]
105
170
  end
106
171
  debug changes.inspect, 2
107
172
 
@@ -110,18 +175,51 @@ class LangSrv
110
175
  text_document: LSP::Interface::VersionedTextDocumentIdentifier.new(uri: fpuri, version: version),
111
176
  content_changes: changes,
112
177
  )
113
- id = new_id
114
178
  pp a
115
- @writer.write(id: id, params: a, method: "textDocument/didChange")
179
+ # A notification: no id. With an id it is a request, which clangd
180
+ # rejects (-32601 method not found) without applying the change, so
181
+ # its copy of the file goes stale and positions point at the wrong text.
182
+ @writer.write(params: a, method: "textDocument/didChange")
183
+ end
184
+ end
185
+
186
+ # Process id of the server (tests / introspection).
187
+ def pid
188
+ @io&.pid
189
+ end
190
+
191
+ # LSP shutdown request + exit notification. The process is reaped in the
192
+ # background, and killed if it has not exited within 2 s.
193
+ def shutdown
194
+ id = new_id
195
+ begin
196
+ @writer.write(id: id, method: "shutdown", params: nil)
197
+ wait_for_response(id, 2)
198
+ @writer.write(method: "exit", params: nil)
199
+ rescue IOError, SystemCallError => e
200
+ debug "LSP shutdown: #{e}"
201
+ end
202
+ @lst&.kill
203
+ io = @io
204
+ @io = nil
205
+ Thread.new do
206
+ pid = io.pid
207
+ 20.times do
208
+ break if Process.waitpid(pid, Process::WNOHANG)
209
+ sleep 0.1
210
+ end
211
+ Process.kill("TERM", pid) rescue nil
212
+ Process.waitpid(pid) rescue nil
213
+ io.close rescue nil
116
214
  end
117
215
  end
118
216
 
119
- def wait_for_response(id)
217
+ def wait_for_response(id, timeout = 5)
120
218
  t = Time.now
121
219
  debug "Waiting for response id:#{id}"
122
220
  while @resp[id].nil?
123
221
  sleep 0.03
124
- if Time.now - t > 5
222
+ if Time.now - t > timeout
125
223
  debug "Timeout LSP call id:#{id}"
126
224
  return nil
127
225
  end
@@ -150,27 +248,108 @@ class LangSrv
150
248
  def get_definition(fpath_or_uri, lpos, cpos)
151
249
  fpuri = fpath_or_uri.start_with?("file://") ? fpath_or_uri : file_uri(fpath_or_uri)
152
250
  a = LSP::Interface::DefinitionParams.new(
153
- position: LSP::Interface::Position.new(line: lpos, character: cpos),
251
+ position: LSP::Interface::Position.new(line: lpos, character: lsp_character(fpuri, lpos, cpos)),
154
252
  text_document: LSP::Interface::TextDocumentIdentifier.new(uri: fpuri),
155
253
  )
156
254
  id = new_id
157
255
  pp a
158
256
  @writer.write(id: id, params: a, method: "textDocument/definition")
159
257
  r = wait_for_response(id)
258
+ @definition_timed_out = r.nil?
160
259
  return nil if r.nil?
161
260
  pp r
162
- line = HSafe.new(r)[:result][0][:range][:start][:line].val
163
- uri = HSafe.new(r)[:result][0][:uri].val
164
-
165
- if !uri.nil? and !line.nil?
166
- puts "LINE:" + line.to_s
167
- puts "URI:" + uri
168
- fpath = URI.parse(uri).path
169
- line = line + 1
170
- return [fpath, line]
261
+
262
+ result = r[:result]
263
+ return nil unless result
264
+
265
+ # result may be: Location, Location[], or LocationLink[]
266
+ items = result.is_a?(Array) ? result : [result]
267
+ return nil if items.empty?
268
+
269
+ item = items[0]
270
+ # Location uses :uri + :range; LocationLink uses :targetUri + :targetSelectionRange
271
+ uri = item[:uri] || item[:targetUri]
272
+ range = item[:range] || item[:targetSelectionRange] || item[:targetRange]
273
+ line = range&.dig(:start, :line)
274
+
275
+ return nil unless uri && line
276
+
277
+ fpath = uri_to_path(uri)
278
+ return [fpath, line + 1]
279
+ end
280
+
281
+ def uri_to_path(uri)
282
+ URI::DEFAULT_PARSER.unescape(URI.parse(uri).path)
283
+ end
284
+
285
+ # Errors the server reported for file fpath (LSP severity 1), sorted by line
286
+ def errors_for(fpath)
287
+ (@diagnostics[fpath] || []).select { |d| (d[:severity] || 1) == 1 }
288
+ .sort_by { |d| d.dig(:range, :start, :line).to_i }
289
+ end
290
+
291
+ # Why get_definition(fpath, ...) just came back empty, for the user: no
292
+ # answer at all, or the server's own errors for the file, which often name
293
+ # the real cause (e.g. "'foo.h' file not found": wrong include paths in
294
+ # compile_commands.json / compile_flags.txt). nil if there is nothing to add.
295
+ def definition_failure_hint(fpath)
296
+ return "no answer from #{@name} within 5 s" if @definition_timed_out
297
+ errs = errors_for(fpath)
298
+ return nil if errs.empty?
299
+ # A missing include usually causes the rest, so show that one first
300
+ e = errs.find { |d| d[:message].to_s =~ /file not found/i } || errs[0]
301
+ n = errs.size
302
+ "#{@name} reports #{n} error#{n == 1 ? "" : "s"} in #{File.basename(fpath)}, e.g. " \
303
+ "line #{e.dig(:range, :start, :line).to_i + 1}: #{e[:message].to_s.lines.first.to_s.strip}"
304
+ end
305
+
306
+ # Async textDocument/completion. Never blocks: the callback is invoked on
307
+ # the GTK main loop with parsed items ([{text:, label:, kind:, detail:,
308
+ # source: :lsp}, ...]) when the server responds. If no response arrives
309
+ # within 3 s the request is silently dropped.
310
+ def request_completion(fpath, lpos, cpos, &callback)
311
+ ensure_file_open(fpath)
312
+ fpuri = file_uri(fpath)
313
+ a = LSP::Interface::CompletionParams.new(
314
+ position: LSP::Interface::Position.new(line: lpos, character: lsp_character(fpath, lpos, cpos)),
315
+ text_document: LSP::Interface::TextDocumentIdentifier.new(uri: fpuri),
316
+ )
317
+ id = new_id
318
+ @pending_mutex.synchronize {
319
+ @pending[id] = proc { |r| callback.call(parse_completion(r)) }
320
+ }
321
+ @writer.write(id: id, params: a, method: "textDocument/completion")
322
+ GLib::Timeout.add(3000) do
323
+ @pending_mutex.synchronize { @pending.delete(id) }
324
+ false
171
325
  end
326
+ end
172
327
 
173
- return nil
328
+ # Parse a completion response: CompletionItem[] or CompletionList.
329
+ # We ignore textEdit ranges and always replace the current word
330
+ # (Buffer#complete_current_word) โ€” correct for word-like completions.
331
+ def parse_completion(r)
332
+ result = r[:result]
333
+ return [] if result.nil?
334
+ items = result.is_a?(Array) ? result : (result[:items] || [])
335
+ return [] if !items.is_a?(Array)
336
+ out = []
337
+ items.first(500).each do |it|
338
+ text = it.dig(:textEdit, :newText) || it[:insertText] || it[:label]
339
+ next if text.nil? or text.empty?
340
+ if it[:insertTextFormat] == 2
341
+ # Snippet format: strip ${1:placeholder} and $1 tab stops
342
+ text = text.gsub(/\$\{\d+:?([^}]*)\}/) { $1 }.gsub(/\$\d+/, "")
343
+ end
344
+ out << { text: text,
345
+ label: it[:label],
346
+ kind: COMPLETION_KINDS[it[:kind]],
347
+ detail: it[:detail],
348
+ sort: (it[:sortText] || it[:label] || text).to_s,
349
+ source: :lsp }
350
+ end
351
+ out.sort_by! { |x| x[:sort] }
352
+ return out.first(100)
174
353
  end
175
354
 
176
355
  # LSP SymbolKind values for callable things
@@ -242,21 +421,28 @@ class LangSrv
242
421
  groups
243
422
  end
244
423
 
245
- # Send textDocument/documentSymbol and return [{name:, line:}, ...] for functions/methods.
246
- # Returns nil on error, empty array if no functions found.
247
- def document_functions(fpath)
424
+ # Send textDocument/documentSymbol for fpath and return the raw symbols array
425
+ # (either DocumentSymbol or SymbolInformation shape), or nil on error /
426
+ # malformed response.
427
+ private def request_document_symbols(fpath)
248
428
  ensure_file_open(fpath)
249
- fpuri = file_uri(fpath)
250
429
  a = LSP::Interface::DocumentSymbolParams.new(
251
- text_document: LSP::Interface::TextDocumentIdentifier.new(uri: fpuri),
430
+ text_document: LSP::Interface::TextDocumentIdentifier.new(uri: file_uri(fpath)),
252
431
  )
253
432
  id = new_id
254
433
  @writer.write(id: id, params: a, method: "textDocument/documentSymbol")
255
434
  r = wait_for_response(id)
256
435
  return nil if r.nil?
257
-
258
436
  symbols = r[:result]
259
- return nil if !symbols.is_a?(Array)
437
+ return nil unless symbols.is_a?(Array)
438
+ symbols
439
+ end
440
+
441
+ # Send textDocument/documentSymbol and return [{name:, line:}, ...] for functions/methods.
442
+ # Returns nil on error, empty array if no functions found.
443
+ def document_functions(fpath)
444
+ symbols = request_document_symbols(fpath)
445
+ return nil if symbols.nil?
260
446
 
261
447
  functions = collect_functions(symbols)
262
448
  functions.map do |s|
@@ -269,18 +455,8 @@ class LangSrv
269
455
  # Returns [{name:, line:, functions: [{name:, line:}, ...]}, ...]
270
456
  # name: nil means top-level (ungrouped) functions.
271
457
  def document_functions_grouped(fpath)
272
- ensure_file_open(fpath)
273
- fpuri = file_uri(fpath)
274
- a = LSP::Interface::DocumentSymbolParams.new(
275
- text_document: LSP::Interface::TextDocumentIdentifier.new(uri: fpuri),
276
- )
277
- id = new_id
278
- @writer.write(id: id, params: a, method: "textDocument/documentSymbol")
279
- r = wait_for_response(id)
280
- return nil if r.nil?
281
-
282
- symbols = r[:result]
283
- return nil unless symbols.is_a?(Array)
458
+ symbols = request_document_symbols(fpath)
459
+ return nil if symbols.nil?
284
460
 
285
461
  # Detect nested (DocumentSymbol) vs flat (SymbolInformation) format
286
462
  if symbols.any? { |s| s.key?(:children) }
@@ -309,6 +485,33 @@ class LangSrv
309
485
  URI.join("file:///", fp).to_s
310
486
  end
311
487
 
488
+ # Ruby character column cpos on line lpos -> Position.character in the
489
+ # server's encoding. They differ when non-ASCII text precedes the column:
490
+ # "รค" is 1 char but 2 bytes in utf-8; "๐Ÿ˜€" is 1 char but 2 utf-16 units.
491
+ def lsp_character(fpath_or_uri, lpos, cpos)
492
+ return cpos if @position_encoding == "utf-32"
493
+ pre = line_text(fpath_or_uri, lpos)[0, cpos].to_s.scrub
494
+ return cpos if pre.ascii_only?
495
+ if @position_encoding == "utf-8"
496
+ pre.bytesize
497
+ else
498
+ pre.encode("UTF-16LE").bytesize / 2
499
+ end
500
+ end
501
+
502
+ # Text of line lpos: from the editor buffer if the file is open (that is
503
+ # what the server has, via didChange), else from disk.
504
+ def line_text(fpath_or_uri, lpos)
505
+ fpath = fpath_or_uri
506
+ fpath = URI::DEFAULT_PARSER.unescape(URI.parse(fpath).path) if fpath.start_with?("file://")
507
+ id = vma.buffers.get_buffer_by_filename(fpath)
508
+ return vma.buffers.get_buffer_by_id(id).line(lpos).to_s if id
509
+ if File.file?(fpath)
510
+ File.foreach(fpath).with_index { |l, i| return l if i == lpos }
511
+ end
512
+ ""
513
+ end
514
+
312
515
  def open_file(fp, fc = nil, lang: nil)
313
516
  debug "open_file", 2
314
517
  @opened_files ||= {}
@@ -336,6 +539,19 @@ class LangSrv
336
539
  end
337
540
  end
338
541
 
542
+ def lsp_restart
543
+ lang = vma.buf&.lang
544
+ if !cnf.lsp.enabled?
545
+ message("LSP is not enabled (Settings > Behavior)")
546
+ elsif lang.nil?
547
+ message("No language detected for this buffer")
548
+ elsif LangSrv.restart(lang)
549
+ message("Restarted LSP server for #{lang}")
550
+ else
551
+ message("No LSP server configured or started for #{lang}")
552
+ end
553
+ end
554
+
339
555
  def lsp_print_functions
340
556
  return unless vma.buf&.fname
341
557
  lsp = LangSrv.get(vma.buf.lang)
@@ -345,3 +561,4 @@ def lsp_print_functions
345
561
  end
346
562
  lsp.print_functions(vma.buf.fname)
347
563
  end
564
+ end # module Vimamsa
data/lib/vimamsa/macro.rb CHANGED
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  def gui_find_macro_update_callback(search_str = "")
2
4
  debug "gui_find_macro_update_callback: #{search_str}"
3
5
  heystack = vma.macro.named_macros
@@ -238,3 +240,4 @@ class Macro
238
240
  }
239
241
  end
240
242
  end
243
+ end # module Vimamsa