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,143 +1,372 @@
1
- # require "trie"
2
- require "rambling-trie"
3
1
 
2
+ module Vimamsa
3
+
4
+ # Word-based completion candidates collected from open buffers.
5
+ # Words are stored per buffer (buffer id => {word => count}) so that a
6
+ # re-scan on save replaces that buffer's words and stale words drop out.
4
7
  class Autocomplete
5
- @@trie = Rambling::Trie.create
8
+ @@buf_words = {} # buffer id => { word => count }
9
+ @@scan_version = {} # buffer id => edit_version at last scan
10
+
11
+ MAX_WORD_LENGTH = 100
12
+ MAX_SCAN_SIZE = 500_000 # don't rescan larger buffers on the fly
6
13
 
7
14
  def self.init
8
15
  vma.hook.register(:file_saved, self.method("update_index"))
9
16
  end
10
17
 
11
18
  def self.update_index(bu)
12
- debug "self.update_index", 2
13
- add_words bu.scan_all_words
19
+ debug "Autocomplete.update_index", 2
20
+ scan_buffer(bu)
14
21
  end
15
22
 
16
- def self.update_dict
17
- for bu in vma.buffers.list
18
- for w in bu.scan_all_words
19
- trie << w
20
- end
23
+ # Called from Buffer#set_content with the initial word list
24
+ def self.add_words(words, buf_id = nil)
25
+ counts = Hash.new(0)
26
+ for w in words
27
+ counts[w] += 1 if w.size < MAX_WORD_LENGTH
21
28
  end
22
- @@trie = trie
29
+ @@buf_words[buf_id || :global] = counts
23
30
  end
24
31
 
25
- def self.add_words(words)
26
- for w in words
27
- if w.size < 100
28
- @@trie << w
29
- end
32
+ def self.scan_buffer(bu)
33
+ counts = Hash.new(0)
34
+ bu.scan(/\b\w+\b/) { |w| counts[w] += 1 if w.size < MAX_WORD_LENGTH }
35
+ @@buf_words[bu.id] = counts
36
+ @@scan_version[bu.id] = bu.edit_version
37
+ end
38
+
39
+ # Re-scan the buffer being edited if it has changed since the last scan,
40
+ # so the popup also offers words typed since the last save.
41
+ def self.refresh_current(bu)
42
+ return if bu.nil? or bu.size >= MAX_SCAN_SIZE
43
+ return if @@scan_version[bu.id] == bu.edit_version
44
+ scan_buffer(bu)
45
+ end
46
+
47
+ def self.remove_buffer(buf_id)
48
+ @@buf_words.delete(buf_id)
49
+ @@scan_version.delete(buf_id)
50
+ end
51
+
52
+ # Match quality tiers: case-sensitive prefix > case-insensitive prefix >
53
+ # substring > in-order subsequence. nil = no match.
54
+ def self.match_tier(word, prefix, prefix_down)
55
+ return 3 if word.start_with?(prefix)
56
+ wd = word.downcase
57
+ return 2 if wd.start_with?(prefix_down)
58
+ return 1 if wd.include?(prefix_down)
59
+ return 0 if subsequence?(prefix_down, wd)
60
+ return nil
61
+ end
62
+
63
+ def self.subsequence?(needle, haystack)
64
+ i = -1
65
+ needle.each_char do |ch|
66
+ i = haystack.index(ch, i + 1)
67
+ return false if i.nil?
30
68
  end
69
+ return true
31
70
  end
32
71
 
33
- def self.word_list
34
- return @@dict.keys
72
+ # Ranked completion candidates for prefix, best match first.
73
+ def self.matching_words(prefix, max: nil)
74
+ return [] if prefix.nil? or prefix.empty?
75
+ max ||= cnf.autocomplete.max_items! || 50
76
+ pd = prefix.downcase
77
+ freq = Hash.new(0)
78
+ @@buf_words.each_value { |h| h.each { |w, c| freq[w] += c } }
79
+ scored = []
80
+ freq.each do |w, c|
81
+ next if w == prefix
82
+ tier = match_tier(w, prefix, pd)
83
+ next if tier.nil?
84
+ scored << [-tier, -[c, 99].min, w.size, w]
85
+ end
86
+ scored.sort!
87
+ return scored[0...max].map { |x| x[3] }
35
88
  end
36
89
 
