switch to nvim-goodies

This commit is contained in:
Scott E. Graves 2022-07-29 22:26:25 -05:00
parent 40a7d3583d
commit 35000f7895
2 changed files with 20 additions and 75 deletions

View File

@ -1,13 +1,16 @@
require("nvim-goodies.table")
local action_state = require("telescope.actions.state") local action_state = require("telescope.actions.state")
local actions = require("telescope.actions") local actions = require("telescope.actions")
local conf = require("telescope.config").values local conf = require("telescope.config").values
local finders = require("telescope.finders") local finders = require("telescope.finders")
local g = require("nvim-goodies")
local global_state = require("telescope.state") local global_state = require("telescope.state")
local gpath = require("nvim-goodies.path")
local os = require("nvim-goodies.os")
local pfiletype = require("plenary.filetype") local pfiletype = require("plenary.filetype")
local pickers = require("telescope.pickers") local pickers = require("telescope.pickers")
local previewers = require("telescope.previewers") local previewers = require("telescope.previewers")
local putils = require("telescope.previewers.utils") local putils = require("telescope.previewers.utils")
local utils = require("nvim-haven.utils")
local ns_previewer = vim.api.nvim_create_namespace("telescope.previewers") local ns_previewer = vim.api.nvim_create_namespace("telescope.previewers")
@ -15,34 +18,34 @@ local M = {}
local active_saves = {} local active_saves = {}
local changed_lookup = {} local changed_lookup = {}
local directory_sep = utils.iff(utils.is_windows, "\\", "/") local directory_sep = g.iff(os.is_windows, "\\", "/")
local haven_config = { local haven_config = {
enabled = true, enabled = true,
exclusions = { exclusions = {
function(path, _) function(path, _)
if utils.is_windows then if os.is_windows then
return path:lower():starts_with((vim.fn.eval("$VIMRUNTIME") .. directory_sep):lower()) return path:lower():starts_with((vim.fn.eval("$VIMRUNTIME") .. directory_sep):lower())
end end
return path:starts_with(vim.fn.eval("$VIMRUNTIME") .. directory_sep) return path:starts_with(vim.fn.eval("$VIMRUNTIME") .. directory_sep)
end, end,
function(path, _) function(path, _)
if utils.is_windows then if os.is_windows then
return path:lower():starts_with((vim.fn.stdpath("data") .. directory_sep):lower()) return path:lower():starts_with((vim.fn.stdpath("data") .. directory_sep):lower())
end end
return path:starts_with(vim.fn.stdpath("data") .. directory_sep) return path:starts_with(vim.fn.stdpath("data") .. directory_sep)
end, end,
function(path, _) function(path, _)
if utils.is_windows then if os.is_windows then
return path:lower():starts_with( return path:lower():starts_with(
(utils.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") .. directory_sep):lower() (gpath.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") .. directory_sep):lower()
) )
end end
return path:starts_with( return path:starts_with(
utils.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") .. directory_sep gpath.create_path(vim.fn.eval("$XDG_CONFIG_HOME"), "coc") .. directory_sep
) )
end, end,
function(path, _) function(path, _)
if utils.is_windows then if os.is_windows then
return path:lower():ends_with( return path:lower():ends_with(
(directory_sep .. ".git" .. directory_sep .. "COMMIT_EDITMSG"):lower() (directory_sep .. ".git" .. directory_sep .. "COMMIT_EDITMSG"):lower()
) )
@ -50,23 +53,23 @@ local haven_config = {
return path:ends_with(directory_sep .. ".git" .. directory_sep .. "COMMIT_EDITMSG") return path:ends_with(directory_sep .. ".git" .. directory_sep .. "COMMIT_EDITMSG")
end, end,
function(path, config) function(path, config)
if utils.is_windows then if os.is_windows then
return path:lower():starts_with((config.haven_path .. directory_sep):lower()) return path:lower():starts_with((config.haven_path .. directory_sep):lower())
end end
return path:starts_with(config.haven_path .. directory_sep) return path:starts_with(config.haven_path .. directory_sep)
end end
}, },
haven_path = utils.create_path(vim.fn.stdpath("data"), "nvim-haven"), haven_path = gpath.create_path(vim.fn.stdpath("data"), "nvim-haven"),
inclusions = {}, inclusions = {},
max_history_count = 200, max_history_count = 200,
save_timeout = 10000 save_timeout = 10000
} }
local line_ending = utils.iff(utils.is_windows, "\r\n", "\n") local line_ending = g.iff(os.is_windows, "\r\n", "\n")
local print_message = function(is_error, msg) local print_message = function(is_error, msg)
vim.notify( vim.notify(
msg, msg,
utils.iff(is_error, "error", "info"), g.iff(is_error, "error", "info"),
{ {
title = "nvim-haven" title = "nvim-haven"
} }
@ -87,7 +90,7 @@ local encode = function(str)
end end
local create_save_file = function(buf_info) local create_save_file = function(buf_info)
return utils.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)
@ -383,7 +386,7 @@ local show_picker = function(entries)
local do_forward_jump = function() local do_forward_jump = function()
if jump_state ~= nil and #jump_state.diff_rows > 0 then if jump_state ~= nil and #jump_state.diff_rows > 0 then
jump_state.cur = jump_state.cur =
utils.iff( g.iff(
(jump_state.cur + 1) <= #jump_state.diff_rows, (jump_state.cur + 1) <= #jump_state.diff_rows,
jump_state.cur + 1, jump_state.cur + 1,
#jump_state.diff_rows #jump_state.diff_rows
@ -398,7 +401,7 @@ local show_picker = function(entries)
local do_reverse_jump = function() local do_reverse_jump = function()
if jump_state ~= nil and #jump_state.diff_rows > 0 then if jump_state ~= nil and #jump_state.diff_rows > 0 then
jump_state.cur = utils.iff((jump_state.cur - 1) > 0, jump_state.cur - 1, 1) jump_state.cur = g.iff((jump_state.cur - 1) > 0, jump_state.cur - 1, 1)
jump_to_line( jump_to_line(
jump_state.self, jump_state.self,
jump_state.self.state.bufnr, jump_state.self.state.bufnr,
@ -488,7 +491,7 @@ M.setup = function(config)
true, true,
"'exlcusions' contains an entry that is not a function. Skipping all exclusions until this is corrected:" "'exlcusions' contains an entry that is not a function. Skipping all exclusions until this is corrected:"
) )
utils.dump_table(e) table.print_table(e)
break break
end end
table.insert(haven_config.exclusions, e) table.insert(haven_config.exclusions, e)
@ -504,7 +507,7 @@ M.setup = function(config)
true, true,
"'inclusions' contains an entry that is not a function. Skipping this inclusion until it is corrected:" "'inclusions' contains an entry that is not a function. Skipping this inclusion until it is corrected:"
) )
utils.dump_table(e) table.print_table(e)
end end
table.insert(haven_config.inclusions, e) table.insert(haven_config.inclusions, e)
end end

View File

@ -1,58 +0,0 @@
local M = {}
function string:ends_with(suffix)
return suffix == "" or self:sub(-(#suffix)) == suffix
end
function string:split(sep)
sep = sep or ":"
local fields = {}
local pattern = string.format("([^%s]+)", sep)
_ =
self:gsub(
pattern,
function(c)
fields[#fields + 1] = c
end
)
return fields
end
function string:starts_with(prefix)
return self:sub(1, #prefix) == prefix
end
function table.reverse(self)
local n = #self
local i = 1
while i < n do
self[i], self[n] = self[n], self[i]
i = i + 1
n = n - 1
end
return self
end
function M.iff(b, l, r)
if b then
return l
end
return r
end
M.is_windows = vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1
M.directory_sep = M.iff(M.is_windows, "\\\\", "/")
M.not_directory_sep = M.iff(M.is_windows, "/", "\\\\")
function M.create_path(...)
local Path = require "plenary.path"
local ret =
Path:new({...}):absolute():gsub(M.not_directory_sep, M.directory_sep):gsub(
M.directory_sep .. M.directory_sep,
M.directory_sep
)
return ret
end
return M