Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/en/chunk.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local default_conf = {
left_bottom = "╰",
right_arrow = ">",
},
textobject = "",
textobject = {},
max_file_size = 1024 * 1024,
error_sign = true,
-- animation related
Expand All @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions lua/hlchunk/mods/chunk/chunk_conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
---@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<string, string>
---@field textobject string
---@field textobject { keymap: string, desc: string }
---@field max_file_size number
---@field error_sign boolean
---@field duration number
Expand All @@ -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,
Expand Down
22 changes: 17 additions & 5 deletions lua/hlchunk/mods/chunk/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 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] },
Expand All @@ -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
2 changes: 1 addition & 1 deletion test/features/class_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down