fix: only expand all when changing state

so now toggling with enter works and doesnt bug out
This commit is contained in:
əlemi 2024-09-08 06:24:34 +02:00
parent 368dd727c5
commit 0c078ddcf1
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 42 additions and 38 deletions

View file

@ -1,30 +1,13 @@
local renderer = require("neo-tree.ui.renderer") local renderer = require("neo-tree.ui.renderer")
local codemp = require("codemp.session") local codemp = require("codemp.session")
local buf_manager = require("codemp.buffers") local buf_manager = require("codemp.buffers")
---@module 'nui.tree'
local M = {} local M = {}
---@class Item
---@field id string
---@field name string
---@field type string
---@field loaded any
---@field filtered_by any
---@field extra table
---@field is_nested any
---@field skip_node any
---@field is_empty_with_hidden_root any
---@field stat any
---@field stat_provider any
---@field is_link any
---@field link_to any
---@field path any
---@field ext any
---@field search_pattern any
---@param workspace string workspace name ---@param workspace string workspace name
---@param path string buffer relative path ---@param path string buffer relative path
---@return Item ---@return NuiTree.Node
local function new_item(workspace, path) local function new_item(workspace, path)
return { return {
id = string.format("codemp://%s/%s", workspace, path), id = string.format("codemp://%s/%s", workspace, path),
@ -37,7 +20,7 @@ end
---@param workspace string workspace name ---@param workspace string workspace name
---@param username string user display name ---@param username string user display name
---@return Item ---@return NuiTree.Node
local function new_user(workspace, username) local function new_user(workspace, username)
return { return {
id = string.format("codemp://%s@%s", username, workspace), id = string.format("codemp://%s@%s", username, workspace),
@ -51,13 +34,12 @@ end
---@param name string workspace name ---@param name string workspace name
---@param owned boolean true if this workspace is owned by us ---@param owned boolean true if this workspace is owned by us
---@param expanded? boolean if node should be pre-expanded ---@param expanded? boolean if node should be pre-expanded
---@return Item ---@return NuiTree.Node
local function new_workspace(name, owned, expanded) local function new_workspace(name, owned, expanded)
return { return {
id = "codemp://" .. name, id = "codemp://" .. name,
name = name, name = name,
type = "workspace", type = "workspace",
['_is_expanded'] = expanded, -- TODO this is nasty can we do better?
extra = { extra = {
owned = owned, owned = owned,
}, },
@ -68,7 +50,7 @@ end
---@param key string ---@param key string
---@param value string ---@param value string
---@return Item ---@return NuiTree.Node
local function new_entry(key, value) local function new_entry(key, value)
return { return {
id = "codemp-entry-" .. key .. "-" .. value, id = "codemp-entry-" .. key .. "-" .. value,
@ -83,6 +65,8 @@ local function new_root(name)
id = "codemp-tree-" .. name, id = "codemp-tree-" .. name,
name = name, name = name,
type = "root", type = "root",
expanded = true,
expand = true,
extra = {}, extra = {},
children = {} children = {}
} }
@ -100,7 +84,7 @@ end
local counter = 0; local counter = 0;
---@return Item ---@return NuiTree.Node
local function spacer() local function spacer()
counter = counter + 1 counter = counter + 1
return { return {
@ -110,8 +94,28 @@ local function spacer()
} }
end 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) M.update_state = function(state)
---@type Item[] ---@type NuiTree.Node[]
local root = { local root = {
{ {
id = "codemp", id = "codemp",
@ -122,8 +126,7 @@ M.update_state = function(state)
} }
if codemp.workspace ~= nil then if codemp.workspace ~= nil then
table.insert(root, spacer()) local ws_section = new_root("#" .. codemp.workspace.name)
local ws_section = new_root("session #" .. codemp.workspace.name)
for i, path in ipairs(codemp.workspace:filetree()) do for i, path in ipairs(codemp.workspace:filetree()) do
table.insert(ws_section.children, new_item(codemp.workspace.name, path)) table.insert(ws_section.children, new_item(codemp.workspace.name, path))
end end
@ -134,21 +137,24 @@ M.update_state = function(state)
end end
table.insert(ws_section.children, spacer()) table.insert(ws_section.children, spacer())
table.insert(ws_section.children, usr_section) table.insert(ws_section.children, usr_section)
table.insert(root, spacer())
table.insert(root, ws_section) table.insert(root, ws_section)
end end
if codemp.client ~= nil then if codemp.client ~= nil then
table.insert(root, spacer())
local ws_section = new_root("workspaces") local ws_section = new_root("workspaces")
for _, ws in ipairs(codemp.available) do for _, ws in ipairs(codemp.available) do
table.insert(ws_section.children, new_workspace(ws.name, ws.owned)) table.insert(ws_section.children, new_workspace(ws.name, ws.owned))
end end
table.insert(root, spacer())
table.insert(root, ws_section) table.insert(root, ws_section)
table.insert(root, spacer())
local status_section = new_root("client") local status_section = new_root("client")
table.insert(status_section.children, new_entry("id", codemp.client.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(status_section.children, new_entry("name", codemp.client.username))
table.insert(root, spacer())
table.insert(root, status_section) table.insert(root, status_section)
end end
@ -158,12 +164,13 @@ M.update_state = function(state)
end end
renderer.show_nodes(root, state) renderer.show_nodes(root, state)
for _, node in ipairs(state.tree:get_nodes()) do
node:expand() local new_state = "disconnected"
for _, child_node in ipairs(state.tree:get_nodes(node)) do if codemp.client ~= nil then new_state = "connected" end
child_node:expand() if codemp.workspace ~= nil then new_state = "joined" end
end
end if last_state ~= new_state then expand(state.tree) end
last_state = new_state
end end
return M return M

View file

@ -38,7 +38,6 @@ M.open = function(state, path, extra)
if session.workspace == nil then if session.workspace == nil then
ws_manager.join(selected.name) ws_manager.join(selected.name)
end end
selected:expand()
manager.refresh("codemp") manager.refresh("codemp")
return return
end end

View file

@ -110,12 +110,10 @@ M.indent = function(config, node, state)
state.skip_marker_at_level = {} state.skip_marker_at_level = {}
end end
local strlen = vim.fn.strdisplaywidth
local skip_marker = state.skip_marker_at_level local skip_marker = state.skip_marker_at_level
local indent_size = config.indent_size or 2 local indent_size = config.indent_size or 2
local padding = config.padding or 0 local padding = config.padding or 0
local level = node.level local level = node.level
local with_markers = config.with_markers
local with_expanders = config.with_expanders == nil and file_nesting.is_enabled() local with_expanders = config.with_expanders == nil and file_nesting.is_enabled()
or config.with_expanders or config.with_expanders
local marker_highlight = config.highlight or highlights.INDENT_MARKER local marker_highlight = config.highlight or highlights.INDENT_MARKER