8000 mini.nvim/lua/mini/visits.lua at main · rafamadriz/mini.nvim · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1561 lines (1403 loc) · 59.7 KB

visits.lua

File metadata and controls

1561 lines (1403 loc) · 59.7 KB
---@return table Normalized index object.
MiniVisits.normalize_index = function(index)
index = index or MiniVisits.get_index()
H.validate_index(index, '`index`')
local config = H.get_config()
local normalize = config.store.normalize
if not vim.is_callable(normalize) then normalize = MiniVisits.gen_normalize.default() end
local new_index = normalize(vim.deepcopy(index))
H.validate_index(new_index, '`index` after normalization')
return new_index
end
--- Read visit index from disk
---
---@param store_path string|nil Path on the disk containing visit index data.
--- Default: `config.store.path`.
--- Notes:
--- - Can return `nil` if path is empty string or file is not readable.
--- - File is sourced with |dofile()| as a regular Lua file.
---
---@return table|nil Output of the file source.
MiniVisits.read_index = function(store_path)
store_path = store_path or H.get_config().store.path
if store_path == '' then return nil end
H.validate_string(store_path, 'store_path')
if vim.fn.filereadable(store_path) == 0 then return nil end
return dofile(store_path)
end
--- Write visit index to disk
---
--- Steps:
--- - Normalize index with |MiniVisits.normalize_index()|.
--- - Ensure path is valid (all parent directories are created, etc.).
--- - Write index object to the path so that it is readable
--- with |MiniVisits.read_index()|.
---
---@param store_path string|nil Path on the disk where to write visit index data.
--- Default: `config.store.path`. Note: if empty string, nothing is written.
---@param index table|nil Index object to write to disk.
--- Default: current session index.
MiniVisits.write_index = function(store_path, index)
store_path = store_path or H.get_config().store.path
H.validate_string(store_path, 'store_path')
if store_path == '' then return end
index = index or MiniVisits.get_index()
H.validate_index(index, '`index`')
-- Normalize index
index = MiniVisits.normalize_index(index)
-- Ensure writable path
store_path = vim.fn.fnamemodify(store_path, ':p')
local path_dir = vim.fn.fnamemodify(store_path, ':h')
vim.fn.mkdir(path_dir, 'p')
-- Write
local lines = vim.split(vim.inspect(index), '\n')
lines[1] = 'return ' .. lines[1]
vim.fn.writefile(lines, store_path)
end
--- Rename path in index
---
--- A helper to react for a path rename/move in order to preserve its visit data.
--- It works both for file and directory paths.
---
--- Notes:
--- - It does not update current index, but returns a modified index object.
--- Use |MiniVisits.set_index()| to make it current.
--- - Use only full paths.
--- - Do not append `/` to directory paths. Use same format as for files.
---
--- Assuming `path_from` and `path_to` are variables containing full paths
--- before and after rename/move, here is an example to update current index: >
---
--- local new_index = MiniVisits.rename_in_index(path_from, path_to)
--- MiniVisits.set_index(new_index)
--- <
---@param path_from string Full path to be renamed.
---@param path_to string Full path to be replaced with.
---@param index table|nil Index object inside which to perform renaming.
--- Default: current session index.
---
---@return table Index object with renamed path.
MiniVisits.rename_in_index = function(path_from, path_to, index)
path_from = H.validate_string(path_from, 'path_from')
path_to = H.validate_string(path_to, 'path_to')
index = index or MiniVisits.get_index()
H.validate_index(index, '`index`')
local path_from_pattern = vim.pesc(path_from)
local pattern_from_full = string.format('^%s$', path_from_pattern)
local pattern_from_parent_dir = string.format('^(%s)/', path_from_pattern)
local path_to_parent_dir = path_to .. '/'
local replace = function(x)
0