Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Jul 28, 2024
1 parent 1132237 commit 16365ad
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 55 deletions.
5 changes: 4 additions & 1 deletion lua/cmdbuf/core/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ end

function Buffer.create(handler, name, line)
local bufnr = vim.api.nvim_create_buf(false, true)
local tbl = { _bufnr = bufnr, _handler = handler }
local tbl = {
_bufnr = bufnr,
_handler = handler,
}
local self = setmetatable(tbl, Buffer)
_buffers[bufnr] = self

Expand Down
28 changes: 19 additions & 9 deletions lua/cmdbuf/lib/history.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@ function M.delete(name, str, prefix)
return result == 1
end

local range = function(s, e, step)
local i = s
return function()
if i > e then
return nil
end
local next = i
i = next + step
return next
end
end

function M.filter_map(name, f)
vim.validate({ name = { name, "string" }, f = { f, "function" } })
local count = vim.fn.histnr(name)
local cmds = {}
for i = 1, count, 1 do
local history = vim.fn.histget(name, i)
local cmd = f(history)
if cmd then
table.insert(cmds, cmd)
end
end
return cmds
return vim
.iter(range(1, count, 1))
:map(function(i)
local history = vim.fn.histget(name, i)
return f(history)
end)
:totable()
end

return M
24 changes: 13 additions & 11 deletions spec/lua/cmdbuf/handler/lua/cmd_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ describe("lua/cmd handler", function()
before_each(helper.before_each)
after_each(helper.after_each)

local typ = "lua/cmd"

it("can open a lua command buffer", function()
cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })

assert.buffer_full_name("cmdbuf://lua/cmd-buffer")
assert.filetype("lua")
end)

it("can execute current line as lua", function()
cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.set_lines([[vim.cmd.tabedit()]])

cmdbuf.execute({ quit = true })
Expand All @@ -22,17 +24,17 @@ describe("lua/cmd handler", function()
end)

it("adds history on execution", function()
cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.set_lines([[vim.cmd.tabedit({range = {0}})]])

cmdbuf.execute({ quit = true })
cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })

assert.exists_pattern([[^vim.cmd.tabedit({range = {0}})$]])
end)

