Skip to content

Commit d9894b7

Browse files
committed
refactor: clean up on_drag function
1 parent 0790a89 commit d9894b7

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

lua/cokeline/hover.lua

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ function M.hovered()
1010
return _G.cokeline.__hovered
1111
end
1212

13+
function M.set_hovered(val)
14+
_G.cokeline.__hovered = val
15+
end
16+
17+
function M.clear_hovered()
18+
_G.cokeline.__hovered = nil
19+
end
20+
21+
function M.dragging()
22+
return _G.cokeline.__dragging
23+
end
24+
25+
function M.set_dragging(val)
26+
_G.cokeline.__dragging = val
27+
end
28+
29+
function M.clear_dragging()
30+
_G.cokeline.__dragging = nil
31+
end
32+
1333
function M.get_current(col)
1434
local bufs = buffers.get_visible()
1535
if not bufs then
@@ -43,7 +63,7 @@ function M.get_current(col)
4363
end
4464

4565
local function on_hover(current)
46-
local hovered = _G.cokeline.__hovered
66+
local hovered = M.hovered()
4767
if vim.o.showtabline == 0 then
4868
return
4969
end
@@ -80,7 +100,7 @@ local function on_hover(current)
80100
hovered.on_mouse_leave(buf)
81101
end
82102
end
83-
_G.cokeline.__hovered = nil
103+
M.clear_hovered()
84104
end
85105
if not component then
86106
vim.cmd.redrawtabline()
@@ -100,12 +120,12 @@ local function on_hover(current)
100120
component.on_mouse_enter(buf, current.screencol)
101121
end
102122
end
103-
_G.cokeline.__hovered = {
123+
M.set_hovered({
104124
index = component.index,
105125
bufnr = buf and buf.number or nil,
106126
on_mouse_leave = component.on_mouse_leave,
107127
kind = component.kind,
108-
}
128+
})
109129
vim.cmd.redrawtabline()
110130
elseif hovered ~= nil then
111131
local buf = buffers.get_buffer(hovered.bufnr)
@@ -121,7 +141,7 @@ local function on_hover(current)
121141
hovered.on_mouse_leave(buf)
122142
end
123143
end
124-
_G.cokeline.__hovered = nil
144+
M.clear_hovered()
125145
vim.cmd.redrawtabline()
126146
end
127147
last_position = current
@@ -155,41 +175,43 @@ end
155175
local function on_drag(pos)
156176
if pos.dragging == "l" then
157177
local current, bufs = M.get_current(pos.screencol)
158-
if not current or current.kind ~= "buffer" or not bufs then
178+
if current == nil or bufs == nil or current.kind ~= "buffer" then
159179
return
160180
end
161181

162-
if not _G.cokeline.__dragging then
163-
local buf = buffers.get_buffer(current.bufnr)
164-
if not buf then
165-
return
166-
end
167-
_G.cokeline.__dragging = current.bufnr
182+
-- if we're not dragging yet or we're dragging the same buffer, start dragging
183+
if M.dragging() == current.bufnr or not M.dragging() then
184+
M.set_dragging(current.bufnr)
185+
return
186+
end
187+
188+
-- dragged buffer
189+
local dragged_buf = buffers.get_buffer(_G.cokeline.__dragging)
190+
if not dragged_buf then
191+
return
168192
end
169-
if _G.cokeline.__dragging == current.bufnr then
193+
194+
-- current (hovered) buffer
195+
local cur_buf = buffers.get_buffer(current.bufnr)
196+
if not cur_buf then
170197
return
171198
end
172-
if _G.cokeline.__dragging then
173-
local buf = buffers.get_buffer(_G.cokeline.__dragging)
174-
local dragging_start = start_pos(bufs, _G.cokeline.__dragging)
175-
local cur_buf_width = width(bufs, current.bufnr)
176-
if buf then
177-
if dragging_start > pos.screencol then
178-
if pos.screencol + cur_buf_width > dragging_start then
179-
buffers.move_buffer(
180-
buf,
181-
buffers.get_buffer(current.bufnr)._valid_index
182-
)
183-
end
184-
else
185-
if dragging_start + cur_buf_width <= pos.screencol then
186-
buffers.move_buffer(
187-
buf,
188-
buffers.get_buffer(current.bufnr)._valid_index
189-
)
190-
end
191-
end
192-
end
199+
200+
-- start position of dragged buffer
201+
local dragging_start = start_pos(bufs, _G.cokeline.__dragging)
202+
-- width of the current (hovered) buffer
203+
local cur_buf_width = width(bufs, current.bufnr)
204+
205+
if
206+
-- buffer is being dragged to the left
207+
(
208+
dragging_start > pos.screencol
209+
and pos.screencol + cur_buf_width > dragging_start
210+
)
211+
-- buffer is being dragged to the right
212+
or dragging_start + cur_buf_width <= pos.screencol
213+
then
214+
buffers.move_buffer(dragged_buf, cur_buf._valid_index)
193215
end
194216
end
195217
end

0 commit comments

Comments
 (0)