feat(config): add helper function for formatters
This commit is contained in:
parent
10b52b8835
commit
ea2b4f0ca1
4 changed files with 38 additions and 22 deletions
|
@ -4,7 +4,11 @@ local config = {}
|
|||
servers = {
|
||||
eslint = false,
|
||||
efm = {
|
||||
format = false -- disable formatting all together
|
||||
disable_formatters = {'eslint', 'prettier', 'stylua'}
|
||||
},
|
||||
tsserver = {
|
||||
format = false,
|
||||
}
|
||||
}
|
||||
} ]]
|
||||
|
|
|
@ -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 = {
|
||||
lsp = {
|
||||
format_on_save = false,
|
||||
servers = {
|
||||
eslint = true,
|
||||
eslint = false,
|
||||
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
|
||||
|
|
|
@ -2,6 +2,7 @@ local config = require('cosmic.config')
|
|||
local M = {}
|
||||
|
||||
local auto_format_lock = false;
|
||||
|
||||
function M.on_attach(client, bufnr)
|
||||
local function buf_set_option(...)
|
||||
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>
|
||||
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
|
||||
|
||||
local formatting_servers = { 'efm', 'eslint' }
|
||||
if vim.tbl_contains(formatting_servers, client.name) then
|
||||
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
|
||||
if config.lsp.format_on_save and not auto_format_lock then
|
||||
auto_format_lock = true -- just run autocommand once
|
||||
vim.cmd([[
|
||||
|
|
|
@ -23,17 +23,17 @@ formatters.prettier = {
|
|||
}
|
||||
|
||||
local filetype_defaults = {
|
||||
css = {},
|
||||
html = {},
|
||||
lua = {},
|
||||
javascript = {},
|
||||
javascriptreact = {},
|
||||
json = {},
|
||||
markdown = {},
|
||||
scss = {},
|
||||
typescript = {},
|
||||
typescriptreact = {},
|
||||
yaml = {},
|
||||
'css',
|
||||
'html',
|
||||
'lua',
|
||||
'javascript',
|
||||
'javascriptreact',
|
||||
'json',
|
||||
'markdown',
|
||||
'scss',
|
||||
'typescript',
|
||||
'typescriptreact',
|
||||
'yaml',
|
||||
}
|
||||
|
||||
formatters.defaults = {
|
||||
|
@ -61,6 +61,7 @@ formatters.defaults = {
|
|||
|
||||
local languages = {}
|
||||
for formatter, filetypes in pairs(formatters.defaults) do
|
||||
-- disable specific formatters
|
||||
if not vim.tbl_contains(config.lsp.servers.efm.disable_formatters, formatter) then
|
||||
for _, filetype in pairs(filetypes) do
|
||||
languages[filetype] = languages[filetype] or {}
|
||||
|
@ -69,9 +70,6 @@ for formatter, filetypes in pairs(formatters.defaults) do
|
|||
end
|
||||
end
|
||||
|
||||
-- print(vim.inspect(languages))
|
||||
-- print(vim.inspect(languages['javascript']['prettier']))
|
||||
|
||||
return {
|
||||
init_options = { documentFormatting = true, codeAction = true },
|
||||
root_dir = function(fname)
|
||||
|
@ -81,6 +79,6 @@ return {
|
|||
or util.root_pattern('.eslintrc.js')(fname)
|
||||
or util.root_pattern('tsconfig.json')(fname)
|
||||
end,
|
||||
filetypes = vim.tbl_keys(filetype_defaults),
|
||||
filetypes = filetype_defaults,
|
||||
settings = { languages = languages },
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue