fix: eslint format on save
This commit is contained in:
parent
359e38969d
commit
a7d4162f2a
5 changed files with 35 additions and 41 deletions
|
@ -45,7 +45,7 @@ function M.init(client, bufnr)
|
|||
|
||||
-- formatting
|
||||
if client.supports_method('textDocument/formatting') then
|
||||
buf_map('n', '<leader>lf', '', { desc = 'Format', callback = lsp_utils.format })
|
||||
buf_map('n', '<leader>lf', '', { desc = 'Format', callback = lsp_utils.buf_format })
|
||||
buf_map('v', '<leader>lf', '<cmd>lua vim.lsp.buf.range_formatting()<cr>', { desc = 'Range Format' })
|
||||
end
|
||||
|
||||
|
|
|
@ -3,9 +3,9 @@ local M = {}
|
|||
local augroup_name = 'CosmicNvimLspFormat'
|
||||
local user_config = require('cosmic.core.user')
|
||||
local u = require('cosmic.utils')
|
||||
local can_format_on_save = require('cosmic.utils.lsp').can_format_on_save
|
||||
local lsp_utils = require('cosmic.utils.lsp')
|
||||
|
||||
M.group = vim.api.nvim_create_augroup(augroup_name, {})
|
||||
M.augroup = vim.api.nvim_create_augroup(augroup_name, { clear = true })
|
||||
|
||||
function M.on_attach(client, bufnr)
|
||||
local function buf_set_option(name, value)
|
||||
|
@ -24,24 +24,22 @@ function M.on_attach(client, bufnr)
|
|||
if client.supports_method('textDocument/formatting') then
|
||||
-- set up :LspFormat for clients that are capable
|
||||
vim.cmd(
|
||||
string.format("command! -nargs=? LspFormat lua require('cosmic.utils.lsp').force_format(%s, <q-args>)", bufnr)
|
||||
string.format("command! -nargs=? LspFormat lua require('cosmic.utils.lsp').buf_format(%s, <q-args>)", bufnr)
|
||||
)
|
||||
|
||||
if can_format_on_save(client) then
|
||||
-- set up auto format on save
|
||||
vim.api.nvim_clear_autocmds({
|
||||
group = M.group,
|
||||
group = M.augroup,
|
||||
buffer = bufnr,
|
||||
})
|
||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||
callback = function()
|
||||
require('cosmic.utils.lsp').format(bufnr)
|
||||
lsp_utils.format_on_save(client, bufnr)
|
||||
end,
|
||||
buffer = bufnr,
|
||||
group = M.group,
|
||||
group = M.augroup,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
-- set up default mappings
|
||||
require('cosmic.lsp.mappings').init(client, bufnr)
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
local defaults = require('cosmic.lsp.providers.defaults')
|
||||
local can_format_on_save = require('cosmic.utils.lsp').can_format_on_save
|
||||
return {
|
||||
on_attach = function(client, bufnr)
|
||||
defaults.on_attach(client, bufnr)
|
||||
if can_format_on_save(client) then
|
||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||
buffer = bufnr,
|
||||
command = 'EslintFixAll',
|
||||
group = defaults.augroup,
|
||||
})
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
local default_on_attach = require('cosmic.lsp.providers.defaults').on_attach
|
||||
local M = {}
|
||||
|
||||
function M.on_attach(client, bufnr)
|
||||
default_on_attach(client, bufnr)
|
||||
end
|
||||
|
||||
M.root_dir = function(fname)
|
||||
local util = require('lspconfig').util
|
||||
return util.root_pattern(
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
local user_config = require('cosmic.core.user')
|
||||
local M = {}
|
||||
|
||||
M.format_disabled_override = false
|
||||
M.format_on_save_disabled = false
|
||||
|
||||
function M.can_format_on_save(client)
|
||||
-- formatting enabled by default if server=true
|
||||
if user_config.lsp.servers[client.name] == true or client.name == 'null-ls' then
|
||||
if user_config.lsp.servers[client.name] == true then
|
||||
return true
|
||||
end
|
||||
|
||||
|
@ -24,11 +24,11 @@ function M.can_format_on_save(client)
|
|||
end
|
||||
|
||||
function M.toggle_format_on_save()
|
||||
M.format_disabled_override = not M.format_disabled_override
|
||||
vim.notify(string.format('Format on save disabled: %s', M.format_disabled_override))
|
||||
M.format_on_save_disabled = not M.format_on_save_disabled
|
||||
vim.notify(string.format('Format on save disabled: %s', M.format_on_save_disabled))
|
||||
end
|
||||
|
||||
function M.force_format(bufnr, timeout)
|
||||
function M.buf_format(bufnr, timeout)
|
||||
if timeout == '' or timeout == nil then
|
||||
timeout = user_config.lsp.format_timeout
|
||||
else
|
||||
|
@ -41,20 +41,17 @@ function M.force_format(bufnr, timeout)
|
|||
end
|
||||
|
||||
-- format current buffer w/user settings
|
||||
function M.format(bufnr, timeout)
|
||||
if M.format_disabled_override then
|
||||
function M.format_on_save(client, bufnr)
|
||||
if M.format_on_save_disabled then
|
||||
return
|
||||
end
|
||||
|
||||
if timeout == '' or timeout == nil then
|
||||
timeout = user_config.lsp.format_timeout
|
||||
else
|
||||
timeout = timeout * 1000
|
||||
end
|
||||
|
||||
vim.lsp.buf.format({
|
||||
timeout_ms = timeout,
|
||||
timeout_ms = user_config.lsp.format_timeout,
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
||||
filter = function()
|
||||
return M.can_format_on_save(client)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue