feat: better/safer buffer helpers

This commit is contained in:
əlemi 2024-08-24 01:57:42 +02:00
parent 83c2a2c7e4
commit 3e26103064
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,31 +1,23 @@
local utils = require('codemp.utils') local utils = require('codemp.utils')
local session = require('codemp.session') local session = require('codemp.session')
---@type table<integer, string>
local id_buffer_map = {} local id_buffer_map = {}
---@type table<string, integer>
local buffer_id_map = {} local buffer_id_map = {}
---@type table<string, string>
local user_buffer_name = {} local user_buffer_name = {}
local ticks = {} local ticks = {}
---@param name string ---@param name string name of buffer to attach to
local function create(name) ---@param buffer? integer if provided, use given buffer (will clear content)
state.workspace:create_buffer(name):await() ---@param content? string if provided, set this content after attaching
print(" ++ created buffer '" .. name .. "' on " .. state.workspace.name) ---@return BufferController
local function attach(name, buffer, content)
if buffer_id_map[name] ~= nil then
error("already attached to buffer " .. name)
end end
if buffer == nil then
---@param name string
local function delete(name)
state.workspace:delete_buffer(name):await()
print(" -- deleted buffer " .. name)
end
---@param name string
---@param current boolean
---@param content string
local function attach(name, current, content)
local buffer = nil
if current ~= nil then
buffer = vim.api.nvim_get_current_buf()
else
buffer = vim.api.nvim_create_buf(true, true) buffer = vim.api.nvim_create_buf(true, true)
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer }) vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
-- vim.api.nvim_buf_set_option(buffer, 'filetype', 'codemp') -- TODO get from codemp? -- vim.api.nvim_buf_set_option(buffer, 'filetype', 'codemp') -- TODO get from codemp?
@ -98,6 +90,7 @@ local function attach(name, current, content)
end end
---@param name string ---@param name string
--TODO this should happen at the level above (Workspace) but accesses tables on this level, badly designed!
local function detach(name) local function detach(name)
local buffer = buffer_id_map[name] local buffer = buffer_id_map[name]
id_buffer_map[buffer] = nil id_buffer_map[buffer] = nil
@ -108,8 +101,11 @@ local function detach(name)
print(" -- detached from buffer " .. name) print(" -- detached from buffer " .. name)
end end
local function sync() ---@param buffer? integer if provided, sync given buffer id, otherwise sync current buf
local buffer = vim.api.nvim_get_current_buf() local function sync(buffer)
if buffer == nil then
buffer = vim.api.nvim_get_current_buf()
end
local name = id_buffer_map[buffer] local name = id_buffer_map[buffer]
if name ~= nil then if name ~= nil then
local controller = session.workspace:get_buffer(name) local controller = session.workspace:get_buffer(name)
@ -126,8 +122,6 @@ end
return { return {
create = create,
delete = delete,
sync = sync, sync = sync,
attach = attach, attach = attach,
detach = detach, detach = detach,