From af2fcb0ffcac54eb9e4092bb860c22e29d2579dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Sun, 26 Mar 2023 01:10:05 +0100 Subject: [PATCH] feat: template cleanup and improvements (#11) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📃 Summary better initial setup and remove useless functions --- doc/neovim-plugin-boilerplate.txt | 17 ---------- doc/tags | 3 -- lua/your-plugin-name/config.lua | 4 ++- lua/your-plugin-name/init.lua | 51 +++++++++-------------------- lua/your-plugin-name/main.lua | 28 ++++++++-------- lua/your-plugin-name/util/debug.lua | 20 +++++++---- lua/your-plugin-name/util/map.lua | 50 ---------------------------- 7 files changed, 47 insertions(+), 126 deletions(-) delete mode 100644 lua/your-plugin-name/util/map.lua diff --git a/doc/neovim-plugin-boilerplate.txt b/doc/neovim-plugin-boilerplate.txt index 618bb5a..b1045cd 100644 --- a/doc/neovim-plugin-boilerplate.txt +++ b/doc/neovim-plugin-boilerplate.txt @@ -25,21 +25,4 @@ Usage~ `require("your-plugin-name").setup()` (add `{}` with your |YourPluginName.options| table) -============================================================================== ------------------------------------------------------------------------------- - *YourPluginName.toggle()* - `YourPluginName.toggle`() -Toggle the plugin by calling the `enable`/`disable` methods respectively. - ------------------------------------------------------------------------------- - *YourPluginName.enable()* - `YourPluginName.enable`() -A method to enable your plugin. - ------------------------------------------------------------------------------- - *YourPluginName.disable()* - `YourPluginName.disable`() -A method to disable your plugin. - - vim:tw=78:ts=8:noet:ft=help:norl: \ No newline at end of file diff --git a/doc/tags b/doc/tags index 8b0bb98..5a74bec 100644 --- a/doc/tags +++ b/doc/tags @@ -1,5 +1,2 @@ -YourPluginName.disable() neovim-plugin-boilerplate.txt /*YourPluginName.disable()* -YourPluginName.enable() neovim-plugin-boilerplate.txt /*YourPluginName.enable()* YourPluginName.options neovim-plugin-boilerplate.txt /*YourPluginName.options* YourPluginName.setup() neovim-plugin-boilerplate.txt /*YourPluginName.setup()* -YourPluginName.toggle() neovim-plugin-boilerplate.txt /*YourPluginName.toggle()* diff --git a/lua/your-plugin-name/config.lua b/lua/your-plugin-name/config.lua index 8c65a4d..c104526 100644 --- a/lua/your-plugin-name/config.lua +++ b/lua/your-plugin-name/config.lua @@ -15,7 +15,9 @@ YourPluginName.options = { --- ---@usage `require("your-plugin-name").setup()` (add `{}` with your |YourPluginName.options| table) function YourPluginName.setup(options) - YourPluginName.options = vim.tbl_deep_extend("keep", options or {}, YourPluginName.options) + options = options or {} + + YourPluginName.options = vim.tbl_deep_extend("keep", options, YourPluginName.options) return YourPluginName.options end diff --git a/lua/your-plugin-name/init.lua b/lua/your-plugin-name/init.lua index 3638245..a1c0151 100644 --- a/lua/your-plugin-name/init.lua +++ b/lua/your-plugin-name/init.lua @@ -1,60 +1,41 @@ +local M = require("your-plugin-name.main") local YourPluginName = {} -- Toggle the plugin by calling the `enable`/`disable` methods respectively. function YourPluginName.toggle() -- when the config is not set to the global object, we set it - if YourPluginName.config == nil then - YourPluginName.config = require("your-plugin-name.config").options - end - local main = require("your-plugin-name.main") - - -- the internal toggle method tell us if the plugin was enabled or disabled. - -- this allows us to init/reset the global object. - if main[1].toggle() then - YourPluginName.internal = { - toggle = main[1].toggle, - enable = main[1].enable, - disable = main[1].disable, - } - else - YourPluginName.internal = { - toggle = nil, - enable = nil, - disable = nil, - } + if _G.YourPluginName.config == nil then + _G.YourPluginName.config = require("your-plugin-name.config").options end - YourPluginName.state = main[2] + _G.YourPluginName.state = M.toggle() end -- starts YourPluginName and set internal functions and state. function YourPluginName.enable() - local main = require("your-plugin-name.main") + if _G.YourPluginName.config == nil then + _G.YourPluginName.config = require("your-plugin-name.config").options + end + + local state = M.enable() - main[1].enable() + if state ~= nil then + _G.YourPluginName.state = state + end - YourPluginName.state = main[2] - YourPluginName.internal = { - toggle = main[1].toggle, - enable = main[1].enable, - disable = main[1].disable, - } + return state end -- disables YourPluginName and reset internal functions and state. function YourPluginName.disable() - local main = require("your-plugin-name.main") - - main[1].disable() - - YourPluginName.state = main[2] + _G.YourPluginName.state = M.disable() end -- setup YourPluginName options and merge them with user provided ones. function YourPluginName.setup(opts) - YourPluginName.config = require("your-plugin-name.config").setup(opts) + _G.YourPluginName.config = require("your-plugin-name.config").setup(opts) end _G.YourPluginName = YourPluginName -return YourPluginName +return _G.YourPluginName diff --git a/lua/your-plugin-name/main.lua b/lua/your-plugin-name/main.lua index 487c0ff..6eba137 100644 --- a/lua/your-plugin-name/main.lua +++ b/lua/your-plugin-name/main.lua @@ -1,5 +1,4 @@ local D = require("your-plugin-name.util.debug") -local M = require("your-plugin-name.util.map") -- internal methods local YourPluginName = {} @@ -10,38 +9,41 @@ local S = { enabled = false, } ---- Toggle the plugin by calling the `enable`/`disable` methods respectively. +---Toggle the plugin by calling the `enable`/`disable` methods respectively. +---@private function YourPluginName.toggle() if S.enabled then - YourPluginName.disable() - - return false + return YourPluginName.disable() end - YourPluginName.enable() - - return true + return YourPluginName.enable() end ---- A method to enable your plugin. +---Initializes the plugin. +---@private function YourPluginName.enable() if S.enabled then - return + return S end S.enabled = true + + return S end ---- A method to disable your plugin. +---Disables the plugin and reset the internal state. +---@private function YourPluginName.disable() if not S.enabled then - return + return S end -- reset the state S = { enabled = false, } + + return S end -return { YourPluginName, S } +return YourPluginName diff --git a/lua/your-plugin-name/util/debug.lua b/lua/your-plugin-name/util/debug.lua index 6b3e084..b550553 100644 --- a/lua/your-plugin-name/util/debug.lua +++ b/lua/your-plugin-name/util/debug.lua @@ -1,9 +1,11 @@ local D = {} --- prints a log if the local state has `debug` set to `true`. --- @param scope string: an identifier for the scope, e.g. the method name. --- @param str string: the string to format (same as string.format()). --- @param args ...: the params to format the string. +---prints only if debug is true. +--- +---@param scope string: the scope from where this function is called. +---@param str string: the formatted string. +---@param ... any: the arguments of the formatted string. +---@private function D.log(scope, str, ...) if _G.YourPluginName.config ~= nil and not _G.YourPluginName.config.debug then return @@ -27,9 +29,11 @@ function D.log(scope, str, ...) ) end --- prints the given `map` if the local state has `debug` set to `true`. --- @param table list: a list to print. --- @param indent int: the default indent of the table, leave empty for 0. +---prints the table if debug is true. +--- +---@param table table: the table to print. +---@param indent number?: the default indent value, starts at 0. +---@private function D.tprint(table, indent) if _G.YourPluginName.config ~= nil and not _G.YourPluginName.config.debug then return @@ -46,6 +50,8 @@ function D.tprint(table, indent) D.tprint(v, indent + 1) elseif type(v) == "boolean" then print(formatting .. tostring(v)) + elseif type(v) == "function" then + print(formatting .. "FUNCTION") else print(formatting .. v) end diff --git a/lua/your-plugin-name/util/map.lua b/lua/your-plugin-name/util/map.lua deleted file mode 100644 index 33fec4e..0000000 --- a/lua/your-plugin-name/util/map.lua +++ /dev/null @@ -1,50 +0,0 @@ -local M = {} - --- search in a `map` if it contains the `element`. --- @param map list --- @param element any --- @return boolean -function M.contains(map, element) - for _, v in pairs(map) do - if v == element then - return true - end - end - - return false -end - --- determines if the given `map` contains every `elements`. --- @param map list --- @param element any... --- @return boolean -function M.every(map, ...) - local nbElements = M.tsize(...) - local count = 0 - - for _, v in pairs(map) do - for _, el in pairs(...) do - if v == el then - count = count + 1 - break - end - end - end - - return count == nbElements -end - --- returns the size of a given `map`. --- @param map list --- @return int -function M.tsize(map) - local count = 0 - - for _ in pairs(map) do - count = count + 1 - end - - return count -end - -return M