Skip to content

Commit db319f0

Browse files
committed
fix checkbox padding with double-wide UTF characters
1 parent 5a04df1 commit db319f0

2 files changed

Lines changed: 63 additions & 16 deletions

File tree

plugin/bullets.vim

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ if !exists('g:bullets_checkbox_markers')
8989
" You can disable partial completion markers like this:
9090
" let g:bullets_checkbox_markers = ' X'
9191
endif
92-
let s:checkbox_markers = split(g:bullets_checkbox_markers,'\zs')
9392

9493
if !exists('g:bullets_checkbox_partials_toggle')
9594
" Should toggling on a partially completed checkbox set it to on (1), off
@@ -221,7 +220,7 @@ fun! s:match_checkbox_bullet_item(input_text)
221220
" default ' ', 'x', and 'X'
222221
let l:checkbox_bullet_regex =
223222
\ '\v(^(\s*)([-\*] \[(['
224-
\ . join(s:checkbox_markers,'')
223+
\ . g:bullets_checkbox_markers
225224
\ . ' xX])?\])(\s+))(.*)'
226225
let l:matches = matchlist(a:input_text, l:checkbox_bullet_regex)
227226

@@ -455,7 +454,11 @@ fun! s:insert_new_bullet()
455454
elseif !(l:bullet.bullet_type ==# 'abc' && s:abc2dec(l:bullet.bullet) + 1 > s:abc_max)
456455

457456
let l:next_bullet = s:next_bullet_str(l:bullet)
458-
let l:next_bullet_list = [s:pad_to_length(l:next_bullet, l:bullet.bullet_length)]
457+
if l:bullet.bullet_type ==# 'chk'
458+
let l:next_bullet_list = [l:next_bullet]
459+
else
460+
let l:next_bullet_list = [s:pad_to_length(l:next_bullet, l:bullet.bullet_length)]
461+
endif
459462

460463
" prepend blank lines if desired
461464
if g:bullets_line_spacing > 1
@@ -531,23 +534,24 @@ fun! s:toggle_checkbox(lnum)
531534
return -1
532535
endif
533536

534-
let l:partial_markers = join(s:checkbox_markers[1:-2], '')
537+
let l:checkbox_markers = split(g:bullets_checkbox_markers, '\zs')
538+
let l:partial_markers = join(l:checkbox_markers[1:-2], '')
535539
if g:bullets_checkbox_partials_toggle > 0
536540
\ && l:checkbox_content =~# '\v[' . l:partial_markers . ']'
537541
" Partially complete
538542
let l:marker = g:bullets_checkbox_partials_toggle ?
539-
\ s:checkbox_markers[-1] :
540-
\ s:checkbox_markers[0]
541-
elseif l:checkbox_content =~# '\v[ ' . s:checkbox_markers[0] . ']'
542-
let l:marker = s:checkbox_markers[-1]
543-
elseif l:checkbox_content =~# '\v[xX' . s:checkbox_markers[-1] . ']'
544-
let l:marker = s:checkbox_markers[0]
543+
\ l:checkbox_markers[-1] :
544+
\ l:checkbox_markers[0]
545+
elseif l:checkbox_content =~# '\v[ ' . l:checkbox_markers[0] . ']'
546+
let l:marker = l:checkbox_markers[-1]
547+
elseif l:checkbox_content =~# '\v[xX' . l:checkbox_markers[-1] . ']'
548+
let l:marker = l:checkbox_markers[0]
545549
else
546550
return -1
547551
endif
548552

549553
call s:set_checkbox(l:lnum, l:marker)
550-
return l:marker ==? s:checkbox_markers[-1]
554+
return l:marker ==? l:checkbox_markers[-1]
551555
endfun
552556

553557
fun! s:set_checkbox(lnum, marker)
@@ -612,9 +616,10 @@ fun! s:set_child_checkboxes(lnum, checked)
612616

613617
let l:children = s:get_children_line_numbers(a:lnum)
614618
if !empty(l:children)
619+
let l:checkbox_markers = split(g:bullets_checkbox_markers, '\zs')
615620
for l:child in l:children
616-
let l:marker = a:checked ? s:checkbox_markers[-1] :
617-
\ s:checkbox_markers[0]
621+
let l:marker = a:checked ? l:checkbox_markers[-1] :
622+
\ l:checkbox_markers[0]
618623
call s:set_checkbox(l:child, l:marker)
619624
call s:set_child_checkboxes(l:child, a:checked)
620625
endfor
@@ -1217,22 +1222,23 @@ fun! s:sibling_checkbox_status(lnum)
12171222
let l:siblings = s:get_sibling_line_numbers(a:lnum)
12181223
let l:num_siblings = len(l:siblings)
12191224
let l:checked = 0
1225+
let l:checkbox_markers = split(g:bullets_checkbox_markers, '\zs')
12201226
for l:lnum in l:siblings
12211227
let l:indent = indent(l:lnum)
12221228
let l:bullet = s:closest_bullet_types(l:lnum, l:indent)
12231229
let l:bullet = s:resolve_bullet_type(l:bullet)
12241230
if !empty(l:bullet) && has_key(l:bullet, 'checkbox_marker')
12251231
let l:checkbox_content = l:bullet.checkbox_marker
12261232

1227-
if l:checkbox_content =~# '\v[xX' . s:checkbox_markers[-1] . ']'
1233+
if l:checkbox_content =~# '\v[xX' . l:checkbox_markers[-1] . ']'
12281234
" Checked
12291235
let l:checked += 1
12301236
endif
12311237
endif
12321238
endfor
1233-
let l:divisions = len(s:checkbox_markers) - 1.0
1239+
let l:divisions = len(l:checkbox_markers) - 1.0
12341240
let l:completion = float2nr(ceil(l:divisions * l:checked / l:num_siblings))
1235-
return s:checkbox_markers[l:completion]
1241+
return l:checkbox_markers[l:completion]
12361242
endfun
12371243

12381244
" ------------------------------------------------------- }}}

spec/checkboxes_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,45 @@
192192
193193
TEXT
194194
end
195+
196+
it 'adds and toggles bullets using UTF characters' do
197+
filename = "#{SecureRandom.hex(6)}.txt"
198+
write_file(filename, <<-TEXT)
199+
# Hello there
200+
- [ ] first bullet
201+
TEXT
202+
203+
vim.edit filename
204+
vim.command 'let g:bullets_checkbox_markers="✗○◐●✓"'
205+
vim.normal 'j'
206+
vim.command 'ToggleCheckbox'
207+
vim.feedkeys 'o'
208+
vim.type 'second bullet'
209+
vim.feedkeys '\<cr>\<C-t>'
210+
vim.type 'third bullet'
211+
vim.feedkeys '\<cr>'
212+
vim.type 'fourth bullet<esc>'
213+
vim.command 'ToggleCheckbox'
214+
vim.feedkeys 'o\<C-d>'
215+
vim.type 'fifth bullet<esc>'
216+
vim.command 'ToggleCheckbox'
217+
vim.feedkeys 'o'
218+
vim.type 'sixth bullet'
219+
vim.command 'ToggleCheckbox'
220+
vim.command 'ToggleCheckbox'
221+
vim.write
222+
223+
file_contents = IO.read(filename)
224+
225+
expect(file_contents).to eq normalize_string_indent(<<-TEXT)
226+
# Hello there
227+
- [✓] first bullet
228+
- [◐] second bullet
229+
\t- [ ] third bullet
230+
\t- [✓] fourth bullet
231+
- [✓] fifth bullet
232+
- [✗] sixth bullet
233+
234+
TEXT
235+
end
195236
end

0 commit comments

Comments
 (0)