-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathutils.lua
211 lines (182 loc) · 7 KB
/
utils.lua
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
local config = require('session_manager.config')
local scandir = require('plenary.scandir')
local Path = require('plenary.path')
local utils = { active_session_filename = nil }
--- A small wrapper around `vim.notify` that adds plugin title.
---@param msg string
---@param log_level number
function utils.notify(msg, log_level) vim.notify(msg, log_level, { title = 'Session manager' }) end
---@return string?: Last used session filename.
function utils.get_last_session_filename()
if not Path:new(config.sessions_dir):is_dir() then
utils.notify('Sessions list is empty', vim.log.levels.INFO)
return nil
end
local most_recent_filename = nil
local most_recent_timestamp = 0
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir))) do
if config.session_filename_to_dir(session_filename):is_dir() then
local timestamp = vim.fn.getftime(session_filename)
if most_recent_timestamp < timestamp then
most_recent_timestamp = timestamp
most_recent_filename = session_filename
end
end
end
return most_recent_filename
end
---@param filename string
---@param discard_current boolean?
function utils.load_session(filename, discard_current)
if not discard_current then
-- Ask to save files in current session before closing them.
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_get_option(buffer, 'modified') then
local choice = vim.fn.confirm('The files in the current session have changed. Save changes?', '&Yes\n&No\n&Cancel')
if choice == 3 or choice == 0 then
return -- Cancel.
elseif choice == 1 then
vim.api.nvim_command('silent wall')
end
break
end
end
end
-- Delete all buffers first except the current one to avoid entering buffers scheduled for deletion.
local current_buffer = vim.api.nvim_get_current_buf()
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buffer) and buffer ~= current_buffer then
vim.api.nvim_buf_delete(buffer, { force = true })
end
end
vim.api.nvim_buf_delete(current_buffer, { force = true })
-- Set the active session filename.
utils.active_session_filename = filename
local swapfile = vim.o.swapfile
vim.o.swapfile = false
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPre' })
vim.api.nvim_command('silent source ' .. filename)
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPost' })
vim.o.swapfile = swapfile
end
---@param filename string
function utils.save_session(filename)
local sessions_dir = Path:new(tostring(config.sessions_dir))
if not sessions_dir:is_dir() then
sessions_dir:mkdir()
end
-- Remove all non-file and utility buffers because they cannot be saved.
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buffer) and not utils.is_restorable(buffer) then
vim.api.nvim_buf_delete(buffer, { force = true })
end
end
-- Clear all passed arguments to avoid re-executing them.
if vim.fn.argc() > 0 then
vim.api.nvim_command('%argdel')
end
-- Set the active session filename.
utils.active_session_filename = filename
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
vim.api.nvim_command('mksession! ' .. filename)
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePost' })
end
---@param filename string
function utils.delete_session(filename)
Path:new(filename):rm()
-- Clear the active session filename if deleted.
if filename == utils.active_session_filename then
utils.active_session_filename = nil
end
end
---@param opts table?: Additional arguments. Currently only `silent` is supported.
---@return table
function utils.get_sessions(opts)
local sessions = {}
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir), opts)) do
-- Add all but the active session to the list.
if config.load_include_current or session_filename ~= utils.active_session_filename then
local dir = config.session_filename_to_dir(session_filename)
if dir:is_dir() then
table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir })
else
Path:new(session_filename):rm()
end
end
end
table.sort(sessions, function(a, b) return a.timestamp > b.timestamp end)
-- If no sessions to list, send a notification.
if not (opts and opts.silent) and #sessions == 0 then
vim.notify('The only available session is your current session. Nothing to select from.', vim.log.levels.INFO)
end
return sessions
end
---@param buffer number: buffer ID.
---@return boolean: `true` if this buffer could be restored later on loading.
function utils.is_restorable(buffer)
if #vim.api.nvim_buf_get_option(buffer, 'bufhidden') ~= 0 then
return false
end
local buftype = vim.api.nvim_buf_get_option(buffer, 'buftype')
if #buftype == 0 then
-- Normal buffer, check if it listed.
if not vim.api.nvim_buf_get_option(buffer, 'buflisted') then
return false
end
-- Check if it has a filename.
if #vim.api.nvim_buf_get_name(buffer) == 0 then
return false
end
elseif buftype ~= 'terminal' and buftype ~= 'help' then
-- Buffers other then normal, terminal and help are impossible to restore.
return false
end
if
vim.tbl_contains(config.autosave_ignore_filetypes, vim.api.nvim_buf_get_option(buffer, 'filetype'))
or vim.tbl_contains(config.autosave_ignore_buftypes, vim.api.nvim_buf_get_option(buffer, 'buftype'))
then
return false
end
return true
end
---@return boolean
function utils.is_restorable_buffer_present()
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buffer) and utils.is_restorable(buffer) then
return true
end
end
return false
end
---@return boolean
function utils.exists_in_session()
local cwd = vim.uv.cwd()
return config.dir_to_session_filename(cwd).filename == utils.active_session_filename
end
---@return boolean
function utils.is_dir_in_ignore_list()
local cwd = vim.uv.cwd()
-- Use `fnamemodify` to allow paths like `~/.config`.
return vim.tbl_contains(config.autosave_ignore_dirs, cwd) or vim.tbl_contains(config.autosave_ignore_dirs, vim.fn.fnamemodify(cwd, ':~'))
end
--- Partially shorten path if length exceeds defined max_path_length.
---@param path table
---@return string
function utils.shorten_path(path)
if config.max_path_length > 0 and #path.filename > config.max_path_length then
-- Index to exclude from shortening, -1 means last
local excludes = { -1 }
-- Gradually increase the tailing excludes
local shortened = path.filename
local next_shortened = path:shorten(1, excludes)
while #next_shortened < config.max_path_length do
shortened = next_shortened
-- Try new shortened path with more excludes
excludes[#excludes + 1] = excludes[#excludes] - 1
next_shortened = path:shorten(1, excludes)
end
return shortened
end
return path.filename
end
return utils