summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/utils/init.lua
diff options
context:
space:
mode:
authorGustaf Rydholm <gustaf.rydholm@gmail.com>2021-08-10 23:15:04 +0200
committerGustaf Rydholm <gustaf.rydholm@gmail.com>2021-08-10 23:15:04 +0200
commit540268d618627079c9b958a955b586e1888b46a8 (patch)
tree1a22a5feb457135178b9d4fe8b6c1755f5ca66bc /.config/nvim/lua/utils/init.lua
parente79bd3273f58ba38e8fcd716090b89326791afbb (diff)
Major refactor of nvim
Diffstat (limited to '.config/nvim/lua/utils/init.lua')
-rw-r--r--.config/nvim/lua/utils/init.lua207
1 files changed, 76 insertions, 131 deletions
diff --git a/.config/nvim/lua/utils/init.lua b/.config/nvim/lua/utils/init.lua
index bad55ce..1176e3b 100644
--- a/.config/nvim/lua/utils/init.lua
+++ b/.config/nvim/lua/utils/init.lua
@@ -1,4 +1,6 @@
local utils = {}
+local Log = require "core.log"
+local uv = vim.loop
-- recursive Print (structure, limit, separator)
local function r_inspect_settings(structure, limit, separator)
@@ -16,7 +18,7 @@ local function r_inspect_settings(structure, limit, separator)
if ts == "table" then
for k, v in pairs(structure) do
- -- replace non alpha keys wih ["key"]
+ -- replace non alpha keys with ["key"]
if tostring(k):match "[^%a_]" then
k = '["' .. tostring(k) .. '"]'
end
@@ -35,9 +37,9 @@ local function r_inspect_settings(structure, limit, separator)
separator = separator:gsub("%.%[", "%[")
if type(structure) == "function" then
-- don't print functions
- io.write("-- O", separator:sub(2), " = function ()\n")
+ io.write("-- options", separator:sub(2), " = function ()\n")
else
- io.write("O", separator:sub(2), " = ", tostring(structure), "\n")
+ io.write("options", separator:sub(2), " = ", tostring(structure), "\n")
end
return limit - 1
end
@@ -49,24 +51,55 @@ function utils.generate_settings()
-- sets the default output file as test.lua
io.output(file)
- -- write all `O` related settings to `settings.lua` file
- r_inspect_settings(O, 10000, ".")
+ -- write all `options` related settings to `lv-settings.lua` file
+ r_inspect_settings(options, 10000, ".")
-- closes the open file
io.close(file)
end
+-- autoformat
+function utils.toggle_autoformat()
+ if options.format_on_save then
+ require("core.autocmds").define_augroups {
+ autoformat = {
+ {
+ "BufWritePre",
+ "*",
+ ":silent lua vim.lsp.buf.formatting_sync()",
+ },
+ },
+ }
+ if Log:get_default() then
+ Log:get_default().info "Format on save active"
+ end
+ end
+
+ if not options.format_on_save then
+ vim.cmd [[
+ if exists('#autoformat#BufWritePre')
+ :autocmd! autoformat
+ endif
+ ]]
+ if Log:get_default() then
+ Log:get_default().info "Format on save off"
+ end
+ end
+end
+
function utils.reload_config()
- vim.cmd "source ~/.config/nvim/config.lua"
+ vim.cmd "source ~/.config/nvim/lua/settings.lua"
+ vim.cmd("source " .. USER_CONFIG_PATH)
+ require("keymappings").setup()
vim.cmd "source ~/.config/nvim/lua/plugins.lua"
local plugins = require "plugins"
local plugin_loader = require("plugin-loader").init()
- plugin_loader:load { plugins, O.user_plugins }
- vim.cmd "source ~/.config/nvim/lua/settings.lua"
- vim.cmd "source ~/.config/nvim/lua/core/formatter.lua"
+ utils.toggle_autoformat()
+ plugin_loader:load { plugins, options.plugins }
vim.cmd ":PackerCompile"
vim.cmd ":PackerInstall"
-- vim.cmd ":PackerClean"
+ Log:get_default().info "Reloaded configuration"
end
function utils.check_lsp_client_active(name)
@@ -79,52 +112,42 @@ function utils.check_lsp_client_active(name)
return false
end
-function utils.add_keymap(mode, opts, keymaps)
- for _, keymap in ipairs(keymaps) do
- vim.api.nvim_set_keymap(mode, keymap[1], keymap[2], opts)
+function utils.get_active_client_by_ft(filetype)
+ local clients = vim.lsp.get_active_clients()
+ for _, client in pairs(clients) do
+ if client.name == options.lang[filetype].lsp.provider then
+ return client
+ end
end
+ return nil
end
-function utils.add_keymap_normal_mode(opts, keymaps)
- utils.add_keymap("n", opts, keymaps)
-end
+-- TODO: consider porting this logic to null-ls instead
+function utils.get_supported_linters_by_filetype(filetype)
+ local null_ls = require "null-ls"
+ local matches = {}
+ for _, provider in pairs(null_ls.builtins.diagnostics) do
+ if vim.tbl_contains(provider.filetypes, filetype) then
+ local provider_name = provider.name
-function utils.add_keymap_visual_mode(opts, keymaps)
- utils.add_keymap("v", opts, keymaps)
-end
-
-function utils.add_keymap_visual_block_mode(opts, keymaps)
- utils.add_keymap("x", opts, keymaps)
-end
-
-function utils.add_keymap_insert_mode(opts, keymaps)
- utils.add_keymap("i", opts, keymaps)
-end
+ table.insert(matches, provider_name)
+ end
+ end
-function utils.add_keymap_term_mode(opts, keymaps)
- utils.add_keymap("t", opts, keymaps)
+ return matches
end
-function utils.define_augroups(definitions) -- {{{1
- -- Create autocommand groups based on the passed definitions
- --
- -- The key will be the name of the group, and each definition
- -- within the group should have:
- -- 1. Trigger
- -- 2. Pattern
- -- 3. Text
- -- just like how they would normally be defined from Vim itself
- for group_name, definition in pairs(definitions) do
- vim.cmd("augroup " .. group_name)
- vim.cmd "autocmd!"
-
- for _, def in pairs(definition) do
- local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ")
- vim.cmd(command)
+function utils.get_supported_formatters_by_filetype(filetype)
+ local null_ls = require "null-ls"
+ local matches = {}
+ for _, provider in pairs(null_ls.builtins.formatting) do
+ if provider.filetypes and vim.tbl_contains(provider.filetypes, filetype) then
+ -- table.insert(matches, { provider.name, ft })
+ table.insert(matches, provider.name)
end
-
- vim.cmd "augroup END"
end
+
+ return matches
end
function utils.unrequire(m)
@@ -132,80 +155,6 @@ function utils.unrequire(m)
_G[m] = nil
end
-utils.define_augroups {
-
- _general_settings = {
- {
- "TextYankPost",
- "*",
- "lua require('vim.highlight').on_yank({higroup = 'Search', timeout = 200})",
- },
- {
- "BufWinEnter",
- "*",
- "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
- },
- {
- "BufWinEnter",
- "dashboard",
- "setlocal cursorline signcolumn=yes cursorcolumn number",
- },
- {
- "BufRead",
- "*",
- "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
- },
- {
- "BufNewFile",
- "*",
- "setlocal formatoptions-=c formatoptions-=r formatoptions-=o",
- },
- { "BufWritePost", "config.lua", "lua require('utils').reload_config()" },
- -- { "VimLeavePre", "*", "set title set titleold=" },
- },
- _solidity = {
- { "BufWinEnter", ".tf", "setlocal filetype=hcl" },
- { "BufRead", "*.tf", "setlocal filetype=hcl" },
- { "BufNewFile", "*.tf", "setlocal filetype=hcl" },
- },
- -- _solidity = {
- -- {'BufWinEnter', '.sol', 'setlocal filetype=solidity'}, {'BufRead', '*.sol', 'setlocal filetype=solidity'},
- -- {'BufNewFile', '*.sol', 'setlocal filetype=solidity'}
- -- },
- -- _gemini = {
- -- {'BufWinEnter', '.gmi', 'setlocal filetype=markdown'}, {'BufRead', '*.gmi', 'setlocal filetype=markdown'},
- -- {'BufNewFile', '*.gmi', 'setlocal filetype=markdown'}
- -- },
- _markdown = {
- { "FileType", "markdown", "setlocal wrap" },
- { "FileType", "markdown", "setlocal spell" },
- },
- _buffer_bindings = {
- { "FileType", "floaterm", "nnoremap <silent> <buffer> q :q<CR>" },
- },
- _auto_resize = {
- -- will cause split windows to be resized evenly if main window is resized
- { "VimResized", "*", "wincmd =" },
- },
- _packer_compile = {
- -- will cause split windows to be resized evenly if main window is resized
- { "BufWritePost", "plugins.lua", "PackerCompile" },
- },
-
- -- _fterm_lazygit = {
- -- -- will cause esc key to exit lazy git
- -- {"TermEnter", "*", "call LazyGitNativation()"}
- -- },
- -- _mode_switching = {
- -- -- will switch between absolute and relative line numbers depending on mode
- -- {'InsertEnter', '*', 'if &relativenumber | let g:ms_relativenumberoff = 1 | setlocal number norelativenumber | endif'},
- -- {'InsertLeave', '*', 'if exists("g:ms_relativenumberoff") | setlocal relativenumber | endif'},
- -- {'InsertEnter', '*', 'if &cursorline | let g:ms_cursorlineoff = 1 | setlocal nocursorline | endif'},
- -- {'InsertLeave', '*', 'if exists("g:ms_cursorlineoff") | setlocal cursorline | endif'},
- -- },
- _user_autocommands = O.user_autocommands,
-}
-
function utils.gsub_args(args)
if args == nil or type(args) ~= "table" then
return args
@@ -217,16 +166,12 @@ function utils.gsub_args(args)
return args
end
-vim.cmd [[
- function! QuickFixToggle()
- if empty(filter(getwininfo(), 'v:val.quickfix'))
- copen
- else
- cclose
- endif
-endfunction
-]]
+--- 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
+end
return utils
-
--- TODO: find a new home for these autocommands"