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

feat(health): Warn about unused keys when normalizing a LazySpec[] #1299

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

pynappo
Copy link

@pynappo pynappo commented Jan 28, 2024

Occasionally, users may put the configuration keys for a plugin into a LazySpec[], rather than a specific LazySpec. Alternatively, they may add a new LazySpec|string into an existing LazySpec for a plugin. This leads to some configuration keys being silently ignored by lazy.nvim after normalization, which can be confusing to new lazy.nvim users.

This PR adds a health check to Spec:normalize() which will warn about the misplaced keys.

Example of a LazySpec[] that will trigger this:

return {
  {
    'lewis6991/gitsigns.nvim',
    event = { 'BufNewFile', 'BufReadPre' },
    dependencies = { 'nvim-lua/plenary.nvim' },
    opts = {
      signs = {
        add = { hl = 'GitSignsAdd', text = '', numhl = 'GitSignsAddNr', linehl = 'GitSignsAddLn' },
        change = { hl = 'GitSignsChange', text = '', numhl = 'GitSignsChangeNr', linehl = 'GitSignsChangeLn' },
        delete = { hl = 'GitSignsDelete', text = '_', numhl = 'GitSignsDeleteNr', linehl = 'GitSignsDeleteLn' },
        topdelete = { hl = 'GitSignsDelete', text = '', numhl = 'GitSignsDeleteNr', linehl = 'GitSignsDeleteLn' },
        changedelete = { hl = 'GitSignsChange', text = '~', numhl = 'GitSignsChangeNr', linehl = 'GitSignsChangeLn' },
      },
      numhl = true, -- Toggle with `:Gitsigns toggle_numhl`
      watch_gitdir = {
        interval = 1000,
        follow_files = true,
      },
    },
    'pwntester/octo.nvim',
  },
}

The resulting healthcheck warning with this PR (:Lazy health):
image

This is how a longer warning looks like:
image

Concerns:

  • On my machine, this PR can slow down spec processsing by a few ms when warning about big tables (I assume due to vim.inspect). For reference, spec processing takes ~5ms for me without this warning.
  • How should I word and format the warning message? The current method is admittedly hacky, but I like how it looks.

This extra check will more optimistically try to normalize a spec
formatted like this:

```lua
return {
  {'foo/bar'},
  baz = "asdf"
}
```

Before, lazy.nvim would see that it's a table with a length of 1 and
that it's not a list, so it tries to add the table containing the spec
directly, instead of the spec itself. This check makes lazy.nvim proceed
to normalize {'foo/bar'} instead.
@pynappo
Copy link
Author

pynappo commented Feb 8, 2024

Added an extra check for normalization, see the f453010 (#1299) description. It happens to fit under the intention of this PR so I added it here (without the extra check, lazy.nvim could error with a vague stacktrace, with this PR it's now a healthcheck warning), but I could split it into a seperate fix(plugin): PR if you would like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants