-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
172 lines (145 loc) · 3.84 KB
/
Copy pathinit.lua
File metadata and controls
172 lines (145 loc) · 3.84 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
local utils = require("workspaces-nvim.utils")
local config = require("workspaces-nvim.defaults")
local ui = require("workspaces-nvim.ui")
local M = {}
---@class Workspaces.Ctrl
---@field config Config
---@field dataPath Path
---@field workspace Workspace
---@field ui Workspaces.UI
local Controller = {}
Controller.__index = Controller
---@param dataPath Path
function Controller:new(dataPath, config)
if vim.uv.fs_stat(dataPath) == nil then
vim.fn.mkdir(vim.fn.fnamemodify(dataPath, ":h"), "p")
utils.write(dataPath, vim.empty_dict())
end
---@type Workspaces.Ctrl
local obj
obj = {
config = config,
dataPath = dataPath,
workspace = {},
ui = nil
} setmetatable(obj, Controller)
obj.ui = ui:new(obj)
obj:setupEvents()
obj:setupKeymaps()
return obj
end
local function refreshWrap(ctrlFunc, uiFunc)
---@param ctrl Workspaces.Ctrl
local callback = function(ctrl)
ctrlFunc(ctrl)
uiFunc(ctrl.ui)
end
return callback
end
function Controller:setupEvents()
---@param func fun(ui: Workspaces.UI)
---@return fun(ctrl: Workspaces.Ctrl)
local function execUIFunc(func)
return function (ctrl)
func(ctrl.ui)
end
end
local events = {
VimLeavePre = Controller.saveWorkspace,
DirChangedPre = Controller.saveWorkspace,
}
for event, callback in pairs(events) do
vim.api.nvim_create_autocmd(event, {
callback = function ()
callback(self)
end
})
end
local scheduledEvents = {
DirChanged = refreshWrap(Controller.openWorkspace, ui.refresh),
UIEnter = refreshWrap(Controller.openWorkspace, ui.init),
BufEnter = execUIFunc(ui.updateCurrentFileHighlight),
VimResized = execUIFunc(ui.updatePos),
WinResized = execUIFunc(ui.updatePos),
WinClosed = execUIFunc(ui.updatePos),
WinNew = execUIFunc(ui.updatePos),
}
for event, callback in pairs(scheduledEvents) do
vim.api.nvim_create_autocmd(event, {
callback = vim.schedule_wrap(function ()
callback(self)
end)
})
end
end
function Controller:init()
self:openWorkspace()
self.ui:init()
end
function Controller:setupKeymaps()
for i = 1, #self.config.keys do
local key = self.config.keys:sub(i, i)
local selectKey = self.config.selectLeaderKey .. key
vim.keymap.set("n", selectKey, function ()
self:accessPin(key)
end, { desc = "Switch to " .. key })
local pinKey = self.config.pinLeaderKey .. key
vim.keymap.set("n", pinKey, function ()
self:pin(key)
end, { desc = "Pin file to " .. key })
end
local clearKey = self.config.clearKey
vim.keymap.set("n", clearKey, function ()
self:clear()
end, { desc = "Clear workspace" })
end
---@param path Path
---@return Workspace
function Controller:getWorkspace(path)
local workspaces = utils.read(self.dataPath)
return workspaces[path] or {}
end
---@param path Path
---@param workspace Workspace
function Controller:setWorkspace(path, workspace)
local workspaces = utils.read(self.dataPath)
workspaces[path] = workspace
utils.write(self.dataPath, workspaces)
end
function Controller:saveWorkspace()
local cwd = utils.getcwd()
self:setWorkspace(cwd, self.workspace)
end
function Controller:openWorkspace()
local cwd = utils.getcwd()
self.workspace = self:getWorkspace(cwd)
end
---@param pin Pin
function Controller:accessPin(pin)
local path = self.workspace[pin]
if path == nil then
utils.alert("Pin " .. pin .. " has no file")
return
end
if vim.uv.fs_stat(path) == nil then
utils.alert("File of pin " .. pin .. " doesn't exist")
return
end
vim.cmd("edit " .. path)
end
---@param pin Pin
function Controller:pin(pin)
local currentPath = utils.sanitizePath(vim.fn.expand("%:p"))
self.workspace[pin] = currentPath
self.ui:refresh()
end
function Controller:clear()
self.workspace = {}
self.ui:refresh()
end
function M.setup(opts)
config = vim.tbl_extend("force", config, opts)
local dataPath = vim.fn.stdpath("data") .. "/workspaces-nvim/workspaces.json"
Controller:new(dataPath, config)
end
return M