Skip to content

Commit

Permalink
Fix lua handler with line option
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Jul 29, 2024
1 parent 16365ad commit 8218d36
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lua/cmdbuf/core/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function Buffer.load(self, line)
vim.validate({ line = { line, "string", true } })

local lines = self._handler:histories()
table.insert(lines, line or "")
table.insert(lines, self._handler:parse(line or ""))
vim.api.nvim_buf_set_lines(self._bufnr, 0, -1, false, lines)

vim.bo[self._bufnr].filetype = self._handler.filetype
Expand Down
34 changes: 21 additions & 13 deletions lua/cmdbuf/handler/lua/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ function M._lua(cmd)
return "lua " .. cmd
end

function M.histories()
return historylib.filter_map("cmd", function(cmd)
do
local s, e = cmd:find("^%s*lua%s*=?")
if s then
return cmd:sub(e + 1)
end
local parse = function(line)
do
local s, e = line:find("^%s*lua%s*=?")
if s then
return line:sub(e + 1)
end
do
local s, e = cmd:find("^%s*=")
if s then
return cmd:sub(e + 1)
end
end
do
local s, e = line:find("^%s*=")
if s then
return line:sub(e + 1)
end
return nil
end
return nil
end

function M.parse(_, line)
return parse(line) or line
end

function M.histories()
return historylib.filter_map("cmd", function(cmd)
return parse(cmd)
end)
end

Expand Down
28 changes: 18 additions & 10 deletions lua/cmdbuf/handler/lua/variable/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@ function M._lua(cmd)
return "lua vim.b." .. cmd
end

local parse = function(cmd)
local s, e = cmd:find("^%s*lua%s+vim%.b%.")
if not s then
return nil
end
local line = cmd:sub(e + 1)
local splitted = vim.split(line, "%s*=%s*", false)
if #splitted < 2 then
return nil
end
return line
end

function M.parse(_, line)
return parse(line) or line
end

function M.histories(self)
if not vim.api.nvim_buf_is_valid(self._bufnr) then
return {}
end

local cmds = historylib.filter_map("cmd", function(cmd)
local s, e = cmd:find("^%s*lua%s+vim%.b%.")
if not s then
return nil
end
local line = cmd:sub(e + 1)
local splitted = vim.split(line, "%s*=%s*", false)
if #splitted < 2 then
return nil
end
return line
return parse(cmd)
end)

local prefix_length = #"b:" + 1
Expand Down
28 changes: 18 additions & 10 deletions lua/cmdbuf/handler/lua/variable/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ function M._lua(cmd)
return "lua vim.g." .. cmd
end

local parse = function(cmd)
local s, e = cmd:find("^%s*lua%s+vim%.g%.")
if not s then
return nil
end
local line = cmd:sub(e + 1)
local splitted = vim.split(line, "%s*=%s*", false)
if #splitted < 2 then
return nil
end
return line
end

function M.parse(_, line)
return parse(line) or line
end

function M.histories(_)
local cmds = historylib.filter_map("cmd", function(cmd)
local s, e = cmd:find("^%s*lua%s+vim%.g%.")
if not s then
return nil
end
local line = cmd:sub(e + 1)
local splitted = vim.split(line, "%s*=%s*", false)
if #splitted < 2 then
return nil
end
return line
return parse(cmd)
end)

local prefix_length = #"g:" + 1
Expand Down
4 changes: 4 additions & 0 deletions lua/cmdbuf/handler/vim/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function M.new()
return setmetatable({}, M)
end

function M.parse(_, line)
return line
end

function M.histories()
return historylib.filter_map("cmd", function(cmd)
if cmd == "" then
Expand Down
5 changes: 5 additions & 0 deletions lua/cmdbuf/handler/vim/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function M.histories()
return lines
end

function M.parse(_, line)
-- not supported
return line
end

function M.add_history(_, _) end

local extract_key_value = function(line)
Expand Down
4 changes: 4 additions & 0 deletions lua/cmdbuf/handler/vim/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function M.new()
return setmetatable({}, M)
end

function M.parse(_, line)
return line
end

function M.histories()
return historylib.filter_map("input", function(input)
if input == "" then
Expand Down
4 changes: 4 additions & 0 deletions lua/cmdbuf/handler/vim/search/forward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ end
M.flags = ""
M.searchforward = 1

function M.parse(_, line)
return line
end

function M.histories()
return historylib.filter_map("search", function(cmd)
if cmd == "" then
Expand Down
9 changes: 9 additions & 0 deletions spec/lua/cmdbuf/handler/lua/cmd_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@ describe("lua/cmd handler", function()

assert.equals(18888, vim.b.cmdbuf_test)
end)

it("can specific lua prefixed line", function()
cmdbuf.open({
type = typ,
line = "lua print(8888)",
})

assert.current_line("print(8888)")
end)
end)
9 changes: 9 additions & 0 deletions spec/lua/cmdbuf/handler/lua/variable/buffer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ describe("lua/variable/buffer handler", function()

assert.equals(18888, vim.b.cmdbuf_test)
end)

it("can specific lua prefixed line", function()
cmdbuf.open({
type = typ,
line = "lua vim.b.test=1",
})

assert.current_line("test=1")
end)
end)
9 changes: 9 additions & 0 deletions spec/lua/cmdbuf/handler/lua/variable/global_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ describe("lua/variable/global handler", function()

assert.equals(18888, vim.g.cmdbuf_global_test)
end)

it("can specific lua prefixed line", function()
cmdbuf.open({
type = typ,
line = "lua vim.g.test=1",
})

assert.current_line("test=1")
end)
end)

0 comments on commit 8218d36

Please sign in to comment.