Skip to content

Commit

Permalink
Clean up new highlight_group config option code
Browse files Browse the repository at this point in the history
Makes some overall cleanup for the highlight_group work:

* Adds highlight_group to the Opts type
* Adds tests ensuring it works and the default is respected
* Adds more descriptive commenting to readme
  • Loading branch information
aaronik committed Dec 25, 2024
1 parent 1bad89f commit 32411d6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ The swap commands intelligently swap nodes, including comments and attributes/de
```lua
{
"aaronik/treewalker.nvim",

-- The following options are the defaults.
-- Treewalker aims for sane defaults, so these are each individually optional,
-- and the whole opts block is optional as well.
opts = {
highlight = true, -- Whether to briefly highlight the node after jumping to it
highlight_duration = 250, -- How long should above highlight last (in ms)
highlight_group = "ColorColumn", -- Highlight group
-- Whether to briefly highlight the node after jumping to it
highlight = true,

-- How long should above highlight last (in ms)
highlight_duration = 250,

-- The color of the above highlight. Must be a valid vim highlight group.
-- (see :h highlight-group)
highlight_group = "ColorColumn",
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions lua/treewalker/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
local util = require('treewalker.util')
local movement = require('treewalker.movement')
local swap = require('treewalker.swap')

local Treewalker = {}

---@alias Opts { highlight: boolean, highlight_duration: integer }
---@alias Opts { highlight: boolean, highlight_duration: integer, highlight_group: string }

-- Default setup() options
---@type Opts
Expand All @@ -14,6 +13,7 @@ Treewalker.opts = {
highlight_group = "ColorColumn",
}

-- This does not need to be called for Treewalker to work. The defaults are preinitialized and aim to be sane.
---@param opts Opts | nil
function Treewalker.setup(opts)
if opts then
Expand Down
7 changes: 4 additions & 3 deletions lua/treewalker/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ local M = {}
---Flash a highlight over the given range
---@param range Range4
---@param duration integer
function M.highlight(range, duration)
---@param hl_group string
function M.highlight(range, duration, hl_group)
local start_row, start_col, end_row, end_col = range[1], range[2], range[3], range[4]
local ns_id = vim.api.nvim_create_namespace("")
local hl_group = require("treewalker").opts.highlight_group

for row = start_row, end_row do
if row == start_row and row == end_row then
Expand Down Expand Up @@ -46,7 +46,8 @@ function M.jump(row, node)
if require("treewalker").opts.highlight then
local range = nodes.range(node)
local duration = require("treewalker").opts.highlight_duration
M.highlight(range, duration)
local hl_group = require("treewalker").opts.highlight_group
M.highlight(range, duration, hl_group)
end
end

Expand Down
14 changes: 14 additions & 0 deletions tests/treewalker/highlight_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ describe("Highlights in a regular lua file: ", function()
assert.equal(duration, duration_arg)
end)

it("respects default highlight_group", function()
tw.setup({ highlight = true, highlight_duration = 250 })
tw.move_down()
local hl_group_arg = highlight_stub.calls[1].refs[3]
assert.equal("ColorColumn", hl_group_arg)
end)

it("respects highlight_group config option", function()
tw.setup({ highlight = true, highlight_duration = 50, highlight_group = "DiffAdd" })
tw.move_down()
local hl_group_arg = highlight_stub.calls[1].refs[3]
assert.equal("DiffAdd", hl_group_arg)
end)

it("highlights whole functions", function()
vim.fn.cursor(10, 1)
tw.move_down()
Expand Down

0 comments on commit 32411d6

Please sign in to comment.