From 43a9193d155312bdd22c2ad6d6b508dd28ae4733 Mon Sep 17 00:00:00 2001 From: Satanx016 Date: Mon, 16 Dec 2024 21:43:23 +0100 Subject: [PATCH 1/4] feat(chunk): make textobject's keymap local to non-excluded ft's buffers && add a description to this keymap --- lua/hlchunk/mods/chunk/chunk_conf.lua | 6 +++--- lua/hlchunk/mods/chunk/init.lua | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lua/hlchunk/mods/chunk/chunk_conf.lua b/lua/hlchunk/mods/chunk/chunk_conf.lua index 0a6ce5d..7fbdb7b 100644 --- a/lua/hlchunk/mods/chunk/chunk_conf.lua +++ b/lua/hlchunk/mods/chunk/chunk_conf.lua @@ -4,14 +4,14 @@ local BaseConf = require("hlchunk.mods.base_mod.base_conf") ---@class HlChunk.UserChunkConf : HlChunk.UserBaseConf ---@field use_treesitter? boolean ---@field chars? table ----@field textobject? string +---@field textobject? { keymap: string, desc: string } ---@field max_file_size? number ---@field error_sign? boolean ---@class HlChunk.ChunkConf : HlChunk.BaseConf ---@field use_treesitter boolean ---@field chars table ----@field textobject string +---@field textobject { keymap: string, desc: string } ---@field max_file_size number ---@field error_sign boolean ---@field duration number @@ -33,7 +33,7 @@ local ChunkConf = class(BaseConf, function(self, conf) left_bottom = "╰", right_arrow = ">", }, - textobject = "", + textobject = {}, max_file_size = 1024 * 1024, error_sign = true, duration = 200, diff --git a/lua/hlchunk/mods/chunk/init.lua b/lua/hlchunk/mods/chunk/init.lua index 4070d84..98c9d81 100644 --- a/lua/hlchunk/mods/chunk/init.lua +++ b/lua/hlchunk/mods/chunk/init.lua @@ -51,10 +51,8 @@ end ---@overload fun(conf?: HlChunk.UserChunkConf, meta?: HlChunk.MetaInfo): HlChunk.ChunkMod local ChunkMod = class(BaseMod, constructor) --- chunk_mod can use text object, so add a new function extra to handle it function ChunkMod:enable() BaseMod.enable(self) - self:extra() self:render(Scope(0, 0, -1)) end @@ -239,14 +237,28 @@ function ChunkMod:createAutocmd() end end, }) + api.nvim_create_autocmd("Filetype", { + group = self.meta.augroup_name, + callback = function() + -- chunk_mod can use text object, so add a new function extra to handle it + local ft = vim.bo[0].filetype + if not self.conf.exclude_filetypes[ft] then + self:extra() + end + end, + }) end function ChunkMod:extra() local textobject = self.conf.textobject - if #textobject == 0 then + local keymap = textobject.keymap + local desc = textobject.desc + + if not keymap or not desc then return end - vim.keymap.set({ "x", "o" }, textobject, function() + + vim.keymap.set({ "x", "o" }, keymap, function() local pos = api.nvim_win_get_cursor(0) local retcode, cur_chunk_range = chunkHelper.get_chunk_range({ pos = { bufnr = 0, row = pos[1] - 1, col = pos[2] }, @@ -266,7 +278,7 @@ function ChunkMod:extra() api.nvim_win_set_cursor(0, { s_row, 0 }) vim.cmd("normal! V") api.nvim_win_set_cursor(0, { e_row, 0 }) - end) + end, { desc = desc, buffer = true }) end return ChunkMod From 9705e701d2d62fecce12e10c06d2a08bc11f0744 Mon Sep 17 00:00:00 2001 From: Satanx016 Date: Mon, 16 Dec 2024 22:03:52 +0100 Subject: [PATCH 2/4] test: make test files compliant to the changes --- test/features/class_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/features/class_spec.lua b/test/features/class_spec.lua index 80a6c2e..b8dd607 100644 --- a/test/features/class_spec.lua +++ b/test/features/class_spec.lua @@ -57,7 +57,7 @@ describe("ChunkConf class", function() left_bottom = "┗", right_arrow = "━", } - user_textobject = "ic" + user_textobject = { keymap = "ic", desc = "scope" } user_max_file_size = 10 * 1024 * 1024 user_error_sign = true user_duration = 300 From 26334b13ec855f4e9ff6d078bdc0e4fc6ea33d05 Mon Sep 17 00:00:00 2001 From: Satanx016 Date: Mon, 16 Dec 2024 22:04:46 +0100 Subject: [PATCH 3/4] docs(en): update EN docs --- docs/en/chunk.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/en/chunk.md b/docs/en/chunk.md index 2e8f12f..56c87e8 100644 --- a/docs/en/chunk.md +++ b/docs/en/chunk.md @@ -23,7 +23,7 @@ local default_conf = { left_bottom = "╰", right_arrow = ">", }, - textobject = "", + textobject = {}, max_file_size = 1024 * 1024, error_sign = true, -- animation related @@ -45,7 +45,14 @@ The unique configuration options are `use_treesitter`, `chars`, `textobject`, `m - left_bottom - right_arrow -- `textobject` is a string, which is empty by default. It is used to specify which string to use to represent the textobject. For example, I use `ic`, which stands for `inner chunk`, and you can modify it to other convenient characters. +- `textobject` is a table, which is empty by default. This table contains two keys, `keymap` which triggers the textobject, and `desc` useful if you use a plugin like which-key, for instance this can be setup as follows: + + ```lua + textobject = { + keymap = "ic", + desc = "inner chunk", + }, + ``` - `max_file_size` is a number, with a default of `1MB`. When the size of the opened file exceeds this value, the mod will be automatically turned off. From 42f28adbbfc2c82af34eb226729703258f160d50 Mon Sep 17 00:00:00 2001 From: Satanx016 Date: Mon, 16 Dec 2024 22:42:25 +0100 Subject: [PATCH 4/4] fix(chunk): no need to check for textobject.desc value --- lua/hlchunk/mods/chunk/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/hlchunk/mods/chunk/init.lua b/lua/hlchunk/mods/chunk/init.lua index 98c9d81..8f40c54 100644 --- a/lua/hlchunk/mods/chunk/init.lua +++ b/lua/hlchunk/mods/chunk/init.lua @@ -254,7 +254,7 @@ function ChunkMod:extra() local keymap = textobject.keymap local desc = textobject.desc - if not keymap or not desc then + if not keymap then return end