From f3151560a7482cae4ffab2ae09df575d284ca006 Mon Sep 17 00:00:00 2001 From: Andrew Kwon Date: Sun, 3 Jan 2021 11:29:19 -0800 Subject: [PATCH] Move autocmd to autoload and validate setup opts --- autoload/presence.vim | 9 +++++++++ lua/presence/init.lua | 33 +++++++++++++++++++++++++++------ plugin/presence.vim | 10 +--------- 3 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 autoload/presence.vim diff --git a/autoload/presence.vim b/autoload/presence.vim new file mode 100644 index 0000000..a4fb83a --- /dev/null +++ b/autoload/presence.vim @@ -0,0 +1,9 @@ +" Define autocommands to handle auto-update events +function presence#SetAutoCmds() + augroup presence_events + autocmd! + if exists("g:presence_auto_update") && g:presence_auto_update + autocmd BufRead * lua package.loaded.presence:update() + endif + augroup END +endfunction diff --git a/lua/presence/init.lua b/lua/presence/init.lua index 46c0479..8cd38c2 100644 --- a/lua/presence/init.lua +++ b/lua/presence/init.lua @@ -6,6 +6,18 @@ local DiscordRPC = require("presence.discord") function Presence:setup(options) options = options or {} + self.options = options + + -- Initialize logger with provided options + self.log = Log { + level = options.log_level or vim.g.presence_log_level, + } + + self.log:debug("Setting up plugin...") + + -- Warn on any duplicate user-defined options + self:check_dup_options("auto_update") + self:check_dup_options("log_level") -- Ensure auto-update config is reflected in its global var setting if options.auto_update ~= nil or not vim.g.presence_auto_update then @@ -15,12 +27,8 @@ function Presence:setup(options) vim.api.nvim_set_var("presence_auto_update", should_auto_update) end - -- Initialize logger with provided options - self.log = Log { - level = options.log_level or vim.g.presence_log_level, - } - - self.log:debug("Setting up plugin...") + -- Set autocommands + vim.fn["presence#SetAutoCmds"]() -- Internal state self.is_connected = false @@ -46,6 +54,19 @@ function Presence:setup(options) return self end +-- Check and warn for duplicate user-defined options +function Presence:check_dup_options(option) + local g_variable = "presence_"..option + + if self.options[option] ~= nil and vim.g[g_variable] ~= nil then + local warning_fmt = "Duplicate options set: `g:%s` and setup option `%s`" + local warning_msg = string.format(warning_fmt, g_variable, option) + + self.log:warn(warning_msg) + end + +end + function Presence:connect(on_done) self.log:debug("Connecting to Discord...") diff --git a/plugin/presence.vim b/plugin/presence.vim index 7892b23..e7c46b2 100644 --- a/plugin/presence.vim +++ b/plugin/presence.vim @@ -1,15 +1,7 @@ -" Define autocommands to handle auto-update events -augroup presence_events - autocmd! - if g:presence_auto_update - autocmd BufRead * lua package.loaded.presence:update() - endif -augroup END - " Fallback to setting up the plugin automatically if !exists("g:presence_has_setup") lua << EOF local Presence = require("presence"):setup() - Presence.log:debug("Custom setup not detected, plugin set up using defaults") + Presence.log:debug("Custom setup not detected, using defaults") EOF endif