feat(config): add helper function for formatters

This commit is contained in:
Matt Leong 2021-10-25 07:13:42 -07:00
parent 10b52b8835
commit ea2b4f0ca1
4 changed files with 38 additions and 22 deletions

View file

@ -4,8 +4,12 @@ local config = {}
servers = { servers = {
eslint = false, eslint = false,
efm = { efm = {
format = false -- disable formatting all together
disable_formatters = {'eslint', 'prettier', 'stylua'} disable_formatters = {'eslint', 'prettier', 'stylua'}
} },
tsserver = {
format = false,
}
} }
} ]] } ]]

View file

@ -1,15 +1,28 @@
local config = require('cosmic.config.config') local user_config = require('cosmic.config.config')
-- 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,
servers = { servers = {
eslint = true, eslint = false,
efm = { efm = {
disable_formatters = { 'eslint' }, format = true,
disable_formatters = {}, -- e.g. 'eslint', 'prettier', 'stylua'
}, },
tsserver = {
format = false,
}
}, },
}, },
} }
return vim.tbl_deep_extend('force', default_config, config) local config = vim.tbl_deep_extend('force', default_config, user_config)
-- default servers that can be formatted
local formatting_servers = { 'efm', 'eslint', 'tsserver', 'sumneko_lua', 'rust_analyzer', 'gopls', 'pyright' }
function default_config.lsp.can_client_format(client_name)
return (config.lsp.servers[client_name] and config.lsp.servers[client_name].format and vim.tbl_contains(formatting_servers, client_name))
end
return config

View file

@ -2,6 +2,7 @@ local config = require('cosmic.config')
local M = {} local M = {}
local auto_format_lock = false; local auto_format_lock = false;
function M.on_attach(client, bufnr) function M.on_attach(client, bufnr)
local function buf_set_option(...) local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...) vim.api.nvim_buf_set_option(bufnr, ...)
@ -10,10 +11,10 @@ function M.on_attach(client, bufnr)
-- Enable completion triggered by <c-x><c-o> -- Enable completion triggered by <c-x><c-o>
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
local formatting_servers = { 'efm', 'eslint' } if config.lsp.can_client_format(client.name) then
if vim.tbl_contains(formatting_servers, 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
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([[ vim.cmd([[

View file

@ -23,17 +23,17 @@ formatters.prettier = {
} }
local filetype_defaults = { local filetype_defaults = {
css = {}, 'css',
html = {}, 'html',
lua = {}, 'lua',
javascript = {}, 'javascript',
javascriptreact = {}, 'javascriptreact',
json = {}, 'json',
markdown = {}, 'markdown',
scss = {}, 'scss',
typescript = {}, 'typescript',
typescriptreact = {}, 'typescriptreact',
yaml = {}, 'yaml',
} }
formatters.defaults = { formatters.defaults = {
@ -61,6 +61,7 @@ formatters.defaults = {
local languages = {} local languages = {}
for formatter, filetypes in pairs(formatters.defaults) do for formatter, filetypes in pairs(formatters.defaults) do
-- disable specific formatters
if not vim.tbl_contains(config.lsp.servers.efm.disable_formatters, formatter) then if not vim.tbl_contains(config.lsp.servers.efm.disable_formatters, formatter) then
for _, filetype in pairs(filetypes) do for _, filetype in pairs(filetypes) do
languages[filetype] = languages[filetype] or {} languages[filetype] = languages[filetype] or {}
@ -69,9 +70,6 @@ for formatter, filetypes in pairs(formatters.defaults) do
end end
end end
-- print(vim.inspect(languages))
-- print(vim.inspect(languages['javascript']['prettier']))
return { return {
init_options = { documentFormatting = true, codeAction = true }, init_options = { documentFormatting = true, codeAction = true },
root_dir = function(fname) root_dir = function(fname)
@ -81,6 +79,6 @@ return {
or util.root_pattern('.eslintrc.js')(fname) or util.root_pattern('.eslintrc.js')(fname)
or util.root_pattern('tsconfig.json')(fname) or util.root_pattern('tsconfig.json')(fname)
end, end,
filetypes = vim.tbl_keys(filetype_defaults), filetypes = filetype_defaults,
settings = { languages = languages }, settings = { languages = languages },
} }