update format_on_save setup

pull/235/head
ray-x 2 years ago
parent e882757fb2
commit 1cbad111c7

@ -246,22 +246,41 @@ local function set_mapping(lsp_attach_info)
-- if user_opts.cap.document_formatting then -- if user_opts.cap.document_formatting then
if doc_fmt and _NgConfigValues.lsp.format_on_save then if doc_fmt and _NgConfigValues.lsp.format_on_save then
local fos = _NgConfigValues.lsp.format_on_save
local gn = api.nvim_create_augroup('NavAuGroupFormat', {}) local gn = api.nvim_create_augroup('NavAuGroupFormat', {})
local fmt = false
if type(fos) == 'boolean' then
fmt = fos
end
if type(fos) == 'table' and (fos.enable or fos.disable) then
if fos.enable then
fmt = vim.tbl_contains(fos.enable, vim.o.ft)
end
if not fmt and fos.disable then
fmt = not vim.tbl_contains(fos.disable, vim.o.ft)
end
end
if type(fos) == 'function' then
fmt = fos(bufnr)
end
local fopts = _NgConfigValues.lsp.format_options local fopts = _NgConfigValues.lsp.format_options
if not fopts.async and vim.api.nvim_buf_line_count(0) > 4000 then if not fopts.async and vim.api.nvim_buf_line_count(0) > 4000 then
fopts.async = true fopts.async = true
end end
api.nvim_create_autocmd({ 'BufWritePre' }, { if fmt then
group = gn, api.nvim_create_autocmd({ 'BufWritePre' }, {
buffer = bufnr, group = gn,
callback = function() desc = 'auto format',
trace('format' .. vim.inspect(fopts)) buffer = bufnr,
vim.lsp.buf.format(fopts) callback = function()
end, trace('format' .. vim.inspect(fopts))
}) vim.lsp.buf.format(fopts)
end,
})
end
elseif fmtkey then elseif fmtkey then
del_keymap('n', fmtkey) del_keymap('n', fmtkey)
end end
@ -285,6 +304,7 @@ local function autocmd()
api.nvim_create_autocmd({ 'BufWritePre' }, { api.nvim_create_autocmd({ 'BufWritePre' }, {
group = gn, group = gn,
desc = 'doc highlight',
callback = require('navigator.dochighlight').cmd_nohl, callback = require('navigator.dochighlight').cmd_nohl,
}) })
end end
@ -365,6 +385,7 @@ function M.setup(attach_opts)
api.nvim_create_autocmd({ 'BufWritePre' }, { api.nvim_create_autocmd({ 'BufWritePre' }, {
group = api.nvim_create_augroup('nvim_nv_event_autos', {}), group = api.nvim_create_augroup('nvim_nv_event_autos', {}),
buffer = attach_opts.bufnr, buffer = attach_opts.bufnr,
desc = 'diagnostic update',
callback = function() callback = function()
require('navigator.diagnostics').set_diag_loclist(attach_opts.bufnr) require('navigator.diagnostics').set_diag_loclist(attach_opts.bufnr)
end, end,

Loading…
Cancel
Save