Skip to content

Commit 60edd76

Browse files
authored
fix: prevent unwanted event trigger (#135)
## 📃 Summary discovered in #134, we triggered some events when entering side buffers, while those should be "transparent" to the user.
1 parent 6df43ae commit 60edd76

File tree

7 files changed

+86
-92
lines changed

7 files changed

+86
-92
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ require("no-neck-pain").setup({
140140
blend = 0,
141141
-- Hexadecimal color code to override the current text color of the buffer. (e.g. #7480c2)
142142
textColor = nil,
143-
-- vim buffer-scoped options: any `vim.bo` options is accepted here.
143+
-- Vim buffer-scoped options: any `vim.bo` options is accepted here.
144144
bo = {
145145
filetype = "no-neck-pain",
146146
buftype = "nofile",
147147
bufhidden = "hide",
148148
buflisted = false,
149149
swapfile = false,
150150
},
151-
-- vim window-scoped options: any `vim.wo` options is accepted here.
151+
-- Vim window-scoped options: any `vim.wo` options is accepted here.
152152
wo = {
153153
cursorline = false,
154154
cursorcolumn = false,
@@ -166,7 +166,7 @@ require("no-neck-pain").setup({
166166
--- See |NoNeckPain.bufferOptions|.
167167
right = NoNeckPain.bufferOptions,
168168
},
169-
-- lists supported integrations that might clash with `no-neck-pain.nvim`'s behavior
169+
-- Supported integrations that might clash with `no-neck-pain.nvim`'s behavior.
170170
integrations = {
171171
-- https://github.com/nvim-tree/nvim-tree.lua
172172
NvimTree = {
@@ -191,15 +191,15 @@ NoNeckPain.bufferOptions = {
191191
blend = 0,
192192
-- Hexadecimal color code to override the current text color of the buffer. (e.g. #7480c2)
193193
textColor = nil,
194-
-- vim buffer-scoped options: any `vim.bo` options is accepted here.
194+
-- Vim buffer-scoped options: any `vim.bo` options is accepted here.
195195
bo = {
196196
filetype = "no-neck-pain",
197197
buftype = "nofile",
198198
bufhidden = "hide",
199199
buflisted = false,
200200
swapfile = false,
201201
},
202-
-- vim window-scoped options: any `vim.wo` options is accepted here.
202+
-- Vim window-scoped options: any `vim.wo` options is accepted here.
203203
wo = {
204204
cursorline = false,
205205
cursorcolumn = false,

doc/no-neck-pain.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ Default values:
5151
blend = 0,
5252
-- Hexadecimal color code to override the current text color of the buffer. (e.g. #7480c2)
5353
textColor = nil,
54-
-- vim buffer-scoped options: any `vim.bo` options is accepted here.
54+
-- Vim buffer-scoped options: any `vim.bo` options is accepted here.
5555
bo = {
5656
filetype = "no-neck-pain",
5757
buftype = "nofile",
5858
bufhidden = "hide",
5959
buflisted = false,
6060
swapfile = false,
6161
},
62-
-- vim window-scoped options: any `vim.wo` options is accepted here.
62+
-- Vim window-scoped options: any `vim.wo` options is accepted here.
6363
wo = {
6464
cursorline = false,
6565
cursorcolumn = false,
@@ -126,15 +126,15 @@ Default values:
126126
blend = 0,
127127
-- Hexadecimal color code to override the current text color of the buffer. (e.g. #7480c2)
128128
textColor = nil,
129-
-- vim buffer-scoped options: any `vim.bo` options is accepted here.
129+
-- Vim buffer-scoped options: any `vim.bo` options is accepted here.
130130
bo = {
131131
filetype = "no-neck-pain",
132132
buftype = "nofile",
133133
bufhidden = "hide",
134134
buflisted = false,
135135
swapfile = false,
136136
},
137-
-- vim window-scoped options: any `vim.wo` options is accepted here.
137+
-- Vim window-scoped options: any `vim.wo` options is accepted here.
138138
wo = {
139139
cursorline = false,
140140
cursorcolumn = false,
@@ -152,7 +152,7 @@ Default values:
152152
--- See |NoNeckPain.bufferOptions|.
153153
right = NoNeckPain.bufferOptions,
154154
},
155-
-- lists supported integrations that might clash with `no-neck-pain.nvim`'s behavior
155+
-- Supported integrations that might clash with `no-neck-pain.nvim`'s behavior.
156156
integrations = {
157157
-- https://github.com/nvim-tree/nvim-tree.lua
158158
NvimTree = {

lua/no-neck-pain/config.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ NoNeckPain.bufferOptions = {
3434
blend = 0,
3535
-- Hexadecimal color code to override the current text color of the buffer. (e.g. #7480c2)
3636
textColor = nil,
37-
-- vim buffer-scoped options: any `vim.bo` options is accepted here.
37+
-- Vim buffer-scoped options: any `vim.bo` options is accepted here.
3838
bo = {
3939
filetype = "no-neck-pain",
4040
buftype = "nofile",
4141
bufhidden = "hide",
4242
buflisted = false,
4343
swapfile = false,
4444
},
45-
-- vim window-scoped options: any `vim.wo` options is accepted here.
45+
-- Vim window-scoped options: any `vim.wo` options is accepted here.
4646
wo = {
4747
cursorline = false,
4848
cursorcolumn = false,
@@ -104,15 +104,15 @@ NoNeckPain.options = {
104104
blend = 0,
105105
-- Hexadecimal color code to override the current text color of the buffer. (e.g. #7480c2)
106106
textColor = nil,
107-
-- vim buffer-scoped options: any `vim.bo` options is accepted here.
107+
-- Vim buffer-scoped options: any `vim.bo` options is accepted here.
108108
bo = {
109109
filetype = "no-neck-pain",
110110
buftype = "nofile",
111111
bufhidden = "hide",
112112
buflisted = false,
113113
swapfile = false,
114114
},
115-
-- vim window-scoped options: any `vim.wo` options is accepted here.
115+
-- Vim window-scoped options: any `vim.wo` options is accepted here.
116116
wo = {
117117
cursorline = false,
118118
cursorcolumn = false,
@@ -130,7 +130,7 @@ NoNeckPain.options = {
130130
--- See |NoNeckPain.bufferOptions|.
131131
right = NoNeckPain.bufferOptions,
132132
},
133-
-- lists supported integrations that might clash with `no-neck-pain.nvim`'s behavior
133+
-- Supported integrations that might clash with `no-neck-pain.nvim`'s behavior.
134134
integrations = {
135135
-- https://github.com/nvim-tree/nvim-tree.lua
136136
NvimTree = {

lua/no-neck-pain/main.lua

+19-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local D = require("no-neck-pain.util.debug")
22
local E = require("no-neck-pain.util.event")
33
local M = require("no-neck-pain.util.map")
44
local W = require("no-neck-pain.util.win")
5+
local T = require("no-neck-pain.util.trees")
56

67
local N = {}
78

@@ -35,7 +36,7 @@ local S = {
3536
-- Toggle the plugin by calling the `enable`/`disable` methods respectively.
3637
function N.toggle()
3738
if S.enabled then
38-
return N.disable()
39+
return N.disable("N.toggle")
3940
end
4041

4142
return N.enable()
@@ -50,7 +51,7 @@ local function init()
5051
end
5152

5253
-- before creating side buffers, we determine if we should consider externals
53-
S.win.external.trees = W.getSideTrees()
54+
S.win.external.trees = T.getSideTrees()
5455
S.win.main.left, S.win.main.right = W.createSideBuffers(S.win)
5556

5657
vim.fn.win_gotoid(S.win.main.curr)
@@ -71,7 +72,7 @@ function N.enable()
7172
vim.api.nvim_create_autocmd({ "VimResized" }, {
7273
callback = function(p)
7374
vim.schedule(function()
74-
if E.skip(S.enabled, nil) then
75+
if E.skip(S.enabled, S.win.main, nil) then
7576
return
7677
end
7778

@@ -91,25 +92,26 @@ function N.enable()
9192
end)
9293
end,
9394
group = "NoNeckPain",
94-
desc = "Resizes side windows after shell has been resized",
95+
desc = "Resizes side windows after terminal has been resized, closes them if not enough space left.",
9596
})
9697

9798
vim.api.nvim_create_autocmd({ "WinEnter" }, {
9899
callback = function(p)
99100
vim.schedule(function()
100-
if E.skip(S.enabled, nil) then
101+
if E.skip(S.enabled, S.win.main, nil) then
101102
return
102103
end
103104

104105
local focusedWin = vim.api.nvim_get_current_win()
105106
local buffers, total = W.listWinsExcept(S.win.main)
106107

107108
if total == 0 or not M.contains(buffers, focusedWin) then
108-
return D.log(p.event, "no valid buffers to handle, no split to handle")
109+
return D.log(p.event, "valid: %s - or no split to handle", total)
109110
end
110111

112+
-- we skip side trees etc. as they are not part of the split manager.
111113
local fileType = vim.api.nvim_buf_get_option(0, "filetype")
112-
if fileType == "NvimTree" or fileType == "undotree" then
114+
if T.isSideTree(fileType) then
113115
return D.log(p.event, "encountered an external window")
114116
end
115117

@@ -152,7 +154,7 @@ function N.enable()
152154
vim.api.nvim_create_autocmd({ "QuitPre", "BufDelete" }, {
153155
callback = function(p)
154156
vim.schedule(function()
155-
if E.skip(S.enabled, nil) then
157+
if E.skip(S.enabled, nil, nil) then
156158
return
157159
end
158160

@@ -184,7 +186,7 @@ function N.enable()
184186
then
185187
D.log(p.event, "found last `wipe` buffer in list, disabling...")
186188

187-
return N.disable()
189+
return N.disable(p.event)
188190
end
189191
end
190192
end)
@@ -196,7 +198,7 @@ function N.enable()
196198
vim.api.nvim_create_autocmd({ "WinClosed", "BufDelete" }, {
197199
callback = function(p)
198200
vim.schedule(function()
199-
if E.skip(S.enabled, nil) or S.win.main.split == nil then
201+
if E.skip(S.enabled, S.win.main, nil) or S.win.main.split == nil then
200202
return
201203
end
202204

@@ -251,17 +253,19 @@ function N.enable()
251253
vim.api.nvim_create_autocmd({ "WinEnter", "WinClosed" }, {
252254
callback = function(p)
253255
vim.schedule(function()
254-
if E.skip(S.enabled, S.win.split) then
256+
if E.skip(S.enabled, S.win.main, S.win.main.split) then
255257
return
256258
end
257259

258260
local wins = vim.api.nvim_list_wins()
259-
local trees = W.getSideTrees()
261+
local trees = T.getSideTrees()
260262

261263
-- we cycle over supported integrations to see which got closed or opened
262264
for name, tree in pairs(S.win.external.trees) do
263265
-- if there was a tree[name] but not anymore, we resize
264-
if tree.id ~= nil and not M.contains(wins, tree.id) then
266+
if tree ~= nil and tree.id ~= nil and not M.contains(wins, tree.id) then
267+
D.log(p.event, "%s have been closed, resizing", name)
268+
265269
S.win.external.trees[name] = {
266270
id = nil,
267271
width = 0,
@@ -272,6 +276,8 @@ function N.enable()
272276

273277
-- we have a new tree registered, we can resize
274278
if S.win.external.trees[name].id == nil and trees[name].id ~= nil then
279+
D.log(p.event, "%s have been opened, resizing", name)
280+
275281
S.win.external.trees = trees
276282
return W.resizeSideBuffers(p.event, S.win)
277283
end

lua/no-neck-pain/util/event.lua

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function E.abortEnable(state, filetype)
2121
end
2222

2323
-- determines if we should skip the event.
24-
function E.skip(enabled, split)
24+
function E.skip(enabled, main, split)
2525
if not enabled then
2626
return true
2727
end
@@ -30,6 +30,14 @@ function E.skip(enabled, split)
3030
return true
3131
end
3232

33+
if main ~= nil then
34+
local curr = vim.api.nvim_get_current_win()
35+
36+
if curr == main.left or curr == main.right then
37+
return true
38+
end
39+
end
40+
3341
return false
3442
end
3543

lua/no-neck-pain/util/trees.lua

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local T = {}
2+
3+
function T.isSideTree(fileType)
4+
return fileType == "NvimTree" or fileType == "undotree"
5+
end
6+
7+
function T.getSideTrees()
8+
local wins = vim.api.nvim_list_wins()
9+
local trees = {
10+
NvimTree = {
11+
id = nil,
12+
width = 0,
13+
},
14+
undotree = {
15+
id = nil,
16+
width = 0,
17+
},
18+
}
19+
20+
for _, win in pairs(wins) do
21+
local fileType = vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(win), "filetype")
22+
if T.isSideTree(fileType) then
23+
trees[fileType] = {
24+
id = win,
25+
width = vim.api.nvim_win_get_width(win) * 2,
26+
}
27+
end
28+
end
29+
30+
return trees
31+
end
32+
33+
return T

0 commit comments

Comments
 (0)