Skip to content

Commit

Permalink
Add cmdbuf.get_context()
Browse files Browse the repository at this point in the history
Fix #11
  • Loading branch information
notomo committed Aug 25, 2024
1 parent 04076b5 commit 9fe2dbb
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 24 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ vim.api.nvim_create_autocmd({ "User" }, {
return require("cmdbuf").cmdline_expr()
end, { buffer = true, expr = true })

-- you can filter buffer lines
local lines = vim
.iter(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false))
:filter(function(line)
return line ~= "q"
end)
:totable()
vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines)
local typ = require("cmdbuf").get_context().type
if typ == "vim/cmd" then
-- you can filter buffer lines
local lines = vim
.iter(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false))
:filter(function(line)
return line ~= "q"
end)
:totable()
vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines)
end
end,
})

Expand Down
36 changes: 28 additions & 8 deletions doc/cmdbuf.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ execute({opts}) *cmdbuf.execute()*
Parameters: ~
{opts} (CmdbufExecuteOption?) |CmdbufExecuteOption|

get_context({opts}) *cmdbuf.get_context()*
Returns cmdbuf buffer context.

Parameters: ~
{opts} (CmdbufGetContextOption?) |CmdbufGetContextOption|

Return: ~
(CmdbufContext) |CmdbufContext|

cmdline_expr() *cmdbuf.cmdline_expr()*
Returns keymap expression to start command-line mode.
This can be used like |cmdline-window|'s CTRL-C.
Expand Down Expand Up @@ -75,6 +84,14 @@ CmdbufExecuteOption *CmdbufExecuteOption*

- {quit} (boolean?) whether quit the window after execution.

CmdbufGetContextOption *CmdbufGetContextOption*

- {bufnr} (integer?) default: current buffer number

CmdbufContext *CmdbufContext*

- {type} (CmdbufHandlerType) |CmdbufHandlerType|

==============================================================================
AUTOCOMMANDS *cmdbuf.nvim-AUTOCOMMANDS*

Expand Down Expand Up @@ -105,14 +122,17 @@ EXAMPLES *cmdbuf.nvim-EXAMPLES*
return require("cmdbuf").cmdline_expr()
end, { buffer = true, expr = true })

-- you can filter buffer lines
local lines = vim
.iter(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false))
:filter(function(line)
return line ~= "q"
end)
:totable()
vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines)
local typ = require("cmdbuf").get_context().type
if typ == "vim/cmd" then
-- you can filter buffer lines
local lines = vim
.iter(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false))
:filter(function(line)
return line ~= "q"
end)
:totable()
vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines)
end
end,
})

Expand Down
11 changes: 11 additions & 0 deletions lua/cmdbuf/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ function M.execute(opts)
Window.current():execute(opts.quit)
end

function M.get_context(raw_opts)
local opts = require("cmdbuf.option").new_get_context_opts(raw_opts)
local typ = vim.api.nvim_buf_get_name(opts.bufnr):match("cmdbuf://(.+)-buffer")
if not typ then
require("cmdbuf.vendor.misclib.message").error(("The buffer(%d) is not cmdbuf buffer"):format(opts.bufnr))
end
return {
type = typ,
}
end

function M.cmdline_expr()
return Window.current():cmdline_expr()
end
Expand Down
13 changes: 13 additions & 0 deletions lua/cmdbuf/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ function M.execute(opts)
require("cmdbuf.command").execute(opts)
end

--- @class CmdbufGetContextOption
--- @field bufnr integer? default: current buffer number

--- @class CmdbufContext
--- @field type CmdbufHandlerType |CmdbufHandlerType|

--- Returns cmdbuf buffer context.
--- @param opts CmdbufGetContextOption?: |CmdbufGetContextOption|
--- @return CmdbufContext # |CmdbufContext|
function M.get_context(opts)
return require("cmdbuf.command").get_context(opts)
end

--- Returns keymap expression to start command-line mode.
--- This can be used like |cmdline-window|'s CTRL-C.
--- @return string
Expand Down
9 changes: 9 additions & 0 deletions lua/cmdbuf/option.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ function M.new_execute_opts(raw_opts)
return vim.tbl_deep_extend("force", M.default_execute_opts, raw_opts)
end

M.default_get_context_opts = {
bufnr = 0,
}
function M.new_get_context_opts(raw_opts)
vim.validate({ raw_opts = { raw_opts, "table", true } })
raw_opts = raw_opts or {}
return vim.tbl_deep_extend("force", M.default_get_context_opts, raw_opts)
end

return M
19 changes: 11 additions & 8 deletions spec/lua/cmdbuf/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ vim.api.nvim_create_autocmd({ "User" }, {
return require("cmdbuf").cmdline_expr()
end, { buffer = true, expr = true })

-- you can filter buffer lines
local lines = vim
.iter(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false))
:filter(function(line)
return line ~= "q"
end)
:totable()
vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines)
local typ = require("cmdbuf").get_context().type
if typ == "vim/cmd" then
-- you can filter buffer lines
local lines = vim
.iter(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false))
:filter(function(line)
return line ~= "q"
end)
:totable()
vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines)
end
end,
})

Expand Down
22 changes: 22 additions & 0 deletions spec/lua/cmdbuf/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,26 @@ history2]])
assert.window_id(window_id)
assert.exists_pattern("history1")
end)

it("can get current buffer's context", function()
cmdbuf.open()

local got = cmdbuf.get_context()

assert.is_same(got, {
type = "vim/cmd",
})
end)

it("can get specified buffer's context", function()
cmdbuf.open()
local bufnr = vim.api.nvim_get_current_buf()
vim.cmd.tabedit()

local got = cmdbuf.get_context({ bufnr = bufnr })

assert.is_same(got, {
type = "vim/cmd",
})
end)
end)

0 comments on commit 9fe2dbb

Please sign in to comment.