mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 15:34:53 +01:00
feat: better/safer buffer helpers
This commit is contained in:
parent
83c2a2c7e4
commit
3e26103064
1 changed files with 18 additions and 24 deletions
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue