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

178 lines
3.9 KiB
Lua
Raw Normal View History

local renderer = require("neo-tree.ui.renderer")
local buf_manager = require("codemp.buffers")
---@module 'nui.tree'
local M = {}
---@param workspace string workspace name
---@param path string buffer relative path
---@return NuiTree.Node
local function new_item(workspace, path)
return {
2024-09-28 01:10:25 +02:00
id = string.format("codemp://%s/item/%s", workspace, path),
name = path,
type = "buffer",
extra = {},
children = {},
}
end
---@param workspace string workspace name
---@param username string user display name
---@return NuiTree.Node
local function new_user(workspace, username)
return {
id = string.format("codemp://%s@%s", username, workspace),
name = username,
type = "user",
extra = {},
children = {},
}
end
---@param name string workspace name
---@param owned boolean true if this workspace is owned by us
---@param expanded? boolean if node should be pre-expanded
---@return NuiTree.Node
local function new_workspace(name, owned, expanded)
local owned_ext = ""
if owned then owned_ext = "_owned" end
return {
id = "codemp://" .. name .. "/workspace" .. owned_ext,
name = name,
type = "workspace",
extra = {
owned = owned,
},
children = {},
}
end
---@param key string
---@param value string
---@return NuiTree.Node
local function new_entry(key, value)
return {
id = "codemp-entry-" .. key .. "-" .. value,
name = key .. ": " .. value,
type = "entry",
extra = {},
}
end
2024-09-07 03:36:16 +02:00
local function new_root(name)
return {
id = "codemp-tree-" .. name,
name = name,
type = "root",
expanded = true,
expand = true,
2024-09-07 03:36:16 +02:00
extra = {},
children = {}
}
end
local function new_button(name)
return {
id = "codemp-button-" .. name,
name = name,
type = "button",
extra = {},
children = {}
}
end
local counter = 0;
---@return NuiTree.Node
local function spacer()
counter = counter + 1
return {
id = "codemp-ws-spacer-" .. counter,
name = "",
type = "spacer",
}
end
local last_state = "N/A"
---@param tree NuiTree
local function expand(tree)
---@param node? NuiTree.Node
local function process(node)
local id = nil
if node ~= nil then id = node:get_id() end
for _, node in ipairs(tree:get_nodes(id)) do
node:expand()
if node:has_children() then
process(node)
end
end
end
process()
end
M.update_state = function(state)
---@type NuiTree.Node[]
2024-09-07 04:01:48 +02:00
local root = {
{
id = "codemp",
name = "codemp",
type = "title",
extra = {},
}
2024-09-07 04:01:48 +02:00
}
2024-09-17 18:43:45 +02:00
state.force_open_folders = true
state.default_expanded_nodes = {}
if CODEMP.workspace ~= nil then
local ws_section = new_root("#" .. CODEMP.workspace.name)
2024-09-17 18:43:45 +02:00
table.insert(state.default_expanded_nodes, ws_section.id)
for i, path in ipairs(CODEMP.workspace:filetree()) do
table.insert(ws_section.children, new_item(CODEMP.workspace.name, path))
end
2024-09-07 04:01:48 +02:00
local usr_section = new_root("users")
2024-09-17 18:43:45 +02:00
table.insert(state.default_expanded_nodes, usr_section.id)
for user, buffer in pairs(buf_manager.users) do
table.insert(usr_section.children, new_user(CODEMP.workspace.name, user))
end
table.insert(ws_section.children, spacer())
table.insert(ws_section.children, usr_section)
table.insert(root, spacer())
table.insert(root, ws_section)
2024-09-07 04:01:48 +02:00
end
if CODEMP.client ~= nil then
2024-09-07 04:01:48 +02:00
local ws_section = new_root("workspaces")
2024-09-17 18:43:45 +02:00
table.insert(state.default_expanded_nodes, ws_section.id)
for _, ws in ipairs(CODEMP.available) do
2024-09-07 04:01:48 +02:00
table.insert(ws_section.children, new_workspace(ws.name, ws.owned))
end
table.insert(root, spacer())
table.insert(root, ws_section)
local status_section = new_root("client")
2024-09-17 18:43:45 +02:00
table.insert(state.default_expanded_nodes, status_section.id)
table.insert(status_section.children, new_entry("id", CODEMP.client.id))
table.insert(status_section.children, new_entry("name", CODEMP.client.username))
table.insert(root, spacer())
table.insert(root, status_section)
end
if CODEMP.client == nil then
table.insert(root, spacer())
table.insert(root, new_button("[connect]"))
end
renderer.show_nodes(root, state)
end
return M