Skip to content

Commit

Permalink
feat: add option to indent line after trailing colon. Fixes #119 (#130)
Browse files Browse the repository at this point in the history
* ignore doc/tags

* feat: add option to indent line after trailing colon

* add colon indentation to readme

* fix colon indentation spec

Co-authored-by: Dorian Karter <[email protected]>
  • Loading branch information
kaymmm and dkarter authored Oct 8, 2022
1 parent 8a4dc35 commit bb2b9f3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
doc/tags
vendor/
spec/examples.txt
doc/tags
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ let g:bullets_pad_right = 0
" ^ no extra space between bullet and text
```

Indent new bullets when the previous bullet ends with a colon:

```vim
let g:bullets_auto_indent_after_colon = 1 " default = 1
" a. text
" b. text:
" i. text
```

Maximum number of alphabetic characters to use for bullets:

```vim
Expand Down
41 changes: 33 additions & 8 deletions plugin/bullets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ if !exists('g:bullets_checkbox_partials_toggle')
let g:bullets_checkbox_partials_toggle = 1
endif

if !exists('g:bullets_auto_indent_after_colon')
" Should a line ending in a colon result in the next line being indented (1)?
let g:bullets_auto_indent_after_colon = 1
endif

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

" Parse Bullet Type ------------------------------------------- {{{
Expand Down Expand Up @@ -456,6 +461,7 @@ fun! s:insert_new_bullet()
" searching up from there
let l:send_return = 1
let l:normal_mode = mode() ==# 'n'
let l:indent_next = s:line_ends_in_colon(l:curr_line_num) && g:bullets_auto_indent_after_colon

" check if current line is a bullet and we are at the end of the line (for
" insert mode only)
Expand Down Expand Up @@ -485,12 +491,23 @@ fun! s:insert_new_bullet()

" insert next bullet
call append(l:curr_line_num, l:next_bullet_list)
" got to next line after the new bullet


" go to next line after the new bullet
let l:col = strlen(getline(l:next_line_num)) + 1
if g:bullets_renumber_on_change
call setpos('.', [0, l:next_line_num, l:col])

" indent if previous line ended in a colon
if l:indent_next
" demote the new bullet
call s:change_bullet_level_and_renumber(-1)
" reset cursor position after indenting
let l:col = strlen(getline(l:next_line_num)) + 1
call setpos('.', [0, l:next_line_num, l:col])
elseif g:bullets_renumber_on_change
call s:renumber_whole_list()
endif
call setpos('.', [0, l:next_line_num, l:col])

let l:send_return = 0
endif
endif
Expand All @@ -514,6 +531,14 @@ fun! s:is_at_eol()
endfun

command! InsertNewBullet call <SID>insert_new_bullet()

" Helper for Colon Indent
" returns 1 if current line ends in a colon, else 0
fun! s:line_ends_in_colon(lnum)
return getline(a:lnum)[strlen(getline(a:lnum))-1:] ==# ':'
endfun
" --------------------------------------------------------- }}}

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

" Checkboxes ---------------------------------------------- {{{
Expand Down Expand Up @@ -945,11 +970,11 @@ fun! s:change_bullet_level(direction)
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
" 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:visual_change_bullet_level(direction)
Expand Down
29 changes: 29 additions & 0 deletions spec/nested_bullets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,5 +565,34 @@
TEXT
end

it 'indents after a line ending in a colon' do
filename = "#{SecureRandom.hex(6)}.txt"
write_file(filename, <<-TEXT)
# Hello there
a. this is the first bullet
TEXT

vim.command 'let g:bullets_auto_indent_after_colon = 1'
vim.edit filename
vim.type 'GA'
vim.feedkeys '\<cr>'
vim.type 'this is the second bullet:'
vim.feedkeys '\<cr>'
vim.type 'this bullet is indented'
vim.feedkeys '\<cr>'
vim.type 'this bullet is also indented'
vim.write

file_contents = IO.read(filename)

expect(file_contents.strip).to eq normalize_string_indent(<<-TEXT)
# Hello there
a. this is the first bullet
b. this is the second bullet:
\ti. this bullet is indented
\tii. this bullet is also indented
TEXT
end
end
end

0 comments on commit bb2b9f3

Please sign in to comment.