Skip to content

Commit

Permalink
Fix buffer.lua bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Oct 13, 2023
1 parent f08a676 commit eedf0ac
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
9 changes: 6 additions & 3 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.workspace.checkThirdParty": false
}
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"workspace.library": [
"${3rd}/luv/library",
"${3rd}/luassert/library"
]
}
36 changes: 23 additions & 13 deletions lua/smoothcursor/callbacks/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
local sc_debug = require('smoothcursor.debug')

-- #require('smoothcursor.callbacks.buffer').buffer

local function create_buffer()
return {
buffer = {},
length = 1,
bufnr = 1, -- cache bufnr
length = 0,
bufnr = 0, -- cache bufnr
indexes = {},

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

resize_buffer = function(self, size)
local diff = size - self.length
local diff = size - #self.indexes
if diff < 0 then
for _ = 1, -diff, 1 do
table.remove(self, self.length + 1)
table.remove(self.indexes, #self.indexes)
end
else
for _ = 1, diff, 1 do
table.insert(self, 1, 0)
table.insert(self.indexes, 1, 0)
end
end
self.length = size
self.length = #self.indexes
end,

switch_buf = function(self)
Expand All @@ -31,15 +34,15 @@ local function create_buffer()
end,

all = function(self, value)
for i = 1, self.length, 1 do
self[i] = value
for i = 1, #self.indexes, 1 do
self.indexes[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
local first_val = self.indexes[1]
for i = 2, #self.indexes do
if first_val ~= self.indexes[i] then
return false
end
end
Expand All @@ -51,12 +54,19 @@ end
local function buffer_metatable()
return {
__index = function(t, k)
if t.indexes[k] ~= nil then
return t.indexes[k]
end
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.indexes[k] ~= nil then
t.indexes[k] = v
return
end
if t.buffer[t.bufnr] == nil then
t.buffer[t.bufnr] = {}
end
Expand Down
8 changes: 5 additions & 3 deletions lua/smoothcursor/callbacks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ local function init()
end
end

---@param value integer | nil
---@param value? integer
local function buffer_set_all(value)
if value == nil then
value = vim.fn.getcurpos(vim.fn.win_getid())[2]
end
buffer['prev'] = value
buffer:all(value)

-- Debug
debug_callback(buffer, { 'Buffer Reset' }, function()
sc_debug.reset_counter = sc_debug.reset_counter + 1
end)
end

--- @param with_timer_stop? boolean
---@param with_timer_stop? boolean
local function unplace_signs(with_timer_stop)
if with_timer_stop == true then
sc_timer:abort()
Expand All @@ -36,7 +38,7 @@ end

-- place 'name' sign to the 'position'
---@param position number
---@param name string
---@param name? string
local function place_sign(position, name)
position = math.floor(position + 0.5)
if position < buffer['w0'] or position > buffer['w$'] then
Expand Down
2 changes: 1 addition & 1 deletion lua/smoothcursor/callbacks/timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function ScTimer:post(func)
if self.is_running then
return
end
vim.loop.timer_start(self.timer, 0, config.config.intervals, vim.schedule_wrap(func))
vim.uv.timer_start(self.timer, 0, config.config.intervals, vim.schedule_wrap(func))
self.is_running = true
end

Expand Down
2 changes: 2 additions & 0 deletions plugin/smoothcursor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ vim.api.nvim_create_user_command('SmoothCursorStatus', function()
end, {})

vim.api.nvim_create_user_command('SmoothCursorDeleteSigns', utils.smoothcursor_delete_signs, {})

vim.api.nvim_create_user_command('SmoothCursorDebug', require('smoothcursor.debug').debug, {})

0 comments on commit eedf0ac

Please sign in to comment.