Skip to content

Commit e387741

Browse files
committed
refactor: break out notification functions
1 parent 8494f93 commit e387741

File tree

4 files changed

+82
-79
lines changed

4 files changed

+82
-79
lines changed

lua/modicator/init.lua

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -41,65 +41,6 @@ function M.get_options()
4141
return options
4242
end
4343

44-
--- @return table
45-
local function get_missing_options(opts)
46-
return vim.iter(opts):filter(function(opt) return not vim.o[opt] end)
47-
end
48-
49-
local function warn_missing_options(opts)
50-
for _, opt in pairs(opts) do
51-
if not vim.o[opt] then
52-
local message = string.format(
53-
'Modicator requires `%s` to be set. Run `:set %s` or add `vim.o.%s '
54-
.. '= true` to your init.lua',
55-
opt,
56-
opt,
57-
opt
58-
)
59-
require('modicator.utils').warn(message)
60-
end
61-
end
62-
end
63-
64-
--- @param opts table
65-
local function check_deprecated_config(opts)
66-
if opts.highlights and opts.highlights.modes then
67-
local message = 'configuration of highlights has changed to highlight '
68-
.. 'groups rather than using `highlights.modes`. Check `:help '
69-
.. 'modicator-configuration` to see the new configuration API.'
70-
require('modicator.utils').warn(message)
71-
end
72-
end
73-
74-
local function show_warnings()
75-
if options.show_warnings then
76-
local missing_options = get_missing_options({
77-
'cursorline',
78-
'number',
79-
'termguicolors',
80-
}):totable()
81-
82-
if #missing_options > 0 then
83-
warn_missing_options(missing_options)
84-
85-
local message = 'If you\'ve you have already set '
86-
.. 'those options in your config, this warning is likely '
87-
.. 'caused by another plugin temporarily modifying those '
88-
.. 'options for this buffer. If Modicator works as expected in '
89-
.. 'other buffers you can remove the `show_warnings` option '
90-
.. 'from your Modicator configuration.'
91-
require('modicator.utils').inform(message)
92-
end
93-
94-
check_deprecated_config(options)
95-
end
96-
end
97-
98-
local function lualine_is_loaded()
99-
local ok, _ = pcall(require, 'lualine')
100-
return ok
101-
end
102-
10344
local function mode_name_from_mode(mode)
10445
local mode_names = {
10546
['n'] = 'Normal',
@@ -175,6 +116,11 @@ local function set_fallback_highlight_groups()
175116
end
176117
end
177118

119+
local function lualine_is_loaded()
120+
local ok, _ = pcall(require, 'lualine')
121+
return ok
122+
end
123+
178124
local function set_highlight_groups()
179125
if lualine_is_loaded() and options.integration.lualine.enabled then
180126
local mode_section = options.integration.lualine.mode_section
@@ -211,7 +157,7 @@ local function create_autocmds()
211157
-- NOTE: VimEnter loads after user's configuration is loaded
212158
api.nvim_create_autocmd('VimEnter', {
213159
callback = function()
214-
show_warnings()
160+
require('modicator.notifications').show_warnings()
215161
update_mode()
216162
end,
217163
group = augroup,

lua/modicator/integration/lualine/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function M.use_lualine_mode_highlights(mode_section)
107107
local message = "'integration.lualine.enabled' is true, but no lualine "
108108
.. "mode section was found. Please set it manually in "
109109
.. "'integration.lualine.mode_section'"
110-
require('modicator.utils').warn(message)
110+
require('modicator.notifications').warn(message)
111111
return
112112
end
113113

lua/modicator/notifications.lua

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
local M = {}
2+
3+
--- @return table
4+
local function get_missing_options(opts)
5+
return vim.iter(opts):filter(function(opt) return not vim.o[opt] end)
6+
end
7+
8+
local function warn_missing_options(opts)
9+
for _, opt in pairs(opts) do
10+
if not vim.o[opt] then
11+
local message = string.format(
12+
'Modicator requires `%s` to be set. Run `:set %s` or add `vim.o.%s '
13+
.. '= true` to your init.lua',
14+
opt,
15+
opt,
16+
opt
17+
)
18+
require('modicator.utils').warn(message)
19+
end
20+
end
21+
end
22+
23+
--- @param opts table
24+
local function check_deprecated_config(opts)
25+
if opts.highlights and opts.highlights.modes then
26+
local message = 'configuration of highlights has changed to highlight '
27+
.. 'groups rather than using `highlights.modes`. Check `:help '
28+
.. 'modicator-configuration` to see the new configuration API.'
29+
require('modicator.utils').warn(message)
30+
end
31+
end
32+
33+
function M.show_warnings()
34+
local options = require('modicator').get_options()
35+
36+
if options.show_warnings then
37+
local missing_options = get_missing_options({
38+
'cursorline',
39+
'number',
40+
'termguicolors',
41+
}):totable()
42+
43+
if #missing_options > 0 then
44+
warn_missing_options(missing_options)
45+
46+
local message = 'If you\'ve you have already set '
47+
.. 'those options in your config, this warning is likely '
48+
.. 'caused by another plugin temporarily modifying those '
49+
.. 'options for this buffer. If Modicator works as expected in '
50+
.. 'other buffers you can remove the `show_warnings` option '
51+
.. 'from your Modicator configuration.'
52+
require('modicator.utils').inform(message)
53+
end
54+
55+
check_deprecated_config(options)
56+
end
57+
end
58+
59+
--- @param message string
60+
function M.warn(message)
61+
if require('modicator').get_options().show_warnings then
62+
local warning = string.format('modicator.nvim: %s', message)
63+
vim.notify(warning, vim.log.levels.WARN)
64+
end
65+
end
66+
67+
--- @param message string
68+
function M.inform(message)
69+
if require('modicator').get_options().show_warnings then
70+
local warning = string.format('modicator.nvim: %s', message)
71+
vim.notify(warning, vim.log.levels.INFO)
72+
end
73+
end
74+
75+
return M

lua/modicator/utils.lua

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
1-
local modicator = require('modicator')
2-
31
local M = {}
42

5-
--- @param message string
6-
function M.warn(message)
7-
if modicator.get_options().show_warnings then
8-
local warning = string.format('modicator.nvim: %s', message)
9-
vim.notify(warning, vim.log.levels.WARN)
10-
end
11-
end
12-
13-
--- @param message string
14-
function M.inform(message)
15-
if modicator.get_options().show_warnings then
16-
local warning = string.format('modicator.nvim: %s', message)
17-
vim.notify(warning, vim.log.levels.INFO)
18-
end
19-
end
20-
213
--- @param hl_group string
224
--- @return boolean
235
function M.highlight_exists(hl_group)

0 commit comments

Comments
 (0)