Skip to content

Commit e0a5752

Browse files
committed
feat: added share command
1 parent 84afc0a commit e0a5752

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/buffers.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ local function delete(name)
1616
print(" -- deleted buffer " .. name)
1717
end
1818

19-
local function attach(name, force)
19+
local function attach(name, current, content)
2020
local buffer = nil
21-
if force then
21+
if current then
2222
buffer = vim.api.nvim_get_current_buf()
23-
utils.buffer.set_content(buffer, "")
2423
else
2524
buffer = vim.api.nvim_create_buf(true, true)
2625
vim.api.nvim_set_option_value('fileformat', 'unix', { buf = buffer })
@@ -36,6 +35,10 @@ local function attach(name, force)
3635
buffer_id_map[name] = buffer
3736
ticks[buffer] = 0
3837

38+
if content ~= nil then
39+
controller:send(0, 0, content)
40+
end
41+
3942
-- hook serverbound callbacks
4043
-- TODO breaks when deleting whole lines at buffer end
4144
vim.api.nvim_buf_attach(buffer, false, {
@@ -77,6 +80,7 @@ local function attach(name, force)
7780
end, 20) -- wait 20ms before polling again because it overwhelms libuv?
7881

7982
print(" ++ attached to buffer " .. name)
83+
return controller
8084
end
8185

8286
local function detach(name)

src/command.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local state = require('codemp.state')
22
local buffers = require('codemp.buffers')
33
local workspace = require('codemp.workspace')
4+
local utils = require('codemp.utils')
45

56
local native = require('codemp.loader').load()
67

@@ -52,6 +53,22 @@ local joined_actions = {
5253
buffers.create(path)
5354
end,
5455

56+
share = function(path)
57+
if path == nil then
58+
local cwd = vim.fn.getcwd()
59+
local full_path = vim.fn.expand("%:p")
60+
path = string.gsub(full_path, cwd .. "/", "")
61+
end
62+
if #path > 0 then
63+
local buf = vim.api.nvim_get_current_buf()
64+
buffers.create(path)
65+
local content = utils.buffer.get_content(buf)
66+
buffers.attach(path, true, content)
67+
else
68+
print(" !! empty path or open a file")
69+
end
70+
end,
71+
5572
delete = function(path)
5673
if path == nil then error("missing buffer name") end
5774
buffers.delete(path)

0 commit comments

Comments
 (0)