-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathide.lua
More file actions
111 lines (98 loc) · 2.76 KB
/
Copy pathide.lua
File metadata and controls
111 lines (98 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- SPDX-FileCopyrightText: 2026 Elouan Martinet <exa@elou.world>
-- SPDX-License-Identifier: BSD-3-Clause
-- Per-tab "IDE mode": when enabled on a tabpage, neo-tree is shown as a side
-- panel on that tab and a few related behaviors change (see lua/config/above.lua).
-- Other tabs are unaffected.
local M = {}
-- enabled[tabid] = true when IDE mode is on for that tab
M.enabled = {}
-- Hooks fired when IDE mode is toggled on/off for a tab. Each hook receives
-- the tabid. Append to these lists from elsewhere to extend IDE mode.
M.on_enable = {}
M.on_disable = {}
local function tab(tabid)
if tabid == nil or tabid == 0 then
return vim.api.nvim_get_current_tabpage()
end
return tabid
end
---@param tabid integer? defaults to current tab
function M.is_enabled(tabid)
return M.enabled[tab(tabid)] == true
end
local function open_side_tree()
require("neo-tree.command").execute({
source = "filesystem",
action = "show", -- show without stealing focus from the current file
position = "left",
reveal = true,
})
end
local function close_side_tree()
-- close only the "left" position of the filesystem source on this tab.
-- Float-position neo-tree on this tab (if any) is left alone.
require("neo-tree.command").execute({
action = "close",
source = "filesystem",
position = "left",
})
-- Reset the per-tab filesystem state's current_position so a later
-- :Neotree call without an explicit position falls back to the
-- configured default (float) instead of resurrecting "left".
local ok, manager = pcall(require, "neo-tree.sources.manager")
if ok then
local state = manager.get_state("filesystem")
if state then
state.current_position = nil
end
end
end
---@param tabid integer? defaults to current tab
function M.enable(tabid)
local id = tab(tabid)
if M.enabled[id] then
return
end
M.enabled[id] = true
open_side_tree()
for _, hook in ipairs(M.on_enable) do
pcall(hook, id)
end
end
---@param tabid integer? defaults to current tab
function M.disable(tabid)
local id = tab(tabid)
if not M.enabled[id] then
return
end
M.enabled[id] = nil
close_side_tree()
for _, hook in ipairs(M.on_disable) do
pcall(hook, id)
end
end
---@param tabid integer? defaults to current tab
function M.toggle(tabid)
if M.is_enabled(tabid) then
M.disable(tabid)
else
M.enable(tabid)
end
end
-- Forget state for tabs that no longer exist.
vim.api.nvim_create_autocmd("TabClosed", {
callback = function(args)
-- args.file contains the tab number as a string (1-indexed display number),
-- not the tabid; iterate and prune any stale ids.
local alive = {}
for _, id in ipairs(vim.api.nvim_list_tabpages()) do
alive[id] = true
end
for id in pairs(M.enabled) do
if not alive[id] then
M.enabled[id] = nil
end
end
end,
})
return M