Skip to content

Commit 1d51fce

Browse files
committed
feat: support ordered bullet continuations
1 parent d16158d commit 1d51fce

5 files changed

Lines changed: 316 additions & 32 deletions

File tree

‎lua/bullets/actions.lua‎

Lines changed: 195 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
local M = {}
2+
local config = require("bullets.config")
3+
local ordinal = require("bullets.ordinal")
24

35
local function parse_standard(line)
46
local indent, marker, spacing, text = line:match("^(%s*)([-*+])(%s+)(.*)$")
@@ -14,6 +16,127 @@ local function parse_standard(line)
1416
}
1517
end
1618

19+
local function parse_numeric(line)
20+
local indent, marker, closure, spacing, text = line:match("^(%s*)(%d+)([.)])(%s+)(.*)$")
21+
if not marker then
22+
return nil
23+
end
24+
25+
return {
26+
type = "num",
27+
indent = indent,
28+
marker = marker,
29+
closure = closure,
30+
spacing = spacing,
31+
text = text,
32+
}
33+
end
34+
35+
local function parse_alpha(line)
36+
local max = config.options.max_alpha_characters
37+
if max == 0 then
38+
return nil
39+
end
40+
41+
local indent, marker, closure, spacing, text = line:match("^(%s*)(%a+)([.)])(%s+)(.*)$")
42+
if not marker or #marker > max or not (marker == marker:lower() or marker == marker:upper()) then
43+
return nil
44+
end
45+
46+
return {
47+
type = "abc",
48+
indent = indent,
49+
marker = marker,
50+
closure = closure,
51+
spacing = spacing,
52+
text = text,
53+
}
54+
end
55+
56+
local function parse_roman(line)
57+
if not config.options.enable_roman_list then
58+
return nil
59+
end
60+
61+
local indent, marker, closure, spacing, text = line:match("^(%s*)(%a+)([.)])(%s+)(.*)$")
62+
if not marker or not (marker == marker:lower() or marker == marker:upper()) or not ordinal.is_roman(marker) then
63+
return nil
64+
end
65+
66+
return {
67+
type = "rom",
68+
indent = indent,
69+
marker = marker,
70+
closure = closure,
71+
spacing = spacing,
72+
text = text,
73+
}
74+
end
75+
76+
local function parse_line(line)
77+
local standard = parse_standard(line)
78+
if standard then
79+
standard.type = "std"
80+
return { standard }
81+
end
82+
83+
local numeric = parse_numeric(line)
84+
if numeric then
85+
return { numeric }
86+
end
87+
88+
local alpha = parse_alpha(line)
89+
local roman = parse_roman(line)
90+
91+
return vim.tbl_filter(function(item)
92+
return item ~= nil
93+
end, { alpha, roman })
94+
end
95+
96+
local resolve_bullet
97+
98+
local function previous_ordered_type(lnum, indent)
99+
for row = lnum - 1, 1, -1 do
100+
local line = vim.api.nvim_buf_get_lines(0, row - 1, row, false)[1]
101+
if line == "" then
102+
return nil
103+
end
104+
105+
local bullet = resolve_bullet(parse_line(line), row)
106+
if bullet and bullet.indent == indent and (bullet.type == "abc" or bullet.type == "rom") then
107+
return bullet.type
108+
end
109+
end
110+
111+
return nil
112+
end
113+
114+
function resolve_bullet(bullets, lnum)
115+
if #bullets == 0 then
116+
return nil
117+
end
118+
if #bullets == 1 then
119+
return bullets[1]
120+
end
121+
122+
local previous_type = previous_ordered_type(lnum, bullets[1].indent)
123+
if previous_type then
124+
for _, bullet in ipairs(bullets) do
125+
if bullet.type == previous_type then
126+
return bullet
127+
end
128+
end
129+
end
130+
131+
for _, bullet in ipairs(bullets) do
132+
if bullet.type == "rom" then
133+
return bullet
134+
end
135+
end
136+
137+
return bullets[1]
138+
end
139+
17140
local function at_eol(line)
18141
return vim.fn.col(".") == #line + 1
19142
end
@@ -25,19 +148,73 @@ local function open_line_below()
25148
vim.cmd.startinsert()
26149
end
27150

