Skip to content

Commit 836c471

Browse files
refactor: modularise inter-plugin integrations (mostly snacks)
1 parent c05cd32 commit 836c471

File tree

8 files changed

+134
-153
lines changed

8 files changed

+134
-153
lines changed

lua/plugins/conform.lua

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,8 @@
1-
-- TODO: Refine config
2-
-- TODO: Modularise snacks integration
31
-- TODO: Lazy load conform
42

5-
local default_opts = {
6-
format_on_save = function(bufnr)
7-
local global_state = vim.g.format_on_save
8-
local buffer_state = vim.b[bufnr].format_on_save
9-
10-
local state = global_state == nil or global_state
11-
12-
if buffer_state ~= nil then
13-
state = buffer_state
14-
end
15-
16-
return state and {
17-
timeout_ms = 1000,
18-
lsp_format = "fallback",
19-
}
20-
end,
21-
}
22-
23-
local function config(_, opts)
24-
vim.g.format_on_save = true
25-
26-
if Snacks then
27-
Snacks.toggle({
28-
name = "Formatter (Global)",
29-
get = function()
30-
return vim.g.format_on_save == nil or vim.g.format_on_save
31-
end,
32-
set = function(state)
33-
vim.g.format_on_save = state
34-
vim.b.format_on_save = nil
35-
end,
36-
}):map "<leader>ulf"
37-
38-
Snacks.toggle({
39-
name = "Formatter (Buffer)",
40-
get = function()
41-
local global_state = vim.g.format_on_save
42-
local buffer_state = vim.b[vim.api.nvim_get_current_buf()].format_on_save
43-
44-
if buffer_state ~= nil then
45-
return buffer_state
46-
end
47-
48-
return global_state == nil or global_state
49-
end,
50-
set = function(state)
51-
vim.b.format_on_save = state
52-
end,
53-
}):map "<leader>ulF"
54-
end
55-
56-
require("conform").setup(opts)
57-
end
58-
3+
-- Config function defined in `plugins/snacks.lua`
594
return {
605
"stevearc/conform.nvim",
61-
626
lazy = false,
63-
64-
opts = default_opts,
65-
config = config,
7+
opts = {},
668
}

lua/plugins/gitsigns.lua

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-- TODO: Lazy load gitsigns
2-
-- TODO: Modularise snacks integration
32

