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.
- checksums.yaml +4 -4
- data/.github/workflows/appimage.yml +93 -0
- data/.gitignore +4 -1
- data/Dockerfile +7 -4
- data/README.md +62 -16
- data/custom_example.rb +43 -13
- data/doc/TODO.md +461 -0
- data/doc/bundling_plan.md +569 -0
- data/doc/key_bindings_report_plan.md +168 -0
- data/doc/missing_vim_bindings.md +71 -0
- data/doc/modules_settings_plan.md +433 -0
- data/doc/refactor_dedup_plan.md +117 -0
- data/doc/unused_code_plan.md +120 -0
- data/doc/vim_compatibility_plan.md +296 -0
- data/doc/vimlike_vim_conflicts.md +254 -0
- data/doc/vm_snapshots_plan.md +146 -0
- data/exe/run_tests.rb +117 -3
- data/exe/vimamsa +24 -4
- data/fonts/OFL-Oxanium.txt +93 -0
- data/fonts/Oxanium[wght].ttf +0 -0
- data/icon.png +0 -0
- data/img/demo.gif +0 -0
- data/lang/hyperplaintext.lang +6 -0
- data/lib/vimamsa/ack.rb +11 -1
- data/lib/vimamsa/actions.rb +4 -1
- data/lib/vimamsa/appimage_env.rb +35 -0
- data/lib/vimamsa/audio.rb +2 -5
- data/lib/vimamsa/bookmarks.rb +302 -0
- data/lib/vimamsa/buffer.rb +593 -82
- data/lib/vimamsa/buffer_changetext.rb +102 -42
- data/lib/vimamsa/buffer_cursor.rb +7 -0
- data/lib/vimamsa/buffer_list.rb +4 -0
- data/lib/vimamsa/buffer_manager.rb +4 -1
- data/lib/vimamsa/bundled_fonts.rb +32 -0
- data/lib/vimamsa/clipboard.rb +3 -0
- data/lib/vimamsa/conf.rb +59 -3
- data/lib/vimamsa/constants.rb +3 -0
- data/lib/vimamsa/crash_handler.rb +149 -0
- data/lib/vimamsa/debug.rb +4 -1
- data/lib/vimamsa/desktop_install.rb +96 -0
- data/lib/vimamsa/diff_buffer.rb +39 -10
- data/lib/vimamsa/easy_jump.rb +3 -0
- data/lib/vimamsa/editor.rb +263 -57
- data/lib/vimamsa/encrypt.rb +78 -29
- data/lib/vimamsa/file_finder.rb +4 -19
- data/lib/vimamsa/file_history.rb +3 -0
- data/lib/vimamsa/file_manager.rb +30 -15
- data/lib/vimamsa/fold.rb +226 -0
- data/lib/vimamsa/gui.rb +520 -106
- data/lib/vimamsa/gui_command_line.rb +208 -0
- data/lib/vimamsa/gui_dialog.rb +94 -42
- data/lib/vimamsa/gui_file_panel.rb +356 -25
- data/lib/vimamsa/gui_form_generator.rb +59 -35
- data/lib/vimamsa/gui_func_panel.rb +67 -22
- data/lib/vimamsa/gui_image.rb +3 -0
- data/lib/vimamsa/gui_keylog_panel.rb +246 -0
- data/lib/vimamsa/gui_menu.rb +23 -11
- data/lib/vimamsa/gui_select_window.rb +19 -48
- data/lib/vimamsa/gui_settings.rb +78 -37
- data/lib/vimamsa/gui_sourceview.rb +273 -19
- data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
- data/lib/vimamsa/gui_text.rb +63 -2
- data/lib/vimamsa/gui_theme.rb +606 -0
- data/lib/vimamsa/hook.rb +3 -0
- data/lib/vimamsa/hyper_plain_text.rb +3 -0
- data/lib/vimamsa/key_actions.rb +112 -13
- data/lib/vimamsa/key_binding_tree.rb +242 -215
- data/lib/vimamsa/key_bindings_notepad.rb +43 -0
- data/lib/vimamsa/key_bindings_report.rb +261 -0
- data/lib/vimamsa/key_bindings_vim.rb +354 -0
- data/lib/vimamsa/key_bindings_vimlike.rb +65 -9
- data/lib/vimamsa/langservp.rb +271 -54
- data/lib/vimamsa/macro.rb +3 -0
- data/lib/vimamsa/main.rb +14 -8
- data/lib/vimamsa/rbvma.rb +26 -1
- data/lib/vimamsa/search.rb +3 -0
- data/lib/vimamsa/search_replace.rb +11 -10
- data/lib/vimamsa/string_util.rb +120 -40
- data/lib/vimamsa/terminal.rb +3 -0
- data/lib/vimamsa/test_framework.rb +28 -2
- data/lib/vimamsa/tests.rb +3 -0
- data/lib/vimamsa/text_objects.rb +353 -0
- data/lib/vimamsa/text_transforms.rb +14 -0
- data/lib/vimamsa/util.rb +46 -7
- data/lib/vimamsa/version.rb +1 -1
- data/lib/vimamsa.rb +0 -1
- data/modules/color_highlight/color_highlight.rb +118 -0
- data/modules/color_highlight/color_highlight_info.rb +3 -0
- data/modules/dictation/dictation.rb +1054 -0
- data/modules/dictation/dictation_info.rb +3 -0
- data/modules/dictation/whisper_worker.py +273 -0
- data/modules/ollama/ollama.rb +107 -0
- data/modules/ollama/ollama_info.rb +3 -0
- data/modules/tts/tts.rb +119 -0
- data/modules/tts/tts_info.rb +3 -0
- data/run_e2e_vm.sh +28 -0
- data/run_tests_vm.sh +130 -0
- data/scripts/appimage/AppRun +54 -0
- data/scripts/appimage/Dockerfile +18 -0
- data/scripts/appimage/build.sh +19 -0
- data/scripts/appimage/build_in_container.sh +192 -0
- data/scripts/appimage/smoke_test.sh +27 -0
- data/scripts/demo/demo_steps.txt +59 -0
- data/scripts/demo/record_demo.rb +177 -0
- data/scripts/demo/shapes.rb +28 -0
- data/styles/134272-molokai.xml +3 -0
- data/styles/molokai_cyber.xml +48 -0
- data/styles/molokai_edit.xml +1 -0
- data/tests/support/virtual_keyboard.rb +254 -0
- data/tests/support/virtual_mic.rb +67 -0
- data/tests/test_ack.rb +22 -0
- data/tests/test_auto_format.rb +122 -0
- data/tests/test_autocomplete.rb +211 -0
- data/tests/test_autosave.rb +145 -0
- data/tests/test_basic_editing.rb +33 -3
- data/tests/test_bookmarks.rb +206 -0
- data/tests/test_bracket_match.rb +85 -0
- data/tests/test_bundling.rb +92 -0
- data/tests/test_color_highlight.rb +92 -0
- data/tests/test_command_line.rb +79 -0
- data/tests/test_command_line_file.rb +27 -0
- data/tests/test_context_menu_file.rb +129 -0
- data/tests/test_copy_paste.rb +80 -0
- data/tests/test_crash_handler.rb +39 -0
- data/tests/test_custom_two_phase.rb +92 -0
- data/tests/test_dialog_callback_errors.rb +44 -0
- data/tests/test_dictation.rb +294 -0
- data/tests/test_diff_buffer_name.rb +51 -0
- data/tests/test_double_click.rb +41 -0
- data/tests/test_drag_selection.rb +125 -0
- data/tests/test_e2e_cmdline.rb +116 -0
- data/tests/test_e2e_dictation.rb +98 -0
- data/tests/test_e2e_replace_char.rb +58 -0
- data/tests/test_extract_file_links.rb +55 -0
- data/tests/test_file_format.rb +217 -0
- data/tests/test_file_manager.rb +72 -0
- data/tests/test_file_panel.rb +269 -0
- data/tests/test_flash.rb +78 -0
- data/tests/test_fold.rb +111 -0
- data/tests/test_font_refresh.rb +56 -0
- data/tests/test_func_panel.rb +68 -0
- data/tests/test_grep_apply.rb +46 -0
- data/tests/test_gtk_sync.rb +126 -0
- data/tests/test_hpt_highlight_visible.rb +75 -0
- data/tests/test_hpt_link_highlight.rb +82 -0
- data/tests/test_jump_to_file.rb +41 -0
- data/tests/test_key_bindings.rb +208 -0
- data/tests/test_keylog_panel.rb +101 -0
- data/tests/test_lsp_clangd.rb +281 -0
- data/tests/test_lsp_jump.rb +87 -0
- data/tests/test_lsp_position_encoding.rb +49 -0
- data/tests/test_lsp_symbols.rb +68 -0
- data/tests/test_macro.rb +137 -0
- data/tests/test_markdown_highlight.rb +57 -0
- data/tests/test_mode_badge.rb +41 -0
- data/tests/test_notepad_bindings.rb +67 -0
- data/tests/test_popup_queue.rb +51 -0
- data/tests/test_random_edit.rb +109 -0
- data/tests/test_revert.rb +127 -0
- data/tests/test_startup_file.rb +58 -0
- data/tests/test_status_area.rb +50 -0
- data/tests/test_text_objects.rb +163 -0
- data/tests/test_text_transforms.rb +329 -0
- data/tests/test_tts.rb +68 -0
- data/tests/test_vim_bindings.rb +252 -0
- data/tests/test_vim_conflicts.rb +63 -0
- data/tests/test_window_layout.rb +219 -0
- data/vimamsa.gemspec +3 -1
- data/vm_guest_run.sh +109 -0
- metadata +112 -10
data/lib/vimamsa/file_history.rb
CHANGED
data/lib/vimamsa/file_manager.rb
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
require "fileutils"
|
|
2
2
|
|
|
3
|
+
module Vimamsa
|
|
4
|
+
|
|
3
5
|
class FileManager
|
|
4
|
-
@@cur
|
|
6
|
+
@@cur = nil # Current object of class
|
|
5
7
|
|
|
6
8
|
def initialize()
|
|
7
9
|
@buf = nil
|
|
@@ -10,21 +12,31 @@ class FileManager
|
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
def self.chdir_parent()
|
|
13
|
-
|
|
15
|
+
with_cur { |fm| fm.chdir_parent }
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
def self.cur()
|
|
17
19
|
return @@cur
|
|
18
20
|
end
|
|
19
21
|
|
|
22
|
+
# Run the block with the active FileManager. No-op (with a message) if the
|
|
23
|
+
# file selector has never been opened, i.e. @@cur is still nil.
|
|
24
|
+
def self.with_cur()
|
|
25
|
+
if @@cur.nil?
|
|
26
|
+
message("File selector is not open")
|
|
27
|
+
return nil
|
|
28
|
+
end
|
|
29
|
+
return yield @@cur
|
|
30
|
+
end
|
|
31
|
+
|
|
20
32
|
def self.init()
|
|
21
33
|
reg_act(:start_file_selector, proc { FileManager.new.run; vma.kbd.set_mode(:file_exp) }, "File selector")
|
|
22
34
|
|
|
23
35
|
reg_act(:fexp_chdir_parent, proc { FileManager.chdir_parent }, "File selector")
|
|
24
36
|
reg_act(:fexp_select, proc { buf.module.select_line }, "")
|
|
25
37
|
|
|
26
|
-
reg_act(:fexp_sort_mtime, proc { FileManager.
|
|
27
|
-
reg_act(:fexp_sort_fname, proc { FileManager.
|
|
38
|
+
reg_act(:fexp_sort_mtime, proc { FileManager.with_cur { |fm| fm.sort_mtime } }, "Sort based on time")
|
|
39
|
+
reg_act(:fexp_sort_fname, proc { FileManager.with_cur { |fm| fm.sort_fname } }, "Sort based on file name")
|
|
28
40
|
|
|
29
41
|
bindkey "C , j f", :start_file_selector
|
|
30
42
|
bindkey "C , f", :start_file_selector
|
|
@@ -38,10 +50,10 @@ class FileManager
|
|
|
38
50
|
|
|
39
51
|
# These are not yet safe to use
|
|
40
52
|
if cnf.fexp.experimental?
|
|
41
|
-
reg_act(:fexp_cut_file, proc { FileManager.
|
|
42
|
-
reg_act(:fexp_copy_file, proc { FileManager.
|
|
43
|
-
reg_act(:fexp_delete_file, proc { FileManager.
|
|
44
|
-
reg_act(:fexp_paste_files, proc { FileManager.
|
|
53
|
+
reg_act(:fexp_cut_file, proc { FileManager.with_cur { |fm| fm.cut_file } }, "Cut file (to paste elsewhere)")
|
|
54
|
+
reg_act(:fexp_copy_file, proc { FileManager.with_cur { |fm| fm.copy_file } }, "Copy file (to paste elsewhere)")
|
|
55
|
+
reg_act(:fexp_delete_file, proc { FileManager.with_cur { |fm| fm.delete_file } }, "Delete current file")
|
|
56
|
+
reg_act(:fexp_paste_files, proc { FileManager.with_cur { |fm| fm.paste_files } }, "Move previously cut files here")
|
|
45
57
|
|
|
46
58
|
bindkey "fexp d d", :fexp_cut_file
|
|
47
59
|
bindkey "fexp y y", :fexp_copy_file
|
|
@@ -51,7 +63,7 @@ class FileManager
|
|
|
51
63
|
|
|
52
64
|
# bindkey "fexp l", [:fexp_right, proc { debug "==fexp_right==" }, ""]
|
|
53
65
|
bindkey "fexp h", :fexp_chdir_parent
|
|
54
|
-
bindkey "fexp esc", [:fexp_quit, proc { FileManager.
|
|
66
|
+
bindkey "fexp esc", [:fexp_quit, proc { FileManager.with_cur { |fm| fm.quit } }, ""]
|
|
55
67
|
bindkey "fexp enter", :fexp_select
|
|
56
68
|
bindkey "fexp l", :fexp_select
|
|
57
69
|
|
|
@@ -179,7 +191,7 @@ class FileManager
|
|
|
179
191
|
@header << "=" * 40
|
|
180
192
|
@ld = dirpath # Path to current directory
|
|
181
193
|
@dlist = Dir.children(@ld).sort
|
|
182
|
-
@cdirs = [] # Dirs in current directory
|
|
194
|
+
@cdirs = [] # Dirs in current directory [name, stat]
|
|
183
195
|
@cfiles = [] # Files in current directory
|
|
184
196
|
for x in @dlist
|
|
185
197
|
fpath = fullp(x)
|
|
@@ -192,13 +204,14 @@ class FileManager
|
|
|
192
204
|
end
|
|
193
205
|
next if x[0] == "."
|
|
194
206
|
if File.directory?(fpath)
|
|
195
|
-
|
|
196
|
-
@cdirs << x
|
|
207
|
+
@cdirs << [x, fstat]
|
|
197
208
|
else
|
|
198
209
|
@cfiles << [x, fstat]
|
|
199
210
|
end
|
|
200
211
|
end
|
|
201
212
|
|
|
213
|
+
@cdirs.sort_by! { |x| x[1].mtime }.reverse! if @sort_by == :mtime
|
|
214
|
+
@cdirs.sort_by! { |x| x[0] } if @sort_by == :name
|
|
202
215
|
@cfiles.sort_by! { |x| x[1].mtime }.reverse! if @sort_by == :mtime
|
|
203
216
|
@cfiles.sort_by! { |x| x[1].size }.reverse! if @sort_by == :size
|
|
204
217
|
@cfiles.sort_by! { |x| x[0] } if @sort_by == :name
|
|
@@ -207,8 +220,9 @@ class FileManager
|
|
|
207
220
|
s << @header.join("\n")
|
|
208
221
|
s << "\n"
|
|
209
222
|
s << "..\n"
|
|
210
|
-
|
|
211
|
-
|
|
223
|
+
@cdirs.each do |d|
|
|
224
|
+
s << "#{d[0]}\n"
|
|
225
|
+
end
|
|
212
226
|
s << "\n"
|
|
213
227
|
jumppos = nil
|
|
214
228
|
for f in @cfiles
|
|
@@ -237,7 +251,7 @@ class FileManager
|
|
|
237
251
|
r = vma.buf.line_range(2, @cdirs.size+1)
|
|
238
252
|
|
|
239
253
|
# Hilight works only if done after buffer is drawn
|
|
240
|
-
run_as_idle proc { Gui.hilight_range(vma.buf, r, color:
|
|
254
|
+
run_as_idle proc { Gui.hilight_range(vma.buf, r, color: theme_color(:search_jump)) }
|
|
241
255
|
end
|
|
242
256
|
end
|
|
243
257
|
|
|
@@ -284,3 +298,4 @@ class FileManager
|
|
|
284
298
|
@buf.close
|
|
285
299
|
end
|
|
286
300
|
end
|
|
301
|
+
end # module Vimamsa
|
data/lib/vimamsa/fold.rb
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
|
|
2
|
+
module Vimamsa
|
|
3
|
+
|
|
4
|
+
# Vim-style marker folds ({{{ ... }}}), "extract & stash" model.
|
|
5
|
+
#
|
|
6
|
+
# When a fold is closed, the whole {{{...}}} block is *removed* from the buffer
|
|
7
|
+
# string and stashed in @folds; the block is replaced by a single placeholder
|
|
8
|
+
# line. Opening re-inserts the stashed text verbatim. Because the folded text is
|
|
9
|
+
# no longer in the buffer string, every path that serialises the buffer to disk
|
|
10
|
+
# (Buffer#write_contents_to_file, Buffer#autosave) routes through
|
|
11
|
+
# #content_for_disk, which re-inflates folds so the file on disk always holds the
|
|
12
|
+
# fully expanded text.
|
|
13
|
+
#
|
|
14
|
+
# Known v1 limitations (inherent to the extract & stash approach): folded text is
|
|
15
|
+
# absent from in-buffer search / replace / LSP while closed, and per-fold state
|
|
16
|
+
# does not persist across restart (the {{{ }}} markers do; with
|
|
17
|
+
# cnf.fold.start_closed — the default — a reopened file starts fully collapsed,
|
|
18
|
+
# otherwise fully expanded). A region that already contains a *closed* fold
|
|
19
|
+
# cannot be folded until the inner fold is opened.
|
|
20
|
+
|
|
21
|
+
# One closed fold. `body` is the exact original block text (the {{{ line through
|
|
22
|
+
# the }}} line, trailing newline included) so open/save round-trips byte-for-byte.
|
|
23
|
+
FoldRecord = Struct.new(:anchor_id, :label, :body, :nlines)
|
|
24
|
+
|
|
25
|
+
class Buffer < String
|
|
26
|
+
FOLD_START_RE = /^\s*\{\{\{/
|
|
27
|
+
FOLD_END_RE = /^\s*\}\}\}/
|
|
28
|
+
|
|
29
|
+
def _fold_state
|
|
30
|
+
@folds ||= []
|
|
31
|
+
@fold_anchors ||= {}
|
|
32
|
+
@fold_next_id ||= 0
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Drop all fold state. Called whenever the buffer content is replaced
|
|
36
|
+
# wholesale (set_content / revert): the stashed bodies and anchors refer to
|
|
37
|
+
# the old text and would splice garbage into the new one.
|
|
38
|
+
def fold_reset
|
|
39
|
+
@folds = []
|
|
40
|
+
@fold_anchors = {}
|
|
41
|
+
@fold_next_id = 0
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def _fold_placeholder(label, nlines)
|
|
45
|
+
head = label.empty? ? "{{{" : "{{{ #{label}"
|
|
46
|
+
"#{head} ⟨#{nlines} lines⟩\n"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Return `str` with every top-level {{{...}}} block replaced by a closed-fold
|
|
50
|
+
# placeholder, the bodies stashed as if each fold had been closed by hand.
|
|
51
|
+
# Runs on the raw string *before* it is installed as buffer content
|
|
52
|
+
# (set_content), so anchors are positions in the returned string and no
|
|
53
|
+
# deltas / undo history / modified state are produced. Unbalanced {{{ lines
|
|
54
|
+
# are left as plain text.
|
|
55
|
+
def fold_collapse_all(str)
|
|
56
|
+
fold_reset
|
|
57
|
+
lines = str.lines
|
|
58
|
+
out = +""
|
|
59
|
+
i = 0
|
|
60
|
+
while i < lines.size
|
|
61
|
+
line = lines[i]
|
|
62
|
+
if line =~ FOLD_START_RE
|
|
63
|
+
depth = 1
|
|
64
|
+
j = i + 1
|
|
65
|
+
while j < lines.size
|
|
66
|
+
depth += 1 if lines[j] =~ FOLD_START_RE
|
|
67
|
+
depth -= 1 if lines[j] =~ FOLD_END_RE
|
|
68
|
+
break if depth == 0
|
|
69
|
+
j += 1
|
|
70
|
+
end
|
|
71
|
+
if depth == 0
|
|
72
|
+
body = lines[i..j].join
|
|
73
|
+
label = line.sub(FOLD_START_RE, "").strip
|
|
74
|
+
nlines = body.count("\n")
|
|
75
|
+
anchor_id = (@fold_next_id += 1)
|
|
76
|
+
@fold_anchors[anchor_id] = out.size
|
|
77
|
+
@folds << FoldRecord.new(anchor_id, label, body, nlines)
|
|
78
|
+
out << _fold_placeholder(label, nlines)
|
|
79
|
+
i = j + 1
|
|
80
|
+
next
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
out << line
|
|
84
|
+
i += 1
|
|
85
|
+
end
|
|
86
|
+
out
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def fold_line_text(line_i)
|
|
90
|
+
self[line_range(line_i, 1)]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Toggle the fold on line `line_i`. Returns true if the line was a fold-start or
|
|
94
|
+
# a closed-fold placeholder (i.e. this consumed the key); false otherwise, so
|
|
95
|
+
# the caller can run its normal line action.
|
|
96
|
+
def fold_toggle_at(line_i)
|
|
97
|
+
_fold_state
|
|
98
|
+
return false if line_i < 0 || line_i >= @line_ends.size
|
|
99
|
+
|
|
100
|
+
lr = line_range(line_i, 1)
|
|
101
|
+
anchor = @fold_anchors.find { |_id, p| p >= lr.begin && p <= lr.end }
|
|
102
|
+
if anchor
|
|
103
|
+
_fold_open(anchor[0])
|
|
104
|
+
return true
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
line = fold_line_text(line_i)
|
|
108
|
+
if line =~ FOLD_START_RE
|
|
109
|
+
_fold_close(line_i)
|
|
110
|
+
return true
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
false
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Close the fold whose start marker is on line `start_lpos`.
|
|
117
|
+
def _fold_close(start_lpos)
|
|
118
|
+
_fold_state
|
|
119
|
+
start_range = line_range(start_lpos, 1)
|
|
120
|
+
start_line = self[start_range]
|
|
121
|
+
label = start_line.sub(FOLD_START_RE, "").strip
|
|
122
|
+
|
|
123
|
+
# Find the depth-matched closing }}} line (nested folds counted so an outer
|
|
124
|
+
# fold swallows inner markers as literal text).
|
|
125
|
+
depth = 1
|
|
126
|
+
end_lpos = nil
|
|
127
|
+
i = start_lpos + 1
|
|
128
|
+
while i < @line_ends.size
|
|
129
|
+
lt = fold_line_text(i)
|
|
130
|
+
depth += 1 if lt =~ FOLD_START_RE
|
|
131
|
+
depth -= 1 if lt =~ FOLD_END_RE
|
|
132
|
+
if depth == 0
|
|
133
|
+
end_lpos = i
|
|
134
|
+
break
|
|
135
|
+
end
|
|
136
|
+
i += 1
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
if end_lpos.nil?
|
|
140
|
+
message("Unbalanced fold marker: no matching }}}")
|
|
141
|
+
return
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
end_range = line_range(end_lpos, 1)
|
|
145
|
+
block_begin = start_range.begin
|
|
146
|
+
block_end = end_range.end # inclusive; covers the }}} line + its \n
|
|
147
|
+
|
|
148
|
+
# v1: refuse to fold a region that already contains a closed fold — its
|
|
149
|
+
# placeholder anchor and stashed body would be orphaned by the extraction.
|
|
150
|
+
if @fold_anchors.values.any? { |p| p > block_begin && p <= block_end }
|
|
151
|
+
message("Cannot fold: region contains a closed fold — open it first")
|
|
152
|
+
return
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
body = self[block_begin..block_end]
|
|
156
|
+
nlines = body.count("\n")
|
|
157
|
+
placeholder = _fold_placeholder(label, nlines)
|
|
158
|
+
|
|
159
|
+
# Insert the placeholder first, then delete the original block (now shifted
|
|
160
|
+
# past it). Deleting first would momentarily empty the buffer and trip
|
|
161
|
+
# add_delta's auto-trailing-newline guard, doubling the final newline.
|
|
162
|
+
new_undo_group
|
|
163
|
+
block_len = block_end - block_begin + 1
|
|
164
|
+
add_delta([block_begin, INSERT, placeholder.size, placeholder], true)
|
|
165
|
+
add_delta([block_begin + placeholder.size, DELETE, block_len], true)
|
|
166
|
+
new_undo_group
|
|
167
|
+
|
|
168
|
+
anchor_id = (@fold_next_id += 1)
|
|
169
|
+
@fold_anchors[anchor_id] = block_begin
|
|
170
|
+
@folds << FoldRecord.new(anchor_id, label, body, nlines)
|
|
171
|
+
|
|
172
|
+
set_pos(block_begin)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Open the closed fold identified by `anchor_id`: replace its placeholder line
|
|
176
|
+
# with the stashed body.
|
|
177
|
+
def _fold_open(anchor_id)
|
|
178
|
+
_fold_state
|
|
179
|
+
rec = @folds.find { |f| f.anchor_id == anchor_id }
|
|
180
|
+
pos = @fold_anchors[anchor_id]
|
|
181
|
+
return if rec.nil? || pos.nil?
|
|
182
|
+
|
|
183
|
+
# The placeholder is a single line starting at `pos`.
|
|
184
|
+
line_end = self.index("\n", pos) || (self.size - 1)
|
|
185
|
+
ph_len = line_end - pos + 1 # placeholder line incl. its \n
|
|
186
|
+
|
|
187
|
+
# Insert the body first, then delete the placeholder (now shifted past it);
|
|
188
|
+
# see _fold_close for why insert-before-delete avoids a doubled newline.
|
|
189
|
+
new_undo_group
|
|
190
|
+
add_delta([pos, INSERT, rec.body.size, rec.body], true)
|
|
191
|
+
add_delta([pos + rec.body.size, DELETE, ph_len], true)
|
|
192
|
+
new_undo_group
|
|
193
|
+
|
|
194
|
+
@folds.delete(rec)
|
|
195
|
+
@fold_anchors.delete(anchor_id)
|
|
196
|
+
set_pos(pos)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# The buffer text with every closed fold re-inflated to its original block.
|
|
200
|
+
# Used for all disk writes so the extract & stash model never loses folded
|
|
201
|
+
# text. Does not mutate the live (folded) buffer.
|
|
202
|
+
def content_for_disk
|
|
203
|
+
_fold_state
|
|
204
|
+
return self.to_s if @folds.empty?
|
|
205
|
+
|
|
206
|
+
result = self.to_s.dup
|
|
207
|
+
# Process placeholders back-to-front so earlier offsets stay valid.
|
|
208
|
+
@folds.sort_by { |f| -(@fold_anchors[f.anchor_id] || -1) }.each do |rec|
|
|
209
|
+
pos = @fold_anchors[rec.anchor_id]
|
|
210
|
+
next if pos.nil? || pos > result.size
|
|
211
|
+
|
|
212
|
+
lb = pos > 0 ? result.rindex("\n", pos - 1) : nil
|
|
213
|
+
lb = lb ? lb + 1 : 0
|
|
214
|
+
le = result.index("\n", pos) || (result.size - 1)
|
|
215
|
+
|
|
216
|
+
# Safety: only splice if the anchored line still looks like a placeholder.
|
|
217
|
+
# If the user deleted the placeholder line, the body was intentionally
|
|
218
|
+
# dropped — leave the (already-collapsed) text as is.
|
|
219
|
+
next unless result[lb..le] =~ FOLD_START_RE
|
|
220
|
+
|
|
221
|
+
result[lb..le] = rec.body
|
|
222
|
+
end
|
|
223
|
+
result
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end # module Vimamsa
|