Skip to content

Commit

Permalink
refactor: break out notification functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mawkler committed Jan 9, 2025
1 parent 491e66e commit b2c25e9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 79 deletions.
66 changes: 6 additions & 60 deletions lua/modicator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,65 +41,6 @@ function M.get_options()
return options
end

--- @return table
local function get_missing_options(opts)
return vim.iter(opts):filter(function(opt) return not vim.o[opt] end)
end

local function warn_missing_options(opts)
for _, opt in pairs(opts) do
if not vim.o[opt] then
local message = string.format(
'Modicator requires `%s` to be set. Run `:set %s` or add `vim.o.%s '
.. '= true` to your init.lua',
opt,
opt,
opt
)
require('modicator.utils').warn(message)
end
end
end

--- @param opts table
local function check_deprecated_config(opts)
if opts.highlights and opts.highlights.modes then
local message = 'configuration of highlights has changed to highlight '
.. 'groups rather than using `highlights.modes`. Check `:help '
.. 'modicator-configuration` to see the new configuration API.'
require('modicator.utils').warn(message)
end
end

local function show_warnings()
if options.show_warnings then
local missing_options = get_missing_options({
'cursorline',
'number',
'termguicolors',
}):totable()

if #missing_options > 0 then
warn_missing_options(missing_options)

local message = 'If you\'ve you have already set '
.. 'those options in your config, this warning is likely '
.. 'caused by another plugin temporarily modifying those '
.. 'options for this buffer. If Modicator works as expected in '
.. 'other buffers you can remove the `show_warnings` option '
.. 'from your Modicator configuration.'
require('modicator.utils').inform(message)
end

check_deprecated_config(options)
end
end

local function lualine_is_loaded()
local ok, _ = pcall(require, 'lualine')
return ok
end

local function mode_name_from_mode(mode)
local mode_names = {
['n'] = 'Normal',
Expand Down Expand Up @@ -175,6 +116,11 @@ local function set_fallback_highlight_groups()
end
end

local function lualine_is_loaded()
local ok, _ = pcall(require, 'lualine')
return ok
end

local function set_highlight_groups()
if lualine_is_loaded() and options.integration.lualine.enabled then
local mode_section = options.integration.lualine.mode_section
Expand Down Expand Up @@ -211,7 +157,7 @@ local function create_autocmds()
-- NOTE: VimEnter loads after user's configuration is loaded
api.nvim_create_autocmd('VimEnter', {
callback = function()
show_warnings()
require('modicator.notifications').show_warnings()
update_mode()
end,
group = augroup,
Expand Down
2 changes: 1 addition & 1 deletion lua/modicator/integration/lualine/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function M.use_lualine_mode_highlights(mode_section)
local message = "'integration.lualine.enabled' is true, but no lualine "
.. "mode section was found. Please set it manually in "
.. "'integration.lualine.mode_section'"
require('modicator.utils').warn(message)
require('modicator.notifications').warn(message)
return
end

Expand Down
75 changes: 75 additions & 0 deletions lua/modicator/notifications.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local M = {}

--- @return table
local function get_missing_options(opts)
return vim.iter(opts):filter(function(opt) return not vim.o[opt] end)
end

local function warn_missing_options(opts)
for _, opt in pairs(opts) do
if not vim.o[opt] then
local message = string.format(
'Modicator requires `%s` to be set. Run `:set %s` or add `vim.o.%s '
.. '= true` to your init.lua',
opt,
opt,
opt
)
require('modicator.utils').warn(message)
end
end
end

--- @param opts table
local function check_deprecated_config(opts)
if opts.highlights and opts.highlights.modes then
local message = 'configuration of highlights has changed to highlight '
.. 'groups rather than using `highlights.modes`. Check `:help '
.. 'modicator-configuration` to see the new configuration API.'
require('modicator.utils').warn(message)
end
end

function M.show_warnings()
local options = require('modicator').get_options()

if options.show_warnings then
local missing_options = get_missing_options({
'cursorline',
'number',
'termguicolors',
}):totable()

if #missing_options > 0 then
warn_missing_options(missing_options)

local message = 'If you\'ve you have already set '
.. 'those options in your config, this warning is likely '
.. 'caused by another plugin temporarily modifying those '
.. 'options for this buffer. If Modicator works as expected in '
.. 'other buffers you can remove the `show_warnings` option '
.. 'from your Modicator configuration.'
require('modicator.utils').inform(message)
end

check_deprecated_config(options)
end
end

--- @param message string
function M.warn(message)
if require('modicator').get_options().show_warnings then
local warning = string.format('modicator.nvim: %s', message)
vim.notify(warning, vim.log.levels.WARN)
end
end

--- @param message string
function M.inform(message)
if require('modicator').get_options().show_warnings then
local warning = string.format('modicator.nvim: %s', message)
vim.notify(warning, vim.log.levels.INFO)
end
end

return M
18 changes: 0 additions & 18 deletions lua/modicator/utils.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
local modicator = require('modicator')

local M = {}

--- @param message string
function M.warn(message)
if modicator.get_options().show_warnings then
local warning = string.format('modicator.nvim: %s', message)
vim.notify(warning, vim.log.levels.WARN)
end
end

--- @param message string
function M.inform(message)
if modicator.get_options().show_warnings then
local warning = string.format('modicator.nvim: %s', message)
vim.notify(warning, vim.log.levels.INFO)
end
end

--- @param hl_group string
--- @return boolean
function M.highlight_exists(hl_group)
Expand Down

0 comments on commit b2c25e9

Please sign in to comment.