151+
local function split_line()
152+
local cursor = vim.api.nvim_win_get_cursor(0)
153+
local lnum, col = cursor[1], cursor[2]
154+
local line = vim.api.nvim_get_current_line()
155+
local before = line:sub(1, col)
156+
local after = line:sub(col + 1)
157+
158+
vim.api.nvim_buf_set_lines(0, lnum - 1, lnum, false, { before, after })
159+
vim.api.nvim_win_set_cursor(0, { lnum + 1, 0 })
160+
vim.cmd.startinsert()
161+
end
162+
28163
local function insert_line(lnum, line)
29164
vim.api.nvim_buf_set_lines(0, lnum, lnum, false, { line })
30165
vim.api.nvim_win_set_cursor(0, { lnum + 1, #line })
31166
end
32167

168+
local function pad_right(prefix, width)
169+
if not config.options.pad_right or #prefix >= width then
170+
return prefix
171+
end
172+
173+
return prefix .. string.rep(" ", width - #prefix)
174+
end
175+
176+
local function next_marker(bullet)
177+
if bullet.type == "num" then
178+
return tostring(tonumber(bullet.marker) + 1)
179+
end
180+
if bullet.type == "abc" then
181+
local marker =
182+
ordinal.number_to_abc(ordinal.abc_to_number(bullet.marker) + 1, bullet.marker == bullet.marker:lower())
183+
if #marker > config.options.max_alpha_characters then
184+
return nil
185+
end
186+
return marker
187+
end
188+
if bullet.type == "rom" then
189+
return ordinal.number_to_roman(ordinal.roman_to_number(bullet.marker) + 1, bullet.marker == bullet.marker:lower())
190+
end
191+
192+
return bullet.marker
193+
end
194+
195+
local function next_prefix(bullet)
196+
local marker = next_marker(bullet)
197+
if not marker then
198+
return nil
199+
end
200+
201+
if bullet.type == "std" then
202+
return bullet.indent .. marker .. bullet.spacing
203+
end
204+
205+
local prefix = marker .. bullet.closure .. " "
206+
return bullet.indent .. pad_right(prefix, #bullet.marker + #bullet.closure + #bullet.spacing)
207+
end
208+
33209
function M.insert_new_bullet()
34210
local mode = vim.fn.mode()
35211
local lnum = vim.api.nvim_win_get_cursor(0)[1]
36212
local line = vim.api.nvim_get_current_line()
37-
local bullet = parse_standard(line)
213+
local bullet = resolve_bullet(parse_line(line), lnum)
38214

39215
if mode ~= "n" and not at_eol(line) then
40-
return "\r"
216+
split_line()
217+
return ""
41218
end
42219

43220
if not bullet then
@@ -46,16 +223,28 @@ function M.insert_new_bullet()
46223
return ""
47224
end
48225

49-
return "\r"
226+
split_line()
227+
return ""
50228
end
51229

52-
local next_bullet = bullet.indent .. bullet.marker .. bullet.spacing
230+
local prefix = next_prefix(bullet)
231+
if not prefix then
232+
if mode == "n" then
233+
open_line_below()
234+
return ""
235+
end
236+
237+
split_line()
238+
return ""
239+
end
53240

54241
if mode ~= "n" then
55-
return "\r\21" .. next_bullet
242+
insert_line(lnum, prefix)
243+
vim.cmd.startinsert({ bang = true })
244+
return ""
56245
end
57246

58-
insert_line(lnum, next_bullet)
247+
insert_line(lnum, prefix)
59248
vim.cmd.startinsert({ bang = true })
60249

61250
return ""

‎lua/bullets/init.lua‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ end
6565

6666
local function add_plug_mappings()
6767
map("i", "<Plug>(bullets-newline)", function()
68-
return actions().insert_new_bullet()
69-
end, { expr = true })
68+
actions().insert_new_bullet()
69+
end)
7070
map("n", "<Plug>(bullets-newline)", function()
7171
actions().insert_new_bullet()
7272
end)

‎lua/bullets/ordinal.lua‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
local M = {}
2+
3+
local roman_values = {
4+
{ 1000, "m" },
5+
{ 900, "cm" },
6+
{ 500, "d" },
7+
{ 400, "cd" },
8+
{ 100, "c" },
9+
{ 90, "xc" },
10+
{ 50, "l" },
11+
{ 40, "xl" },
12+
{ 10, "x" },
13+
{ 9, "ix" },
14+
{ 5, "v" },
15+
{ 4, "iv" },
16+
{ 1, "i" },
17+
}
18+
19+
function M.abc_to_number(value)
20+
local result = 0
21+
local lower = value:lower()
22+
23+
for i = 1, #lower do
24+
result = result * 26 + lower:byte(i) - string.byte("a") + 1
25+
end
26+
27+
return result
28+
end
29+
30+
function M.number_to_abc(value, lower)
31+
local base = lower and string.byte("a") or string.byte("A")
32+
local result = ""
33+
34+
while value > 0 do
35+
value = value - 1
36+
result = string.char(base + value % 26) .. result
37+
value = math.floor(value / 26)
38+
end
39+
40+
return result
41+
end
42+
43+
function M.roman_to_number(value)
44+
local roman = value:lower()
45+
local result = 0
46+
local index = 1
47+
48+
while index <= #roman do
49+
local matched = false
50+
for _, pair in ipairs(roman_values) do
51+
local number, letters = pair[1], pair[2]
52+
if roman:sub(index, index + #letters - 1) == letters then
53+
result = result + number
54+
index = index + #letters
55+
matched = true
56+
break
57+
end
58+
end
59+
60+
if not matched then
61+
return nil
62+
end
63+
end
64+
65+
return result
66+
end
67+
68+
function M.number_to_roman(value, lower)
69+
local result = ""
70+
71+
for _, pair in ipairs(roman_values) do
72+
local number, letters = pair[1], pair[2]
73+
while value >= number do
74+
result = result .. letters
75+
value = value - number
76+
end
77+
end
78+
79+
if lower then
80+
return result
81+
end
82+
83+
return result:upper()
84+
end
85+
86+
function M.is_roman(value)
87+
local number = M.roman_to_number(value)
88+
if not number then
89+
return false
90+
end
91+
92+
return M.number_to_roman(number, value == value:lower()) == value
93+
end
94+
95+
return M

0 commit comments

Comments
 (0)