Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions lua/bullets/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,54 @@ local function feed(keys)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), "n", false)
end

local function parse_standard(line)
local indent, marker, spacing, text = line:match("^(%s*)([-*+])(%s+)(.*)$")
if not marker then
return nil
end

return {
indent = indent,
marker = marker,
spacing = spacing,
text = text,
}
end

local function at_eol(line)
return vim.fn.col(".") == #line + 1
end

local function insert_line(lnum, line)
vim.api.nvim_buf_set_lines(0, lnum, lnum, false, { line })
vim.api.nvim_win_set_cursor(0, { lnum + 1, #line })
end

function M.insert_new_bullet()
if vim.fn.mode() == "n" then
vim.cmd.startinsert({ bang = true })
else
local mode = vim.fn.mode()
local lnum = vim.api.nvim_win_get_cursor(0)[1]
local line = vim.api.nvim_get_current_line()
local bullet = parse_standard(line)

if mode ~= "n" and not at_eol(line) then
feed("<CR>")
return ""
end

if not bullet then
if mode == "n" then
feed("o")
return ""
end

feed("<CR>")
return ""
end

insert_line(lnum, bullet.indent .. bullet.marker .. bullet.spacing)

if mode == "n" then
vim.cmd.startinsert({ bang = true })
end

return ""
Expand Down
4 changes: 2 additions & 2 deletions lua/bullets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ end

local function add_plug_mappings()
map("i", "<Plug>(bullets-newline)", function()
return actions().insert_new_bullet()
end, { expr = true })
actions().insert_new_bullet()
end)
map("n", "<Plug>(bullets-newline)", function()
actions().insert_new_bullet()
end)
Expand Down
7 changes: 4 additions & 3 deletions test/bullets_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local helpers = require("test.helpers")
local active_it = it
local it = pending

describe("Bullets.vim", function()
Expand All @@ -9,7 +10,7 @@ describe("Bullets.vim", function()
end)

describe("on return key when cursor is not at EOL", function()
it("splits the line and does not add a bullet", function()
active_it("splits the line and does not add a bullet", function()
-- G$i places cursor on last char 't' in "- this is the first bullet"
-- i inserts before 't', CR is deferred (not at EOL), splits the line
-- then "second bullet" is typed before 't': "second bullett"
Expand All @@ -36,7 +37,7 @@ describe("Bullets.vim", function()
end)

describe("on return key when cursor is at EOL", function()
it("adds a new bullet if the previous line had a known bullet type", function()
active_it("adds a new bullet if the previous line had a known bullet type", function()
helpers.test_bullet_inserted(
"do that",
{ "# Hello there", "- do this" },
Expand Down Expand Up @@ -102,7 +103,7 @@ describe("Bullets.vim", function()
)
end)

it("adds a new - bullet with right padding", function()
active_it("adds a new - bullet with right padding", function()
helpers.test_bullet_inserted(
"second bullet",
{ "# Hello there", "- this is the first bullet" },
Expand Down
2 changes: 1 addition & 1 deletion test/poc_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("Bullets.vim", function()
assert.equals(2, vim.fn.exists(":InsertNewBullet"))
end)

pending("inserts a new bullet on <CR>", function()
it("inserts a new bullet on <CR>", function()
vim.cmd("enew")
vim.bo.filetype = "text"
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "- first item" })
Expand Down