codemp-nvim/lua/codemp/neo-tree/commands.lua

151 lines
5.1 KiB
Lua
Raw Normal View History

local cc = require("neo-tree.sources.common.commands")
local utils = require("neo-tree.utils")
2024-09-15 12:43:59 +02:00
local codemp_utils = require("codemp.utils")
local manager = require("neo-tree.sources.manager")
local session = require("codemp.session")
local buf_manager = require("codemp.buffers")
local ws_manager = require("codemp.workspace")
local client_manager = require("codemp.client")
local M = {}
2024-09-07 05:03:34 +02:00
local function toggle(node)
if node:is_expanded() then
node:collapse()
else
node:expand()
end
2024-09-07 05:10:58 +02:00
manager.refresh("codemp")
2024-09-07 05:03:34 +02:00
end
M.refresh = require("neo-tree.utils").wrap(manager.refresh, "codemp")
M.open = function(state, path, extra)
local selected = state.tree:get_node()
if selected.type == "spacer" then return end
2024-09-07 04:05:29 +02:00
if selected.type == "title" then return end
2024-09-07 05:03:34 +02:00
if selected.type == "entry" then return end
2024-09-07 05:07:06 +02:00
if selected.type == "root" then return toggle(selected) end
2024-09-07 04:26:21 +02:00
if selected.type == "button" then
2024-09-07 04:05:29 +02:00
if selected.name == "[connect]" and session.client == nil then
client_manager.connect()
end
return
end
if selected.type == "workspace" then
2024-09-07 05:03:34 +02:00
if session.workspace ~= nil and session.workspace.name ~= selected.name then
error("must leave current workspace first")
end
2024-09-07 05:03:34 +02:00
if session.workspace == nil then
ws_manager.join(selected.name)
end
manager.refresh("codemp")
return
end
if selected.type == "buffer" then
local window = utils.get_appropriate_window(state)
vim.api.nvim_set_current_win(window)
if buf_manager.map_rev[selected.name] ~= nil then
vim.api.nvim_win_set_buf(window, buf_manager.map_rev[selected.name])
return
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
end
if selected.type == "user" then
print("another remote user")
return
end
error("unrecognized node type")
end
M.move = function(state, path, extra)
local selected = state.tree:get_node()
if selected.type == "buffer" then
2024-09-15 12:43:59 +02:00
return vim.ui.input({ prompt = "move content into open buffer?" }, function (input)
if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end
local window = utils.get_appropriate_window(state)
local buf = vim.api.nvim_win_get_buf(window)
buf_manager.attach(selected.name, buf)
end)
end
error("only buffers can be moved to current file")
end
2024-09-15 12:43:59 +02:00
M.copy = function(state, path, extra)
local selected = state.tree:get_node()
if selected.type == "buffer" then
return vim.ui.input({ prompt = "copy content to remote buffer?" }, function (input)
if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end
local window = utils.get_appropriate_window(state)
local buf = vim.api.nvim_win_get_buf(window)
local content = codemp_utils.buffer.get_content(buf)
buf_manager.attach(selected.name, buf, content)
end)
end
error("current file can only be copied into buffers")
end
2024-09-07 01:31:04 +02:00
M.delete = function(state, path, extra)
local selected = state.tree:get_node()
2024-09-07 05:03:34 +02:00
if selected.type == "root" and vim.startswith(selected.name, "#") then
vim.ui.input({ prompt = "disconnect from workspace?" }, function (input)
if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end
2024-09-07 05:03:34 +02:00
ws_manager.leave()
manager.refresh("codemp")
end)
elseif selected.type == "buffer" then
2024-09-07 01:31:04 +02:00
if session.workspace == nil then error("join a workspace first") end
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end
session.workspace:delete_buffer(selected.name):await()
print("deleted buffer " .. selected.name)
manager.refresh("codemp")
end)
2024-09-07 01:31:04 +02:00
elseif selected.type == "workspace" then
if session.client == nil then error("connect to server first") end
vim.ui.input({ prompt = "delete buffer '" .. selected.name .. "'?" }, function (input)
if input == nil then return end
if not vim.startswith("y", string.lower(input)) then return end
session.client:delete_workspace(selected.name):await()
print("deleted workspace " .. selected.name)
manager.refresh("codemp")
end)
2024-09-07 01:31:04 +02:00
end
end
2024-09-07 05:03:34 +02:00
M.add = function(state, path, extra)
local selected = state.tree:get_node()
if selected.type == "root" then
if vim.startswith(selected.name, "#") then
2024-09-15 12:46:43 +02:00
vim.ui.input({ prompt = "new buffer path" }, function(input)
2024-09-07 05:03:34 +02:00
if input == nil or input == "" then return end
session.workspace:create_buffer(input):await()
manager.refresh("codemp")
end)
elseif selected.name == "workspaces" then
2024-09-15 12:46:43 +02:00
vim.ui.input({ prompt = "new workspace name" }, function(input)
2024-09-07 05:03:34 +02:00
if input == nil or input == "" then return end
session.client:create_workspace(input):await()
vim.schedule(function () require('codemp.workspace').list() end)
2024-09-07 05:03:34 +02:00
end)
end
elseif selected.type == "workspace" then
2024-09-15 12:46:43 +02:00
vim.ui.input({ prompt = "user name to invite" }, function(input)
if input == nil or input == "" then return end
2024-09-07 05:03:34 +02:00
session.client:invite_to_workspace(selected.name, input):await()
print("invited user " .. input .. " to workspace " .. selected.name)
end)
end
2024-09-07 01:31:04 +02:00
manager.refresh("codemp")
end
cc._add_common_commands(M)
return M