Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Scope indent uses indent column number instead of indent level for choosing hl group #422

Closed
4 tasks done
tzhouhc opened this issue Jan 5, 2025 · 0 comments · Fixed by #646
Closed
4 tasks done
Labels
bug Something isn't working

Comments

@tzhouhc
Copy link

tzhouhc commented Jan 5, 2025

Did you check docs and existing issues?

  • I have read all the snacks.nvim docs
  • I have updated the plugin to the latest version before submitting this issue
  • I have searched the existing issues of snacks.nvim
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

NVIM v0.10.3

Operating system/version

MacOS 14.6.1

Describe the bug

In line 311 of indent.lua, get_hl uses scope.indent for computing the index of the scope.hl to use. However this number appears not to be the indent level but rather the indent spaces. This causes the coloring of consecutive indent levels to not be consecutive order in the scope.hl table.

By changing this line into

  local hl = get_hl(scope.indent / state.shiftwidth + 1, config.scope.hl)

I was able to get the expected behavior, where if I set both indent.hl and scope.hl to the same colors with different brightness, then the indent highlight keeps the color as expected (for languages with simple indent/scoping rules at least), however I don't know if this is quite the right place to make the change properly.

Steps To Reproduce

Create a series of color hl groups and set both indent.hl and scope.hl to them. Open a lua file and observe the scope hl color going in normal order when cursor is not over them, and going in alternating order when the cursor is; open a python file and observe (assuming shiftwidth 4) scope colors going at steps of 4.

Expected Behavior

Consecutive scope levels use consecutive hl group in the specified hl table.

Repro

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

local M = {}

-- rainbow delimiters
M.rainbow_colors = {
  "#F6ED56",
  "#A6C955",
  "#4BA690",
  "#4191C9",
  "#2258A0",
  "#654997",
  "#994D95",
  "#D45196",
  "#DB3A35",
  "#E5783A",
  "#EC943F",
  "#F7C247",
}

local function strip_hash(s)
  if s:find("^#") ~= nil then
    return s:sub(2)
  end
  return s
end

local function hex_to_table(h)
  local r = tonumber(h:sub(1, 2), 16)
  local g = tonumber(h:sub(3, 4), 16)
  local b = tonumber(h:sub(5), 16)
  -- print("r: "..r.." g: "..g.." b: "..b)
  return { r, g, b }
end

-- mix a _single_ color among r, g, and b.
local function mix_color(a, b, pa)
  -- a linear combination of a and b with weight pa for a.
  local num = math.floor(a * pa) + math.floor(b * (1 - pa))
  -- keep between 2 hex digits.
  local adjusted = math.min(math.max(num, 0), 255)
  -- convert to hex string
  local res = string.format("%x", adjusted)
  return res
end

--- Mixes two RGB color strings with an optional ratio.
---@param a string The first color.
---@param b string The second color.
---@param pa number The mixture ratio for a, should be a number between 0 and 1. The mixture
---of b is automatically (1 - a).
---@return string
local function mix_colors(a, b, pa)
  local color_a = hex_to_table(strip_hash(a))
  local color_b = hex_to_table(strip_hash(b))
  pa = pa or 0.5 -- default to even mix
  local res = "#"
  for i, _ in ipairs(color_a) do
    res = res .. mix_color(color_a[i], color_b[i], pa)
  end
  return res
end

-- default background color for nord theme
local bg = "#2E3440"

-- mix each color from `colors` with `bg`. `bright` determines how much original
-- color there is compared to the darker `bg`.
local make_rainbow = function(colors, prefix, bright)
  local out = {}
  for i, color in ipairs(colors) do
    local darker = mix_colors(color, bg, bright)
    vim.api.nvim_set_hl(0, prefix .. i, { fg = darker })
    table.insert(out, prefix .. i)
  end
  return out
end

-- vanilla version of the colors
M.rainbow_groups = make_rainbow(M.rainbow_colors, "RainbowDelim", 1)

-- slightly dim variant for less sharp colors
M.rainbow_dim_groups = make_rainbow(M.rainbow_colors, "RainbowDimDelim", 0.85)

-- significantly darker variant
M.rainbow_dark_groups = make_rainbow(M.rainbow_colors, "RainbowDarkDelim", 0.2)

