mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 07:24:52 +01:00
feat: prompt if overwrite buf, better attach fn
This commit is contained in:
parent
faa68b235c
commit
b3315dff9b
3 changed files with 39 additions and 16 deletions
|
@ -8,19 +8,38 @@ local buffer_id_map = {}
|
||||||
local user_buffer_name = {}
|
local user_buffer_name = {}
|
||||||
local ticks = {}
|
local ticks = {}
|
||||||
|
|
||||||
|
---@class AttachOptions
|
||||||
|
---@field content? string if provided, set this as content after attaching
|
||||||
|
---@field nowait? boolean skip waiting for initial content sync
|
||||||
|
---@field window? integer if given, open attached buffer in this window
|
||||||
|
---@field buffer? integer if given, use provided buffer instead of creating a new one
|
||||||
|
---@field skip_exists_check? boolean skip vim.fn.bufexists check, used for recursive call
|
||||||
|
---
|
||||||
---@param name string name of buffer to attach to
|
---@param name string name of buffer to attach to
|
||||||
---@param buffer? integer buffer to use for attaching (will clear content)
|
---@param opts AttachOptions options for attaching
|
||||||
---@param content? string if provided, set this content after attaching
|
local function attach(name, opts)
|
||||||
---@param nowait? boolean skip waiting for initial content sync
|
if not opts.skip_exists_check and vim.fn.bufexists(name) == 1 then
|
||||||
local function attach(name, buffer, content, nowait)
|
vim.ui.select(
|
||||||
if vim.fn.bufexists(name) == 1 then
|
{ "download content", "upload content" },
|
||||||
error("buffer '" .. name .. "' already exists!")
|
{ prompt = "buffer is already open" },
|
||||||
|
function (choice)
|
||||||
|
if choice == nil then return end
|
||||||
|
opts.buffer = vim.fn.bufnr(name)
|
||||||
|
opts.skip_exists_check = true
|
||||||
|
if choice == "upload content" then
|
||||||
|
opts.content = utils.buffer.get_content(opts.buffer)
|
||||||
|
end
|
||||||
|
attach(name, opts)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if buffer_id_map[name] ~= nil then
|
if buffer_id_map[name] ~= nil then
|
||||||
error("already attached to buffer " .. name)
|
return print(" !! already attached to buffer " .. name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local buffer = opts.buffer
|
||||||
if buffer == nil then
|
if buffer == nil then
|
||||||
buffer = vim.api.nvim_get_current_buf()
|
buffer = vim.api.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
|
@ -29,7 +48,7 @@ local function attach(name, buffer, content, nowait)
|
||||||
vim.api.nvim_buf_set_name(buffer, name)
|
vim.api.nvim_buf_set_name(buffer, name)
|
||||||
CODEMP.workspace:attach(name):and_then(function (controller)
|
CODEMP.workspace:attach(name):and_then(function (controller)
|
||||||
-- TODO disgusting! but poll blocks forever on empty buffers...
|
-- TODO disgusting! but poll blocks forever on empty buffers...
|
||||||
if not nowait then
|
if not opts.nowait then
|
||||||
local promise = controller:poll()
|
local promise = controller:poll()
|
||||||
for i=1, 20, 1 do
|
for i=1, 20, 1 do
|
||||||
if promise.ready then break end
|
if promise.ready then break end
|
||||||
|
@ -37,6 +56,11 @@ local function attach(name, buffer, content, nowait)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if opts.window ~= nil then
|
||||||
|
vim.api.nvim_win_set_buf(opts.window, buffer)
|
||||||
|
vim.api.nvim_set_current_win(opts.window)
|
||||||
|
end
|
||||||
|
|
||||||
-- TODO map name to uuid
|
-- TODO map name to uuid
|
||||||
|
|
||||||
id_buffer_map[buffer] = name
|
id_buffer_map[buffer] = name
|
||||||
|
@ -115,10 +139,10 @@ local function attach(name, buffer, content, nowait)
|
||||||
end))
|
end))
|
||||||
|
|
||||||
local remote_content = controller:content():await()
|
local remote_content = controller:content():await()
|
||||||
if content ~= nil then
|
if opts.content ~= nil then
|
||||||
-- TODO this may happen too soon!!
|
-- TODO this may happen too soon!!
|
||||||
local _ = controller:send({
|
local _ = controller:send({
|
||||||
start = 0, finish = #remote_content, content = content
|
start = 0, finish = #remote_content, content = opts.content
|
||||||
}) -- no need to await
|
}) -- no need to await
|
||||||
else
|
else
|
||||||
local current_content = utils.buffer.get_content(buffer)
|
local current_content = utils.buffer.get_content(buffer)
|
||||||
|
|
|
@ -136,7 +136,7 @@ local joined_actions = {
|
||||||
buffer = vim.api.nvim_create_buf(true, false)
|
buffer = vim.api.nvim_create_buf(true, false)
|
||||||
vim.api.nvim_set_current_buf(buffer)
|
vim.api.nvim_set_current_buf(buffer)
|
||||||
end
|
end
|
||||||
buffers.attach(p, buffer)
|
buffers.attach(p, { buffer = buffer })
|
||||||
end
|
end
|
||||||
if path == nil then
|
if path == nil then
|
||||||
local filetree = CODEMP.workspace:filetree(nil, false)
|
local filetree = CODEMP.workspace:filetree(nil, false)
|
||||||
|
|
|
@ -42,14 +42,13 @@ M.open = function(state, path, extra)
|
||||||
end
|
end
|
||||||
if selected.type == "buffer" then
|
if selected.type == "buffer" then
|
||||||
local window = utils.get_appropriate_window(state)
|
local window = utils.get_appropriate_window(state)
|
||||||
vim.api.nvim_set_current_win(window)
|
|
||||||
if buf_manager.map_rev[selected.name] ~= nil then
|
if buf_manager.map_rev[selected.name] ~= nil then
|
||||||
|
vim.api.nvim_set_current_win(window)
|
||||||
vim.api.nvim_win_set_buf(window, buf_manager.map_rev[selected.name])
|
vim.api.nvim_win_set_buf(window, buf_manager.map_rev[selected.name])
|
||||||
return
|
else
|
||||||
|
local buf = vim.api.nvim_create_buf(true, false)
|
||||||
|
buf_manager.attach(selected.name, { buffer = buf, window = window })
|
||||||
end
|
end
|
||||||
local buf = vim.api.nvim_create_buf(true, false)
|
|
||||||
vim.api.nvim_win_set_buf(window, buf)
|
|
||||||
buf_manager.attach(selected.name, buf)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if selected.type == "user" then
|
if selected.type == "user" then
|
||||||
|
|
Loading…
Reference in a new issue