43
local default_opts = {
54
signs = {},
@@ -11,26 +10,7 @@ for name, sign in pairs(require("icons").git_gutter) do
1110
default_opts.signs[name] = { text = sign }
1211
end
1312

14-
local function config(_, opts)
15-
local gitsigns = require "gitsigns"
16-
17-
if Snacks then
18-
local gitsigns_config = require("gitsigns.config").config
19-
20-
Snacks.toggle({
21-
name = "Git Line Blame",
22-
get = function()
23-
return gitsigns_config.current_line_blame
24-
end,
25-
set = function(state)
26-
gitsigns.toggle_current_line_blame(state)
27-
end,
28-
}):map "<leader>ug"
29-
end
30-
31-
gitsigns.setup(opts)
32-
end
33-
13+
-- Config function defined in `plugins/snacks.lua`
3414
return {
3515
"lewis6991/gitsigns.nvim",
3616

@@ -52,7 +32,6 @@ return {
5232
},
5333

5434
opts = default_opts,
55-
config = config,
5635

5736
specs = {
5837
"catppuccin/nvim",

lua/plugins/lightbulb.lua

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
-- TODO: Modularise snacks integration
21
-- TODO: Lazy load lightbulb
32

43
local bulb_icons = require("icons").lightbulb
@@ -22,29 +21,9 @@ local default_opts = {
2221
end,
2322
}
2423

25-
local function config(_, opts)
26-
vim.g.enable_lightbulb = true
27-
28-
if Snacks then
29-
Snacks.toggle({
30-
name = "Lightbulb",
31-
get = function()
32-
return vim.g.enable_lightbulb
33-
end,
34-
set = function(state)
35-
vim.g.enable_lightbulb = state
36-
end,
37-
}):map "<leader>ull"
38-
end
39-
40-
require("nvim-lightbulb").setup(opts)
41-
end
42-
24+
-- Config function defined in `plugins/snacks.lua`
4325
return {
4426
"kosayoda/nvim-lightbulb",
45-
4627
lazy = false,
47-
4828
opts = default_opts,
49-
config = config,
5029
}

lua/plugins/lspconfig.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-- TODO: Lazy load lspconfig
2-
-- TODO: Modularise snacks integration
32

43
local default_opts = {
54
servers = {},
@@ -51,17 +50,10 @@ local function config(_, opts)
5150
})
5251

5352
if Snacks then
54-
Snacks.toggle.diagnostics({ name = "Diagnostics (Global)" }):map "<leader>uld"
55-
Snacks.toggle({
56-
name = "Diagnostics (Buffer)",
57-
get = function()
58-
return vim.diagnostic.is_enabled { bufnr = 0 }
59-
end,
60-
set = function(state)
61-
vim.diagnostic.enable(state, { bufnr = 0 })
62-
end,
63-
}):map "<leader>ulD"
53+
Snacks.toggle.diagnostics():map "<leader>uld"
6454

55+
-- HACK: Snacks.toggle.inlay_hint (exceptionally) only toggles locally
56+
-- TODO: Upstream snacks inlay_hint toggle?
6557
Snacks.toggle({
6658
name = "Inlay Hints",
6759
get = function()

lua/plugins/neo-tree.lua

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
-- TODO: Modularise snacks integration
2-
-- TODO: Improve neo-tree config
3-
41
local icons = require "icons"
52

63
local default_opts = {
@@ -39,25 +36,7 @@ local default_opts = {
3936
},
4037
}
4138

42-
local function config(_, opts)
43-
if Snacks then
44-
local events = require "neo-tree.events"
45-
46-
local function on_move(data)
47-
Snacks.rename.on_rename_file(data.source, data.destination)
48-
end
49-
50-
opts.event_handlers = opts.event_handlers or {}
51-
52-
vim.list_extend(opts.event_handlers, {
53-
{ event = events.FILE_MOVED, handler = on_move },
54-
{ event = events.FILE_RENAMED, handler = on_move },
55-
})
56-
end
57-
58-
require("neo-tree").setup(opts)
59-
end
60-
39+
-- Config function defined in `plugins/snacks.lua`
6140
return {
6241
"nvim-neo-tree/neo-tree.nvim",
6342
dependencies = {
@@ -86,15 +65,32 @@ return {
8665
end,
8766

8867
opts = default_opts,
89-
config = config,
9068

9169
specs = {
92-
"catppuccin/nvim",
93-
optional = true,
94-
opts = {
95-
integrations = {
96-
neotree = true,
70+
{
71+
"catppuccin/nvim",
72+
optional = true,
73+
opts = {
74+
integrations = {
75+
neotree = true,
76+
},
9777
},
9878
},
79+
80+
{
81+
"NeogitOrg/neogit",
82+
optional = true,
83+
config = function()
84+
vim.api.nvim_create_autocmd("BufWinLeave", {
85+
pattern = "NeogitStatus",
86+
group = vim.api.nvim_create_augroup("frosty_neotree_git_update", { clear = true }),
87+
desc = "Update neotree's git icons after neogit updates its status",
88+
callback = function()
89+
local events = require "neo-tree.events"
90+
events.fire_event(events.GIT_EVENT)
91+
end,
92+
})
93+
end,
94+
},
9995
},
10096
}

lua/plugins/neogit.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local default_opts = {
66
},
77
}
88

9+
-- Config function defined in `plugins/neo-tree.lua`
910
return {
1011
"NeogitOrg/neogit",
1112
dependencies = {

lua/plugins/snacks.lua

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
-- TODO: Modularise opts table
2-
31
local function config()
42
-- TODO: Are all these enabled lines needed?
53
local opts = {
@@ -24,10 +22,9 @@ local function config()
2422
},
2523
}
2624

27-
-- BUG: Wrap option being applied globally
2825
Snacks.toggle.option("wrap", { name = "Line Wrap", global = true }):map "<leader>uw"
2926

30-
-- TODO: Better way to rename snack toggles
27+
-- HACK: Equivalent to `Snacks.toggle.words` but renamed
3128
Snacks.toggle({
3229
name = "References",
3330
get = function()
@@ -72,7 +69,20 @@ return {
7269

7370
config = config,
7471

72+
-- Some snacks dependent integrations are defined in the respective plugins' config functions
73+
-- Search for `if Snacks then` to find these
7574
specs = {
75+
{
76+
"akinsho/bufferline.nvim",
77+
optional = true,
78+
opts = {
79+
options = {
80+
close_command = "lua Snacks.bufdelete.delete(%d)",
81+
right_mouse_command = "lua Snacks.bufdelete.delete(%d)",
82+
},
83+
},
84+
},
85+
7686
{
7787
"catppuccin/nvim",
7888
optional = true,
@@ -84,14 +94,96 @@ return {
8494
},
8595

8696
{
87-
"akinsho/bufferline.nvim",
97+
"stevearc/conform.nvim",
8898
optional = true,
99+
89100
opts = {
90-
options = {
91-
close_command = "lua Snacks.bufdelete.delete(%d)",
92-
right_mouse_command = "lua Snacks.bufdelete.delete(%d)",
93-
},
101+
format_on_save = function()
102+
return vim.g.format_on_save
103+
and {
104+
timeout_ms = 1000,
105+
lsp_format = "fallback",
106+
}
107+
end,
94108
},
109+
110+
config = function(_, opts)
111+
vim.g.format_on_save = true
112+
113+
Snacks.toggle({
114+
name = "Formatter",
115+
get = function()
116+
return vim.g.format_on_save
117+
end,
118+
set = function(state)
119+
vim.g.format_on_save = state
120+
end,
121+
}):map "<leader>ulf"
122+
123+
require("conform").setup(opts)
124+
end,
125+
},
126+
127+
{
128+
"lewis6991/gitsigns.nvim",
129+
optional = true,
130+
config = function(_, opts)
131+
local gitsigns = require "gitsigns"
132+
local gitsigns_config = require("gitsigns.config").config
133+
134+
Snacks.toggle({
135+
name = "Git Line Blame",
136+
get = function()
137+
return gitsigns_config.current_line_blame
138+
end,
139+
set = function(state)
140+
gitsigns.toggle_current_line_blame(state)
141+
end,
142+
}):map "<leader>ug"
143+
144+
gitsigns.setup(opts)
145+
end,
146+
},
147+
148+
{
149+
"kosayoda/nvim-lightbulb",
150+
optional = true,
151+
config = function(_, opts)
152+
vim.g.enable_lightbulb = true
153+
154+
Snacks.toggle({
155+
name = "Lightbulb",
156+
get = function()
157+
return vim.g.enable_lightbulb
158+
end,
159+
set = function(state)
160+
vim.g.enable_lightbulb = state
161+
end,
162+
}):map "<leader>ull"
163+
164+
require("nvim-lightbulb").setup(opts)
165+
end,
166+
},
167+
168+
{
169+
"nvim-neo-tree/neo-tree.nvim",
170+
optional = true,
171+
config = function(_, opts)
172+
local events = require "neo-tree.events"
173+
174+
local function on_move(data)
175+
Snacks.rename.on_rename_file(data.source, data.destination)
176+
end
177+
178+
opts.event_handlers = opts.event_handlers or {}
179+
180+
vim.list_extend(opts.event_handlers, {
181+
{ event = events.FILE_MOVED, handler = on_move },
182+
{ event = events.FILE_RENAMED, handler = on_move },
183+
})
184+
185+
require("neo-tree").setup(opts)
186+
end,
95187
},
96188
},
97189
}

0 commit comments

Comments
 (0)