refactor: cleaner table merging

This commit is contained in:
Matt Leong 2021-12-21 11:04:27 -08:00
parent 18c4971497
commit 77a4f36784
21 changed files with 76 additions and 56 deletions

View file

@ -37,7 +37,8 @@ local default_config = {
}, },
} }
local config = vim.tbl_deep_extend('force', default_config, user_config) local utils = require('cosmic.utils')
local config = utils.merge(default_config, user_config)
local user_servers = vim.tbl_keys(config.lsp.servers) local user_servers = vim.tbl_keys(config.lsp.servers)
function config.lsp.can_client_format(client_name) function config.lsp.can_client_format(client_name)

View file

@ -1,3 +1,4 @@
local utils = require('cosmic.utils')
local icons = require('cosmic.theme.icons') local icons = require('cosmic.theme.icons')
local config = require('cosmic.config') local config = require('cosmic.config')
@ -16,7 +17,7 @@ end
-- set up vim.diagnostics -- set up vim.diagnostics
-- vim.diagnostic.config opts -- vim.diagnostic.config opts
vim.diagnostic.config(vim.tbl_deep_extend('force', { vim.diagnostic.config(utils.merge({
underline = true, underline = true,
signs = true, signs = true,
update_in_insert = false, update_in_insert = false,

View file

@ -1,3 +1,4 @@
local utils = require('cosmic.utils')
local default_config = require('cosmic.lsp.providers.defaults') local default_config = require('cosmic.lsp.providers.defaults')
local config = require('cosmic.config') local config = require('cosmic.config')
local lsp_installer = require('nvim-lsp-installer') local lsp_installer = require('nvim-lsp-installer')
@ -54,17 +55,17 @@ lsp_installer.on_server_ready(function(server)
-- set up default cosmic options -- set up default cosmic options
if server.name == 'tsserver' then if server.name == 'tsserver' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.tsserver')) opts = utils.merge(opts, require('cosmic.lsp.providers.tsserver'))
elseif server.name == 'jsonls' then elseif server.name == 'jsonls' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.jsonls')) opts = utils.merge(opts, require('cosmic.lsp.providers.jsonls'))
elseif server.name == 'sumneko_lua' then elseif server.name == 'sumneko_lua' then
opts = vim.tbl_deep_extend('force', opts, require('cosmic.lsp.providers.sumneko_lua')) opts = utils.merge(opts, require('cosmic.lsp.providers.sumneko_lua'))
end end
-- override options if user definds them -- override options if user definds them
if type(config.lsp.servers[server.name]) == 'table' then if type(config.lsp.servers[server.name]) == 'table' then
if config.lsp.servers[server.name].opts ~= nil then if config.lsp.servers[server.name].opts ~= nil then
opts = vim.tbl_deep_extend('force', opts, config.lsp.servers[server.name].opts) opts = utils.merge(opts, config.lsp.servers[server.name].opts)
end end
end end

View file

@ -1,20 +1,21 @@
local utils = require('cosmic.utils')
local defaults = require('cosmic.lsp.providers.defaults') local defaults = require('cosmic.lsp.providers.defaults')
local config = require('cosmic.config') local config = require('cosmic.config')
local null_ls = require('null-ls') local null_ls = require('null-ls')
local has_eslint_config = function(utils) local has_eslint_config = function(u)
return utils.root_has_file('.eslintrc') return u.root_has_file('.eslintrc')
or utils.root_has_file('.eslintrc.json') or u.root_has_file('.eslintrc.json')
or utils.root_has_file('.eslintrc.js') or u.root_has_file('.eslintrc.js')
or utils.root_has_file('package.json') or u.root_has_file('package.json')
or utils.root_has_file('.eslintrc.cjs') or u.root_has_file('.eslintrc.cjs')
or utils.root_has_file('.eslintrc.yaml') or u.root_has_file('.eslintrc.yaml')
or utils.root_has_file('.eslintrc.yml') or u.root_has_file('.eslintrc.yml')
end end
local config_opts = config.lsp.servers.null_ls or {} local config_opts = config.lsp.servers.null_ls or {}
require('null-ls').setup(vim.tbl_deep_extend('force', { require('null-ls').setup(utils.merge({
-- you must define at least one source for the plugin to work -- you must define at least one source for the plugin to work
sources = { sources = {
null_ls.builtins.code_actions.eslint_d.with({ null_ls.builtins.code_actions.eslint_d.with({
@ -30,5 +31,4 @@ require('null-ls').setup(vim.tbl_deep_extend('force', {
null_ls.builtins.formatting.prettierd, null_ls.builtins.formatting.prettierd,
null_ls.builtins.formatting.stylua, null_ls.builtins.formatting.stylua,
}, },
}, defaults, config_opts or {})) }, defaults, config_opts or {}))

View file

@ -1,3 +1,4 @@
local utils = require('cosmic.utils')
local default_on_attach = require('cosmic.lsp.providers.defaults').on_attach local default_on_attach = require('cosmic.lsp.providers.defaults').on_attach
local config = require('cosmic.config') local config = require('cosmic.config')
local M = {} local M = {}
@ -8,7 +9,7 @@ function M.on_attach(client, bufnr)
local ts_utils = require('nvim-lsp-ts-utils') local ts_utils = require('nvim-lsp-ts-utils')
-- defaults -- defaults
ts_utils.setup(vim.tbl_deep_extend('force', { ts_utils.setup(utils.merge({
debug = false, debug = false,
disable_commands = false, disable_commands = false,
enable_import_on_completion = true, enable_import_on_completion = true,

View file

@ -1,4 +1,6 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local utils = require('cosmic.utils')
local defaults = { local defaults = {
pre_save_cmds = { 'NvimTreeClose', 'cclose', 'lua vim.notify.dismiss()' }, pre_save_cmds = { 'NvimTreeClose', 'cclose', 'lua vim.notify.dismiss()' },
post_restore_cmds = { 'NvimTreeRefresh' }, post_restore_cmds = { 'NvimTreeRefresh' },
@ -7,4 +9,4 @@ local defaults = {
auto_restore_enabled = false, auto_restore_enabled = false,
} }
require('auto-session').setup(vim.tbl_deep_extend('force', defaults, config.auto_session or {})) require('auto-session').setup(utils.merge(defaults, config.auto_session or {}))

View file

@ -1,6 +1,7 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local utils = require('cosmic.utils')
require('Comment').setup(vim.tbl_deep_extend('force', { require('Comment').setup(utils.merge({
pre_hook = function(ctx) pre_hook = function(ctx)
local U = require('Comment.utils') local U = require('Comment.utils')
local location = nil local location = nil

View file

@ -1,7 +1,8 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local utils = require('cosmic.utils')
local defaults = { local defaults = {
border = 'rounded', border = 'rounded',
} }
require('cosmic-ui').setup(vim.tbl_deep_extend('force', defaults, config.cosmic_ui or {})) require('cosmic-ui').setup(utils.merge(defaults, config.cosmic_ui or {}))

View file

@ -11,7 +11,7 @@ local config = require('cosmic.config')
local get_highlight = require('cosmic.theme.utils').get_highlight local get_highlight = require('cosmic.theme.utils').get_highlight
local statusline_colors = get_highlight('StatusLine') local statusline_colors = get_highlight('StatusLine')
local defaults = vim.tbl_deep_extend('force', { local defaults = utils.merge({
main_icon = icons.cosmic, main_icon = icons.cosmic,
}, config.statusline or {}) }, config.statusline or {})
local main_icon = defaults.main_icon local main_icon = defaults.main_icon

View file

@ -1,6 +1,7 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local utils = require('cosmic.utils')
require('gitsigns').setup(vim.tbl_deep_extend('force', { require('gitsigns').setup(utils.merge({
signs = { signs = {
add = { hl = 'GitSignsAdd', text = '', numhl = 'GitSignsAddNr', linehl = 'GitSignsAddLn' }, add = { hl = 'GitSignsAdd', text = '', numhl = 'GitSignsAddNr', linehl = 'GitSignsAddLn' },
change = { hl = 'GitSignsChange', text = '', numhl = 'GitSignsChangeNr', linehl = 'GitSignsChangeLn' }, change = { hl = 'GitSignsChange', text = '', numhl = 'GitSignsChangeNr', linehl = 'GitSignsChangeLn' },

View file

@ -1,6 +1,7 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local utils = require('cosmic.utils')
require('lsp_signature').setup(vim.tbl_deep_extend('force', { require('lsp_signature').setup(utils.merge({
bind = true, -- This is mandatory, otherwise border config won't get registered. bind = true, -- This is mandatory, otherwise border config won't get registered.
handler_opts = { handler_opts = {
border = config.border, border = config.border,

View file

@ -1,5 +1,7 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local ls = require('luasnip') local ls = require('luasnip')
local utils = require('cosmic.utils')
-- some shorthands... -- some shorthands...
--[[ local s = ls.snippet --[[ local s = ls.snippet
local sn = ls.snippet_node local sn = ls.snippet_node
@ -10,7 +12,7 @@ local c = ls.choice_node
local d = ls.dynamic_node ]] local d = ls.dynamic_node ]]
-- Every unspecified option will be set to the default. -- Every unspecified option will be set to the default.
ls.config.set_config(vim.tbl_deep_extend('force', { ls.config.set_config(utils.merge({
history = true, history = true,
-- Update more often, :h events for more info. -- Update more often, :h events for more info.
updateevents = 'TextChanged,TextChangedI', updateevents = 'TextChanged,TextChangedI',

View file

@ -1,7 +1,8 @@
local icons = require('cosmic.theme.icons')
local config = require('cosmic.config') local config = require('cosmic.config')
local icons = require('cosmic.theme.icons')
local utils = require('cosmic.utils')
require('notify').setup(vim.tbl_deep_extend('force', { require('notify').setup(utils.merge({
icons = { icons = {
ERROR = icons.error, ERROR = icons.error,
WARN = icons.warn, WARN = icons.warn,

View file

@ -1,5 +1,5 @@
local cmp = require('cmp') local cmp = require('cmp')
local utils = require('cosmic-ui.utils') local utils = require('cosmic.utils')
local luasnip = require('luasnip') local luasnip = require('luasnip')
local config = require('cosmic.config') local config = require('cosmic.config')
local icons = require('cosmic.theme.icons') local icons = require('cosmic.theme.icons')

View file

@ -1,6 +1,7 @@
local config = require('cosmic.config')
local g = vim.g local g = vim.g
local icons = require('cosmic.theme.icons') local icons = require('cosmic.theme.icons')
local config = require('cosmic.config') local utils = require('cosmic.utils')
-- settings -- settings
g.nvim_tree_git_hl = 1 g.nvim_tree_git_hl = 1
@ -48,4 +49,4 @@ local args = {
}, },
} }
require('nvim-tree').setup(vim.tbl_deep_extend('force', args, config.nvim_tree or {})) require('nvim-tree').setup(utils.merge(args, config.nvim_tree or {}))

View file

@ -1,6 +1,7 @@
local actions = require('telescope.actions') local actions = require('telescope.actions')
local icons = require('cosmic.theme.icons')
local config = require('cosmic.config') local config = require('cosmic.config')
local icons = require('cosmic.theme.icons')
local utils = require('cosmic.utils')
local default_mappings = { local default_mappings = {
n = { n = {
@ -47,7 +48,7 @@ local opts_flex = {
}, },
} }
require('telescope').setup(vim.tbl_deep_extend('force', { require('telescope').setup(utils.merge({
defaults = { defaults = {
prompt_prefix = '🔍 ', prompt_prefix = '🔍 ',
selection_caret = icons.folder.arrow_closed, selection_caret = icons.folder.arrow_closed,
@ -78,9 +79,9 @@ require('telescope').setup(vim.tbl_deep_extend('force', {
}, },
}, },
pickers = { pickers = {
buffers = vim.tbl_deep_extend('force', opts_flex, { buffers = utils.merge(opts_flex, {
prompt_title = '✨ Search Buffers ✨', prompt_title = '✨ Search Buffers ✨',
mappings = vim.tbl_deep_extend('force', { mappings = utils.merge({
n = { n = {
['d'] = actions.delete_buffer, ['d'] = actions.delete_buffer,
}, },
@ -88,46 +89,46 @@ require('telescope').setup(vim.tbl_deep_extend('force', {
sort_mru = true, sort_mru = true,
preview_title = false, preview_title = false,
}), }),
lsp_code_actions = vim.tbl_deep_extend('force', opts_cursor, { lsp_code_actions = utils.merge(opts_cursor, {
prompt_title = 'Code Actions', prompt_title = 'Code Actions',
}), }),
lsp_range_code_actions = vim.tbl_deep_extend('force', opts_vertical, { lsp_range_code_actions = utils.merge(opts_vertical, {
prompt_title = 'Code Actions', prompt_title = 'Code Actions',
}), }),
lsp_document_diagnostics = vim.tbl_deep_extend('force', opts_vertical, { lsp_document_diagnostics = utils.merge(opts_vertical, {
prompt_title = 'Document Diagnostics', prompt_title = 'Document Diagnostics',
mappings = default_mappings, mappings = default_mappings,
}), }),
lsp_implementations = vim.tbl_deep_extend('force', opts_cursor, { lsp_implementations = utils.merge(opts_cursor, {
prompt_title = 'Implementations', prompt_title = 'Implementations',
mappings = default_mappings, mappings = default_mappings,
}), }),
lsp_definitions = vim.tbl_deep_extend('force', opts_cursor, { lsp_definitions = utils.merge(opts_cursor, {
prompt_title = 'Definitions', prompt_title = 'Definitions',
mappings = default_mappings, mappings = default_mappings,
}), }),
lsp_references = vim.tbl_deep_extend('force', opts_vertical, { lsp_references = utils.merge(opts_vertical, {
prompt_title = 'References', prompt_title = 'References',
mappings = default_mappings, mappings = default_mappings,
}), }),
find_files = vim.tbl_deep_extend('force', opts_flex, { find_files = utils.merge(opts_flex, {
prompt_title = '✨ Search Project ✨', prompt_title = '✨ Search Project ✨',
mappings = default_mappings, mappings = default_mappings,
hidden = true, hidden = true,
}), }),
diagnostics = vim.tbl_deep_extend('force', opts_vertical, { diagnostics = utils.merge(opts_vertical, {
mappings = default_mappings, mappings = default_mappings,
}), }),
git_files = vim.tbl_deep_extend('force', opts_flex, { git_files = utils.merge(opts_flex, {
prompt_title = '✨ Search Git Project ✨', prompt_title = '✨ Search Git Project ✨',
mappings = default_mappings, mappings = default_mappings,
hidden = true, hidden = true,
}), }),
live_grep = vim.tbl_deep_extend('force', opts_flex, { live_grep = utils.merge(opts_flex, {
prompt_title = '✨ Live Grep ✨', prompt_title = '✨ Live Grep ✨',
mappings = default_mappings, mappings = default_mappings,
}), }),
grep_string = vim.tbl_deep_extend('force', opts_vertical, { grep_string = utils.merge(opts_vertical, {
prompt_title = '✨ Grep String ✨', prompt_title = '✨ Grep String ✨',
mappings = default_mappings, mappings = default_mappings,
}), }),

View file

@ -1,7 +1,8 @@
local icons = require('cosmic.theme.icons')
local config = require('cosmic.config') local config = require('cosmic.config')
local icons = require('cosmic.theme.icons')
local utils = require('cosmic.utils')
require('todo-comments').setup(vim.tbl_deep_extend('force', { require('todo-comments').setup(utils.merge({
keywords = { keywords = {
FIX = { FIX = {
icon = icons.debug, -- icon used for the sign, and in search results icon = icons.debug, -- icon used for the sign, and in search results

View file

@ -1,4 +1,5 @@
local config = require('cosmic.config') local config = require('cosmic.config')
local utils = require('cosmic.utils')
local defaults = { local defaults = {
ensure_installed = { ensure_installed = {
@ -35,4 +36,4 @@ local defaults = {
}, },
} }
require('nvim-treesitter.configs').setup(vim.tbl_deep_extend('force', defaults, config.treesitter or {})) require('nvim-treesitter.configs').setup(utils.merge(defaults, config.treesitter or {}))

View file

@ -5,12 +5,13 @@ end
local highlight = require('cosmic.theme.utils').highlight local highlight = require('cosmic.theme.utils').highlight
local get_highlight = require('cosmic.theme.utils').get_highlight local get_highlight = require('cosmic.theme.utils').get_highlight
local set_highlight = require('cosmic.theme.utils').set_highlight local set_highlight = require('cosmic.theme.utils').set_highlight
local utils = require('cosmic.utils')
local statusline_colors = get_highlight('StatusLine') local statusline_colors = get_highlight('StatusLine')
local error_colors = get_highlight('DiagnosticError') local error_colors = get_highlight('DiagnosticError')
set_highlight( set_highlight(
'DiagnosticErrorInv', 'DiagnosticErrorInv',
vim.tbl_extend('force', error_colors, { utils.merge(error_colors, {
guibg = error_colors.guifg, guibg = error_colors.guifg,
guifg = statusline_colors.guibg, guifg = statusline_colors.guibg,
}) })
@ -19,7 +20,7 @@ set_highlight(
local warning_colors = get_highlight('DiagnosticWarn') local warning_colors = get_highlight('DiagnosticWarn')
set_highlight( set_highlight(
'DiagnosticWarnInv', 'DiagnosticWarnInv',
vim.tbl_extend('force', warning_colors, { utils.merge(warning_colors, {
guibg = warning_colors.guifg, guibg = warning_colors.guifg,
guifg = statusline_colors.guibg, guifg = statusline_colors.guibg,
}) })
@ -28,7 +29,7 @@ set_highlight(
local hint_colors = get_highlight('DiagnosticHint') local hint_colors = get_highlight('DiagnosticHint')
set_highlight( set_highlight(
'DiagnosticHintInv', 'DiagnosticHintInv',
vim.tbl_extend('force', hint_colors, { utils.merge(hint_colors, {
guibg = hint_colors.guifg, guibg = hint_colors.guifg,
guifg = statusline_colors.guibg, guifg = statusline_colors.guibg,
}) })
@ -37,7 +38,7 @@ set_highlight(
local info_colors = get_highlight('DiagnosticInfo') local info_colors = get_highlight('DiagnosticInfo')
set_highlight( set_highlight(
'DiagnosticInfoInv', 'DiagnosticInfoInv',
vim.tbl_extend('force', info_colors, { utils.merge(info_colors, {
guibg = info_colors.guifg, guibg = info_colors.guifg,
guifg = statusline_colors.guibg, guifg = statusline_colors.guibg,
}) })

View file

@ -1,4 +1,3 @@
local Logger = require('cosmic.utils.logger')
local M = {} local M = {}
function M.map(mode, lhs, rhs, opts) function M.map(mode, lhs, rhs, opts)
@ -17,7 +16,7 @@ function M.buf_map(bufnr, mode, lhs, rhs, opts)
vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, options) vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, options)
end end
M.merge = function(...) function M.merge(...)
return vim.tbl_deep_extend('force', ...) return vim.tbl_deep_extend('force', ...)
end end
@ -67,6 +66,7 @@ local function clear_cache()
end end
function M.post_reload(msg) function M.post_reload(msg)
local Logger = require('cosmic.utils.logger')
unload('cosmic.utils', true) unload('cosmic.utils', true)
unload('cosmic.theme', true) unload('cosmic.theme', true)
unload('cosmic.plugins.statusline', true) unload('cosmic.plugins.statusline', true)
@ -102,6 +102,7 @@ end
-- update instance of CosmicNvim -- update instance of CosmicNvim
function M.update() function M.update()
local Logger = require('cosmic.utils.logger')
local Job = require('plenary.job') local Job = require('plenary.job')
local path = M.get_install_dir() local path = M.get_install_dir()
local errors = {} local errors = {}

View file

@ -1,3 +1,4 @@
local utils = require('cosmic.utils')
local Logger = {} local Logger = {}
Logger.__index = Logger Logger.__index = Logger
@ -8,7 +9,7 @@ function Logger:log(msg, opts)
vim.notify( vim.notify(
msg, msg,
vim.log.levels.INFO, vim.log.levels.INFO,
vim.tbl_deep_extend('force', { utils.merge({
title = title, title = title,
}, opts) }, opts)
) )
@ -19,7 +20,7 @@ function Logger:warn(msg, opts)
vim.notify( vim.notify(
msg, msg,
vim.log.levels.WARN, vim.log.levels.WARN,
vim.tbl_deep_extend('force', { utils.merge({
title = title, title = title,
}, opts) }, opts)
) )
@ -30,7 +31,7 @@ function Logger:error(msg, opts)
vim.notify( vim.notify(
msg, msg,
vim.log.levels.ERROR, vim.log.levels.ERROR,
vim.tbl_deep_extend('force', { utils.merge({
title = title, title = title,
}, opts) }, opts)
) )