feat: added share command

This commit is contained in:
əlemi 2024-08-15 01:22:48 +02:00
parent 84afc0a864
commit e0a5752d11
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 24 additions and 3 deletions

View file

@ -16,11 +16,10 @@ local function delete(name)
print(" -- deleted buffer " .. name) print(" -- deleted buffer " .. name)
end end
local function attach(name, force) local function attach(name, current, content)
local buffer = nil local buffer = nil
if force then if current then
buffer = vim.api.nvim_get_current_buf() buffer = vim.api.nvim_get_current_buf()
utils.buffer.set_content(buffer, "")
else 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 })
@ -36,6 +35,10 @@ local function attach(name, force)
buffer_id_map[name] = buffer buffer_id_map[name] = buffer
ticks[buffer] = 0 ticks[buffer] = 0
if content ~= nil then
controller:send(0, 0, content)
end
-- hook serverbound callbacks -- hook serverbound callbacks
-- TODO breaks when deleting whole lines at buffer end -- TODO breaks when deleting whole lines at buffer end
vim.api.nvim_buf_attach(buffer, false, { vim.api.nvim_buf_attach(buffer, false, {
@ -77,6 +80,7 @@ local function attach(name, force)
end, 20) -- wait 20ms before polling again because it overwhelms libuv? end, 20) -- wait 20ms before polling again because it overwhelms libuv?
print(" ++ attached to buffer " .. name) print(" ++ attached to buffer " .. name)
return controller
end end
local function detach(name) local function detach(name)

View file

@ -1,6 +1,7 @@
local state = require('codemp.state') local state = require('codemp.state')
local buffers = require('codemp.buffers') local buffers = require('codemp.buffers')
local workspace = require('codemp.workspace') local workspace = require('codemp.workspace')
local utils = require('codemp.utils')
local native = require('codemp.loader').load() local native = require('codemp.loader').load()
@ -52,6 +53,22 @@ local joined_actions = {
buffers.create(path) buffers.create(path)
end, end,
share = function(path)
if path == nil then
local cwd = vim.fn.getcwd()
local full_path = vim.fn.expand("%:p")
path = string.gsub(full_path, cwd .. "/", "")
end
if #path > 0 then
local buf = vim.api.nvim_get_current_buf()
buffers.create(path)
local content = utils.buffer.get_content(buf)
buffers.attach(path, true, content)
else
print(" !! empty path or open a file")
end
end,
delete = function(path) delete = function(path)
if path == nil then error("missing buffer name") end if path == nil then error("missing buffer name") end
buffers.delete(path) buffers.delete(path)