From 9fe2dbb6a6000da7dc6760483f32d59fdb5149e9 Mon Sep 17 00:00:00 2001 From: notomo Date: Sun, 25 Aug 2024 16:58:35 +0900 Subject: [PATCH] Add cmdbuf.get_context() Fix #11 --- README.md | 19 ++++++++++-------- doc/cmdbuf.nvim.txt | 36 +++++++++++++++++++++++++++-------- lua/cmdbuf/command.lua | 11 +++++++++++ lua/cmdbuf/init.lua | 13 +++++++++++++ lua/cmdbuf/option.lua | 9 +++++++++ spec/lua/cmdbuf/example.lua | 19 ++++++++++-------- spec/lua/cmdbuf/init_spec.lua | 22 +++++++++++++++++++++ 7 files changed, 105 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index fe7ceb5..2da2551 100644 --- a/README.md +++ b/README.md @@ -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, }) diff --git a/doc/cmdbuf.nvim.txt b/doc/cmdbuf.nvim.txt index 25bf801..b7eb44d 100644 --- a/doc/cmdbuf.nvim.txt +++ b/doc/cmdbuf.nvim.txt @@ -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. @@ -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* @@ -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, }) diff --git a/lua/cmdbuf/command.lua b/lua/cmdbuf/command.lua index d9e55c6..1b3b78d 100644 --- a/lua/cmdbuf/command.lua +++ b/lua/cmdbuf/command.lua @@ -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 diff --git a/lua/cmdbuf/init.lua b/lua/cmdbuf/init.lua index 0af1d1e..f9d768d 100644 --- a/lua/cmdbuf/init.lua +++ b/lua/cmdbuf/init.lua @@ -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 diff --git a/lua/cmdbuf/option.lua b/lua/cmdbuf/option.lua index 4e0e397..3fd244f 100644 --- a/lua/cmdbuf/option.lua +++ b/lua/cmdbuf/option.lua @@ -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 diff --git a/spec/lua/cmdbuf/example.lua b/spec/lua/cmdbuf/example.lua index 30f3050..5883dd7 100644 --- a/spec/lua/cmdbuf/example.lua +++ b/spec/lua/cmdbuf/example.lua @@ -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, }) diff --git a/spec/lua/cmdbuf/init_spec.lua b/spec/lua/cmdbuf/init_spec.lua index 978cbaa..f637931 100644 --- a/spec/lua/cmdbuf/init_spec.lua +++ b/spec/lua/cmdbuf/init_spec.lua @@ -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)