Skip to content

Commit

Permalink
Refactoring callbacks.buffer.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Oct 13, 2023
1 parent 2d64b40 commit f08a676
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 90 deletions.
125 changes: 64 additions & 61 deletions lua/smoothcursor/callbacks/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
local sc_debug = require('smoothcursor.debug')

return {
buffer = (function()
local obj = {
buffer = {},
length = 1,
bufnr = 1, -- chach bufnr
push_front = function(self, data)
table.insert(self, 1, data)
table.remove(self, self.length + 1)
end,
--- Resize the buffer size
--- @param size number
resize_buffer = function(self, size)
if self.length == size then
return
end
local diff = size - self.length
if diff < 0 then
for _ = 1, -diff, 1 do
table.remove(self, self.length + 1)
end
else
for _ = 1, diff, 1 do
table.insert(self, 1, 0)
end
end
self.length = size
end,
switch_buf = function(self)
self.bufnr = vim.fn.bufnr()
sc_debug.buf_switch_counter = sc_debug.buf_switch_counter + 1
end,
---@param value number
all = function(self, value)
for i = 1, self.length, 1 do
self[i] = value
end
end,
--- check if
is_stay_still = function(self)
local first_val = self[1]
for i = 2, self.length do
if first_val ~= self[i] then
return false
end
local function create_buffer()
return {
buffer = {},
length = 1,
bufnr = 1, -- cache bufnr

push_front = function(self, data)
table.insert(self, 1, data)
table.remove(self, self.length + 1)
end,

resize_buffer = function(self, size)
local diff = size - self.length
if diff < 0 then
for _ = 1, -diff, 1 do
table.remove(self, self.length + 1)
end
return true
end,
}
return setmetatable(obj, {
__index = function(t, k)
if t.buffer[t.bufnr] == nil then
t.buffer[t.bufnr] = {}
else
for _ = 1, diff, 1 do
table.insert(self, 1, 0)
end
return t.buffer[t.bufnr][k]
end,
__newindex = function(t, k, v)
if t.buffer[t.bufnr] == nil then
t.buffer[t.bufnr] = {}
end
self.length = size
end,

switch_buf = function(self)
self.bufnr = vim.fn.bufnr()
sc_debug.buf_switch_counter = sc_debug.buf_switch_counter + 1
end,

all = function(self, value)
for i = 1, self.length, 1 do
self[i] = value
end
end,

is_stay_still = function(self)
local first_val = self[1]
for i = 2, self.length do
if first_val ~= self[i] then
return false
end
t.buffer[t.bufnr][k] = v
end,
})
end)(),
end
return true
end,
}
end

local function buffer_metatable()
return {
__index = function(t, k)
if t.buffer[t.bufnr] == nil then
t.buffer[t.bufnr] = {}
end
return t.buffer[t.bufnr][k]
end,
__newindex = function(t, k, v)
if t.buffer[t.bufnr] == nil then
t.buffer[t.bufnr] = {}
end
t.buffer[t.bufnr][k] = v
end,
}
end

return {
buffer = setmetatable(create_buffer(), buffer_metatable()),
}
31 changes: 3 additions & 28 deletions lua/smoothcursor/debug.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local is_debug_mode = false
---@type integer | nil
local debug_bufid = nil

local sc = {}
Expand All @@ -17,24 +17,6 @@ function sc.debug()
vim.opt_local.buflisted = false
end)
vim.cmd([[wincmd p]])
is_debug_mode = true
end

local function dump(o, level)
local tab = ' '
level = level or 0
if type(o) == 'table' then
local s = '{\n'
for k, v in pairs(o) do
s = s .. string.rep(tab, level + 1) .. tostring(k) .. ' = ' .. dump(v, level + 1) .. ',\n'
end
return s .. string.rep(tab, level) .. '}'
else
if type(o) == 'string' then
return string.format('"%s"', o)
end
return tostring(o)
end
end

local counter = 0
Expand All @@ -43,20 +25,13 @@ sc.buf_switch_counter = 0
sc.unplace_signs_conuter = 0

function sc.debug_callback(obj, extrainfo, extrafunc)
if not is_debug_mode then
if not debug_bufid then
return
end
extrainfo = extrainfo or {}
extrafunc = extrafunc or function() end
counter = counter + 1
vim.api.nvim_buf_set_lines(debug_bufid, 0, 1000, false, vim.split(dump(obj), '\n'))
-- vim.api.nvim_buf_set_lines(
-- debug_bufid,
-- 0,
-- 1000,
-- false,
-- vim.split(dump(require('smoothcursor.config').default_args), '\n')
-- )
vim.api.nvim_buf_set_lines(debug_bufid, 0, 1000, false, vim.split(vim.inspect(obj), '\n'))
vim.api.nvim_buf_set_lines(debug_bufid, 0, 0, false, extrainfo)
vim.api.nvim_buf_set_lines(
debug_bufid,
Expand Down
15 changes: 14 additions & 1 deletion plugin/smoothcursor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@
local utils = require('smoothcursor.utils')

vim.api.nvim_create_user_command('SmoothCursorStart', utils.smoothcursor_start, {})

vim.api.nvim_create_user_command('SmoothCursorStop', function(args)
if #args.fargs > 1 then
vim.notify("Too many arguments. '--keep-signs' or empty are allowed.", vim.log.levels.ERROR)
return
end
if args.fargs[1] == nil then
utils.smoothcursor_stop()
elseif args.fargs[1] == '--keep-signs' then
utils.smoothcursor_stop(false)
else
vim.notify([[bad argument, "--keep-signs" or empty]], vim.log.levels.ERROR)
end
end, { nargs = '*' })
end, {
nargs = '*',
complete = function()
return { '--keep-signs' }
end,
})

vim.api.nvim_create_user_command('SmoothCursorToggle', utils.smoothcursor_toggle, {})

vim.api.nvim_create_user_command('SmoothCursorFancyOn', utils.smoothcursor_fancy_on, {})

vim.api.nvim_create_user_command('SmoothCursorFancyOff', utils.smoothcursor_fancy_off, {})

vim.api.nvim_create_user_command('SmoothCursorFancyToggle', utils.smoothcursor_fancy_toggle, {})

vim.api.nvim_create_user_command('SmoothCursorStatus', function()
Expand Down

0 comments on commit f08a676

Please sign in to comment.