37
- def self.matching_words(beginning)
38
- return @@trie.scan(beginning)
90
+ # Filter LSP completion items ({text:, ...}) against the currently typed
91
+ # prefix, keeping the server's order within each match tier.
92
+ def self.filter_lsp_items(items, prefix)
93
+ return [] if items.nil? or items.empty?
94
+ return items.dup if prefix.nil? or prefix.empty?
95
+ pd = prefix.downcase
96
+ scored = []
97
+ items.each_with_index do |it, i|
98
+ tier = match_tier(it[:text], prefix, pd)
99
+ next if tier.nil?
100
+ scored << [[-tier, i], it]
101
+ end
102
+ scored.sort_by! { |x| x[0] }
103
+ return scored.map { |x| x[1] }
39
104
  end
40
105
  end
41
106
 
42
107
  class VSourceView < GtkSource::View
108
+ AUTOCP_ROW_HEIGHT = 26
109
+
110
+ def autocp_init
111
+ @autocp_active = false
112
+ @autocp_items = []
113
+ @autocp_selected = 0
114
+ @autocp_prefix = nil
115
+ @autocp_word_start = nil
116
+ @autocp_cursor_pos = nil
117
+ @autocp_gen = 0
118
+ @autocp_lsp_items = []
119
+ @autocp_idle_pending = false
120
+ @autocp_manual = false
121
+ @acwin = nil
122
+ @ac_list = nil
123
+ @ac_sw = nil
124
+ end
125
+
126
+ # Texts of the current candidate list (used by tests)
127
+ def autocp_texts
128
+ @autocp_items.map { |x| x[:text] }
129
+ end
130
+
43
131
  def hide_completions
44
- if @acwin.class == Gtk::Popover
45
- @acwin.hide
46
- end
132
+ @acwin.popdown if @acwin and @acwin.visible?
47
133
  @autocp_active = false
134
+ @autocp_manual = false
135
+ @autocp_items = []
136
+ @autocp_lsp_items = []
137
+ @autocp_prefix = nil
138
+ @autocp_word_start = nil
139
+ @autocp_cursor_pos = nil
140
+ @autocp_gen += 1 # invalidate pending LSP callbacks
48
141
  end
49
142
 
50
143
  def autocp_select
51
144
  return if !@autocp_active
52
- bufo.complete_current_word(@cpl_list[@autocp_selected])
53
- autocp_exit
145
+ item = @autocp_items[@autocp_selected]
146
+ hide_completions
147
+ # dup: word-hash keys are frozen, insert_txt_at calls force_encoding
148
+ bufo.complete_current_word(item[:text].dup) if item
149
+ grab_focus
150
+ end
151
+
152
+ def autocp_select_next
153
+ return if !@autocp_active
154
+ autocp_set_selected(@autocp_selected + 1) if @autocp_selected < @autocp_items.size - 1
54
155
  end
55
156
 
56
157
  def autocp_select_previous
57
- return if @autocp_selected <= 0
58
- autocp_hilight(@autocp_selected)
59
- autocp_unhilight(@autocp_selected)
60
- @autocp_selected -= 1
61
- autocp_hilight(@autocp_selected)
158
+ return if !@autocp_active
159
+ autocp_set_selected(@autocp_selected - 1) if @autocp_selected > 0
62
160
  end
63
161
 
64
- def autocp_hilight(id)
65
- l = @autocp_items[id]
66
- l.set_text("<span foreground='#00ff00' weight='ultrabold'>#{cpl_list[id]}</span>")
67
- l.use_markup = true
162
+ def autocp_manual_trigger
163
+ return unless cnf.autocomplete.enabled?
164
+ @autocp_manual = true
165
+ autocp_refresh(request_lsp: true)
68
166
  end
69
167
 
70
- def autocp_unhilight(id)
71
- l = @autocp_items[id]
72
- l.set_text("<span>#{cpl_list[id]}</span>")
73
- l.use_markup = true
168
+ # Called from Buffer#insert_txt while in insert mode (covers both the key
169
+ # binding path and the Wayland IM insert-text path).
170
+ def autocp_on_insert(c)
171
+ return unless cnf.autocomplete.enabled?
172
+ if c.nil? or c.empty? or c[-1] !~ /\w/
173
+ hide_completions if @autocp_active
174
+ return
175
+ end
176
+ if @autocp_active or cnf.autocomplete.auto_trigger? != false
177
+ autocp_schedule_refresh
178
+ end
74
179
  end
75
180
 
76
- def autocp_select_next
77
- return if @autocp_selected >= cpl_list.size - 1
78
- debug "autocp_select_next", 2
79
- autocp_unhilight(@autocp_selected)
80
- @autocp_selected += 1
81
- autocp_hilight(@autocp_selected)
181
+ # Called from Buffer#delete while in insert mode (backspace etc.)
182
+ def autocp_on_delete
183
+ autocp_schedule_refresh if @autocp_active
82
184
  end
