From 9e72bafe0f386f5d90041a7e73ec6d85e07b5f1a Mon Sep 17 00:00:00 2001 From: Matt Leong Date: Mon, 25 Oct 2021 08:41:36 -0700 Subject: [PATCH] chore(configs): clean up --- lua/cosmic/config/config.lua | 24 +++++++++++++++++++----- lua/cosmic/config/init.lua | 11 ++++++----- lua/cosmic/lsp/providers/defaults.lua | 14 ++++++++++---- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lua/cosmic/config/config.lua b/lua/cosmic/config/config.lua index 55f630a..99d378c 100644 --- a/lua/cosmic/config/config.lua +++ b/lua/cosmic/config/config.lua @@ -1,19 +1,33 @@ -- Override Cosmic configuration options ---[[ local config = {} +--[[ +local config = {} + config.lsp = { - format_on_save = true, + + -- true/false or table of filetypes {'.ts', '.js',} + format_on_save = false, + servers = { + + -- enable/disable server eslint = false, + + -- enable non-default servers (todo: support for custom server configs) + rust_analyzer = true, + efm = { - format = false, -- disable formatting all together - disable_formatters = {}, -- e.g. 'eslint', 'prettier', 'lua' + -- specifc to efm, e.g. 'eslint', 'prettier', 'lua' + disable_formatters = {}, }, + tsserver = { + -- disable formatting all together format = false, } + } } return config - ]] +]] diff --git a/lua/cosmic/config/init.lua b/lua/cosmic/config/init.lua index 18dcc9c..f75c83a 100644 --- a/lua/cosmic/config/init.lua +++ b/lua/cosmic/config/init.lua @@ -12,15 +12,16 @@ end -- these settings will be merged with any settings definined in config.lua local default_config = { lsp = { - format_on_save = false, + format_on_save = false, -- true/false or table of filetypes {'.ts', '.js',} servers = { - eslint = true, + eslint = true, -- enable/disable server + -- rust_analyzer = true, -- enable non-default servers (todo: support for custom server configs) efm = { - format = true, + format = true, -- true or false disable_formatters = { 'eslint' }, -- e.g. 'eslint', 'prettier', 'stylua' }, tsserver = { - format = false, + format = false, -- disable formatting all together }, }, }, @@ -31,7 +32,7 @@ local config = vim.tbl_deep_extend('force', default_config, user_config) local formatting_servers = { 'efm', 'eslint', 'tsserver', 'sumneko_lua', 'rust_analyzer', 'gopls', 'pyright' } local user_servers = vim.tbl_keys(config.lsp.servers) -function default_config.lsp.can_client_format(client_name) +function config.lsp.can_client_format(client_name) if not user_servers[client_name] or vim.tbl_contains(formatting_servers, client_name) then return false end diff --git a/lua/cosmic/lsp/providers/defaults.lua b/lua/cosmic/lsp/providers/defaults.lua index d04fdbb..265eccb 100644 --- a/lua/cosmic/lsp/providers/defaults.lua +++ b/lua/cosmic/lsp/providers/defaults.lua @@ -14,12 +14,18 @@ function M.on_attach(client, bufnr) if config.lsp.can_client_format(client.name) then client.resolved_capabilities.document_formatting = true client.resolved_capabilities.document_range_formatting = true - -- auto format on save + -- check user config to see if we can format on save if config.lsp.format_on_save and not auto_format_lock then auto_format_lock = true -- just run autocommand once - vim.cmd([[ - autocmd BufWritePre * lua vim.lsp.buf.formatting() - ]]) + local format_filetypes = '' + if (vim.tbl_islist(config.lsp.format_on_save)) then + for _, ft in pairs(config.lsp.format_on_save) do + format_filetypes = format_filetypes .. '*' .. ft + end + else + format_filetypes = '*' + end + vim.cmd(string.format('autocomd BufWritePre %s lua vim.lsp.buf.formatting()', format_filetypes)) end else client.resolved_capabilities.document_formatting = false