2021-07-26 19:14:23 +02:00
|
|
|
local utils = {}
|
2021-08-09 19:02:37 +02:00
|
|
|
local Log = require "core.log"
|
|
|
|
local uv = vim.loop
|
2021-03-17 05:17:41 +01:00
|
|
|
|
2021-07-18 19:57:05 +02:00
|
|
|
-- recursive Print (structure, limit, separator)
|
|
|
|
local function r_inspect_settings(structure, limit, separator)
|
|
|
|
limit = limit or 100 -- default item limit
|
|
|
|
separator = separator or "." -- indent string
|
|
|
|
if limit < 1 then
|
|
|
|
print "ERROR: Item limit reached."
|
|
|
|
return limit - 1
|
|
|
|
end
|
|
|
|
if structure == nil then
|
|
|
|
io.write("-- O", separator:sub(2), " = nil\n")
|
|
|
|
return limit - 1
|
|
|
|
end
|
|
|
|
local ts = type(structure)
|
|
|
|
|
|
|
|
if ts == "table" then
|
|
|
|
for k, v in pairs(structure) do
|
2021-08-06 13:20:25 +02:00
|
|
|
-- replace non alpha keys with ["key"]
|
2021-07-18 19:57:05 +02:00
|
|
|
if tostring(k):match "[^%a_]" then
|
|
|
|
k = '["' .. tostring(k) .. '"]'
|
|
|
|
end
|
|
|
|
limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
|
|
|
|
if limit < 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return limit
|
|
|
|
end
|
|
|
|
|
|
|
|
if ts == "string" then
|
|
|
|
-- escape sequences
|
|
|
|
structure = string.format("%q", structure)
|
|
|
|
end
|
|
|
|
separator = separator:gsub("%.%[", "%[")
|
|
|
|
if type(structure) == "function" then
|
|
|
|
-- don't print functions
|
2021-07-25 07:46:09 +02:00
|
|
|
io.write("-- lvim", separator:sub(2), " = function ()\n")
|
2021-07-18 19:57:05 +02:00
|
|
|
else
|
2021-07-25 07:46:09 +02:00
|
|
|
io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
|
2021-07-18 19:57:05 +02:00
|
|
|
end
|
|
|
|
return limit - 1
|
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.generate_settings()
|
2021-07-18 19:57:05 +02:00
|
|
|
-- Opens a file in append mode
|
|
|
|
local file = io.open("lv-settings.lua", "w")
|
|
|
|
|
|
|
|
-- sets the default output file as test.lua
|
|
|
|
io.output(file)
|
|
|
|
|
2021-07-25 07:45:08 +02:00
|
|
|
-- write all `lvim` related settings to `lv-settings.lua` file
|
|
|
|
r_inspect_settings(lvim, 10000, ".")
|
2021-07-18 19:57:05 +02:00
|
|
|
|
|
|
|
-- closes the open file
|
|
|
|
io.close(file)
|
|
|
|
end
|
|
|
|
|
2021-07-25 03:17:11 +02:00
|
|
|
-- autoformat
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.toggle_autoformat()
|
2021-07-25 03:17:11 +02:00
|
|
|
if lvim.format_on_save then
|
2021-07-25 13:55:19 +02:00
|
|
|
require("core.autocmds").define_augroups {
|
2021-07-25 03:17:11 +02:00
|
|
|
autoformat = {
|
|
|
|
{
|
2021-07-26 14:57:18 +02:00
|
|
|
"BufWritePre",
|
2021-07-25 03:17:11 +02:00
|
|
|
"*",
|
2021-07-26 09:02:25 +02:00
|
|
|
":silent lua vim.lsp.buf.formatting_sync()",
|
2021-07-25 03:17:11 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-08-26 20:32:16 +02:00
|
|
|
Log:debug "Format on save active"
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not lvim.format_on_save then
|
2021-07-26 19:14:23 +02:00
|
|
|
vim.cmd [[
|
|
|
|
if exists('#autoformat#BufWritePre')
|
|
|
|
:autocmd! autoformat
|
|
|
|
endif
|
|
|
|
]]
|
2021-08-26 20:32:16 +02:00
|
|
|
Log:debug "Format on save off"
|
2021-07-25 03:17:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.reload_lv_config()
|
2021-08-20 11:16:29 +02:00
|
|
|
require("core.lualine").config()
|
2021-08-25 07:47:48 +02:00
|
|
|
|
|
|
|
local config = require "config"
|
|
|
|
config:load()
|
|
|
|
|
2021-08-10 20:53:56 +02:00
|
|
|
require("keymappings").setup() -- this should be done before loading the plugins
|
2021-07-20 04:50:07 +02:00
|
|
|
vim.cmd "source ~/.local/share/lunarvim/lvim/lua/plugins.lua"
|
|
|
|
local plugins = require "plugins"
|
|
|
|
local plugin_loader = require("plugin-loader").init()
|
2021-07-26 19:14:23 +02:00
|
|
|
utils.toggle_autoformat()
|
2021-07-25 03:17:11 +02:00
|
|
|
plugin_loader:load { plugins, lvim.plugins }
|
2021-07-07 01:15:37 +02:00
|
|
|
vim.cmd ":PackerCompile"
|
|
|
|
vim.cmd ":PackerInstall"
|
2021-07-13 03:59:16 +02:00
|
|
|
-- vim.cmd ":PackerClean"
|
2021-08-13 22:32:56 +02:00
|
|
|
local null_ls = require "lsp.null-ls"
|
|
|
|
null_ls.setup(vim.bo.filetype, { force_reload = true })
|
2021-08-26 12:49:29 +02:00
|
|
|
Log:info "Reloaded configuration"
|
2021-08-06 16:27:19 +02:00
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.unrequire(m)
|
2021-07-13 06:48:37 +02:00
|
|
|
package.loaded[m] = nil
|
|
|
|
_G[m] = nil
|
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
function utils.gsub_args(args)
|
2021-07-18 20:36:08 +02:00
|
|
|
if args == nil or type(args) ~= "table" then
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
local buffer_filepath = vim.fn.fnameescape(vim.api.nvim_buf_get_name(0))
|
|
|
|
for i = 1, #args do
|
2021-07-19 18:12:59 +02:00
|
|
|
args[i] = string.gsub(args[i], "${FILEPATH}", buffer_filepath)
|
2021-07-18 20:36:08 +02:00
|
|
|
end
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
|
2021-08-09 19:02:37 +02:00
|
|
|
--- Checks whether a given path exists and is a file.
|
|
|
|
--@param filename (string) path to check
|
|
|
|
--@returns (bool)
|
|
|
|
function utils.is_file(filename)
|
|
|
|
local stat = uv.fs_stat(filename)
|
|
|
|
return stat and stat.type == "file" or false
|
2021-07-31 06:06:08 +02:00
|
|
|
end
|
|
|
|
|
2021-07-26 19:14:23 +02:00
|
|
|
return utils
|
2021-07-05 20:49:07 +02:00
|
|
|
|
2021-07-10 23:34:20 +02:00
|
|
|
-- TODO: find a new home for these autocommands
|