updated README.md
This commit is contained in:
7
.cspell/words.txt
Normal file
7
.cspell/words.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
darcula
|
||||||
|
keymaps
|
||||||
|
neovim
|
||||||
|
nmap
|
||||||
|
nvim
|
||||||
|
sgraves76
|
||||||
|
stdpath
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -40,4 +40,4 @@ luac.out
|
|||||||
*.x86_64
|
*.x86_64
|
||||||
*.hex
|
*.hex
|
||||||
|
|
||||||
|
cspell.json
|
||||||
|
|||||||
96
README.md
96
README.md
@@ -1,3 +1,97 @@
|
|||||||
# nvim-haven
|
# nvim-haven
|
||||||
|
|
||||||
Local file history save and restore for Neovim
|
Local file history save and restore for Neovim (requires `nvim-telescope`)
|
||||||
|
|
||||||
|
### Global functions
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- Remove history for files that no longer exist
|
||||||
|
_G.Nvim_Haven_Clean = M.clean
|
||||||
|
|
||||||
|
-- Disable nvim-haven
|
||||||
|
_G.Nvim_Haven_Disable = M.disable
|
||||||
|
|
||||||
|
-- Enable nvim-haven
|
||||||
|
_G.Nvim_Haven_Enable = M.enable
|
||||||
|
|
||||||
|
-- Display history in telescope
|
||||||
|
_G.Nvim_Haven_History = M.history
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
> Example utilizes my custom Neovim distribution `darcula`
|
||||||
|
> This is not publicly available yet, but should be self-explanatory nonetheless :)
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local M = {
|
||||||
|
disabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
M.keymaps = function() {
|
||||||
|
local km = require("darcula.utils.keymap")
|
||||||
|
km.nmap(
|
||||||
|
km.leader "fy",
|
||||||
|
require("nvim-haven").history,
|
||||||
|
{remap = true, silent = true}
|
||||||
|
)
|
||||||
|
km.nmap(
|
||||||
|
km.leader "ch",
|
||||||
|
require("nvim-haven").clean,
|
||||||
|
{remap = true, silent = true}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
M.lua_add_library = function(library_list)
|
||||||
|
table.insert(library_list, "nvim-haven")
|
||||||
|
end
|
||||||
|
|
||||||
|
M.plug = function(Plug)
|
||||||
|
Plug "sgraves76/nvim-haven"
|
||||||
|
end
|
||||||
|
|
||||||
|
M.setup = function()
|
||||||
|
local plugins = require("nvim-goodies.plugins")
|
||||||
|
if not plugins.check_requires("nvim-haven") then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
require("nvim-goodies.string")
|
||||||
|
local gos = require("nvim-goodies.os")
|
||||||
|
|
||||||
|
require("nvim-haven").setup(
|
||||||
|
{
|
||||||
|
exclusions = {
|
||||||
|
function(path, _)
|
||||||
|
local tmp = vim.env.TEMP or vim.env.TMP
|
||||||
|
if tmp ~= nil and tmp:len() > 0 then
|
||||||
|
if gos.is_windows then
|
||||||
|
return path:lower():starts_with(tmp:lower())
|
||||||
|
end
|
||||||
|
|
||||||
|
return path:starts_with(tmp)
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
},
|
||||||
|
inclusions = {
|
||||||
|
function(path, _)
|
||||||
|
local dest =
|
||||||
|
require("nvim-goodies.path").create_path(
|
||||||
|
vim.fn.stdpath("data"),
|
||||||
|
"plugged",
|
||||||
|
"telescope-coc.nvim"
|
||||||
|
)
|
||||||
|
if gos.is_windows then
|
||||||
|
return path:lower():starts_with(dest:lower())
|
||||||
|
end
|
||||||
|
|
||||||
|
return path:starts_with(dest)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
```
|
||||||
|
|||||||
@@ -24,30 +24,37 @@ local haven_config = {
|
|||||||
exclusions = {
|
exclusions = {
|
||||||
function(path, _)
|
function(path, _)
|
||||||
if gos.is_windows then
|
if gos.is_windows then
|
||||||
return path:lower():starts_with((vim.fn.eval("$VIMRUNTIME") .. gpath.directory_sep):lower())
|
return path:lower():starts_with(
|
||||||
|
(vim.fn.eval("$VIMRUNTIME") .. gpath.directory_sep):lower()
|
||||||
|
)
|
||||||
end
|
end
|
||||||
return path:starts_with(vim.fn.eval("$VIMRUNTIME") .. gpath.directory_sep)
|
return path:starts_with(vim.fn.eval("$VIMRUNTIME") .. gpath.directory_sep)
|
||||||
end,
|
end,
|
||||||
function(path, _)
|
function(path, _)
|
||||||
if gos.is_windows then
|
if gos.is_windows then
|
||||||
return path:lower():starts_with((vim.fn.stdpath("data") .. gpath.directory_sep):lower())
|
return path:lower():starts_with(
|
||||||
|
(vim.fn.stdpath("data") .. gpath.directory_sep):lower()
|
||||||
|
)
|
||||||
end
|
end
|
||||||
return path:starts_with(vim.fn.stdpath("data") .. gpath.directory_sep)
|
return path:starts_with(vim.fn.stdpath("data") .. gpath.directory_sep)
|
||||||
end,
|
end,
|
||||||
function(path, _)
|
function(path, _)
|
||||||
if gos.is_windows then
|
if gos.is_windows then
|
||||||
return path:lower():starts_with(
|
return path:lower():starts_with(
|
||||||
(gpath.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") .. gpath.directory_sep):lower()
|
(gpath.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") ..
|
||||||
|
gpath.directory_sep):lower()
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
return path:starts_with(
|
return path:starts_with(
|
||||||
gpath.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") .. gpath.directory_sep
|
gpath.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") ..
|
||||||
|
gpath.directory_sep
|
||||||
)
|
)
|
||||||
end,
|
end,
|
||||||
function(path, _)
|
function(path, _)
|
||||||
if gos.is_windows then
|
if gos.is_windows then
|
||||||
return path:lower():ends_with(
|
return path:lower():ends_with(
|
||||||
(gpath.directory_sep .. ".git" .. gpath.directory_sep .. "COMMIT_EDITMSG"):lower()
|
(gpath.directory_sep ..
|
||||||
|
".git" .. gpath.directory_sep .. "COMMIT_EDITMSG"):lower()
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
return path:ends_with(
|
return path:ends_with(
|
||||||
@@ -56,7 +63,9 @@ local haven_config = {
|
|||||||
end,
|
end,
|
||||||
function(path, config)
|
function(path, config)
|
||||||
if gos.is_windows then
|
if gos.is_windows then
|
||||||
return path:lower():starts_with((config.haven_path .. gpath.directory_sep):lower())
|
return path:lower():starts_with(
|
||||||
|
(config.haven_path .. gpath.directory_sep):lower()
|
||||||
|
)
|
||||||
end
|
end
|
||||||
return path:starts_with(config.haven_path .. gpath.directory_sep)
|
return path:starts_with(config.haven_path .. gpath.directory_sep)
|
||||||
end
|
end
|
||||||
@@ -93,7 +102,10 @@ local create_save_file_path = function(buf_info)
|
|||||||
):gsub(" ", "+")
|
):gsub(" ", "+")
|
||||||
end
|
end
|
||||||
|
|
||||||
return gpath.create_path(haven_config.haven_path, encode(buf_info.name) .. ".save")
|
return gpath.create_path(
|
||||||
|
haven_config.haven_path,
|
||||||
|
encode(buf_info.name) .. ".save"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
local save_change_file = function(buf_info, lines, save_file)
|
local save_change_file = function(buf_info, lines, save_file)
|
||||||
@@ -151,7 +163,10 @@ local read_change_file = function(save_file)
|
|||||||
end
|
end
|
||||||
file:close()
|
file:close()
|
||||||
|
|
||||||
local entries = vim.json.decode("[" .. table.concat(save_data:split(line_ending), ",") .. "]")
|
local entries =
|
||||||
|
vim.json.decode(
|
||||||
|
"[" .. table.concat(save_data:split(line_ending), ",") .. "]"
|
||||||
|
)
|
||||||
if #entries > haven_config.max_history_count then
|
if #entries > haven_config.max_history_count then
|
||||||
while #entries > haven_config.max_history_count do
|
while #entries > haven_config.max_history_count do
|
||||||
table.remove(entries, 1)
|
table.remove(entries, 1)
|
||||||
@@ -168,12 +183,16 @@ local process_file_changed = function(buf_info)
|
|||||||
local immediate = vim.fn.filereadable(save_file) == 0
|
local immediate = vim.fn.filereadable(save_file) == 0
|
||||||
|
|
||||||
local update_changed_lookup = function()
|
local update_changed_lookup = function()
|
||||||
changed_lookup[save_file] = {changed = buf_info.changed, changedtick = buf_info.changedtick}
|
changed_lookup[save_file] = {
|
||||||
|
changed = buf_info.changed,
|
||||||
|
changedtick = buf_info.changedtick
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
if
|
if
|
||||||
not immediate and
|
not immediate and
|
||||||
(changed_data == nil or (buf_info.changed == 0 and changed_data.changed == 0) or
|
(changed_data == nil or
|
||||||
|
(buf_info.changed == 0 and changed_data.changed == 0) or
|
||||||
buf_info.changedtick == changed_data.changedtick)
|
buf_info.changedtick == changed_data.changedtick)
|
||||||
then
|
then
|
||||||
update_changed_lookup()
|
update_changed_lookup()
|
||||||
@@ -265,7 +284,8 @@ local handle_vim_leave = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local setup_autocmds = function()
|
local setup_autocmds = function()
|
||||||
local group_id = vim.api.nvim_create_augroup("nvim-haven-internal", {clear = true})
|
local group_id =
|
||||||
|
vim.api.nvim_create_augroup("nvim-haven-internal", {clear = true})
|
||||||
if haven_config.enabled then
|
if haven_config.enabled then
|
||||||
vim.api.nvim_create_autocmd(
|
vim.api.nvim_create_autocmd(
|
||||||
"BufEnter",
|
"BufEnter",
|
||||||
@@ -448,7 +468,13 @@ local show_picker = function(entries)
|
|||||||
)
|
)
|
||||||
previous_lines = nil
|
previous_lines = nil
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, buffer_lines)
|
vim.api.nvim_buf_set_lines(
|
||||||
|
self.state.bufnr,
|
||||||
|
0,
|
||||||
|
-1,
|
||||||
|
false,
|
||||||
|
buffer_lines
|
||||||
|
)
|
||||||
putils.regex_highlighter(self.state.bufnr, "diff")
|
putils.regex_highlighter(self.state.bufnr, "diff")
|
||||||
|
|
||||||
jump_state = {self = self, cur = 0, diff_rows = diff_rows}
|
jump_state = {self = self, cur = 0, diff_rows = diff_rows}
|
||||||
@@ -458,7 +484,13 @@ local show_picker = function(entries)
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entry.value.lines)
|
vim.api.nvim_buf_set_lines(
|
||||||
|
self.state.bufnr,
|
||||||
|
0,
|
||||||
|
-1,
|
||||||
|
false,
|
||||||
|
entry.value.lines
|
||||||
|
)
|
||||||
putils.highlighter(self.state.bufnr, entry.value.ft, {})
|
putils.highlighter(self.state.bufnr, entry.value.ft, {})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -516,7 +548,8 @@ M.setup = function(config)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
haven_config.enabled = vim.F.if_nil(config.enabled, haven_config.enabled)
|
haven_config.enabled = vim.F.if_nil(config.enabled, haven_config.enabled)
|
||||||
haven_config.haven_path = vim.F.if_nil(config.haven_path, haven_config.haven_path)
|
haven_config.haven_path =
|
||||||
|
vim.F.if_nil(config.haven_path, haven_config.haven_path)
|
||||||
|
|
||||||
if config.inclusions ~= nil then
|
if config.inclusions ~= nil then
|
||||||
for _, e in pairs(config.inclusions) do
|
for _, e in pairs(config.inclusions) do
|
||||||
@@ -534,22 +567,38 @@ M.setup = function(config)
|
|||||||
haven_config.max_history_count =
|
haven_config.max_history_count =
|
||||||
vim.F.if_nil(config.max_history_count, haven_config.max_history_count)
|
vim.F.if_nil(config.max_history_count, haven_config.max_history_count)
|
||||||
if haven_config.max_history_count < 10 then
|
if haven_config.max_history_count < 10 then
|
||||||
print_message(true, "'max_history_count' too low: " .. haven_config.max_history_count)
|
print_message(
|
||||||
|
true,
|
||||||
|
"'max_history_count' too low: " .. haven_config.max_history_count
|
||||||
|
)
|
||||||
haven_config.max_history_count = 100
|
haven_config.max_history_count = 100
|
||||||
print_message(true, "reset 'max_history_count': " .. haven_config.max_history_count)
|
print_message(
|
||||||
|
true,
|
||||||
|
"reset 'max_history_count': " .. haven_config.max_history_count
|
||||||
|
)
|
||||||
elseif haven_config.max_history_count > 500 then
|
elseif haven_config.max_history_count > 500 then
|
||||||
print_message(true, "'max_history_count' too high: " .. haven_config.max_history_count)
|
print_message(
|
||||||
|
true,
|
||||||
|
"'max_history_count' too high: " .. haven_config.max_history_count
|
||||||
|
)
|
||||||
haven_config.max_history_count = 500
|
haven_config.max_history_count = 500
|
||||||
print_message(true, "reset 'max_history_count': " .. haven_config.max_history_count)
|
print_message(
|
||||||
|
true,
|
||||||
|
"reset 'max_history_count': " .. haven_config.max_history_count
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
haven_config.save_timeout = vim.F.if_nil(config.save_timeout, haven_config.save_timeout)
|
haven_config.save_timeout =
|
||||||
|
vim.F.if_nil(config.save_timeout, haven_config.save_timeout)
|
||||||
if haven_config.save_timeout < 135 then
|
if haven_config.save_timeout < 135 then
|
||||||
print_message(true, "'save_timeout' too low: " .. haven_config.save_timeout)
|
print_message(true, "'save_timeout' too low: " .. haven_config.save_timeout)
|
||||||
haven_config.save_timeout = 135
|
haven_config.save_timeout = 135
|
||||||
print_message(true, "reset 'save_timeout': " .. haven_config.save_timeout)
|
print_message(true, "reset 'save_timeout': " .. haven_config.save_timeout)
|
||||||
elseif haven_config.save_timeout > 10000 then
|
elseif haven_config.save_timeout > 10000 then
|
||||||
print_message(true, "'save_timeout' too high: " .. haven_config.save_timeout)
|
print_message(
|
||||||
|
true,
|
||||||
|
"'save_timeout' too high: " .. haven_config.save_timeout
|
||||||
|
)
|
||||||
haven_config.save_timeout = 10000
|
haven_config.save_timeout = 10000
|
||||||
print_message(true, "reset 'save_timeout': " .. haven_config.save_timeout)
|
print_message(true, "reset 'save_timeout': " .. haven_config.save_timeout)
|
||||||
end
|
end
|
||||||
@@ -583,10 +632,16 @@ M.clean = function()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
local history_files = scan.scan_dir(haven_config.haven_path, {hidden = true, depth = 1})
|
local history_files =
|
||||||
|
scan.scan_dir(haven_config.haven_path, {hidden = true, depth = 1})
|
||||||
for _, history_file in ipairs(history_files) do
|
for _, history_file in ipairs(history_files) do
|
||||||
local source_path =
|
local source_path =
|
||||||
Path:new(decode(Path:new(history_file):make_relative(haven_config.haven_path)):sub(1, -6))
|
Path:new(
|
||||||
|
decode(Path:new(history_file):make_relative(haven_config.haven_path)):sub(
|
||||||
|
1,
|
||||||
|
-6
|
||||||
|
)
|
||||||
|
)
|
||||||
if not source_path:exists() then
|
if not source_path:exists() then
|
||||||
local history_path = Path:new(history_file)
|
local history_path = Path:new(history_file)
|
||||||
print("removing: " .. history_path:absolute())
|
print("removing: " .. history_path:absolute())
|
||||||
@@ -629,6 +684,7 @@ M.history = function(bufname)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
_G.Nvim_Haven_Clean = M.clean
|
||||||
_G.Nvim_Haven_Disable = M.disable
|
_G.Nvim_Haven_Disable = M.disable
|
||||||
_G.Nvim_Haven_Enable = M.enable
|
_G.Nvim_Haven_Enable = M.enable
|
||||||
_G.Nvim_Haven_History = M.history
|
_G.Nvim_Haven_History = M.history
|
||||||
|
|||||||
Reference in New Issue
Block a user