83
185
 
84
- def autocp_exit
85
- @autocp_items = []
86
- @autocp_active = true
87
- hide_completions
186
+ # Called from Buffer#set_pos: dismiss if the cursor moved anywhere we
187
+ # don't expect (mouse click, arrows, jumps). One backspace between
188
+ # refreshes is tolerated; delete() calls set_pos before autocp_on_delete.
189
+ def autocp_check_pos(new_pos)
190
+ return if !@autocp_active or @autocp_cursor_pos.nil?
191
+ if new_pos != @autocp_cursor_pos and new_pos != @autocp_cursor_pos - 1
192
+ hide_completions
193
+ end
88
194
  end
89
195
 
90
- def try_autocomplete
196
+ # Coalesce refreshes into one idle callback per batch of edits. Running as
197
+ # idle also guarantees handle_deltas has synced the GTK buffer before we
198
+ # compute popup coordinates.
199
+ def autocp_schedule_refresh
200
+ return if @autocp_idle_pending
201
+ @autocp_idle_pending = true
202
+ GLib::Idle.add do
203
+ @autocp_idle_pending = false
204
+ autocp_refresh
205
+ false
206
+ end
91
207
  end
92
208
 
93
- def show_completions
94
- hide_completions
95
- bu = vma.buf
96
- (w, range) = bu.get_word_in_pos(bu.pos - 1, boundary: :word)
97
- debug [w, range].to_s, 2
98
- matches = Autocomplete.matching_words w
99
- return if matches.empty?
209
+ def autocp_refresh(request_lsp: false)
210
+ bu = @bufo
211
+ return if bu.nil?
212
+ p = bu.pos - 1
213
+ if !bu.is_legal_pos(p) or bu[p] !~ /\w/
214
+ hide_completions if @autocp_active
215
+ return
216
+ end
217
+ (prefix, range) = bu.get_word_in_pos(p, boundary: :word)
218
+ min_chars = cnf.autocomplete.min_chars! || 2
219
+ if prefix.nil? or prefix !~ /\A\w+\z/ or (!@autocp_manual and prefix.size < min_chars)
220
+ hide_completions if @autocp_active
221
+ return
222
+ end
223
+
224
+ if range.begin != @autocp_word_start
225
+ # Word start changed: LSP results for the old word are stale
226
+ @autocp_lsp_items = []
227
+ @autocp_gen += 1
228
+ request_lsp = true if @autocp_active or cnf.autocomplete.auto_trigger? != false
229
+ end
230
+ @autocp_word_start = range.begin
231
+ @autocp_prefix = prefix
232
+ @autocp_cursor_pos = bu.pos
233
+
234
+ autocp_request_lsp if request_lsp
235
+ Autocomplete.refresh_current(bu)
236
+ autocp_render
237
+ end
238
+
239
+ # Merge LSP items (server order within match tiers) with buffer words,
240
+ # dedupe by completion text, and show the popup.
241
+ def autocp_render
242
+ prefix = @autocp_prefix
243
+ return if prefix.nil?
244
+ max = cnf.autocomplete.max_items! || 50
245
+ items = []
246
+ seen = {}
247
+ Autocomplete.filter_lsp_items(@autocp_lsp_items, prefix).each do |it|
248
+ next if seen[it[:text]] or it[:text] == prefix
249
+ seen[it[:text]] = true
250
+ items << it
251
+ end
252
+ Autocomplete.matching_words(prefix, max: max).each do |w|
253
+ next if seen[w]
254
+ seen[w] = true
255
+ items << { text: w, kind: nil, detail: nil, source: :buffer }
256
+ end
257
+ items = items[0...max]
258
+ if items.empty?
259
+ hide_completions if @autocp_active
260
+ return
261
+ end
100
262
  @autocp_active = true
101
- @cpl_list = cpl_list = matches
102
- win = Gtk::Popover.new()
103
- win.parent = self
104
- vbox = Gtk::Grid.new()
105
- win.set_child(vbox)
263
+ @autocp_items = items
264
+ autocp_update_list(items)
265
+ (x, y) = pos_to_coord(@autocp_word_start)
266
+ @acwin.set_pointing_to(Gdk::Rectangle.new(x, y + 8, 10, 10))
267
+ @acwin.popup if !@acwin.visible?
268
+ autocp_set_selected(0)
269
+ grab_focus
270
+ end
106
271
 
