Skip to content

Commit

Permalink
Move autocmd to autoload and validate setup opts
Browse files Browse the repository at this point in the history
  • Loading branch information
andweeb committed Jan 3, 2021
1 parent a656ba5 commit f315156
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
9 changes: 9 additions & 0 deletions autoload/presence.vim
Original file line number Diff line number Diff line change
@@ -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
33 changes: 27 additions & 6 deletions lua/presence/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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...")

Expand Down
10 changes: 1 addition & 9 deletions plugin/presence.vim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f315156

Please sign in to comment.