fix(lsp): run autoformat command just once

This commit is contained in:
Matt Leong 2021-10-25 06:34:53 -07:00
parent 554593e526
commit 10b52b8835
5 changed files with 22 additions and 32 deletions

View file

@ -1,12 +1,12 @@
local config = {}
-- config.lsp = {
-- format_on_save = true,
-- servers = {
-- eslint = false,
-- efm = {
-- disable_formatters = {'eslint', 'prettier', 'stylua'}
-- }
-- }
-- }
--[[ config.lsp = {
format_on_save = true,
servers = {
eslint = false,
efm = {
disable_formatters = {'eslint', 'prettier', 'stylua'}
}
}
} ]]
return config

View file

@ -6,7 +6,7 @@ local default_config = {
servers = {
eslint = true,
efm = {
disable_formatters = { 'eslint', },
disable_formatters = { 'eslint' },
},
},
},

View file

@ -1,6 +1,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, ...)
@ -13,8 +14,8 @@ function M.on_attach(client, bufnr)
if vim.tbl_contains(formatting_servers, client.name) then
client.resolved_capabilities.document_formatting = true
client.resolved_capabilities.document_range_formatting = true
if config.lsp and config.lsp.format_on_save then
-- todo: don't run more than once
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()
]])

View file

@ -56,21 +56,12 @@ formatters.defaults = {
'scss',
'yaml',
},
stylua = { 'lua' }
stylua = { 'lua' },
}
local function is_formatter_disabled(formatter)
if config.lsp and config.lsp.servers and config.lsp.servers.efm and config.lsp.servers.efm.disable_formatters then
if vim.tbl_contains(config.lsp.servers.efm.disable_formatters, formatter) then
return true
end
end
return false
end
local languages = {}
for formatter, filetypes in pairs(formatters.defaults) do
if not is_formatter_disabled(formatter) then
if not vim.tbl_contains(config.lsp.servers.efm.disable_formatters, formatter) then
for _, filetype in pairs(filetypes) do
languages[filetype] = languages[filetype] or {}
table.insert(languages[filetype], formatters[formatter])

View file

@ -30,8 +30,7 @@ local requested_servers = {
-- get disabled servers from config
local disabled_servers = {}
if config.lsp and config.lsp.servers then
for config_server, config_opt in pairs(config.lsp.servers) do
for config_server, config_opt in pairs(config.lsp.servers) do
if config_opt == false then
table.insert(disabled_servers, config_server)
elseif not vim.tbl_contains(requested_servers, config_server) then
@ -39,7 +38,6 @@ if config.lsp and config.lsp.servers then
-- todo: how to handle non-default server opts?
table.insert(requested_servers, config_server)
end
end
end
-- go through requested_servers and ensure installation