107
- i = 0
108
- @autocp_items = []
109
- @autocp_selected = 0
110
- for x in cpl_list
111
- l = Gtk::Label.new(x)
112
- @autocp_items << l
113
- # numbers: left, top, width, height
114
- vbox.attach(l, 0, i, 1, 1)
115
- i += 1
116
- end
117
- autocp_hilight(0)
118
- (x, y) = cur_pos_xy
119
- rec = Gdk::Rectangle.new(x, y + 8, 10, 10)
272
+ # Request completions from the LSP server (async; the callback is
273
+ # marshaled to the GTK thread by LangSrv). Debounced per word start.
274
+ def autocp_request_lsp
275
+ return if cnf.autocomplete.lsp.enabled? == false
276
+ bu = @bufo
277
+ lsp = bu.lsp
278
+ return if lsp.nil? or !lsp.completion_supported or bu.fname.nil?
279
+ gen = @autocp_gen
280
+ debounce = ((cnf.autocomplete.lsp.debounce! || 0.15) * 1000).to_i
281
+ debounce = 1 if @autocp_manual
282
+ GLib::Timeout.add(debounce) do
283
+ if gen == @autocp_gen
284
+ lsp.request_completion(bu.fname, bu.lpos, bu.cpos) do |items|
285
+ # Discard if the word or popup state changed while waiting
286
+ if gen == @autocp_gen and !items.empty?
287
+ @autocp_lsp_items = items
288
+ autocp_render
289
+ end
290
+ end
291
+ end
292
+ false
293
+ end
294
+ end
295
+
296
+ private
297
+
298
+ def autocp_ensure_win
299
+ return @acwin if @acwin
300
+ win = Gtk::Popover.new
301
+ win.parent = self
120
302
  win.has_arrow = false
121
- win.set_pointing_to(rec)
122
303
  win.autohide = false
123
- win.popup
124
- gui_remove_controllers(win)
304
+ win.focusable = false
305
+ sw = Gtk::ScrolledWindow.new
306
+ sw.set_policy(:never, :automatic)
307
+ sw.propagate_natural_height = true
308
+ sw.propagate_natural_width = true
309
+ sw.max_content_height = (cnf.autocomplete.visible_items! || 10) * AUTOCP_ROW_HEIGHT
310
+ list = Gtk::ListBox.new
311
+ list.selection_mode = :browse
312
+ list.focusable = false
313
+ list.signal_connect("row-activated") do |_l, row|
314
+ @autocp_selected = row.index
315
+ autocp_select
316
+ end
317
+ sw.child = list
318
+ win.set_child(sw)
319
+ gui_remove_controllers(win) # don't let the popover steal key events
125
320
  @acwin = win
321
+ @ac_sw = sw
322
+ @ac_list = list
323
+ return win
126
324
  end
127
325
 
128
- def start_autocomplete
129
- return
130
- # Roughly following these examples:
131
- # https://stackoverflow.com/questions/52359721/howto-maintain-gtksourcecompletion-when-changing-buffers-in-a-gtksourceview
132
- # and gedit-plugins-41.0/plugins/wordcompletion/gedit-word-completion-plugin.c
133
- # .. but it doesn't work. So implementing using Popover.
134
- # Keeping this for reference
326
+ def autocp_update_list(items)
327
+ autocp_ensure_win
328
+ while (row = @ac_list.first_child)
329
+ @ac_list.remove(row)
330
+ end
331
+ show_kind = items.any? { |x| x[:kind] }
332
+ for it in items
333
+ hbox = Gtk::Box.new(:horizontal, 8)
334
+ if show_kind
335
+ kl = Gtk::Label.new(it[:kind].to_s)
336
+ kl.width_chars = 5
337
+ kl.xalign = 0
338
+ kl.add_css_class("dim-label")
339
+ hbox.append(kl)
340
+ end
341
+ tl = Gtk::Label.new(it[:text])
342
+ tl.xalign = 0
343
+ hbox.append(tl)
344
+ if it[:detail] and !it[:detail].empty?
345
+ dl = Gtk::Label.new(it[:detail])
346
+ dl.xalign = 0
347
+ dl.ellipsize = :end
348
+ dl.max_width_chars = 40
349
+ dl.add_css_class("dim-label")
350
+ hbox.append(dl)
351
+ end
352
+ @ac_list.append(hbox)
353
+ end
354
+ end
135
355
 
