Skip to content

Commit

Permalink
feat(link): remove input request when adding links
Browse files Browse the repository at this point in the history
  • Loading branch information
tadmccorkle committed Nov 14, 2023
1 parent 2638252 commit 44ef530
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 47 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Tools for working with markdown files in Neovim.
- Table of contents
- Supports ATX and setext headings
- Omit headings with an HTML tag
- Improved list editing
- Lists
- Insert new items
- Auto-number ordered lists
- Toggle task list items (GFM)
Expand All @@ -41,8 +41,6 @@ Tools for working with markdown files in Neovim.
- Configurable default list marker type
- Specify list marker type
- Omit section with HTML tag
- Links
- Completion when adding heading and relative file links
- Tables (GFM)
- Formatting
- Insert rows and columns
Expand Down
15 changes: 9 additions & 6 deletions lua/markdown/link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,22 @@ function M.add(motion)
return
end

local ok, input = util.try_get_input("Link destination: ")
if not ok then
return
end

util.insert_text(r[3], r[4], "](" .. input .. ")")
util.insert_text(r[3], r[4], "]()")
util.insert_text(r[1], r[2], "[")

-- leave visual mode if successful
if is_visual then
local esc = api.nvim_replace_termcodes("<Esc>", true, false, true)
api.nvim_feedkeys(esc, "n", false)
end

-- enter insert mode to type destination
local col = r[4] + 2
if r[1] == r[3] then
col = col + 1
end
api.nvim_win_set_cursor(0, { r[3] + 1, col })
vim.cmd("startinsert")
end

---@param dest string
Expand Down
4 changes: 1 addition & 3 deletions lua/markdown/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ local function insert_list_item(loc)

new_row = new_row + 1
api.nvim_win_set_cursor(0, { new_row, vim.fn.charcol({ new_row, "$" }) })
if api.nvim_get_mode().mode == "n" then
api.nvim_feedkeys("a", "n", true)
end
vim.cmd("startinsert!")
end

--- Inserts a list item above the cursor in the current buffer.
Expand Down
12 changes: 10 additions & 2 deletions lua/markdown/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ function M.try_get_char_input()
end

---@param prompt string|nil
---@param completion (fun(a: string, c: string): string[])|nil
---@return boolean, string|nil
function M.try_get_input(prompt)
function M.try_get_input(prompt, completion)
prompt = prompt or ""
local ok, input = pcall(vim.fn.input, prompt)

local ok, input
if completion ~= nil then
ok, input = pcall(vim.fn["markdown_nvim#input"], prompt, "", completion)
else
ok, input = pcall(vim.fn.input, prompt)
end

if not ok or input == "" then
return false, nil
end
Expand Down
18 changes: 4 additions & 14 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ local function assert_buf_eq(bufnr, lines)
assert.are.same(lines, api.nvim_buf_get_lines(bufnr, 0, -1, false))
end

local function provide_input(trigger, input)
-- HACK: is there a more appropriate way to do this?
-- input consumes remaining characters from a mapping,
-- so while this feels a little weird, it works...
local map = "provideinput"
vim.keymap.set({ "n", "x" }, map, trigger .. input .. "<CR>", { remap = true })
vim.cmd("normal " .. map)
vim.keymap.del({ "n", "x" }, map)
end

describe("config", function()
vim.cmd("runtime plugin/markdown.lua")

Expand Down Expand Up @@ -167,7 +157,7 @@ describe("config", function()

set_buf(bufnr, { "test" })
api.nvim_win_set_cursor(0, { 1, 1 })
provide_input("gliw", "destination")
vim.cmd("normal gliw")
assert_buf_eq(bufnr, { "test" })

set_buf(bufnr, { "[test](#test)", "", "# test" })
Expand All @@ -183,7 +173,7 @@ describe("config", function()

set_buf(bufnr, { "test" })
api.nvim_win_set_cursor(0, { 1, 1 })
provide_input("gliw", "destination")
vim.cmd("normal gliw")
assert_buf_eq(bufnr, { "test" })

set_buf(bufnr, { "[test](#test)", "", "# test" })
Expand All @@ -206,8 +196,8 @@ describe("config", function()

set_buf(bufnr, { "test" })
api.nvim_win_set_cursor(0, { 1, 1 })
provide_input("yxiw", "destination")
assert_buf_eq(bufnr, { "[test](destination)" })
vim.cmd("normal yxiw")
assert_buf_eq(bufnr, { "[test]()" })

set_buf(bufnr, { "[test](#test)", "", "# test" })
api.nvim_win_set_cursor(0, { 1, 1 })
Expand Down
19 changes: 6 additions & 13 deletions tests/link_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ local function assert_buf_eq(bufnr, lines)
assert.are.same(lines, api.nvim_buf_get_lines(bufnr, 0, -1, false))
end

local function provide_input(trigger, input)
-- HACK: is there a more appropriate way to do this?
-- input consumes remaining characters from a mapping,
-- so while this feels a little weird, it works...
local map = "provideinput"
vim.keymap.set({ "n", "x" }, map, trigger .. input .. "<CR>", { remap = true })
vim.cmd("normal " .. map)
vim.keymap.del({ "n", "x" }, map)
end

describe("link", function()
vim.cmd("runtime plugin/markdown.lua")
require("markdown").setup()
Expand All @@ -35,16 +25,19 @@ describe("link", function()
local bufnr = new_md_buf()
set_buf(bufnr, { "test" })
api.nvim_win_set_cursor(0, { 1, 1 })
provide_input("gliw", "destination")
vim.cmd("normal gliw")
assert_buf_eq(bufnr, { "[test]()" })
vim.cmd("normal adestination")
assert_buf_eq(bufnr, { "[test](destination)" })
end)

it("can add over visual selection", function()
local bufnr = new_md_buf()
set_buf(bufnr, { "test" })
api.nvim_win_set_cursor(0, { 1, 1 })
vim.cmd("normal vl")
provide_input("gl", "destination")
vim.cmd("normal vlgl")
assert_buf_eq(bufnr, { "t[es]()t" })
vim.cmd("normal adestination")
assert_buf_eq(bufnr, { "t[es](destination)t" })
end)

Expand Down
8 changes: 2 additions & 6 deletions tests/list_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ local function set_buf(bufnr, lines)
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end

local function feed(keys)
api.nvim_feedkeys(keys, "x", true)
end

local function assert_buf_eq(bufnr, lines)
assert.are.same(lines, api.nvim_buf_get_lines(bufnr, 0, -1, false))
end
Expand All @@ -27,13 +23,13 @@ describe("list", function()
local function insert_li_above(pos, text)
api.nvim_win_set_cursor(0, pos)
list.insert_list_item_above()
feed(text)
vim.cmd("normal a" .. text)
end

local function insert_li_below(pos, text)
api.nvim_win_set_cursor(0, pos)
list.insert_list_item_below()
feed(text)
vim.cmd("normal a" .. text)
end

it("resets list numbering in buffer", function()
Expand Down

0 comments on commit 44ef530

Please sign in to comment.