Skip to content

Commit

Permalink
refactor: use on_key instead of MouseMove
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Jun 29, 2023
1 parent d9894b7 commit 6aa74e7
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions lua/cokeline/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,52 +217,51 @@ local function on_drag(pos)
end

function M.setup()
if version.minor < 8 then
if version.minor < 8 or not vim.on_key then
return
end

vim.api.nvim_create_autocmd("MouseMove", {
callback = function(ev)
if ev.data.dragging then
local hov = M.hovered()
if hov ~= nil then
local buf = buffers.get_buffer(hov.bufnr)
if buf then
buf.is_hovered = false
end
if hov.kind == "buffer" then
if buf and hov.on_mouse_leave then
hov.on_mouse_leave(buf)
end
elseif hov.on_mouse_leave then
hov.on_mouse_leave()
local mouse_move = vim.keycode("<MouseMove>")
local drag = {
[vim.keycode("<LeftDrag>")] = "l",
[vim.keycode("<RightDrag>")] = "r",
[vim.keycode("<MiddleDrag>")] = "m",
}
vim.on_key(function(key)
local pos = vim.fn.getmousepos()

local data = {
dragging = drag[key],
screencol = pos.screencol,
screenrow = pos.screenrow,
line = pos.line,
column = pos.column,
winid = pos.winid,
winrow = pos.winrow,
wincol = pos.wincol,
}
if key == mouse_move then
_G.cokeline.__dragging = nil
on_hover(data)
elseif drag[key] then
local hov = M.hovered()
if hov ~= nil then
local buf = buffers.get_buffer(hov.bufnr)
if buf then
buf.is_hovered = false
end
if hov.kind == "buffer" then
if buf and hov.on_mouse_leave then
hov.on_mouse_leave(buf)
end
_G.cokeline.__hovered = nil
elseif hov.on_mouse_leave then
hov.on_mouse_leave()
end
on_drag(ev.data)
else
_G.cokeline.__dragging = nil
on_hover(ev.data)
_G.cokeline.__hovered = nil
end
return "<MouseMove>"
end,
})

-- vim.keymap.set({ "", "i" }, "<MouseMove>", function()
-- local ok, pos = pcall(vim.fn.getmousepos)
-- if ok then
-- on_hover(pos)
-- end
-- return "<MouseMove>"
-- end, { expr = true, noremap = true })
-- vim.keymap.set({ "", "i" }, "<LeftDrag>", function()
-- local ok, pos = pcall(vim.fn.getmousepos)
-- if ok then
-- pos.dragging = "l"
-- on_drag(pos)
-- end
-- return "<MouseMove>"
-- end, { expr = true, noremap = true })
on_drag(data)
end
end)
end

return M

0 comments on commit 6aa74e7

Please sign in to comment.