136
- cp = self.completion
137
- prov = GtkSource::CompletionWords.new("Autocomplete") # (name,icon)
138
- prov.register(self.buffer)
139
- cp.add_provider(prov)
140
- pp prov
141
- self.show_completion
356
+ def autocp_set_selected(i)
357
+ @autocp_selected = i
358
+ row = @ac_list.get_row_at_index(i)
359
+ return if row.nil?
360
+ @ac_list.select_row(row)
361
+ # Keep the selection visible; allocation is valid only once mapped
362
+ GLib::Idle.add do
363
+ r = @ac_list.get_row_at_index(@autocp_selected)
364
+ if r and @ac_sw
365
+ a = r.allocation
366
+ @ac_sw.vadjustment.clamp_page(a.y, a.y + a.height)
367
+ end
368
+ false
369
+ end
142
370
  end
143
371
  end
372
+ end # module Vimamsa
@@ -1,5 +1,7 @@
1
+
2
+ module Vimamsa
1
3
  module Gui
2
- def self.hilight_range(bf, r, color: "#aa0000ff", weight: nil, tag: nil)
4
+ def self.hilight_range(bf, r, color: theme_color(:highlight_default), weight: nil, tag: nil)
3
5
  vbuf = bf.view.buffer
4
6
 
5
7
  if tag.nil?
@@ -13,7 +15,65 @@ module Gui
13
15
  vbuf.apply_tag(tag, itr, itr2)
14
16
  end
15
17
 
16
- def self.highlight_match(bf, str, color: "#aa0000ff", weight: 650)
18
+ # Briefly highlight buffer range `r`, then clear it after cnf.flash.duration.
19
+ # If a block is given it runs *after* the flash clears — used by delete so the
20
+ # range is shown before the text is removed. When flashing is inactive
21
+ # (disabled in config, running under --test, running a macro, no GUI view, or
22
+ # an empty/inverted range) the block runs immediately and no highlight is
23
+ # drawn, preserving synchronous behaviour. Macros must stay synchronous: the
24
+ # deferred timeout used for the flash reorders the delete relative to the rest
25
+ # of the macro and breaks playback.
26
+ def self.flash_range(bf, r, &after)
27
+ active = cnf.flash.enabled? && !ARGV.include?("--test") &&
28
+ !vma.macro&.running_macro &&
29
+ bf.view && r && r.begin <= r.last
30
+ unless active
31
+ after&.call
32
+ return
33
+ end
34
+
35
+ vbuf = bf.view.buffer
36
+ tag = vbuf.tag_table.lookup("vma_flash")
37
+ if tag.nil?
38
+ tag = vbuf.create_tag("vma_flash")
39
+ tag.background = cnf.match.highlight.color!
40
+ end
41
+ # An earlier flash's edit must land before this range is computed on.
42
+ flush_flash
43
+ lo = r.begin
44
+ hi = [r.last + 1, bf.size].min # +1: cover the last char inclusively
45
+ vbuf.apply_tag(tag, vbuf.get_iter_at(:offset => lo), vbuf.get_iter_at(:offset => hi))
46
+
47
+ finish = proc {
48
+ vbuf.remove_tag(tag, vbuf.get_iter_at(:offset => lo), vbuf.get_iter_at(:offset => hi))
49
+ after&.call
50
+ # `after` may have edited the buffer (e.g. delete-to-mark). The normal
51
+ # post-action refresh already ran when the key was pressed, so flush the
52
+ # deferred edit to the GTK view and redraw the cursor here.
53
+ bf.view.handle_deltas
54
+ bf.view.draw_cursor
55
+ }
56
+ src = GLib::Timeout.add((cnf.flash.duration! * 1000).to_i) do
57
+ @pending_flash = nil
58
+ finish.call
59
+ false
60
+ end
61
+ @pending_flash = [src, finish]
62
+ end
63
+
64
+ # Finish a flash that is still showing now: clear it and run its deferred
65
+ # block (e.g. the delete). Called when the next key arrives, so that key's
66
+ # action sees the edit done — otherwise typing "d w j" within
67
+ # cnf.flash.duration moves first and the late delete snaps the cursor back.
68
+ def self.flush_flash
69
+ pf = @pending_flash
70
+ return if pf.nil?
71
+ @pending_flash = nil
72
+ GLib::Source.remove(pf[0])
73
+ pf[1].call
74
+ end
75
+
76
+ def self.highlight_match(bf, str, color: theme_color(:highlight_default), weight: 650)
17
77
  r = Regexp.new(Regexp.escape(str), Regexp::IGNORECASE)
18
78
  tag = vma.gui.view.buffer.create_tag
19
79
  tag.weight = weight
@@ -25,3 +85,4 @@ module Gui
25
85
  }
26
86
  end
27
87
  end
88
+ end # module Vimamsa