Skip to content

Commit

Permalink
feat: template cleanup and improvements (#11)
Browse files Browse the repository at this point in the history
## 📃 Summary

better initial setup and remove useless functions
  • Loading branch information
shortcuts committed Mar 26, 2023
1 parent 6c2f360 commit af2fcb0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 126 deletions.
17 changes: 0 additions & 17 deletions doc/neovim-plugin-boilerplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
3 changes: 0 additions & 3 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -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()*
4 changes: 3 additions & 1 deletion lua/your-plugin-name/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 16 additions & 35 deletions lua/your-plugin-name/init.lua
Original file line number Diff line number Diff line change
@@ -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
28 changes: 15 additions & 13 deletions lua/your-plugin-name/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local D = require("your-plugin-name.util.debug")
local M = require("your-plugin-name.util.map")

-- internal methods
local YourPluginName = {}
Expand All @@ -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
20 changes: 13 additions & 7 deletions lua/your-plugin-name/util/debug.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
50 changes: 0 additions & 50 deletions lua/your-plugin-name/util/map.lua

This file was deleted.

0 comments on commit af2fcb0

Please sign in to comment.