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
|
-- formatting
|
||||||
if client.supports_method('textDocument/formatting') then
|
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' })
|
buf_map('v', '<leader>lf', '<cmd>lua vim.lsp.buf.range_formatting()<cr>', { desc = 'Range Format' })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,9 @@ local M = {}
|
||||||
local augroup_name = 'CosmicNvimLspFormat'
|
local augroup_name = 'CosmicNvimLspFormat'
|
||||||
local user_config = require('cosmic.core.user')
|
local user_config = require('cosmic.core.user')
|
||||||
local u = require('cosmic.utils')
|
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)
|
function M.on_attach(client, bufnr)
|
||||||
local function buf_set_option(name, value)
|
local function buf_set_option(name, value)
|
||||||
|
@ -24,23 +24,21 @@ function M.on_attach(client, bufnr)
|
||||||
if client.supports_method('textDocument/formatting') then
|
if client.supports_method('textDocument/formatting') then
|
||||||
-- set up :LspFormat for clients that are capable
|
-- set up :LspFormat for clients that are capable
|
||||||
vim.cmd(
|
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
|
||||||
-- set up auto format on save
|
vim.api.nvim_clear_autocmds({
|
||||||
vim.api.nvim_clear_autocmds({
|
group = M.augroup,
|
||||||
group = M.group,
|
buffer = bufnr,
|
||||||
buffer = bufnr,
|
})
|
||||||
})
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
callback = function()
|
||||||
callback = function()
|
lsp_utils.format_on_save(client, bufnr)
|
||||||
require('cosmic.utils.lsp').format(bufnr)
|
end,
|
||||||
end,
|
buffer = bufnr,
|
||||||
buffer = bufnr,
|
group = M.augroup,
|
||||||
group = M.group,
|
})
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- set up default mappings
|
-- set up default mappings
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
local defaults = require('cosmic.lsp.providers.defaults')
|
local defaults = require('cosmic.lsp.providers.defaults')
|
||||||
|
local can_format_on_save = require('cosmic.utils.lsp').can_format_on_save
|
||||||
return {
|
return {
|
||||||
on_attach = function(client, bufnr)
|
on_attach = function(client, bufnr)
|
||||||
defaults.on_attach(client, bufnr)
|
defaults.on_attach(client, bufnr)
|
||||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
if can_format_on_save(client) then
|
||||||
buffer = bufnr,
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||||
command = 'EslintFixAll',
|
buffer = bufnr,
|
||||||
})
|
command = 'EslintFixAll',
|
||||||
|
group = defaults.augroup,
|
||||||
|
})
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
local default_on_attach = require('cosmic.lsp.providers.defaults').on_attach
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.on_attach(client, bufnr)
|
|
||||||
default_on_attach(client, bufnr)
|
|
||||||
end
|
|
||||||
|
|
||||||
M.root_dir = function(fname)
|
M.root_dir = function(fname)
|
||||||
local util = require('lspconfig').util
|
local util = require('lspconfig').util
|
||||||
return util.root_pattern(
|
return util.root_pattern(
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
local user_config = require('cosmic.core.user')
|
local user_config = require('cosmic.core.user')
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.format_disabled_override = false
|
M.format_on_save_disabled = false
|
||||||
|
|
||||||
function M.can_format_on_save(client)
|
function M.can_format_on_save(client)
|
||||||
-- formatting enabled by default if server=true
|
-- 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
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -24,11 +24,11 @@ function M.can_format_on_save(client)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.toggle_format_on_save()
|
function M.toggle_format_on_save()
|
||||||
M.format_disabled_override = not 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_disabled_override))
|
vim.notify(string.format('Format on save disabled: %s', M.format_on_save_disabled))
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.force_format(bufnr, timeout)
|
function M.buf_format(bufnr, timeout)
|
||||||
if timeout == '' or timeout == nil then
|
if timeout == '' or timeout == nil then
|
||||||
timeout = user_config.lsp.format_timeout
|
timeout = user_config.lsp.format_timeout
|
||||||
else
|
else
|
||||||
|
@ -41,20 +41,17 @@ function M.force_format(bufnr, timeout)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- format current buffer w/user settings
|
-- format current buffer w/user settings
|
||||||
function M.format(bufnr, timeout)
|
function M.format_on_save(client, bufnr)
|
||||||
if M.format_disabled_override then
|
if M.format_on_save_disabled then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if timeout == '' or timeout == nil then
|
|
||||||
timeout = user_config.lsp.format_timeout
|
|
||||||
else
|
|
||||||
timeout = timeout * 1000
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.lsp.buf.format({
|
vim.lsp.buf.format({
|
||||||
timeout_ms = timeout,
|
timeout_ms = user_config.lsp.format_timeout,
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
||||||
|
filter = function()
|
||||||
|
return M.can_format_on_save(client)
|
||||||
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue