Skip to content

Commit 85ffeeb

Browse files
author
Dorian Karter
committed
Insert empty checkbox even if prev was checked
1 parent 5309828 commit 85ffeeb

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

plugin/bullets.vim

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
" Vim plugin for automated bulleted lists
2-
" Last Change: Mon 9 Apr 2018
2+
" Last Change: 10 Apr 2018
33
" Maintainer: Dorian Karter
44
" License: MIT
55
" FileTypes: markdown, text, gitcommit
@@ -42,7 +42,7 @@ if !exists('g:bullets_line_spacing')
4242
end
4343
" ------------------------------------------------------ }}}
4444

45-
" Helper methods ---------------------------------------- {{{
45+
" Bullet type detection ---------------------------------------- {{{
4646
fun! s:match_numeric_list_item(input_text)
4747
let l:num_bullet_regex = '\v^((\s*)(\d+)(\.|\))(\s+))(.*)'
4848
let l:matches = matchlist(a:input_text, l:num_bullet_regex)
@@ -88,25 +88,42 @@ fun! s:match_roman_list_item(input_text)
8888
\ }
8989
endfun
9090

91+
fun! s:match_checkbox_bullet_item(input_text)
92+
let l:checkbox_bullet_regex = '\v(^(\s*)- \[[x ]?\] )(.*)'
93+
let l:matches = matchlist(a:input_text, l:checkbox_bullet_regex)
94+
95+
if empty(l:matches)
96+
return {}
97+
endif
98+
99+
let l:leading_space = l:matches[2]
100+
let l:text_after_bullet = l:matches[3]
101+
102+
return {
103+
\ 'leading_space': l:leading_space,
104+
\ 'text_after_bullet': l:text_after_bullet
105+
\ }
106+
endfun
107+
91108
fun! s:match_bullet_list_item(input_text)
92-
let l:std_bullet_regex = '\v(^\s*(-|\*+|#\.|\\item)( \[[x ]?\])? )(.*)'
109+
let l:std_bullet_regex = '\v(^\s*(-|\*+|#\.|\\item) )(.*)'
93110
let l:matches = matchlist(a:input_text, l:std_bullet_regex)
94111

95112
if empty(l:matches)
96113
return {}
97114
endif
98115

99116
let l:whole_bullet = l:matches[1]
100-
let l:bullet = l:matches[2]
101-
let l:text_after_bullet = l:matches[4]
117+
let l:text_after_bullet = l:matches[3]
102118

103119
return {
104120
\ 'whole_bullet': l:whole_bullet,
105-
\ 'bullet': l:bullet,
106121
\ 'text_after_bullet': l:text_after_bullet
107122
\ }
108123
endfun
124+
" ------------------------------------------------------- }}}
109125

126+
" Helper methods ---------------------------------------- {{{
110127
fun! s:get_visual_selection_lines()
111128
let [l:lnum1, l:col1] = getpos("'<")[1:2]
112129
let [l:lnum2, l:col2] = getpos("'>")[1:2]
@@ -131,6 +148,8 @@ fun! s:next_bullet_str(bullet_type, line_data)
131148
elseif a:bullet_type ==# 'num'
132149
let l:next_num = a:line_data.bullet + 1
133150
return a:line_data.leading_space . l:next_num . a:line_data.closure . ' '
151+
elseif a:bullet_type ==# 'chk'
152+
return a:line_data.leading_space . '- [ ] '
134153
else
135154
return a:line_data.whole_bullet
136155
endif
@@ -147,6 +166,7 @@ fun! s:insert_new_bullet()
147166
let l:next_line_num = l:curr_line_num + g:bullets_line_spacing
148167
let l:curr_line = getline(l:curr_line_num)
149168
let l:std_bullet_matches = s:match_bullet_list_item(l:curr_line)
169+
let l:chk_bullet_matches = s:match_checkbox_bullet_item(l:curr_line)
150170
let l:num_bullet_matches = s:match_numeric_list_item(l:curr_line)
151171
let l:rom_bullet_matches = s:match_roman_list_item(l:curr_line)
152172
let l:bullet_type = ''
@@ -156,7 +176,10 @@ fun! s:insert_new_bullet()
156176
let l:send_return = 1
157177
let l:normal_mode = mode() ==# 'n'
158178

159-
if !empty(l:std_bullet_matches)
179+
if !empty(l:chk_bullet_matches)
180+
let l:bullet_type = 'chk'
181+
let l:bullet = l:chk_bullet_matches
182+
elseif !empty(l:std_bullet_matches)
160183
let l:bullet_type = 'std'
161184
let l:bullet = l:std_bullet_matches
162185
elseif !empty(l:num_bullet_matches)

spec/checkboxes_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
- [ ] do that
1414
EXPECTED
1515
end
16+
17+
it 'inserts an empty checkbox even if prev line was checked' do
18+
test_bullet_inserted('do that', <<-INIT, <<-EXPECTED)
19+
# Hello there
20+
- [x] do this
21+
INIT
22+
# Hello there
23+
- [x] do this
24+
- [ ] do that
25+
EXPECTED
26+
end
1627
end

0 commit comments

Comments
 (0)