it("shows a raw command error", function()
cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.set_lines([[invalid_lua_test_cmd]])

cmdbuf.execute({ quit = true })
Expand All @@ -43,7 +45,7 @@ describe("lua/cmd handler", function()
it("can delete a lua command from history", function()
vim.fn.histadd("cmd", [[lua vim.cmd.target_lua_cmd()]])

cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.search("target_lua_cmd")
cmdbuf.delete()
assert.no.exists_pattern("target_lua_cmd")
Expand All @@ -55,7 +57,7 @@ describe("lua/cmd handler", function()
it("can delete a lua= command from history", function()
vim.fn.histadd("cmd", [[lua=vim.cmd.target_lua_cmd()]])

cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.search("target_lua_cmd")
cmdbuf.delete()
assert.no.exists_pattern("target_lua_cmd")
Expand All @@ -67,7 +69,7 @@ describe("lua/cmd handler", function()
it("can delete a = command from history", function()
vim.fn.histadd("cmd", [[=vim.cmd.target_lua_cmd()]])

cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.search("target_lua_cmd")
cmdbuf.delete()
assert.no.exists_pattern("target_lua_cmd")
Expand All @@ -79,7 +81,7 @@ describe("lua/cmd handler", function()
it("lists including lua= command", function()
vim.fn.histadd("cmd", [[lua="equal_test"]])

cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.search("equal_test")

assert.exists_pattern([["equal_test"]])
Expand All @@ -88,7 +90,7 @@ describe("lua/cmd handler", function()
it("lists including = command", function()
vim.fn.histadd("cmd", [[="equal_test"]])

cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.search("equal_test")

assert.exists_pattern([["equal_test"]])
Expand All @@ -97,7 +99,7 @@ describe("lua/cmd handler", function()
it("can enter cmdline", function()
vim.fn.histadd("cmd", [[lua vim.b.cmdbuf_test = 8888]])

cmdbuf.open({ type = "lua/cmd" })
cmdbuf.open({ type = typ })
helper.search("8888")

helper.execute_as_expr_keymap(cmdbuf.cmdline_expr() .. "1<CR>")
Expand Down
18 changes: 10 additions & 8 deletions spec/lua/cmdbuf/handler/vim/cmd_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ describe("vim/cmd handler", function()
before_each(helper.before_each)
after_each(helper.after_each)

local typ = "vim/cmd"

it("can open a buffer", function()
local origin = vim.api.nvim_get_current_buf()

cmdbuf.open()
cmdbuf.open({ type = typ })

assert.buffer_full_name("cmdbuf://vim/cmd-buffer")
assert.filetype("vim")
assert.no.buffer_number(origin)
end)

it("can execute current line command", function()
cmdbuf.open()
cmdbuf.open({ type = typ })
helper.set_lines([[tabedit]])

cmdbuf.execute({ quit = true })
Expand All @@ -25,17 +27,17 @@ describe("vim/cmd handler", function()
end)

it("adds history on execution", function()
cmdbuf.open()
cmdbuf.open({ type = typ })
helper.set_lines([[1tabedit]])

cmdbuf.execute({ quit = true })
cmdbuf.open()
cmdbuf.open({ type = typ })

assert.exists_pattern([[^1tabedit$]])
end)

it("shows a raw `Vim:` error", function()
cmdbuf.open()
cmdbuf.open({ type = typ })
helper.set_lines([[invalid_test_cmd]])

cmdbuf.execute({ quit = true })
Expand All @@ -46,7 +48,7 @@ describe("vim/cmd handler", function()
it("shows a raw `Vim(cmdname):` error", function()
vim.bo.modifiable = false

cmdbuf.split_open()
cmdbuf.split_open(vim.o.cmdwinheight, { type = typ })
helper.set_lines([[append]])

cmdbuf.execute({ quit = true })
Expand All @@ -57,7 +59,7 @@ describe("vim/cmd handler", function()
it("can delete a command from history", function()
vim.fn.histadd("cmd", "delete_target_cmd")

cmdbuf.open()
cmdbuf.open({ type = typ })
helper.search("delete_target_cmd")
cmdbuf.delete()
assert.no.exists_pattern("delete_target_cmd")
Expand All @@ -69,7 +71,7 @@ describe("vim/cmd handler", function()
it("can enter cmdline", function()
vim.fn.histadd("cmd", [[let g:cmdbuf_test = 8888]])

cmdbuf.open()
cmdbuf.open({ type = typ })
helper.search("8888")

helper.execute_as_expr_keymap(cmdbuf.cmdline_expr() .. "1<CR>")
Expand Down
12 changes: 7 additions & 5 deletions spec/lua/cmdbuf/handler/vim/env_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ describe("vim/env handler", function()
end)
after_each(helper.after_each)

local typ = "vim/env"

it("can open a buffer", function()
local origin = vim.api.nvim_get_current_buf()

cmdbuf.open({ type = "vim/env" })
cmdbuf.open({ type = typ })

assert.buffer_full_name("cmdbuf://vim/env-buffer")
assert.filetype("")
assert.no.buffer_number(origin)
end)

it("sets environment variable on execution", function()
cmdbuf.open({ type = "vim/env" })
cmdbuf.open({ type = typ })
helper.set_lines([[CMDBUF_TEST=value]])

cmdbuf.execute({ quit = true })
cmdbuf.open({ type = "vim/env" })
cmdbuf.open({ type = typ })

assert.exists_pattern([[^CMDBUF_TEST=value$]])
end)

it("can unset variable by delete", function()
vim.env.CMDBUF_TEST = "delete_target"

cmdbuf.open({ type = "vim/env" })
cmdbuf.open({ type = typ })
helper.search("delete_target")
cmdbuf.delete()
assert.no.exists_pattern("CMDBUF_TEST=delete_target")
Expand All @@ -43,7 +45,7 @@ describe("vim/env handler", function()
it("can enter cmdline", function()
vim.env.CMDBUF_CMDLINE_TEST = "cmdline"

cmdbuf.open({ type = "vim/env" })
cmdbuf.open({ type = typ })

helper.execute_as_expr_keymap(cmdbuf.cmdline_expr() .. "<CR>")

Expand Down
12 changes: 7 additions & 5 deletions spec/lua/cmdbuf/handler/vim/input_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ describe("vim/input handler", function()
before_each(helper.before_each)
after_each(helper.after_each)

local typ = "vim/input"

it("can open a buffer", function()
local origin = vim.api.nvim_get_current_buf()

cmdbuf.open({ type = "vim/input" })
cmdbuf.open({ type = typ })

assert.buffer_full_name("cmdbuf://vim/input-buffer")
assert.filetype("")
assert.no.buffer_number(origin)
end)

it("adds history on execution", function()
cmdbuf.open({ type = "vim/input" })
cmdbuf.open({ type = typ })
helper.set_lines([[test_input]])

cmdbuf.execute({ quit = true })
cmdbuf.open({ type = "vim/input" })
cmdbuf.open({ type = typ })

assert.exists_pattern([[^test_input$]])
end)

it("can delete an input from history", function()
vim.fn.histadd("input", "delete_target_input")

cmdbuf.open({ type = "vim/input" })
cmdbuf.open({ type = typ })
helper.search("delete_target_input")
cmdbuf.delete()
assert.no.exists_pattern("delete_target_input")
Expand All @@ -40,7 +42,7 @@ describe("vim/input handler", function()
it("can enter cmdline", function()
vim.fn.histadd("input", "cmdline_test_input")

cmdbuf.open({ type = "vim/input" })
cmdbuf.open({ type = typ })

helper.execute_as_expr_keymap(cmdbuf.cmdline_expr() .. "let b:cmdbuf_input=1<CR>")

Expand Down
18 changes: 10 additions & 8 deletions spec/lua/cmdbuf/handler/vim/search/backward_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ describe("vim/search/backward handler", function()
before_each(helper.before_each)
after_each(helper.after_each)

local typ = "vim/search/backward"

it("can open a buffer", function()
cmdbuf.open({ type = "vim/search/backward" })
cmdbuf.open({ type = typ })

assert.buffer_full_name("cmdbuf://vim/search/backward-buffer")
assert.filetype("")
Expand All @@ -15,7 +17,7 @@ describe("vim/search/backward handler", function()
it("can search current line pattern", function()
helper.set_lines([[search backward target]])

cmdbuf.split_open(10, { type = "vim/search/backward" })
cmdbuf.split_open(10, { type = typ })
helper.set_lines([[target]])
cmdbuf.execute({ quit = true })

Expand All @@ -28,16 +30,16 @@ describe("vim/search/backward handler", function()
it("adds history on execution", function()
helper.set_lines([[search backward history]])

cmdbuf.split_open(10, { type = "vim/search/backward" })
cmdbuf.split_open(10, { type = typ })
helper.set_lines([[search backward history]])
cmdbuf.execute({ quit = true })
cmdbuf.split_open(10, { type = "vim/search/backward" })
cmdbuf.split_open(10, { type = typ })

assert.exists_pattern([[^search backward history$]])
end)

it("shows a raw `Pattern not found` error", function()
cmdbuf.open({ type = "vim/search/backward" })
cmdbuf.open({ type = typ })
helper.set_lines([[invalid_search_backward]])

cmdbuf.execute({ quit = true })
Expand All @@ -48,7 +50,7 @@ describe("vim/search/backward handler", function()
it("can delete a command from history", function()
vim.fn.histadd("search", "delete_search_backward")

cmdbuf.open({ type = "vim/search/backward" })
cmdbuf.open({ type = typ })
helper.search("delete_search_backward")
cmdbuf.delete()
assert.no.exists_pattern("delete_search_backward")
Expand All @@ -58,7 +60,7 @@ describe("vim/search/backward handler", function()
end)

it("does not raise error even if command is empty line", function()
cmdbuf.open({ type = "vim/search/backward" })
cmdbuf.open({ type = typ })
cmdbuf.execute()

assert.window_count(1)
Expand All @@ -71,7 +73,7 @@ search backward target]])

vim.fn.histadd("search", "cmdline")

cmdbuf.open({ type = "vim/search/backward" })
cmdbuf.open({ type = typ })
helper.search("cmdline")

helper.execute_as_expr_keymap(cmdbuf.cmdline_expr() .. "<CR>")
Expand Down
Loading

0 comments on commit 16365ad

Please sign in to comment.