Skip to content

Commit 8ce5e2d

Browse files
committed
add renumbering on insert, fix bugs in renumbering, and fix tests
1 parent 764b342 commit 8ce5e2d

5 files changed

Lines changed: 272 additions & 57 deletions

File tree

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,32 @@ let g:bullets_outline_levels = ['num', 'abc', 'std*']
134134
" 2. second parent [ <cr><C-d> ]
135135
```
136136

137-
Enable/disable automatically renumbering the current ordered bullet list when changing the indent level of bullets:
137+
Enable/disable automatically renumbering the current ordered bullet list when changing the indent level of bullets or inserting a new bullet:
138138

139139
```vim
140140
let g:bullets_renumber_on_change = 1 " default = 1
141-
" Example:
141+
" Example 1:
142142
" 1. first existing bullet
143143
" a. second existing bullet [ hit <C-t> ]
144144
" 2. third existing bullet [ this got renumbered 3 -> 2 when bullet 2 got demoted ]
145+
"
146+
" Example 2:
147+
" 1. first existing bullet
148+
" 2. second existing bullet [ use <cr>/o to add a new bullet below this ]
149+
" 3. new bullet
150+
" 4. third existing bullet [ this got renumbered 3 -> 2 when bullet 2 got demoted ]
145151
146152
let g:bullets_renumber_on_change = 0
147153
" Example:
148154
" 1. first existing bullet
149155
" a. second existing bullet [ hit <C-t> ]
150156
" 3. third existing bullet [ no renumbering so this bullet remained `3` ]
157+
"
158+
" Example 2:
159+
" 1. first existing bullet
160+
" 2. second existing bullet [ use <cr>/o to add a new bullet below this ]
161+
" 3. new bullet
162+
" 3. third existing bullet [ no renumbering so this bullet remained `3` ]
151163
```
152164

153165
# Mappings

plugin/bullets.vim

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ set cpoptions&vim
1010
" ------------------------------------------------------- }}}
1111

1212
" Prevent execution if already loaded ------------------ {{{
13-
if exists('g:loaded_bullets_vim')
14-
finish
15-
endif
13+
" if exists('g:loaded_bullets_vim')
14+
" finish
15+
" endif
1616
let g:loaded_bullets_vim = 1
1717
" Prevent execution if already loaded ------------------ }}}
1818

@@ -186,7 +186,7 @@ fun! s:match_alphabetical_list_item(input_text)
186186
endfun
187187

188188
fun! s:match_checkbox_bullet_item(input_text)
189-
let l:checkbox_bullet_regex = '\v(^(\s*)- \[[x ]?\](\s+))(.*)'
189+
let l:checkbox_bullet_regex = '\v(^(\s*)(- \[[x ]?\])(\s+))(.*)'
190190
let l:matches = matchlist(a:input_text, l:checkbox_bullet_regex)
191191

192192
if empty(l:matches)
@@ -195,13 +195,16 @@ fun! s:match_checkbox_bullet_item(input_text)
195195

196196
let l:bullet_length = strlen(l:matches[1])
197197
let l:leading_space = l:matches[2]
198-
let l:trailing_space = l:matches[3]
199-
let l:text_after_bullet = l:matches[4]
198+
let l:bullet = l:matches[3]
199+
let l:trailing_space = l:matches[4]
200+
let l:text_after_bullet = l:matches[5]
200201

201202
return {
202203
\ 'bullet_type': 'chk',
203204
\ 'bullet_length': l:bullet_length,
204205
\ 'leading_space': l:leading_space,
206+
\ 'bullet': l:bullet,
207+
\ 'closure': '',
205208
\ 'trailing_space': l:trailing_space,
206209
\ 'text_after_bullet': l:text_after_bullet
207210
\ }
@@ -421,6 +424,9 @@ fun! s:insert_new_bullet()
421424
call append(l:curr_line_num, l:next_bullet_list)
422425
" got to next line after the new bullet
423426
let l:col = strlen(getline(l:next_line_num)) + 1
427+
if g:bullets_renumber_on_change
428+
call s:renumber_whole_list()
429+
endif
424430
call setpos('.', [0, l:next_line_num, l:col])
425431
let l:send_return = 0
426432
endif
@@ -575,6 +581,11 @@ fun! s:renumber_selection()
575581
let l:indent = indent(l:line.nr)
576582
let l:bullet = s:closest_bullet_types(l:line.nr, l:indent)
577583
let l:bullet = s:resolve_bullet_type(l:bullet)
584+
let l:curr_level = s:get_level(l:bullet)
585+
if l:curr_level > 1
586+
" then it's an AsciiDoc list and shouldn't be renumbered
587+
break
588+
endif
578589

579590
if !empty(l:bullet) && l:bullet.starting_at_line_num == l:line.nr
580591
" skip wrapped lines and lines that aren't bullets
@@ -619,9 +630,12 @@ fun! s:renumber_selection()
619630
let l:bullet_num = s:dec2abc(l:levels[l:indent].index, l:levels[l:indent].islower)
620631
elseif l:levels[l:indent].type ==# 'num'
621632
let l:bullet_num = l:levels[l:indent].index
622-
else
623-
" standard or checkbox
633+
elseif l:levels[l:indent].type ==# 'std'
634+
" normalize standard bullets
624635
let l:bullet_num = l:levels[l:indent].bullet
636+
else
637+
" checkboxes shouldn't change their checked status
638+
let l:bullet_num = l:bullet.bullet
625639
endif
626640

627641
let l:new_bullet =
@@ -921,6 +935,14 @@ fun! s:has_item(list, fn)
921935
return !empty(s:find(a:list, a:fn))
922936
endfun
923937

938+
fun! s:get_level(bullet)
939+
if a:bullet == {} || a:bullet.bullet_type !=# 'std'
940+
return 0
941+
else
942+
return len(a:bullet.bullet)
943+
endif
944+
endfun
945+
924946
fun! s:first_bullet_line()
925947
" returns the line number of the first bullet in the list containing the
926948
" cursor, up to the first blank line
@@ -929,16 +951,20 @@ fun! s:first_bullet_line()
929951
let l:first_line = -1
930952
let l:curr_indent = indent(l:lnum)
931953
let l:bullet_kinds = s:closest_bullet_types(l:lnum, l:curr_indent)
954+
let l:blank_lines = 0
955+
let l:list_start = 0
932956

933-
while l:lnum >= 1 && (l:curr_indent != 0 || l:bullet_kinds != [])
957+
while l:lnum >= 1 && !l:list_start
934958
if l:bullet_kinds != []
935959
let l:first_line = l:lnum
936-
let l:lnum = l:lnum - g:bullets_line_spacing
960+
let l:blank_lines = 0
937961
else
938-
let l:lnum = l:lnum - 1
962+
let l:blank_lines += 1
963+
let l:list_start = l:blank_lines >= g:bullets_line_spacing
939964
endif
940-
let l:bullet_kinds = s:closest_bullet_types(l:lnum, l:curr_indent)
965+
let l:lnum -= 1
941966
let l:curr_indent = indent(l:lnum)
967+
let l:bullet_kinds = s:closest_bullet_types(l:lnum, l:curr_indent)
942968
endwhile
943969
return l:first_line
944970
endfun
@@ -952,16 +978,20 @@ fun! s:last_bullet_line()
952978
let l:last_line = -1
953979
let l:curr_indent = indent(l:lnum)
954980
let l:bullet_kinds = s:closest_bullet_types(l:lnum, l:curr_indent)
981+
let l:blank_lines = 0
982+
let l:list_end = 0
955983

956-
while l:lnum <= l:buf_end && (l:curr_indent != 0 || l:bullet_kinds != [])
984+
while l:lnum <= l:buf_end && !l:list_end
957985
if l:bullet_kinds != []
958986
let l:last_line = l:lnum
959-
let l:lnum = l:lnum + g:bullets_line_spacing
987+
let l:blank_lines = 0
960988
else
961-
let l:lnum = l:lnum + 1
989+
let l:blank_lines += 1
990+
let l:list_end = l:blank_lines >= g:bullets_line_spacing
962991
endif
963-
let l:bullet_kinds = s:closest_bullet_types(l:lnum, l:curr_indent)
992+
let l:lnum += 1
964993
let l:curr_indent = indent(l:lnum)
994+
let l:bullet_kinds = s:closest_bullet_types(l:lnum, l:curr_indent)
965995
endwhile
966996
return l:last_line
967997
endfun

spec/alphabetic_bullets_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
TEXT
105105

106106
vim.edit filename
107+
vim.command('let g:bullets_renumber_on_change=0')
107108
vim.type 'GA'
108109
vim.feedkeys '\<cr>'
109110
vim.type 'second bullet'
@@ -184,6 +185,7 @@
184185
TEXT
185186

186187
vim.edit filename
188+
vim.command('let g:bullets_renumber_on_change=0')
187189
vim.type 'GA'
188190
vim.feedkeys '\<cr>'
189191
vim.type 'second bullet'

spec/bullets_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
end
103103

104104
it 'maintains total bullet width from 9. to 10. with reduced padding' do
105+
vim.command('let g:bullets_renumber_on_change=0')
105106
test_bullet_inserted('second bullet', <<-INIT, <<-EXPECTED)
106107
# Hello there
107108
9. this is the first bullet

0 commit comments

Comments
 (0)