@@ -10,6 +10,26 @@ function M.hovered()
10
10
return _G .cokeline .__hovered
11
11
end
12
12
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
+
13
33
function M .get_current (col )
14
34
local bufs = buffers .get_visible ()
15
35
if not bufs then
@@ -43,7 +63,7 @@ function M.get_current(col)
43
63
end
44
64
45
65
local function on_hover (current )
46
- local hovered = _G . cokeline . __hovered
66
+ local hovered = M . hovered ()
47
67
if vim .o .showtabline == 0 then
48
68
return
49
69
end
@@ -80,7 +100,7 @@ local function on_hover(current)
80
100
hovered .on_mouse_leave (buf )
81
101
end
82
102
end
83
- _G . cokeline . __hovered = nil
103
+ M . clear_hovered ()
84
104
end
85
105
if not component then
86
106
vim .cmd .redrawtabline ()
@@ -100,12 +120,12 @@ local function on_hover(current)
100
120
component .on_mouse_enter (buf , current .screencol )
101
121
end
102
122
end
103
- _G . cokeline . __hovered = {
123
+ M . set_hovered ( {
104
124
index = component .index ,
105
125
bufnr = buf and buf .number or nil ,
106
126
on_mouse_leave = component .on_mouse_leave ,
107
127
kind = component .kind ,
108
- }
128
+ })
109
129
vim .cmd .redrawtabline ()
110
130
elseif hovered ~= nil then
111
131
local buf = buffers .get_buffer (hovered .bufnr )
@@ -121,7 +141,7 @@ local function on_hover(current)
121
141
hovered .on_mouse_leave (buf )
122
142
end
123
143
end
124
- _G . cokeline . __hovered = nil
144
+ M . clear_hovered ()
125
145
vim .cmd .redrawtabline ()
126
146
end
127
147
last_position = current
@@ -155,41 +175,43 @@ end
155
175
local function on_drag (pos )
156
176
if pos .dragging == " l" then
157
177
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
159
179
return
160
180
end
161
181
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
168
192
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
170
197
return
171
198
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 )
193
215
end
194
216
end
195
217
end
0 commit comments