From fb2606c8c54ffe0f9db99cc1e5b4c1c92681c379 Mon Sep 17 00:00:00 2001 From: Keith Miyake Date: Sat, 8 Oct 2022 14:51:38 -0700 Subject: [PATCH 1/3] feat: visual toggle checkboxes --- plugin/bullets.vim | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/plugin/bullets.vim b/plugin/bullets.vim index a8a1b66..79cbb2d 100644 --- a/plugin/bullets.vim +++ b/plugin/bullets.vim @@ -580,11 +580,10 @@ fun! s:set_checkbox(lnum, marker) endif endfun -fun! s:toggle_checkboxes_nested() +fun! s:toggle_checkboxes_nested(lnum) " toggle checkbox on the current line, as well as its parents and children - let l:lnum = line('.') - let l:indent = indent(l:lnum) - let l:bullet = s:closest_bullet_types(l:lnum, l:indent) + let l:indent = indent(a:lnum) + let l:bullet = s:closest_bullet_types(a:lnum, l:indent) let l:bullet = s:resolve_bullet_type(l:bullet) " Is this a checkbox? Do nothing if it's not, otherwise toggle the checkbox @@ -592,16 +591,16 @@ fun! s:toggle_checkboxes_nested() return endif - let l:checked = s:toggle_checkbox(l:lnum) + let l:checked = s:toggle_checkbox(a:lnum) if g:bullets_nested_checkboxes " Toggle children and parents - let l:completion_marker = s:sibling_checkbox_status(l:lnum) - call s:set_parent_checkboxes(l:lnum, l:completion_marker) + let l:completion_marker = s:sibling_checkbox_status(a:lnum) + call s:set_parent_checkboxes(a:lnum, l:completion_marker) " Toggle children if l:checked >= 0 - call s:set_child_checkboxes(l:lnum, l:checked) + call s:set_child_checkboxes(a:lnum, l:checked) endif endif endfun @@ -642,9 +641,17 @@ fun! s:set_child_checkboxes(lnum, checked) endif endfun +fun! s:visual_toggle_checkboxes() + let l:selection_lines = s:get_visual_selection_lines() + for l:line in l:selection_lines + call s:toggle_checkboxes_nested(l:line.nr) + endfor +endfun + command! SelectCheckboxInside call select_checkbox(1) command! SelectCheckbox call select_checkbox(0) -command! ToggleCheckbox call toggle_checkboxes_nested() +command! ToggleCheckbox call toggle_checkboxes_nested(line('.')) +command! -range=% ToggleCheckboxVisual call visual_toggle_checkboxes() " Checkboxes ---------------------------------------------- }}} " Roman numerals --------------------------------------------- {{{ @@ -989,6 +996,7 @@ nnoremap (bullets-renumber) :RenumberList " Toggle checkbox nnoremap (bullets-toggle-checkbox) :ToggleCheckbox +vnoremap (bullets-toggle-checkbox) :ToggleCheckboxVisual " Promote and Demote outline level inoremap (bullets-demote) :BulletDemote @@ -1038,6 +1046,7 @@ augroup TextBulletsMappings " Toggle checkbox call s:add_local_mapping(1, 'nmap', 'x', '(bullets-toggle-checkbox)') + call s:add_local_mapping(1, 'vmap', 'x', '(bullets-toggle-checkbox)') " Promote and Demote outline level call s:add_local_mapping(1, 'imap', '', '(bullets-demote)') From 3a4689b10d7dc267182a5a873852e5f2198d9de5 Mon Sep 17 00:00:00 2001 From: Keith Miyake Date: Sat, 8 Oct 2022 15:00:06 -0700 Subject: [PATCH 2/3] add spec for visual checkbox toggle --- spec/checkboxes_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/checkboxes_spec.rb b/spec/checkboxes_spec.rb index 814e354..5a3a478 100644 --- a/spec/checkboxes_spec.rb +++ b/spec/checkboxes_spec.rb @@ -248,4 +248,30 @@ TEXT end + + it 'visual toggle a bullet' do + filename = "#{SecureRandom.hex(6)}.txt" + write_file(filename, <<-TEXT) + # Hello there + - [ ] first bullet + - [ ] second bullet + - [ ] third bullet + TEXT + + vim.edit filename + vim.normal 'j' + vim.normal 'jv2j' + vim.command 'ToggleCheckboxVisual' + vim.write + + file_contents = IO.read(filename) + + expect(file_contents).to eq normalize_string_indent(<<-TEXT) + # Hello there + - [X] first bullet + - [X] second bullet + - [X] third bullet + + TEXT + end end From 61b8efe8a54d88a0118a2ba9e88589a5a7cba4b4 Mon Sep 17 00:00:00 2001 From: Keith Miyake Date: Sat, 8 Oct 2022 16:58:29 -0700 Subject: [PATCH 3/3] fix visual checkbox toggle for parent/children --- plugin/bullets.vim | 10 +++++++++- spec/checkboxes_spec.rb | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/plugin/bullets.vim b/plugin/bullets.vim index ae6314d..4fb14d3 100644 --- a/plugin/bullets.vim +++ b/plugin/bullets.vim @@ -669,7 +669,15 @@ endfun fun! s:visual_toggle_checkboxes() let l:selection_lines = s:get_visual_selection_lines() for l:line in l:selection_lines - call s:toggle_checkboxes_nested(l:line.nr) + " call s:toggle_checkboxes_nested(l:line.nr) + let l:checked = s:toggle_checkbox(l:line.nr) + + if g:bullets_nested_checkboxes + " Toggle children and parents + let l:completion_marker = s:sibling_checkbox_status(l:line.nr) + call s:set_parent_checkboxes(l:line.nr, l:completion_marker) + endif + endfor endfun diff --git a/spec/checkboxes_spec.rb b/spec/checkboxes_spec.rb index 5a3a478..56eae0f 100644 --- a/spec/checkboxes_spec.rb +++ b/spec/checkboxes_spec.rb @@ -259,8 +259,7 @@ TEXT vim.edit filename - vim.normal 'j' - vim.normal 'jv2j' + vim.normal 'jV2j' vim.command 'ToggleCheckboxVisual' vim.write