diff --git a/src/buffers.lua b/src/buffers.lua index fcc62c1..d2588e6 100644 --- a/src/buffers.lua +++ b/src/buffers.lua @@ -16,11 +16,10 @@ local function delete(name) print(" -- deleted buffer " .. name) end -local function attach(name, force) +local function attach(name, current, content) local buffer = nil - if force then + if current then buffer = vim.api.nvim_get_current_buf() - utils.buffer.set_content(buffer, "") else buffer = vim.api.nvim_create_buf(true, true) vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer }) @@ -36,6 +35,10 @@ local function attach(name, force) buffer_id_map[name] = buffer ticks[buffer] = 0 + if content ~= nil then + controller:send(0, 0, content) + end + -- hook serverbound callbacks -- TODO breaks when deleting whole lines at buffer end 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? print(" ++ attached to buffer " .. name) + return controller end local function detach(name) diff --git a/src/command.lua b/src/command.lua index e5825ce..eb04b9b 100644 --- a/src/command.lua +++ b/src/command.lua @@ -1,6 +1,7 @@ local state = require('codemp.state') local buffers = require('codemp.buffers') local workspace = require('codemp.workspace') +local utils = require('codemp.utils') local native = require('codemp.loader').load() @@ -52,6 +53,22 @@ local joined_actions = { buffers.create(path) 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) if path == nil then error("missing buffer name") end buffers.delete(path)