Skip to content

Commit

Permalink
Add vim/env handler
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Aug 18, 2023
1 parent f7d982e commit f9adae8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/cmdbuf.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ CmdbufOpenOption *CmdbufOpenOption*
- {type} (CmdbufHandlerType?) (default = "vim/cmd") |CmdbufHandlerType|

CmdbufHandlerType *CmdbufHandlerType*
= "vim/cmd" | "vim/search/forward" | "vim/search/backward" | "vim/input" | "lua/cmd" | "lua/variable/buffer" | "lua/variable/global"
= "vim/cmd" | "vim/search/forward" | "vim/search/backward" | "vim/input" | "vim/env" | "lua/cmd" | "lua/variable/buffer" | "lua/variable/global"

- "vim/cmd": |q:| alternative
- "vim/search/forward": |q/| alternative
- "vim/search/backward": |q?| alternative
- "vim/input": show input history
- "vim/env": environment variable
- "lua/cmd": |q:| alternative for lua command
- "lua/variable/buffer": buffer variable and command
- "lua/variable/global": global variable and command
Expand Down
45 changes: 45 additions & 0 deletions lua/cmdbuf/handler/vim/env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local M = {}
M.__index = M

function M.new()
return setmetatable({}, M)
end

function M.histories()
local names = vim.fn.getcompletion("*", "environment")
local key_values = {}
for _, name in ipairs(names) do
local key_value = ("%s=%s"):format(name, os.getenv(name):gsub("\n", "\\n"))
table.insert(key_values, key_value)
end
return key_values
end

function M.add_history(_, _) end

local extract_key_value = function(line)
local index = line:find("=")
if not index then
return line
end

local key = line:sub(1, index - 1)
local value = line:sub(index + 1)
return key, value
end

function M.delete_histories(_, lines)
for _, line in ipairs(lines) do
local key, _ = extract_key_value(line)
vim.env[key] = nil
end
end

function M.execute(_, line)
local key, value = extract_key_value(line)
vim.env[key] = value
end

M.filetype = ""

return M
1 change: 1 addition & 0 deletions lua/cmdbuf/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local M = {}
--- | '"vim/search/forward"' # |q/| alternative
--- | '"vim/search/backward"' # |q?| alternative
--- | '"vim/input"' # show input history
--- | '"vim/env"' # environment variable
--- | '"lua/cmd"' # |q:| alternative for lua command
--- | '"lua/variable/buffer"' # buffer variable and command
--- | '"lua/variable/global"' # global variable and command
Expand Down
42 changes: 42 additions & 0 deletions spec/lua/cmdbuf/handler/vim/env_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local helper = require("cmdbuf.test.helper")
local cmdbuf = helper.require("cmdbuf")

describe("vim/env handler", function()
before_each(function()
vim.env.CMDBUF_TEST = nil
helper.before_each()
end)
after_each(helper.after_each)

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

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

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" })
helper.set_lines([[CMDBUF_TEST=value]])

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

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" })
helper.search("delete_target")
cmdbuf.delete()
assert.no.exists_pattern("CMDBUF_TEST=delete_target")

vim.cmd.edit({ bang = true })
assert.no.exists_pattern("CMDBUF_TEST=delete_target")
end)
end)

0 comments on commit f9adae8

Please sign in to comment.