Skip to content

Commit

Permalink
feat(csvview): add event hooks for attach/detach
Browse files Browse the repository at this point in the history
Added new events `CsvViewAttach` and `CsvViewDetach` to allow users
to hook into the attach and detach lifecycle of the CsvView. Updated
README with examples on how to use these events.
  • Loading branch information
hat0uma committed Jan 29, 2025
1 parent b5e57ee commit 092fada
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ To toggle CSV view with a custom field delimiter, a custom string delimiter and
- `require('csvview').toggle()`: Toggle CSV view.
- `require('csvview').is_enabled()`: Check if CSV view is enabled.

## Events

This plugin provides the following events:

- `CsvViewAttach`: Triggered after the initial metrics calculation is completed and the CsvView is attached.
- `CsvViewDetach`: Triggered after the CsvView is detached.

### Example

You can hook into these events as follows:

```lua
local group = vim.api.nvim_create_augroup("CsvViewEvents", {})
vim.api.nvim_create_autocmd("User", {
pattern = "CsvViewAttach",
group = group,
callback = function()
print("CsvView is attached")
end,
})
```

## Highlights

| Group | Default | Description |
Expand Down
4 changes: 3 additions & 1 deletion lua/csvview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ function M.enable(bufnr, opts)

local detach_bufevent_handle --- @type fun()
local metrics = CsvViewMetrics:new(bufnr, opts)
local view = CsvView:new(bufnr, metrics, opts, function()
local view = CsvView:new(bufnr, metrics, opts, function() -- on detach
detach_bufevent_handle()
metrics:clear()
vim.api.nvim_exec_autocmds("User", { pattern = "CsvViewDetach" })
end)

-- Register buffer-update events.
Expand All @@ -54,6 +55,7 @@ function M.enable(bufnr, opts)
-- Calculate metrics and attach view.
metrics:compute_buffer(function()
attach_view(bufnr, view)
vim.api.nvim_exec_autocmds("User", { pattern = "CsvViewAttach" })
end)
end

Expand Down

0 comments on commit 092fada

Please sign in to comment.