require("lazy.minit").repro({
  spec = {
    {
      "folke/snacks.nvim",
      opts = {
        indent = {
          enabled = true,
          char = "",
          only_scope = true,
          only_current = true,
          indent = {
            hl = (function()
              local groups = {}
              for i = 1, 12 do
                groups[i] = "RainbowDarkDelim" .. i
              end
              return groups
            end)()
          },
          animate = {
            enabled = false,
          },
          scope = {
            char = "",
            only_current = true,
            -- no idea why but the scope-based highlighting seems to be skipping
            -- groups? If the color ever goes back wrong again see if this was
            -- fixed.
            hl = (function()
              local groups = {}
              for i = 1, 12 do
                groups[i] = "RainbowDelim" .. i
              end
              return groups
            end)()
          }
        },
      }
    },
    -- add any other plugins here
  },
})
@tzhouhc tzhouhc added the bug Something isn't working label Jan 5, 2025
@folke folke closed this as completed in 627af73 Jan 20, 2025
folke pushed a commit that referenced this issue Jan 20, 2025
🤖 I have created a release *beep* *boop*
---


##
[2.14.0](v2.13.0...v2.14.0)
(2025-01-20)


### Features

* **picker.buffer:** add filetype to bufname for buffers without name
([83baea0](83baea0))
* **picker.debug:** debug option to detect garbage collection leaks
([b59f4ff](b59f4ff))
* **picker.matcher:** new `opts.matcher.file_pos` which defaults to
`true` to support patterns like `file:line:col` or `file:line`. Closes
[#517](#517). Closes
[#496](#496). Closes
[#651](#651)
([5e00b0a](5e00b0a))
* **picker:** added `args` option for `files` and `grep`. Closes
[#621](#621)
([781b6f6](781b6f6))
* **picker:** added `undo` picker to navigate the undo tree. Closes
[#638](#638)
([5c45f1c](5c45f1c))
* **picker:** added support for item.resolve that gets called if needed
during list rendering / preview
([b0d3266](b0d3266))
* **terminal:** allow overriding default shell. Closes
[#450](#450)
([3146fd1](3146fd1))
* **terminal:** close terminals on `ExitPre`. Fixes
[#419](#419)
([2abf208](2abf208))


### Bug Fixes

* **dashboard:** added optional filter for recent files
([32cd343](32cd343))
* **debug.run:** schedule only nvim_buf_set_extmark in on_print
([#425](#425))
([81572b5](81572b5))
* **indent:** use correct hl based on indent. Fixes
[#422](#422)
([627af73](627af73))
* **input:** put the cursor right after the default prompt
([#549](#549))
([f904481](f904481))
* **notifier:** added `SnacksNotifierMinimal`. Closes
[#410](#410)
([daa575e](daa575e))
* **notifier:** win:close instead of win:hide
([f29f7a4](f29f7a4))
* **picker.buffers:** add buf number to text
([70106a7](70106a7))
* **picker.buffer:** unselect on delete. Fixes
[#653](#653)
([0ac5605](0ac5605))
* **picker.grep:** correctly insert args from pattern. See
[#601](#601)
([8601a8c](8601a8c))
* **picker.grep:** debug
([f0d51ce](f0d51ce))
* **picker.lsp.symbols:** only include filename for search with
workspace symbols
([eb0e5b7](eb0e5b7))
* **picker.lsp:** backward compat with Neovim 0.95
([3df2408](3df2408))
* **picker.lsp:** lazy resolve item lsp locations. Fixes
[#650](#650)
([d0a0046](d0a0046))
* **picker.preview:** disable relativenumber by default. Closes
[#664](#664)
([384b9a7](384b9a7))
* **picker.preview:** off-by-one for cmd output
([da5556a](da5556a))
* **picker.preview:** reset before notify
([e50f2e3](e50f2e3))
* **picker.undo:** disable number and signcolumn in preview
([40cea79](40cea79))
* **picker.util:** item.resolve for nil item
([2ff21b4](2ff21b4))
* **picker.util:** relax pattern for args
([6b7705c](6b7705c))
* **scope:** parse treesitter injections. Closes
[#430](#430)
([985ada3](985ada3))
* **statusline:** fix status line cache key
([#656](#656))
([af55934](af55934))
* **win:** always close created scratch buffers when win closes
([abd7e61](abd7e61))
* **zen:** properly handle close
([920a9d2](920a9d2))
* **zen:** sync cursor with parent window
([#547](#547))
([ba45c28](ba45c28)),
closes [#539](#539)


### Performance Improvements

* **picker:** fixed some issues with closed pickers not always being
garbage collected
([eebf44a](eebf44a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant