chore(configs): clean up

This commit is contained in:
Matt Leong 2021-10-25 08:41:36 -07:00
parent fb512d0036
commit 9e72bafe0f
3 changed files with 35 additions and 14 deletions

View file

@ -1,19 +1,33 @@
-- Override Cosmic configuration options -- Override Cosmic configuration options
--[[ local config = {} --[[
local config = {}
config.lsp = { config.lsp = {
format_on_save = true,
-- true/false or table of filetypes {'.ts', '.js',}
format_on_save = false,
servers = { servers = {
-- enable/disable server
eslint = false, eslint = false,
-- enable non-default servers (todo: support for custom server configs)
rust_analyzer = true,
efm = { efm = {
format = false, -- disable formatting all together -- specifc to efm, e.g. 'eslint', 'prettier', 'lua'
disable_formatters = {}, -- e.g. 'eslint', 'prettier', 'lua' disable_formatters = {},
}, },
tsserver = { tsserver = {
-- disable formatting all together
format = false, format = false,
} }
} }
} }
return config return config
]] ]]

View file

@ -12,15 +12,16 @@ end
-- these settings will be merged with any settings definined in config.lua -- these settings will be merged with any settings definined in config.lua
local default_config = { local default_config = {
lsp = { lsp = {
format_on_save = false, format_on_save = false, -- true/false or table of filetypes {'.ts', '.js',}
servers = { servers = {
eslint = true, eslint = true, -- enable/disable server
-- rust_analyzer = true, -- enable non-default servers (todo: support for custom server configs)
efm = { efm = {
format = true, format = true, -- true or false
disable_formatters = { 'eslint' }, -- e.g. 'eslint', 'prettier', 'stylua' disable_formatters = { 'eslint' }, -- e.g. 'eslint', 'prettier', 'stylua'
}, },
tsserver = { 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 formatting_servers = { 'efm', 'eslint', 'tsserver', 'sumneko_lua', 'rust_analyzer', 'gopls', 'pyright' }
local user_servers = vim.tbl_keys(config.lsp.servers) 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 if not user_servers[client_name] or vim.tbl_contains(formatting_servers, client_name) then
return false return false
end end

View file

@ -14,12 +14,18 @@ function M.on_attach(client, bufnr)
if config.lsp.can_client_format(client.name) then if config.lsp.can_client_format(client.name) then
client.resolved_capabilities.document_formatting = true client.resolved_capabilities.document_formatting = true
client.resolved_capabilities.document_range_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 if config.lsp.format_on_save and not auto_format_lock then
auto_format_lock = true -- just run autocommand once auto_format_lock = true -- just run autocommand once
vim.cmd([[ local format_filetypes = ''
autocmd BufWritePre * lua vim.lsp.buf.formatting() 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 end
else else
client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_formatting = false