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

Make calling require'lean'.setup{} idempotent #145

Closed
Julian opened this issue Oct 4, 2021 · 1 comment · Fixed by #277
Closed

Make calling require'lean'.setup{} idempotent #145

Julian opened this issue Oct 4, 2021 · 1 comment · Fixed by #277
Labels
enhancement New feature or request

Comments

@Julian
Copy link
Owner

Julian commented Oct 4, 2021

Users occasionally will put this in a file they reload or re-source, so it'd be nice if it were able to be run multiple times.

Right now most of the state issues are simply with initializing global configuration, so this may not be too difficult to do.

(See #132 for some context.)

Relates to #39.

@Julian Julian added the enhancement New feature or request label Oct 4, 2021
@4e554c4c
Copy link
Contributor

the solution in my config

local success, err = pcall(function ()
  require('lean').setup{}
end)
if not success then
  print(err)
end

4e554c4c added a commit to 4e554c4c/lean.nvim that referenced this issue Sep 6, 2022
It turns out that most of these come from the `_DEFAULTS` pattern. This
is because of the following:
original state:
```lua
tbl = { _DEFAULTS = ... }
```
after configuration with empty table, `tbl = vim.tbl_extend(tbl._DEFAULTS, {})`
which practically replaces `tbl` by `tbl._DEFAULTS`:
```lua
tbl = { ... }
```
after this, the next call to `vim.tbl_extend(tbl._DEFAULTS, {})` fails, as
`tbl._DEFAULTS` is nil.

The solution to this is to just call `vim.tbl_extend(tbl, ...)` and store the
defaults in `tbl` itself. The `._DEFAULTS` field can be preserved by copying to
it after initialization.

Also `command` has been replaced with `command!` in vimscript and
`autocmd`s have been wrapped in an `augroup` which resets its state each
call. This is also common practice that avoids duplicate autocommands.

Fixes Julian#145
4e554c4c added a commit to 4e554c4c/lean.nvim that referenced this issue Sep 7, 2022
It turns out that most of these come from the `_DEFAULTS` pattern. This
is because of the following:
original state:
```lua
tbl = { _DEFAULTS = ... }
```
after configuration with empty table, `tbl = vim.tbl_extend(tbl._DEFAULTS, {})`
which practically replaces `tbl` by `tbl._DEFAULTS`:
```lua
tbl = { ... }
```
after this, the next call to `vim.tbl_extend(tbl._DEFAULTS, {})` fails, as
`tbl._DEFAULTS` is nil.

The solution to this is to just call `vim.tbl_extend(tbl, ...)` and store the
defaults in `tbl` itself. The `._DEFAULTS` field can be preserved by copying to
it after initialization.

Also `command` has been replaced with `command!` in vimscript and
`autocmd`s have been wrapped in an `augroup` which resets its state each
call. This is also common practice that avoids duplicate autocommands.

Fixes Julian#145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants