diff --git a/plugin/bullets.vim b/plugin/bullets.vim index dcc2ed7..7a0e53c 100644 --- a/plugin/bullets.vim +++ b/plugin/bullets.vim @@ -572,11 +572,21 @@ fun! s:insert_new_bullet() " searching up from there let l:send_return = 1 let l:normal_mode = mode() ==# 'n' + let l:insert_mode = mode() ==# 'i' let l:indent_next = s:line_ends_in_colon(l:curr_line_num) && g:bullets_auto_indent_after_colon + let l:text_after_cursor = '' + + " check if current line is a bullet + if l:bullet != {} + " check whether we're in the middle of the line (for insert mode only) + if !s:is_at_eol() && !s:is_at_first_col() && l:insert_mode + let l:col = col('.') + let l:curr_line = getline('.') + let l:text_before_cursor = l:curr_line[:l:col-2] + let l:text_after_cursor = l:curr_line[l:col-1:] + call setline(l:curr_line_num, l:text_before_cursor) + endif - " check if current line is a bullet and we are at the end of the line (for - " insert mode only) - if l:bullet != {} && (l:normal_mode || s:is_at_eol()) " was any text entered after the bullet? if l:bullet.text_after_bullet ==# '' " We don't want to create a new bullet if the previous one was not used, @@ -598,6 +608,10 @@ fun! s:insert_new_bullet() let l:next_bullet_list = [s:pad_to_length(l:next_bullet, l:bullet.bullet_length)] endif + if l:text_after_cursor !=# '' + let l:next_bullet_list[0] = l:next_bullet_list[0] . l:text_after_cursor + endif + " prepend blank lines if desired if g:bullets_line_spacing > 1 let l:next_bullet_list += map(range(g:bullets_line_spacing - 1), '""') @@ -610,7 +624,11 @@ fun! s:insert_new_bullet() " go to next line after the new bullet let l:col = strlen(getline(l:next_line_num)) + 1 - call setpos('.', [0, l:next_line_num, l:col]) + if l:text_after_cursor ==# '' + call setpos('.', [0, l:next_line_num, l:col]) + else + call setpos('.', [0, l:next_line_num, l:bullet.bullet_length + 1]) + endif " indent if previous line ended in a colon if l:indent_next @@ -645,6 +663,10 @@ fun! s:is_at_eol() return strlen(getline('.')) + 1 ==# col('.') endfun +fun! s:is_at_first_col() + return col('.') ==# 1 +endfun + command! InsertNewBullet call insert_new_bullet() " Helper for Colon Indent diff --git a/spec/asciidoc_spec.rb b/spec/asciidoc_spec.rb index 18d7981..2d90b8c 100644 --- a/spec/asciidoc_spec.rb +++ b/spec/asciidoc_spec.rb @@ -28,7 +28,6 @@ end it 'supports nested dot bullets' do - pending('FIXME: this test fails, but the functionality works') test_bullet_inserted('rats', <<-INIT, <<-EXPECTED) = Pets! . dogs diff --git a/spec/bullets_spec.rb b/spec/bullets_spec.rb index cf370f0..18fe565 100644 --- a/spec/bullets_spec.rb +++ b/spec/bullets_spec.rb @@ -4,30 +4,6 @@ RSpec.describe 'Bullets.vim' do describe 'inserting new bullets' do - context 'on return key when cursor is not at EOL' do - it 'splits the line and does not add a bullet' do - filename = "#{SecureRandom.hex(6)}.txt" - write_file(filename, <<-TEXT) - # Hello there - - this is the first bullet - TEXT - - vim.edit filename - vim.type 'G$i' - vim.feedkeys '\' - vim.type 'second bullet' - vim.write - - file_contents = IO.read(filename) - - expect(file_contents).to eq normalize_string_indent(<<-TEXT) - # Hello there - - this is the first bulle - second bullett\n - TEXT - end - end - context 'on return key when cursor is at EOL' do it 'adds a new bullet if the previous line had a known bullet type' do test_bullet_inserted('do that', <<-INIT, <<-EXPECTED) @@ -57,6 +33,30 @@ EXPECTED end + context 'on return key when cursor is not at EOL' do + it 'splits the line and adds a new bullet if the previous line had a known bullet type' do + filename = "#{SecureRandom.hex(6)}.txt" + write_file(filename, <<-TEXT) + # Hello there + - this is the first bullet + TEXT + + vim.edit filename + vim.type 'G$i' + vim.feedkeys '\' + vim.type 'second bullet' + vim.write + + file_contents = IO.read(filename) + + expect(file_contents).to eq normalize_string_indent(<<-TEXT) + # Hello there + - this is the first bulle + - second bullett\n + TEXT + end + end + it 'adds a pandoc bullet if the prev line had one' do test_bullet_inserted('second bullet', <<-INIT, <<-EXPECTED) Hello there