Skip to content

Commit 8429a4c

Browse files
author
Dorian Karter
committed
Refactor bullet parsing and detection
1 parent 85ffeeb commit 8429a4c

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

plugin/bullets.vim

+36-32
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fun! s:match_numeric_list_item(input_text)
5858
let l:text_after_bullet = l:matches[6]
5959

6060
return {
61+
\ 'bullet_type': 'num',
6162
\ 'leading_space': l:leading_space,
6263
\ 'trailing_space': l:trailing_space,
6364
\ 'bullet': l:num,
@@ -80,6 +81,7 @@ fun! s:match_roman_list_item(input_text)
8081
let l:text_after_bullet = l:matches[6]
8182

8283
return {
84+
\ 'bullet_type': 'rom',
8385
\ 'leading_space': l:leading_space,
8486
\ 'trailing_space': l:trailing_space,
8587
\ 'bullet': l:rom,
@@ -100,6 +102,7 @@ fun! s:match_checkbox_bullet_item(input_text)
100102
let l:text_after_bullet = l:matches[3]
101103

102104
return {
105+
\ 'bullet_type': 'chk',
103106
\ 'leading_space': l:leading_space,
104107
\ 'text_after_bullet': l:text_after_bullet
105108
\ }
@@ -117,10 +120,30 @@ fun! s:match_bullet_list_item(input_text)
117120
let l:text_after_bullet = l:matches[3]
118121

119122
return {
123+
\ 'bullet_type': 'std',
120124
\ 'whole_bullet': l:whole_bullet,
121125
\ 'text_after_bullet': l:text_after_bullet
122126
\ }
123127
endfun
128+
129+
fun! s:parse_bullet(line_text)
130+
let l:std_bullet_matches = s:match_bullet_list_item(a:line_text)
131+
let l:chk_bullet_matches = s:match_checkbox_bullet_item(a:line_text)
132+
let l:num_bullet_matches = s:match_numeric_list_item(a:line_text)
133+
let l:rom_bullet_matches = s:match_roman_list_item(a:line_text)
134+
135+
if !empty(l:chk_bullet_matches)
136+
return l:chk_bullet_matches
137+
elseif !empty(l:std_bullet_matches)
138+
return l:std_bullet_matches
139+
elseif !empty(l:num_bullet_matches)
140+
return l:num_bullet_matches
141+
elseif !empty(l:rom_bullet_matches)
142+
return l:rom_bullet_matches
143+
else
144+
return {}
145+
endif
146+
endfun
124147
" ------------------------------------------------------- }}}
125148

126149
" Helper methods ---------------------------------------- {{{
@@ -141,17 +164,17 @@ endfun
141164
" ------------------------------------------------------- }}}
142165

143166
" Generate bullets -------------------------------------- {{{
144-
fun! s:next_bullet_str(bullet_type, line_data)
145-
if a:bullet_type ==# 'rom'
146-
let l:next_num = s:arabic2roman(s:roman2arabic(a:line_data.bullet) + 1)
147-
return a:line_data.leading_space . l:next_num . a:line_data.closure . ' '
148-
elseif a:bullet_type ==# 'num'
149-
let l:next_num = a:line_data.bullet + 1
150-
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 . '- [ ] '
167+
fun! s:next_bullet_str(bullet)
168+
if a:bullet.bullet_type ==# 'rom'
169+
let l:next_num = s:arabic2roman(s:roman2arabic(a:bullet.bullet) + 1)
170+
return a:bullet.leading_space . l:next_num . a:bullet.closure . ' '
171+
elseif a:bullet.bullet_type ==# 'num'
172+
let l:next_num = a:bullet.bullet + 1
173+
return a:bullet.leading_space . l:next_num . a:bullet.closure . ' '
174+
elseif a:bullet.bullet_type ==# 'chk'
175+
return a:bullet.leading_space . '- [ ] '
153176
else
154-
return a:line_data.whole_bullet
177+
return a:bullet.whole_bullet
155178
endif
156179
endfun
157180

@@ -165,42 +188,23 @@ fun! s:insert_new_bullet()
165188
let l:curr_line_num = line('.')
166189
let l:next_line_num = l:curr_line_num + g:bullets_line_spacing
167190
let l:curr_line = getline(l:curr_line_num)
168-
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)
170-
let l:num_bullet_matches = s:match_numeric_list_item(l:curr_line)
171-
let l:rom_bullet_matches = s:match_roman_list_item(l:curr_line)
172-
let l:bullet_type = ''
173-
let l:bullet = {}
191+
let l:bullet = s:parse_bullet(l:curr_line)
174192
let l:bullet_content = ''
175193
let l:text_after_bullet = ''
176194
let l:send_return = 1
177195
let l:normal_mode = mode() ==# 'n'
178196

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)
183-
let l:bullet_type = 'std'
184-
let l:bullet = l:std_bullet_matches
185-
elseif !empty(l:num_bullet_matches)
186-
let l:bullet_type = 'num'
187-
let l:bullet = l:num_bullet_matches
188-
elseif !empty(l:rom_bullet_matches)
189-
let l:bullet_type = 'rom'
190-
let l:bullet = l:rom_bullet_matches
191-
endif
192-
193197
" check if current line is a bullet and we are at the end of the line (for
194198
" insert mode only)
195-
if strlen(l:bullet_type) && (l:normal_mode || s:is_at_eol())
199+
if l:bullet != {} && (l:normal_mode || s:is_at_eol())
196200
" was any text entered after the bullet?
197201
if l:bullet.text_after_bullet ==# ''
198202
" We don't want to create a new bullet if the previous one was not used,
199203
" instead we want to delete the empty bullet - like word processors do
200204
call s:delete_empty_bullet(l:curr_line_num)
201205
else
202206

203-
let l:next_bullet_list = [s:next_bullet_str(l:bullet_type, l:bullet)]
207+
let l:next_bullet_list = [s:next_bullet_str(l:bullet)]
204208

205209
" prepend blank lines if desired
206210
if g:bullets_line_spacing > 1

0 commit comments

Comments
 (0)