fix(LSP): respect user configs for formatting
This commit is contained in:
parent
885ab36992
commit
b968af5b9c
1 changed files with 27 additions and 26 deletions
|
@ -13,34 +13,34 @@ 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')
|
||||||
|
|
||||||
if config.lsp.can_client_format(client.name) then
|
if config.lsp.format_on_save and not auto_format_lock then
|
||||||
client.server_capabilities.documentFormatting = true
|
auto_format_lock = true -- just run autocommand once
|
||||||
client.server_capabilities.documentRangeFormatting = true
|
local format_filetypes = ''
|
||||||
-- check user config to see if we can format on save
|
if vim.tbl_islist(config.lsp.format_on_save) then
|
||||||
if config.lsp.format_on_save and not auto_format_lock then
|
for _, ft in pairs(config.lsp.format_on_save) do
|
||||||
auto_format_lock = true -- just run autocommand once
|
format_filetypes = format_filetypes .. '*' .. ft
|
||||||
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
|
end
|
||||||
|
else
|
||||||
vim.api.nvim_create_autocmd(string.format('BufWritePre %s', format_filetypes), {
|
format_filetypes = '*'
|
||||||
callback = function()
|
|
||||||
vim.lsp.buf.format({
|
|
||||||
timeout_ms = config.lsp.format_timeout,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
group = group,
|
|
||||||
nested = true,
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
client.server_capabilities.documentFormatting = false
|
vim.api.nvim_create_autocmd(string.format('BufWritePre %s', format_filetypes), {
|
||||||
client.server_capabilities.documentRangeFormatting = false
|
callback = function()
|
||||||
|
vim.lsp.buf.format({
|
||||||
|
timeout_ms = config.lsp.format_timeout,
|
||||||
|
-- check user config to see if we can format on save
|
||||||
|
filter = function(clients)
|
||||||
|
return vim.tbl_filter(function(ct)
|
||||||
|
if config.lsp.can_client_format(ct.name) then
|
||||||
|
return ct.name
|
||||||
|
end
|
||||||
|
end, clients)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
group = group,
|
||||||
|
nested = true,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
require('cosmic.lsp.mappings').init(client, bufnr)
|
require('cosmic.lsp.mappings').init(client, bufnr)
|
||||||
|
@ -58,6 +58,7 @@ M.root_dir = function(fname)
|
||||||
or util.root_pattern('tsconfig.base.json')(fname)
|
or util.root_pattern('tsconfig.base.json')(fname)
|
||||||
or util.root_pattern('package.json')(fname)
|
or util.root_pattern('package.json')(fname)
|
||||||
or util.root_pattern('.eslintrc.js')(fname)
|
or util.root_pattern('.eslintrc.js')(fname)
|
||||||
|
or util.root_pattern('.eslintrc.json')(fname)
|
||||||
or util.root_pattern('tsconfig.json')(fname)
|
or util.root_pattern('tsconfig.json')(fname)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue