diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-24 21:46:04 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-24 21:46:04 +0200 |
| commit | 7bcf47b6349f7077b4b6605952a5f48388f18abe (patch) | |
| tree | a6d59a473e82da07afd756458928becf159af23d /plugin | |
| parent | d5887f31a416034300a8675d8883d615b31ced83 (diff) | |
| download | nvim-config-7bcf47b6349f7077b4b6605952a5f48388f18abe.tar.gz nvim-config-7bcf47b6349f7077b4b6605952a5f48388f18abe.zip | |
refactor(nvim): rewrite extmark plugin
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/50-extmarks.lua | 96 |
1 files changed, 12 insertions, 84 deletions
diff --git a/plugin/50-extmarks.lua b/plugin/50-extmarks.lua index 593321d..2cb7862 100644 --- a/plugin/50-extmarks.lua +++ b/plugin/50-extmarks.lua @@ -1,41 +1,16 @@ -- -- 50-extmarks.lua -- --- * Creates a global highlight namespace for extended marks. --- * Keeps track of the current global highlight namespace in `vim.g.hl_ns`. --- * Creates an autocommand that marks matches in each buffer on 'FileType' and keeps them in sync --- as the buffer changes. Marks follow the theme on their own: the `dotfiles.Todo` group links to --- `Todo`, so a colorscheme / 'background' change re-colors them with no extra wiring. --- For now, only "TODO" strings are marked but the system can be easily extended by adding items --- in `marks`. --- Doesn't support multi-line patterns. --- --- Global variables: --- `vim.g.hl_ns`: global highlight namespace +-- * Marks "TODO" strings through the dotfiles.extmark engine. +-- * Only "TODO" in comments is marked (in markdown, "TODO" outside code), classified from the +-- treesitter tree; with no parser for the filetype, every "TODO" is marked. +-- * Multi-line patterns are not supported. -- -- User commands: --- `ExtmarkToggle`: toggle extended marks highlight namespace +-- ExtmarkToggle: toggle the extended-marks highlight namespace -- -vim.g.hl_ns = 0 - -local extmarks_hl_ns = vim.api.nvim_create_namespace("dotfiles_extmarks") - -local hl_group_todo = "dotfiles.Todo" -vim.api.nvim_set_hl(extmarks_hl_ns, hl_group_todo, { link = "Todo" }) - --- Wrapper around `nvim_set_hl_ns()`; sets the global highlight namespace to `hl_ns` and save it to --- `vim.g.hl_ns`. Useful because the global highlight namespace is not saved anywhere by default. -local function set_global_hl_ns(hl_ns) - vim.g.hl_ns = hl_ns - vim.api.nvim_set_hl_ns(hl_ns) -end - --- Toggles extended marks highlight namespace -local function toggle_marks() - local new_hl_ns = vim.g.hl_ns == 0 and extmarks_hl_ns or 0 - set_global_hl_ns(new_hl_ns) -end +local extmark = require("dotfiles.extmark") -- Parses the treesitter tree synchronously, including injections, in the 0-based, end-exclusive -- line range `first`:`last` of buffer `buf`. @@ -106,59 +81,12 @@ local function find_todo(buf, first, last) end end --- List of extended marks to apply with `mark_all()` --- --- Each item is a table with two fields: `hl_group`, the name of the highlight group to use --- for the marks, and `find`, which must return an iterator over the positions to mark (see --- `find_todo()` for the format). -local marks = { - { hl_group = hl_group_todo, find = find_todo }, -} - --- Updates extended marks in buffer `buf` in range `first`:`last`. --- For each item in `marks`, sets extended marks on positions returned by `item.find` to the --- highlight group `item.hl_group`. -local function mark_all(buf, first, last) - vim.api.nvim_buf_clear_namespace(buf, extmarks_hl_ns, first, last) - for _, marks_opts in ipairs(marks) do - for p in marks_opts.find(buf, first, last) do - vim.api.nvim_buf_set_extmark( - buf, - extmarks_hl_ns, - p.y, - p.x1, - { end_col = p.x2, hl_group = marks_opts.hl_group } - ) - end - end -end - --- Create extended marks in buffer `ev.buf` and updates them when the buffer changes. -local function watch_extmarks(ev) - mark_all(ev.buf, 0, -1) - if vim.b[ev.buf].dotfiles_todo_attached then - return - end - vim.b[ev.buf].dotfiles_todo_attached = true - vim.api.nvim_buf_attach(ev.buf, false, { - on_lines = function(_, b, _, first, _, last_new) - vim.schedule(function() - if vim.api.nvim_buf_is_valid(b) then - mark_all(b, first, last_new) - end - end) - end, - }) -end +extmark.register("dotfiles.Todo", { link = "Todo" }, find_todo) ---------------------------------------------------------------------------------------------------- -vim.api.nvim_create_autocmd("FileType", { - desc = "Set extended marks watcher", - group = vim.g.dotfiles.augroup, - callback = watch_extmarks, -}) - -vim.api.nvim_create_user_command("ExtmarkToggle", toggle_marks, { desc = "Toggle extended marks" }) - -set_global_hl_ns(extmarks_hl_ns) +vim.api.nvim_create_user_command( + "ExtmarkToggle", + extmark.toggle, + { desc = "Toggle extended marks" } +) |
