diff --git a/lua/bullets/actions.lua b/lua/bullets/actions.lua index 244e232..cb75b0d 100644 --- a/lua/bullets/actions.lua +++ b/lua/bullets/actions.lua @@ -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("") + return "" + end + + if not bullet then + if mode == "n" then + feed("o") + return "" + end + feed("") + return "" + end + + insert_line(lnum, bullet.indent .. bullet.marker .. bullet.spacing) + + if mode == "n" then + vim.cmd.startinsert({ bang = true }) end return "" diff --git a/lua/bullets/init.lua b/lua/bullets/init.lua index 1d465cd..9fdcdb9 100644 --- a/lua/bullets/init.lua +++ b/lua/bullets/init.lua @@ -64,8 +64,8 @@ end local function add_plug_mappings() map("i", "(bullets-newline)", function() - return actions().insert_new_bullet() - end, { expr = true }) + actions().insert_new_bullet() + end) map("n", "(bullets-newline)", function() actions().insert_new_bullet() end) diff --git a/test/bullets_spec.lua b/test/bullets_spec.lua index abfc346..d886ce5 100644 --- a/test/bullets_spec.lua +++ b/test/bullets_spec.lua @@ -1,4 +1,5 @@ local helpers = require("test.helpers") +local active_it = it local it = pending describe("Bullets.vim", function() @@ -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" @@ -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" }, @@ -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" }, diff --git a/test/poc_spec.lua b/test/poc_spec.lua index 27f8d18..9e2f9c8 100644 --- a/test/poc_spec.lua +++ b/test/poc_spec.lua @@ -7,7 +7,7 @@ describe("Bullets.vim", function() assert.equals(2, vim.fn.exists(":InsertNewBullet")) end) - pending("inserts a new bullet on ", function() + it("inserts a new bullet on ", function() vim.cmd("enew") vim.bo.filetype = "text" vim.api.nvim_buf_set_lines(0, 0, -1, false, { "- first item" })