Skip to content

Commit 167140b

Browse files
committed
Add support for inserting new bullets mid-line
1 parent 2253f97 commit 167140b

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

plugin/bullets.vim

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,21 @@ fun! s:insert_new_bullet()
566566
" searching up from there
567567
let l:send_return = 1
568568
let l:normal_mode = mode() ==# 'n'
569+
let l:insert_mode = mode() ==# 'i'
569570
let l:indent_next = s:line_ends_in_colon(l:curr_line_num) && g:bullets_auto_indent_after_colon
571+
let l:text_after_cursor = ''
572+
573+
" check if current line is a bullet
574+
if l:bullet != {}
575+
" check whether we're in the middle of the line (for insert mode only)
576+
if !s:is_at_eol() && !s:is_at_first_col() && l:insert_mode
577+
let l:col = col('.')
578+
let l:curr_line = getline('.')
579+
let l:text_before_cursor = l:curr_line[:l:col-2]
580+
let l:text_after_cursor = l:curr_line[l:col-1:]
581+
call setline(l:curr_line_num, l:text_before_cursor)
582+
endif
570583

571-
" check if current line is a bullet and we are at the end of the line (for
572-
" insert mode only)
573-
if l:bullet != {} && (l:normal_mode || s:is_at_eol())
574584
" was any text entered after the bullet?
575585
if l:bullet.text_after_bullet ==# ''
576586
" We don't want to create a new bullet if the previous one was not used,
@@ -588,6 +598,10 @@ fun! s:insert_new_bullet()
588598
let l:next_bullet_list = [s:pad_to_length(l:next_bullet, l:bullet.bullet_length)]
589599
endif
590600

601+
if l:text_after_cursor !=# ''
602+
let l:next_bullet_list[0] = l:next_bullet_list[0] . l:text_after_cursor
603+
endif
604+
591605
" prepend blank lines if desired
592606
if g:bullets_line_spacing > 1
593607
let l:next_bullet_list += map(range(g:bullets_line_spacing - 1), '""')
@@ -600,7 +614,11 @@ fun! s:insert_new_bullet()
600614

601615
" go to next line after the new bullet
602616
let l:col = strlen(getline(l:next_line_num)) + 1
603-
call setpos('.', [0, l:next_line_num, l:col])
617+
if l:text_after_cursor ==# ''
618+
call setpos('.', [0, l:next_line_num, l:col])
619+
else
620+
call setpos('.', [0, l:next_line_num, l:bullet.bullet_length + 1])
621+
endif
604622

605623
" indent if previous line ended in a colon
606624
if l:indent_next
@@ -635,6 +653,10 @@ fun! s:is_at_eol()
635653
return strlen(getline('.')) + 1 ==# col('.')
636654
endfun
637655

656+
fun! s:is_at_first_col()
657+
return col('.') ==# 1
658+
endfun
659+
638660
command! InsertNewBullet call <SID>insert_new_bullet()
639661

640662
" Helper for Colon Indent

spec/asciidoc_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
end
2929

3030
it 'supports nested dot bullets' do
31-
pending('FIXME: this test fails, but the functionality works')
3231
test_bullet_inserted('rats', <<-INIT, <<-EXPECTED)
3332
= Pets!
3433
. dogs

spec/bullets_spec.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,6 @@
44

55
RSpec.describe 'Bullets.vim' do
66
describe 'inserting new bullets' do
7-
context 'on return key when cursor is not at EOL' do
8-
it 'splits the line and does not add a bullet' do
9-
filename = "#{SecureRandom.hex(6)}.txt"
10-
write_file(filename, <<-TEXT)
11-
# Hello there
12-
- this is the first bullet
13-
TEXT
14-
15-
vim.edit filename
16-
vim.type 'G$i'
17-
vim.feedkeys '\<cr>'
18-
vim.type 'second bullet'
19-
vim.write
20-
21-
file_contents = IO.read(filename)
22-
23-
expect(file_contents).to eq normalize_string_indent(<<-TEXT)
24-
# Hello there
25-
- this is the first bulle
26-
second bullett\n
27-
TEXT
28-
end
29-
end
30-
317
context 'on return key when cursor is at EOL' do
328
it 'adds a new bullet if the previous line had a known bullet type' do
339
test_bullet_inserted('do that', <<-INIT, <<-EXPECTED)
@@ -57,6 +33,30 @@
5733
EXPECTED
5834
end
5935

36+
context 'on return key when cursor is not at EOL' do
37+
it 'splits the line and adds a new bullet if the previous line had a known bullet type' do
38+
filename = "#{SecureRandom.hex(6)}.txt"
39+
write_file(filename, <<-TEXT)
40+
# Hello there
41+
- this is the first bullet
42+
TEXT
43+
44+
vim.edit filename
45+
vim.type 'G$i'
46+
vim.feedkeys '\<cr>'
47+
vim.type 'second bullet'
48+
vim.write
49+
50+
file_contents = IO.read(filename)
51+
52+
expect(file_contents).to eq normalize_string_indent(<<-TEXT)
53+
# Hello there
54+
- this is the first bulle
55+
- second bullett\n
56+
TEXT
57+
end
58+
end
59+
6060
it 'adds a pandoc bullet if the prev line had one' do
6161
test_bullet_inserted('second bullet', <<-INIT, <<-EXPECTED)
6262
Hello there

0 commit comments

Comments
 (0)