mini.nvim icon indicating copy to clipboard operation
mini.nvim copied to clipboard

Create "loaded" variable

Open joshuaMarple opened this issue 4 years ago • 3 comments

Contributing guidelines

Module(s)

All

Description

It would be helpful to have a "loaded" variable, similar to other vim plugins such as https://github.com/glepnir/galaxyline.nvim/blob/main/plugin/galaxyline.vim. This recommendation is also further outlined in https://vimways.org/2019/writing-vim-plugin/ and https://vi.stackexchange.com/questions/26132/why-do-plugins-have-to-check-if-they-are-already-loaded.

I make use of these variables to optionally enable certain settings depending on if a plugin is available or not.

joshuaMarple avatar Sep 13 '22 04:09 joshuaMarple

The recommended way to check if module 'mini.xxx' is "active" is with _G.MiniXxx ~= nil Lua snippet. For example, the 'mini.surround' check is _G.MiniSurround ~= nil.

Some clarification. Every module of this plugin is an independent Lua module which should be set up explicitly via require('mini.xxx').setup() (except some cases which only provide Lua functions). Every setup() call creates a global variable _G.MiniXxx (that is by design and always be like that). This is also the way 'mini.nvim' itself uses in rare occasions when it needs such a check (like in mini.starter).

Does this solve your problem?

echasnovski avatar Sep 13 '22 06:09 echasnovski

That is close to what I want, but since that variable is only set when the plugin is required, it didn't quite fix it.

Essentially I wanted to add a check that only called "require('mini.surround').setup()" if it could detect that mini.nvim was installed. The global variable "g:loaded_*" allows you to check for that. I think it is an old vimscript-ism, and isn't necessary anymore with lua.

I ended up writing a checker for lua modules instead using something similar to https://stackoverflow.com/questions/15429236/how-to-check-if-a-module-exists-in-lua, which fixes my issue.

Thanks for your quick reply!

joshuaMarple avatar Sep 13 '22 14:09 joshuaMarple

Oh, I see.

The current standard approach to this kind of problem is via using pcall():

local ok, surround = pcall(require, 'mini.surround')
if ok then surround.setup() end

echasnovski avatar Sep 13 '22 15:09 echasnovski