From bb2b9f3cae530fcc9e70055243cbbdda4daa497e Mon Sep 17 00:00:00 2001 From: Keith Miyake Date: Sat, 8 Oct 2022 22:53:19 +0000 Subject: [PATCH] feat: add option to indent line after trailing colon. Fixes #119 (#130) * 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 --- .gitignore | 1 + README.md | 9 ++++++++ plugin/bullets.vim | 41 +++++++++++++++++++++++++++++-------- spec/nested_bullets_spec.rb | 29 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index cc60121..0d1204a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ doc/tags vendor/ spec/examples.txt +doc/tags diff --git a/README.md b/README.md index 95cf307..e918a9d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/plugin/bullets.vim b/plugin/bullets.vim index a8a1b66..e66a548 100644 --- a/plugin/bullets.vim +++ b/plugin/bullets.vim @@ -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 ------------------------------------------- {{{ @@ -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) @@ -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 @@ -514,6 +531,14 @@ fun! s:is_at_eol() endfun command! InsertNewBullet call 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 ---------------------------------------------- {{{ @@ -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) diff --git a/spec/nested_bullets_spec.rb b/spec/nested_bullets_spec.rb index 7cf7ee4..a20b44e 100644 --- a/spec/nested_bullets_spec.rb +++ b/spec/nested_bullets_spec.rb @@ -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 '\' + vim.type 'this is the second bullet:' + vim.feedkeys '\' + vim.type 'this bullet is indented' + vim.feedkeys '\' + 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