Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Visual checkbox toggle #131

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions plugin/bullets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ fun! s:line_ends_in_colon(lnum)
return l:line[strlen(l:line)-1:] ==# ':'
endif
endfun

" --------------------------------------------------------- }}}

" Checkboxes ---------------------------------------------- {{{
Expand Down Expand Up @@ -715,28 +716,27 @@ 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
if empty(l:bullet) || l:bullet.bullet_type !=# 'chk'
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
Expand Down Expand Up @@ -777,9 +777,25 @@ 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)
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

command! SelectCheckboxInside call <SID>select_checkbox(1)
command! SelectCheckbox call <SID>select_checkbox(0)
command! ToggleCheckbox call <SID>toggle_checkboxes_nested()
command! ToggleCheckbox call <SID>toggle_checkboxes_nested(line('.'))
command! -range=% ToggleCheckboxVisual call <SID>visual_toggle_checkboxes()
" Checkboxes ---------------------------------------------- }}}

" Roman numerals --------------------------------------------- {{{
Expand Down Expand Up @@ -1071,6 +1087,13 @@ fun! s:change_line_bullet_level(direction, lnum)
call setline(a:lnum, l:next_bullet_str)
endfun

fun! s:change_bullet_level_and_renumber(direction)
" Calls change_bullet_level and then renumber_whole_list if required
call s:change_bullet_level(a:direction)
if g:bullets_renumber_on_change
call s:renumber_whole_list()
endif
endfun

fun! s:change_bullet_level(direction, is_visual)
" Changes the bullet level for each of the selected lines
Expand Down Expand Up @@ -1104,6 +1127,7 @@ nnoremap <silent> <Plug>(bullets-renumber) :RenumberList<cr>

" Toggle checkbox
nnoremap <silent> <Plug>(bullets-toggle-checkbox) :ToggleCheckbox<cr>
vnoremap <silent> <Plug>(bullets-toggle-checkbox) :ToggleCheckboxVisual<cr>

" Promote and Demote outline level
inoremap <silent> <Plug>(bullets-demote) <C-o>:BulletDemote<cr>
Expand Down Expand Up @@ -1153,6 +1177,7 @@ augroup TextBulletsMappings

" Toggle checkbox
call s:add_local_mapping(1, 'nmap', '<leader>x', '<Plug>(bullets-toggle-checkbox)')
call s:add_local_mapping(1, 'vmap', '<leader>x', '<Plug>(bullets-toggle-checkbox)')

" Promote and Demote outline level
call s:add_local_mapping(1, 'imap', '<C-t>', '<Plug>(bullets-demote)')
Expand Down
25 changes: 25 additions & 0 deletions spec/checkboxes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,29 @@

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 '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
Loading