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,80 @@
|
|
|
1
|
+
module Vimamsa
|
|
2
|
+
# The editor's names for GTK key events ("j", "ctrl-r", "shift-tab", "esc",
|
|
3
|
+
# "ctrl" for the key alone), used by the editor window (VSourceView#
|
|
4
|
+
# handle_key_event) and by forms (form_input.rb), so both feed the key
|
|
5
|
+
# bindings the same names.
|
|
6
|
+
|
|
7
|
+
GUI_KEYVAL_NAMES = {
|
|
8
|
+
Gdk::Keyval::KEY_Control_L => "ctrl",
|
|
9
|
+
Gdk::Keyval::KEY_Control_R => "ctrl",
|
|
10
|
+
Gdk::Keyval::KEY_Escape => "esc",
|
|
11
|
+
Gdk::Keyval::KEY_Return => "enter",
|
|
12
|
+
Gdk::Keyval::KEY_ISO_Enter => "enter",
|
|
13
|
+
Gdk::Keyval::KEY_KP_Enter => "enter",
|
|
14
|
+
Gdk::Keyval::KEY_Alt_L => "alt",
|
|
15
|
+
Gdk::Keyval::KEY_Alt_R => "alt",
|
|
16
|
+
Gdk::Keyval::KEY_Caps_Lock => "caps",
|
|
17
|
+
Gdk::Keyval::KEY_BackSpace => "backspace",
|
|
18
|
+
Gdk::Keyval::KEY_KP_Page_Down => "pagedown",
|
|
19
|
+
Gdk::Keyval::KEY_KP_Page_Up => "pageup",
|
|
20
|
+
Gdk::Keyval::KEY_Page_Down => "pagedown",
|
|
21
|
+
Gdk::Keyval::KEY_Page_Up => "pageup",
|
|
22
|
+
Gdk::Keyval::KEY_Left => "left",
|
|
23
|
+
Gdk::Keyval::KEY_Right => "right",
|
|
24
|
+
Gdk::Keyval::KEY_Down => "down",
|
|
25
|
+
Gdk::Keyval::KEY_Up => "up",
|
|
26
|
+
Gdk::Keyval::KEY_space => "space",
|
|
27
|
+
Gdk::Keyval::KEY_Shift_L => "shift",
|
|
28
|
+
Gdk::Keyval::KEY_Shift_R => "shift",
|
|
29
|
+
Gdk::Keyval::KEY_Tab => "tab",
|
|
30
|
+
Gdk::Keyval::KEY_ISO_Left_Tab => "tab",
|
|
31
|
+
Gdk::Keyval::KEY_Home => "home",
|
|
32
|
+
Gdk::Keyval::KEY_KP_Home => "home",
|
|
33
|
+
Gdk::Keyval::KEY_End => "end",
|
|
34
|
+
Gdk::Keyval::KEY_KP_End => "end",
|
|
35
|
+
Gdk::Keyval::KEY_Delete => "delete",
|
|
36
|
+
Gdk::Keyval::KEY_KP_Delete => "delete",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
# The key name for a key event: keyval (Gdk keyval), keystr (its character,
|
|
40
|
+
# Gdk::Keyval.to_unicode), modifiers (vma.kbd.modifiers: :ctrl, :alt, :shift,
|
|
41
|
+
# :meta, :super). "" for a key with no name.
|
|
42
|
+
def gui_key_name(keyval, keystr, modifiers = vma.kbd.modifiers)
|
|
43
|
+
keyname = GUI_KEYVAL_NAMES[keyval] || keystr
|
|
44
|
+
parts = []
|
|
45
|
+
parts << "ctrl" if modifiers[:ctrl]
|
|
46
|
+
parts << "alt" if modifiers[:alt]
|
|
47
|
+
parts << "shift" if modifiers[:shift]
|
|
48
|
+
parts << "meta" if modifiers[:meta]
|
|
49
|
+
parts << "super" if modifiers[:super]
|
|
50
|
+
parts << keyname
|
|
51
|
+
|
|
52
|
+
# After remapping capslock to control in gnome-tweak tool, pressing and
|
|
53
|
+
# immediately releasing the capslock (control) key gives first "caps" on
|
|
54
|
+
# keydown and ctrl-"caps" on keyup (keyval 65509, KEY_Caps_Lock). Mapping
|
|
55
|
+
# capslock to ctrl in hardware gives keyval 65507 (KEY_Control_L) instead.
|
|
56
|
+
parts.delete_at(1) if parts[0] == "ctrl" && parts[1] == "caps"
|
|
57
|
+
|
|
58
|
+
# No "ctrl-ctrl" or "alt-alt"
|
|
59
|
+
parts.delete_at(0) if parts[0] == parts[1]
|
|
60
|
+
|
|
61
|
+
# "shift-P" is just "P", but shift-tab stays
|
|
62
|
+
parts.delete_at(0) if parts[0] == "shift" && parts.size == 2 && parts[1].size == 1
|
|
63
|
+
|
|
64
|
+
key_str = parts.join("-")
|
|
65
|
+
key_str == "\u0000" ? "" : key_str
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# vma.kbd.modifiers from a key controller's "modifiers" signal (modtype:
|
|
69
|
+
# Gdk::ModifierType), for the editor window and forms
|
|
70
|
+
def gui_set_modifiers(modtype)
|
|
71
|
+
m = vma.kbd.modifiers
|
|
72
|
+
m[:ctrl] = modtype.control_mask?
|
|
73
|
+
m[:alt] = modtype.alt_mask?
|
|
74
|
+
m[:hyper] = modtype.hyper_mask?
|
|
75
|
+
m[:lock] = modtype.lock_mask?
|
|
76
|
+
m[:meta] = modtype.meta_mask?
|
|
77
|
+
m[:shift] = modtype.shift_mask?
|
|
78
|
+
m[:super] = modtype.super_mask?
|
|
79
|
+
end
|
|
80
|
+
end # module Vimamsa
|
data/lib/vimamsa/langservp.rb
CHANGED
|
@@ -5,6 +5,10 @@ LSP = LanguageServer::Protocol
|
|
|
5
5
|
|
|
6
6
|
class LangSrv
|
|
7
7
|
@@languages = {}
|
|
8
|
+
# Languages whose server failed to start: not retried for every buffer (each
|
|
9
|
+
# try blocks the GTK thread, so a session of N files stalled N times) until
|
|
10
|
+
# LangSrv.restart
|
|
11
|
+
@@failed = {}
|
|
8
12
|
attr_accessor :error
|
|
9
13
|
attr_reader :completion_supported, :position_encoding
|
|
10
14
|
|
|
@@ -28,6 +32,7 @@ class LangSrv
|
|
|
28
32
|
def self.restart(lang)
|
|
29
33
|
lang = LSP_LANG.fetch(lang, lang)
|
|
30
34
|
@@languages.delete(lang)&.shutdown
|
|
35
|
+
@@failed.delete(lang)
|
|
31
36
|
vma.buffers.list.each do |b|
|
|
32
37
|
next if b.fname.nil? || b.lang.nil?
|
|
33
38
|
next unless LSP_LANG.fetch(b.lang, b.lang) == lang
|
|
@@ -36,21 +41,55 @@ class LangSrv
|
|
|
36
41
|
@@languages[lang]
|
|
37
42
|
end
|
|
38
43
|
|
|
39
|
-
|
|
44
|
+
# The server for lang, started if needed. fname: the file it is wanted
|
|
45
|
+
# for; a new server runs in that file's project (project_root).
|
|
46
|
+
def self.get(lang, fname = nil)
|
|
40
47
|
lang = LSP_LANG.fetch(lang, lang)
|
|
41
|
-
if @@languages[lang].nil?
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
if @@languages[lang].nil? && !@@failed[lang]
|
|
49
|
+
srv = LangSrv.new(lang, root: project_root(fname))
|
|
50
|
+
if srv.error
|
|
51
|
+
# Only a configured server that did not start counts as failed
|
|
52
|
+
@@failed[lang] = true if srv.configured?
|
|
53
|
+
else
|
|
54
|
+
@@languages[lang] = srv
|
|
55
|
+
end
|
|
44
56
|
end
|
|
45
57
|
return @@languages[lang]
|
|
46
58
|
end
|
|
47
59
|
|
|
60
|
+
# Files and dirs that mark a project's top folder
|
|
61
|
+
PROJECT_MARKERS = %w[.git Gemfile compile_commands.json compile_flags.txt]
|
|
62
|
+
|
|
63
|
+
# The folder a server for fname works in (its working directory and
|
|
64
|
+
# rootUri): the nearest folder up from fname with a PROJECT_MARKERS entry,
|
|
65
|
+
# else fname's own folder. Never the home directory or /: ruby-lsp indexes
|
|
66
|
+
# its whole workspace, so a file directly in ~ gets an empty folder.
|
|
67
|
+
def self.project_root(fname)
|
|
68
|
+
return nil if fname.nil?
|
|
69
|
+
home = File.expand_path("~")
|
|
70
|
+
dir = File.dirname(File.expand_path(fname))
|
|
71
|
+
d = dir
|
|
72
|
+
until d == home || d == "/" || d == "."
|
|
73
|
+
return d if PROJECT_MARKERS.any? { |m| File.exist?(File.join(d, m)) }
|
|
74
|
+
d = File.dirname(d)
|
|
75
|
+
end
|
|
76
|
+
return dir if dir != home && dir != "/"
|
|
77
|
+
@@empty_root ||= Dir.mktmpdir("vimamsa-lsp-")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# A server is configured for this language (whether or not it started)
|
|
81
|
+
def configured?
|
|
82
|
+
@configured
|
|
83
|
+
end
|
|
84
|
+
|
|
48
85
|
def new_id()
|
|
49
86
|
return @id += 1
|
|
50
87
|
end
|
|
51
88
|
|
|
52
|
-
|
|
89
|
+
# root: the project folder to run the server in (project_root)
|
|
90
|
+
def initialize(lang, root: nil)
|
|
53
91
|
@error = true
|
|
92
|
+
@root = root
|
|
54
93
|
|
|
55
94
|
# Use LSP server specified by user if available
|
|
56
95
|
@lang = lang
|
|
@@ -58,11 +97,12 @@ class LangSrv
|
|
|
58
97
|
lspconf = nil
|
|
59
98
|
ret = (cnf.lsp.server? || {}).find { |k, v| v[:languages].include?(@lang) }
|
|
60
99
|
lspconf = ret[1] unless ret.nil?
|
|
100
|
+
@configured = !lspconf.nil?
|
|
61
101
|
|
|
62
102
|
if !lspconf.nil?
|
|
63
103
|
error = false
|
|
64
104
|
begin
|
|
65
|
-
@io = IO.popen(lspconf[:command], "r+")
|
|
105
|
+
@io = @root ? IO.popen(lspconf[:command], "r+", chdir: @root) : IO.popen(lspconf[:command], "r+")
|
|
66
106
|
rescue Errno::ENOENT => e
|
|
67
107
|
pp e
|
|
68
108
|
error = true
|
|
@@ -95,6 +135,10 @@ class LangSrv
|
|
|
95
135
|
|
|
96
136
|
pid = Process.pid
|
|
97
137
|
|
|
138
|
+
# The project folder, unless workspace folders are configured
|
|
139
|
+
if wf.empty? && @root
|
|
140
|
+
wf << LSP::Interface::WorkspaceFolder.new(uri: file_uri(@root), name: File.basename(@root))
|
|
141
|
+
end
|
|
98
142
|
root_uri = lspconf[:rooturi] || (wf.empty? ? nil : wf.first.uri)
|
|
99
143
|
initp = LSP::Interface::InitializeParams.new(
|
|
100
144
|
process_id: pid,
|
|
@@ -115,7 +159,9 @@ class LangSrv
|
|
|
115
159
|
|
|
116
160
|
@writer.write(id: init_id, params: initp, method: "initialize")
|
|
117
161
|
|
|
162
|
+
@server_gone = false
|
|
118
163
|
@lst = Thread.new {
|
|
164
|
+
begin
|
|
119
165
|
@reader.read do |r|
|
|
120
166
|
if r[:method] == "textDocument/publishDiagnostics"
|
|
121
167
|
path = uri_to_path(r.dig(:params, :uri)) rescue nil
|
|
@@ -128,12 +174,20 @@ class LangSrv
|
|
|
128
174
|
GLib::Idle.add { cb.call(r); false } if cb
|
|
129
175
|
pp r
|
|
130
176
|
end
|
|
177
|
+
rescue StandardError => e
|
|
178
|
+
debug "LSP reader stopped: #{e}"
|
|
179
|
+
ensure
|
|
180
|
+
# EOF or unreadable output: the server is gone, stop waiting for it
|
|
181
|
+
@server_gone = true
|
|
182
|
+
end
|
|
131
183
|
}
|
|
132
184
|
|
|
133
185
|
# LSP spec: wait for initialize result, then send initialized notification.
|
|
134
186
|
# Servers such as ruby-lsp will not respond to any request until this is done.
|
|
135
187
|
init_result = wait_for_response(init_id)
|
|
136
188
|
if init_result.nil?
|
|
189
|
+
message("LSP server #{lspconf[:name]} did not start (command: #{lspconf[:command]})")
|
|
190
|
+
shutdown
|
|
137
191
|
@error = true
|
|
138
192
|
return
|
|
139
193
|
end
|
|
@@ -218,6 +272,7 @@ class LangSrv
|
|
|
218
272
|
t = Time.now
|
|
219
273
|
debug "Waiting for response id:#{id}"
|
|
220
274
|
while @resp[id].nil?
|
|
275
|
+
return @resp[id] if @server_gone # a last reply may have come with EOF
|
|
221
276
|
sleep 0.03
|
|
222
277
|
if Time.now - t > timeout
|
|
223
278
|
debug "Timeout LSP call id:#{id}"
|
|
@@ -554,7 +609,7 @@ end
|
|
|
554
609
|
|
|
555
610
|
def lsp_print_functions
|
|
556
611
|
return unless vma.buf&.fname
|
|
557
|
-
lsp = LangSrv.get(vma.buf.lang)
|
|
612
|
+
lsp = LangSrv.get(vma.buf.lang, vma.buf.fname)
|
|
558
613
|
if lsp.nil?
|
|
559
614
|
message("No LSP server available for #{vma.buf.lang}")
|
|
560
615
|
return
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
module Vimamsa
|
|
2
|
+
|
|
3
|
+
# The line ends of a buffer, as an implicit treap of lines (see
|
|
4
|
+
# doc/line_tree_plan.md).
|
|
5
|
+
#
|
|
6
|
+
# One node per line, in document order: `len` is the line's length in
|
|
7
|
+
# characters with its "\n", `prio` a random priority (a parent's is higher than
|
|
8
|
+
# its children's), and `cnt` / `sum` the number of lines and characters in the
|
|
9
|
+
# subtree. Line numbers are not stored: a node's line number is the count of
|
|
10
|
+
# lines before it. The last node holds the text after the last "\n" (usually
|
|
11
|
+
# empty), so every position belongs to a node.
|
|
12
|
+
#
|
|
13
|
+
# Answers the Array methods Buffer code uses on @line_ends (size, [i],
|
|
14
|
+
# bsearch_index, bsearch) with the newline positions, so callers don't change.
|
|
15
|
+
# Lookups and edits are O(log n) expected; the old Array needed O(n) per edit.
|
|
16
|
+
class LineTree
|
|
17
|
+
class Node
|
|
18
|
+
attr_accessor :len, :prio, :left, :right, :cnt, :sum
|
|
19
|
+
|
|
20
|
+
def initialize(len)
|
|
21
|
+
@len = len
|
|
22
|
+
@prio = rand
|
|
23
|
+
@left = nil
|
|
24
|
+
@right = nil
|
|
25
|
+
@cnt = 1
|
|
26
|
+
@sum = len
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.from_string(str)
|
|
31
|
+
lens = []
|
|
32
|
+
str.each_line { |l| lens << l.size }
|
|
33
|
+
lens << 0 if lens.empty? || str.end_with?("\n") # the (empty) text after the last "\n"
|
|
34
|
+
new(lens)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# lens: the length of each line, "\n" included; the last entry is the text
|
|
38
|
+
# after the last "\n".
|
|
39
|
+
def initialize(lens = [0])
|
|
40
|
+
@root = build(lens)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# ---------------------------------------------------------------- Array interface
|
|
44
|
+
|
|
45
|
+
# Number of "\n" characters (= the size of the old @line_ends Array).
|
|
46
|
+
def size
|
|
47
|
+
@root.cnt - 1
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def length = size
|
|
51
|
+
def empty? = size == 0
|
|
52
|
+
|
|
53
|
+
# Position of the i-th "\n" (negative i counts from the end, as in Array).
|
|
54
|
+
def [](i)
|
|
55
|
+
i += size if i < 0
|
|
56
|
+
return nil if i < 0 || i >= size
|
|
57
|
+
line_start(i) + line_len(i) - 1
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def first = self[0]
|
|
61
|
+
def last = self[-1]
|
|
62
|
+
|
|
63
|
+
# Array#bsearch_index in find-minimum mode over the "\n" positions: the index
|
|
64
|
+
# of the first position for which the block is true, or nil.
|
|
65
|
+
def bsearch_index
|
|
66
|
+
n = @root
|
|
67
|
+
base = 0 # characters before n's subtree
|
|
68
|
+
idx = 0 # lines before n's subtree
|
|
69
|
+
found = nil
|
|
70
|
+
last_line = size
|
|
71
|
+
while n
|
|
72
|
+
lc = cnt(n.left)
|
|
73
|
+
line_i = idx + lc
|
|
74
|
+
if line_i == last_line
|
|
75
|
+
# The final node has no "\n": it's after every newline, so look left.
|
|
76
|
+
n = n.left
|
|
77
|
+
next
|
|
78
|
+
end
|
|
79
|
+
nl_pos = base + sum(n.left) + n.len - 1
|
|
80
|
+
if yield(nl_pos)
|
|
81
|
+
found = line_i
|
|
82
|
+
n = n.left
|
|
83
|
+
else
|
|
84
|
+
base += sum(n.left) + n.len
|
|
85
|
+
idx = line_i + 1
|
|
86
|
+
n = n.right
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
found
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def bsearch(&blk)
|
|
93
|
+
i = bsearch_index(&blk)
|
|
94
|
+
i.nil? ? nil : self[i]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def to_a
|
|
98
|
+
out = []
|
|
99
|
+
pos = -1
|
|
100
|
+
each_len { |len| pos += len; out << pos }
|
|
101
|
+
out.pop # the final node has no "\n"
|
|
102
|
+
out
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def ==(other)
|
|
106
|
+
other.respond_to?(:to_a) && to_a == other.to_a
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def dup
|
|
110
|
+
LineTree.new(lens)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def clone(freeze: nil) = dup
|
|
114
|
+
|
|
115
|
+
def inspect
|
|
116
|
+
"#<LineTree #{size} newlines, #{total} chars>"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# ---------------------------------------------------------------- line queries
|
|
120
|
+
|
|
121
|
+
def total
|
|
122
|
+
@root.sum
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def line_count
|
|
126
|
+
@root.cnt
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Character position where line i starts (0 <= i < line_count).
|
|
130
|
+
def line_start(i)
|
|
131
|
+
pos = 0
|
|
132
|
+
n = @root
|
|
133
|
+
while n
|
|
134
|
+
lc = cnt(n.left)
|
|
135
|
+
if i < lc
|
|
136
|
+
n = n.left
|
|
137
|
+
elsif i == lc
|
|
138
|
+
return pos + sum(n.left)
|
|
139
|
+
else
|
|
140
|
+
pos += sum(n.left) + n.len
|
|
141
|
+
i -= lc + 1
|
|
142
|
+
n = n.right
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
raise IndexError, "line #{i} out of range"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Length of line i, "\n" included.
|
|
149
|
+
def line_len(i)
|
|
150
|
+
node_at(i).len
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# [line, column] of character position p. A position at or past the end
|
|
154
|
+
# belongs to the last line.
|
|
155
|
+
def line_at(p)
|
|
156
|
+
if p >= total
|
|
157
|
+
last = line_count - 1
|
|
158
|
+
return [last, p - line_start(last)]
|
|
159
|
+
end
|
|
160
|
+
p = 0 if p < 0
|
|
161
|
+
line = 0
|
|
162
|
+
n = @root
|
|
163
|
+
while n
|
|
164
|
+
ls = sum(n.left)
|
|
165
|
+
if p < ls
|
|
166
|
+
n = n.left
|
|
167
|
+
elsif p < ls + n.len
|
|
168
|
+
return [line + cnt(n.left), p - ls]
|
|
169
|
+
else
|
|
170
|
+
p -= ls + n.len
|
|
171
|
+
line += cnt(n.left) + 1
|
|
172
|
+
n = n.right
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
raise IndexError, "position out of range"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def lens
|
|
179
|
+
out = []
|
|
180
|
+
each_len { |len| out << len }
|
|
181
|
+
out
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# ---------------------------------------------------------------- edits
|
|
185
|
+
|
|
186
|
+
# `text` was inserted at character position pos.
|
|
187
|
+
def insert(pos, text)
|
|
188
|
+
return if text.nil? || text.empty?
|
|
189
|
+
j, c = line_at(pos)
|
|
190
|
+
if !text.include?("\n")
|
|
191
|
+
add_len(j, text.size)
|
|
192
|
+
return
|
|
193
|
+
end
|
|
194
|
+
pieces = text.split("\n", -1) # k newlines -> k + 1 pieces
|
|
195
|
+
old = line_len(j)
|
|
196
|
+
set_len(j, c + pieces[0].size + 1)
|
|
197
|
+
new_lens = pieces[1...-1].map { |s| s.size + 1 }
|
|
198
|
+
new_lens << pieces[-1].size + (old - c) # the rest of line j
|
|
199
|
+
insert_lines(j + 1, new_lens)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# `text` (the removed text) was deleted at character position pos.
|
|
203
|
+
def delete(pos, text)
|
|
204
|
+
return if text.nil? || text.empty?
|
|
205
|
+
n = text.size
|
|
206
|
+
m = text.count("\n")
|
|
207
|
+
j, c = line_at(pos)
|
|
208
|
+
if m == 0
|
|
209
|
+
add_len(j, -n)
|
|
210
|
+
return
|
|
211
|
+
end
|
|
212
|
+
je = j + m
|
|
213
|
+
ce = pos + n - line_start(je) # where the deletion ends, as a column of line je
|
|
214
|
+
set_len(j, c + (line_len(je) - ce))
|
|
215
|
+
delete_lines(j + 1, m)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Raises if heap order or a subtree count is wrong (tests, debugging).
|
|
219
|
+
def check!
|
|
220
|
+
walk = lambda do |n|
|
|
221
|
+
return [0, 0] if n.nil?
|
|
222
|
+
[n.left, n.right].each do |ch|
|
|
223
|
+
raise "heap order broken" if ch && ch.prio > n.prio
|
|
224
|
+
end
|
|
225
|
+
lc, ls = walk.call(n.left)
|
|
226
|
+
rc, rs = walk.call(n.right)
|
|
227
|
+
raise "cnt wrong" if n.cnt != 1 + lc + rc
|
|
228
|
+
raise "sum wrong" if n.sum != n.len + ls + rs
|
|
229
|
+
[n.cnt, n.sum]
|
|
230
|
+
end
|
|
231
|
+
walk.call(@root)
|
|
232
|
+
true
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
private
|
|
236
|
+
|
|
237
|
+
def cnt(n) = n ? n.cnt : 0
|
|
238
|
+
def sum(n) = n ? n.sum : 0
|
|
239
|
+
|
|
240
|
+
def update(n)
|
|
241
|
+
n.cnt = 1 + cnt(n.left) + cnt(n.right)
|
|
242
|
+
n.sum = n.len + sum(n.left) + sum(n.right)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# In-order lengths, without recursion.
|
|
246
|
+
def each_len
|
|
247
|
+
stack = []
|
|
248
|
+
n = @root
|
|
249
|
+
while n || !stack.empty?
|
|
250
|
+
while n
|
|
251
|
+
stack << n
|
|
252
|
+
n = n.left
|
|
253
|
+
end
|
|
254
|
+
n = stack.pop
|
|
255
|
+
yield n.len
|
|
256
|
+
n = n.right
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def node_at(i)
|
|
261
|
+
n = @root
|
|
262
|
+
while n
|
|
263
|
+
lc = cnt(n.left)
|
|
264
|
+
if i < lc
|
|
265
|
+
n = n.left
|
|
266
|
+
elsif i == lc
|
|
267
|
+
return n
|
|
268
|
+
else
|
|
269
|
+
i -= lc + 1
|
|
270
|
+
n = n.right
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
raise IndexError, "line out of range"
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Path from the root to line i, root first.
|
|
277
|
+
def path_to(i)
|
|
278
|
+
path = []
|
|
279
|
+
n = @root
|
|
280
|
+
while n
|
|
281
|
+
path << n
|
|
282
|
+
lc = cnt(n.left)
|
|
283
|
+
if i < lc
|
|
284
|
+
n = n.left
|
|
285
|
+
elsif i == lc
|
|
286
|
+
return path
|
|
287
|
+
else
|
|
288
|
+
i -= lc + 1
|
|
289
|
+
n = n.right
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
raise IndexError, "line out of range"
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def add_len(i, d)
|
|
296
|
+
path = path_to(i)
|
|
297
|
+
path.last.len += d
|
|
298
|
+
path.each { |n| n.sum += d } # only sums change; counts stay
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def set_len(i, len)
|
|
302
|
+
add_len(i, len - line_len(i))
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def insert_lines(i, lens)
|
|
306
|
+
l, r = split(@root, i)
|
|
307
|
+
@root = merge(merge(l, build(lens)), r)
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def delete_lines(i, k)
|
|
311
|
+
l, rest = split(@root, i)
|
|
312
|
+
_mid, r = split(rest, k)
|
|
313
|
+
@root = merge(l, r)
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# [lines 0...i, lines i..]
|
|
317
|
+
def split(n, i)
|
|
318
|
+
return [nil, nil] if n.nil?
|
|
319
|
+
if i <= cnt(n.left)
|
|
320
|
+
l, r = split(n.left, i)
|
|
321
|
+
n.left = r
|
|
322
|
+
update(n)
|
|
323
|
+
[l, n]
|
|
324
|
+
else
|
|
325
|
+
l, r = split(n.right, i - cnt(n.left) - 1)
|
|
326
|
+
n.right = l
|
|
327
|
+
update(n)
|
|
328
|
+
[n, r]
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# Every line of a comes before every line of b.
|
|
333
|
+
def merge(a, b)
|
|
334
|
+
return b if a.nil?
|
|
335
|
+
return a if b.nil?
|
|
336
|
+
|
|
337
|
+
if a.prio > b.prio
|
|
338
|
+
a.right = merge(a.right, b)
|
|
339
|
+
update(a)
|
|
340
|
+
a
|
|
341
|
+
else
|
|
342
|
+
b.left = merge(a, b.left)
|
|
343
|
+
update(b)
|
|
344
|
+
b
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# O(k) build from lengths in document order (a Cartesian tree on the
|
|
349
|
+
# priorities, using a stack of the right spine).
|
|
350
|
+
def build(lens)
|
|
351
|
+
stack = []
|
|
352
|
+
lens.each do |len|
|
|
353
|
+
n = Node.new(len)
|
|
354
|
+
last = nil
|
|
355
|
+
while !stack.empty? && stack[-1].prio < n.prio
|
|
356
|
+
last = stack.pop
|
|
357
|
+
update(last)
|
|
358
|
+
end
|
|
359
|
+
n.left = last
|
|
360
|
+
stack[-1].right = n if !stack.empty?
|
|
361
|
+
stack << n
|
|
362
|
+
end
|
|
363
|
+
stack.reverse_each { |s| update(s) }
|
|
364
|
+
stack[0]
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
end # module Vimamsa
|
data/lib/vimamsa/macro.rb
CHANGED
|
@@ -39,6 +39,10 @@ class Macro
|
|
|
39
39
|
|
|
40
40
|
@recorded_macros = vma.marshal_load("macros", {})
|
|
41
41
|
@named_macros = load_named_macros
|
|
42
|
+
# Old action ids (ACTION_ALIASES) => new ids; the recorded ones are saved
|
|
43
|
+
# at shutdown, the named ones now
|
|
44
|
+
convert_action_aliases(@recorded_macros)
|
|
45
|
+
save_named_macros if convert_action_aliases(@named_macros)
|
|
42
46
|
vma.hook.register(:shutdown, self.method("save"))
|
|
43
47
|
end
|
|
44
48
|
|
|
@@ -70,6 +74,21 @@ class Macro
|
|
|
70
74
|
{}
|
|
71
75
|
end
|
|
72
76
|
|
|
77
|
+
# Replace old action ids in macros ({name => [action, ...]}) with their new
|
|
78
|
+
# dotted ids. Returns true if anything changed.
|
|
79
|
+
def convert_action_aliases(macros)
|
|
80
|
+
changed = false
|
|
81
|
+
macros.each_value { |acts|
|
|
82
|
+
next if acts.class != Array
|
|
83
|
+
acts.map! { |x|
|
|
84
|
+
n = (x.class == Symbol || x.class == String) ? ACTION_ALIASES[x.to_sym] : nil
|
|
85
|
+
changed = true if n
|
|
86
|
+
n || x
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
changed
|
|
90
|
+
end
|
|
91
|
+
|
|
73
92
|
def save()
|
|
74
93
|
vma.marshal_save("macros", @recorded_macros)
|
|
75
94
|
# named_macros are kept current via save_named_macros; save a Marshal copy as backup
|
|
@@ -216,9 +235,7 @@ class Macro
|
|
|
216
235
|
m = @recorded_macros[name]
|
|
217
236
|
return if !(m.kind_of?(Array) and m.any?)
|
|
218
237
|
contents = m.join(";")
|
|
219
|
-
|
|
220
|
-
Dir.mkdir(dot_dir) unless File.exist?(dot_dir)
|
|
221
|
-
save_fn = "#{dot_dir}/macro_#{name}.rb"
|
|
238
|
+
save_fn = get_dot_path("macro_#{name}.rb") # with the named macros
|
|
222
239
|
|
|
223
240
|
Thread.new {
|
|
224
241
|
File.open(save_fn, "w+") do |io|
|
data/lib/vimamsa/rbvma.rb
CHANGED
|
@@ -27,8 +27,18 @@ require "uri"
|
|
|
27
27
|
require "vimamsa/conf"
|
|
28
28
|
require "vimamsa/util"
|
|
29
29
|
require "vimamsa/string_util"
|
|
30
|
+
require "vimamsa/line_tree"
|
|
30
31
|
# Clipboard must load before main: main.rb runs Editor.new at load time,
|
|
31
32
|
# and Editor#initialize creates a Clipboard.
|
|
33
|
+
require "vimamsa/registers"
|
|
34
|
+
require "vimamsa/dot_repeat"
|
|
35
|
+
require "vimamsa/demo_player"
|
|
36
|
+
require "vimamsa/demo_recorder"
|
|
37
|
+
require "vimamsa/run_ruby"
|
|
38
|
+
require "vimamsa/intro_page"
|
|
39
|
+
require "vimamsa/cli_help"
|
|
40
|
+
require_relative "tmp_user_dir" # also loaded by exe/vimamsa, before the rest
|
|
41
|
+
require_relative "user_dirs" # also loaded by exe/vimamsa, before the rest
|
|
32
42
|
require "vimamsa/clipboard"
|
|
33
43
|
# exit!
|
|
34
44
|
require "vimamsa/main"
|
|
@@ -44,6 +54,7 @@ require "vimamsa/appimage_env"
|
|
|
44
54
|
require "vimamsa/bundled_fonts"
|
|
45
55
|
Vimamsa::BundledFonts.setup! # install bundled Oxanium before the GUI renders
|
|
46
56
|
require "vimamsa/gui"
|
|
57
|
+
require "vimamsa/form_input"
|
|
47
58
|
require "vimamsa/gui_form_generator"
|
|
48
59
|
require "vimamsa/gui_text"
|
|
49
60
|
require "vimamsa/gui_menu"
|
|
@@ -53,8 +64,10 @@ require "vimamsa/gui_settings"
|
|
|
53
64
|
require "vimamsa/gui_theme"
|
|
54
65
|
require "vimamsa/gui_file_panel"
|
|
55
66
|
require "vimamsa/gui_func_panel"
|
|
67
|
+
require "vimamsa/gui_key_hint"
|
|
56
68
|
require "vimamsa/gui_keylog_panel"
|
|
57
69
|
require "vimamsa/gui_select_window"
|
|
70
|
+
require "vimamsa/key_names"
|
|
58
71
|
require "vimamsa/gui_sourceview"
|
|
59
72
|
require "vimamsa/gui_sourceview_autocomplete"
|
|
60
73
|
require "vimamsa/gui_image"
|
|
@@ -65,6 +78,8 @@ require "vimamsa/buffer"
|
|
|
65
78
|
require "vimamsa/buffer_cursor"
|
|
66
79
|
require "vimamsa/buffer_changetext"
|
|
67
80
|
require "vimamsa/text_objects"
|
|
81
|
+
require "vimamsa/vim_operators"
|
|
82
|
+
require "vimamsa/key_bindings_vim_core"
|
|
68
83
|
require "vimamsa/fold"
|
|
69
84
|
# require "vimamsa/buffer_list"
|
|
70
85
|
require "vimamsa/buffer_list"
|