diff --git a/README.md b/README.md index d6900e4..679a3db 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,11 @@ 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 don't need to be included. 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 + highlight_group = "ColorColumn", -- The color of the above highlight. Must be a valid vim highlight group (see :h highlight-group) } } ``` diff --git a/lua/treewalker/init.lua b/lua/treewalker/init.lua index 06cd44a..754e180 100644 --- a/lua/treewalker/init.lua +++ b/lua/treewalker/init.lua @@ -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 @@ -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 diff --git a/lua/treewalker/ops.lua b/lua/treewalker/ops.lua index 2d124ad..4f63e9d 100644 --- a/lua/treewalker/ops.lua +++ b/lua/treewalker/ops.lua @@ -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 @@ -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 diff --git a/tests/treewalker/highlight_spec.lua b/tests/treewalker/highlight_spec.lua index f5a1bf7..1566eea 100644 --- a/tests/treewalker/highlight_spec.lua +++ b/tests/treewalker/highlight_spec.lua @@ -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()