feat: clean up format on save
This commit is contained in:
parent
18e6882d0e
commit
11e7a914cb
8 changed files with 61 additions and 98 deletions
|
@ -1,8 +1,5 @@
|
|||
-- Override Cosmic configuration options
|
||||
|
||||
-- You can require null-ls if needed
|
||||
-- local null_ls = require('null-ls')
|
||||
|
||||
local config = {
|
||||
-- See :h nvim_open_win for possible border options
|
||||
border = 'rounded',
|
||||
|
@ -10,8 +7,6 @@ local config = {
|
|||
lsp = {
|
||||
-- Enable/disable inlay hints
|
||||
inlay_hint = false,
|
||||
-- True/false or table of filetypes {'.ts', '.js',}
|
||||
format_on_save = true,
|
||||
-- Time in MS before format timeout
|
||||
format_timeout = 3000,
|
||||
-- Set to false to disable rename notification
|
||||
|
@ -24,30 +19,13 @@ local config = {
|
|||
'rust_analyzer',
|
||||
},
|
||||
|
||||
-- lsp servers that should be enabled
|
||||
servers = {
|
||||
-- Enable rust_analyzer
|
||||
rust_analyzer = true,
|
||||
|
||||
-- Enable tsserver w/custom settings
|
||||
tsserver = {
|
||||
-- Disable formatting (defaults to true)
|
||||
format = false,
|
||||
-- OR add/override server options
|
||||
opts = {
|
||||
on_attach = function(client, bufnr) end,
|
||||
flags = {
|
||||
debounce_text_changes = 150,
|
||||
},
|
||||
},
|
||||
},
|
||||
-- See Cosmic defaults lsp/providers/null_ls.lua and https://github.com/jose-elias-alvarez/null-ls.nvim/
|
||||
-- If adding additional sources, be sure to also copy the defaults that you would like to preserve from lsp/providers/null_ls.lua
|
||||
null_ls = {
|
||||
-- Disable default list of sources provided by CosmicNvim
|
||||
default_cosmic_sources = false,
|
||||
--disable formatting
|
||||
format = false,
|
||||
format_on_save = false,
|
||||
-- Add additional sources here
|
||||
get_sources = function()
|
||||
local null_ls = require('null-ls')
|
||||
|
@ -62,6 +40,25 @@ local config = {
|
|||
}
|
||||
end,
|
||||
},
|
||||
|
||||
-- lsp servers that should be enabled
|
||||
servers = {
|
||||
-- Enable rust_analyzer
|
||||
rust_analyzer = true,
|
||||
|
||||
-- Enable tsserver w/custom settings
|
||||
tsserver = {
|
||||
-- Disable formatting (defaults to true)
|
||||
format_on_save = false,
|
||||
-- OR add/override server options
|
||||
opts = {
|
||||
on_attach = function(client, bufnr) end,
|
||||
flags = {
|
||||
debounce_text_changes = 150,
|
||||
},
|
||||
settings = {},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
|
@ -77,14 +74,6 @@ local config = {
|
|||
diagnostic = {},
|
||||
-- See :h gitsigns-usage
|
||||
gitsigns = {},
|
||||
-- See https://git.sr.ht/~whynothugo/lsp_lines.nvim
|
||||
lsp_lines = {
|
||||
-- additional flag only for CosmicNvim
|
||||
-- true - loads plugin and is enabled at start
|
||||
-- false - loads plugin but is not enabled at start
|
||||
-- you may use <leader>ld to toggle
|
||||
enable_on_start = true,
|
||||
},
|
||||
-- See https://github.com/nvim-lualine/lualine.nvim#default-configuration
|
||||
lualine = {},
|
||||
-- See https://github.com/L3MON4D3/LuaSnip/blob/577045e9adf325e58f690f4d4b4a293f3dcec1b3/README.md#config
|
||||
|
|
|
@ -12,7 +12,6 @@ local default_config = {
|
|||
plugins = {},
|
||||
lsp = {
|
||||
inlay_hint = false,
|
||||
format_on_save = true, -- true/false or table of filetypes {'.ts', '.js',}
|
||||
format_timeout = 500,
|
||||
rename_notification = true,
|
||||
-- table of callbacks pushed via plugins
|
||||
|
@ -40,16 +39,16 @@ local default_config = {
|
|||
tailwindcss = true,
|
||||
eslint = true,
|
||||
jsonls = {
|
||||
format = false,
|
||||
format_on_save = false,
|
||||
},
|
||||
pyright = true,
|
||||
lua_ls = {
|
||||
format = false,
|
||||
format_on_save = false,
|
||||
},
|
||||
gopls = true,
|
||||
html = true,
|
||||
tsserver = {
|
||||
format = false,
|
||||
format_on_save = false,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
@ -44,8 +44,10 @@ function M.init(client, bufnr)
|
|||
buf_map('v', '<leader>la', '<cmd>lua vim.lsp.buf.range_code_actions()<cr>', { desc = 'Range Code Actions' })
|
||||
|
||||
-- formatting
|
||||
if client.supports_method('textDocument/formatting') then
|
||||
buf_map('n', '<leader>lf', '', { desc = 'Format', callback = lsp_utils.format })
|
||||
buf_map('v', '<leader>lf', '<cmd>lua vim.lsp.buf.range_formatting()<cr>', { desc = 'Range Format' })
|
||||
end
|
||||
|
||||
-- lsp workspace
|
||||
buf_map('n', '<leader>lwa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<cr>', { desc = 'Add workspace folder' })
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
local capabilities = require('cmp_nvim_lsp').default_capabilities()
|
||||
local M = {}
|
||||
local augroup_name = 'CosmicNvimLspFormat'
|
||||
local group = vim.api.nvim_create_augroup(augroup_name, { clear = true })
|
||||
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
|
||||
|
||||
M.group = vim.api.nvim_create_augroup(augroup_name, {})
|
||||
|
||||
function M.on_attach(client, bufnr)
|
||||
local function buf_set_option(name, value)
|
||||
|
@ -19,45 +21,25 @@ function M.on_attach(client, bufnr)
|
|||
vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
|
||||
end
|
||||
|
||||
if client.supports_method('textDocument/formatting') then
|
||||
if client.supports_method('textDocument/formatting') and can_format_on_save(client) 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)
|
||||
)
|
||||
|
||||
-- set up auto format on save
|
||||
if user_config.lsp.format_on_save then
|
||||
-- check user config to see if we can format on save
|
||||
-- collect filetype(s) from user config
|
||||
local filetype_patterns = {}
|
||||
local filetype_allowed = false
|
||||
if vim.islist(user_config.lsp.format_on_save) then
|
||||
filetype_patterns = user_config.lsp.format_on_save
|
||||
else -- any filetype if none set
|
||||
filetype_allowed = true
|
||||
end
|
||||
|
||||
vim.api.nvim_clear_autocmds({
|
||||
group = group,
|
||||
group = M.group,
|
||||
buffer = bufnr,
|
||||
})
|
||||
-- autocommand for format on save with specified filetype(s)
|
||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||
callback = function(ev)
|
||||
for _, pattern in pairs(filetype_patterns) do
|
||||
if string.match(ev.file, pattern) then
|
||||
filetype_allowed = true
|
||||
end
|
||||
end
|
||||
if filetype_allowed then
|
||||
callback = function()
|
||||
require('cosmic.utils.lsp').format(bufnr)
|
||||
end
|
||||
end,
|
||||
buffer = bufnr,
|
||||
group = group,
|
||||
group = M.group,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
-- set up default mappings
|
||||
require('cosmic.lsp.mappings').init(client, bufnr)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
local default_on_attach = require('cosmic.lsp.providers.defaults').on_attach
|
||||
local defaults = require('cosmic.lsp.providers.defaults')
|
||||
return {
|
||||
on_attach = function(client, bufnr)
|
||||
default_on_attach(client, bufnr)
|
||||
defaults.on_attach(client, bufnr)
|
||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||
buffer = bufnr,
|
||||
command = 'EslintFixAll',
|
||||
|
|
|
@ -17,11 +17,6 @@ return {
|
|||
local lspconfig = require('lspconfig')
|
||||
|
||||
local start_server = function(server)
|
||||
-- don't match servers not started by lspconfig
|
||||
if server == 'null_ls' or server == 'typescript-tools' then
|
||||
return
|
||||
end
|
||||
|
||||
local opts = default_config
|
||||
|
||||
-- set up default cosmic options
|
||||
|
|
|
@ -10,9 +10,9 @@ return {
|
|||
config = function()
|
||||
local defaults = require('cosmic.lsp.providers.defaults')
|
||||
local null_ls = require('null-ls')
|
||||
local config_opts = u.merge(user_config.lsp.servers.null_ls or {}, {
|
||||
local config_opts = u.merge({
|
||||
default_cosmic_sources = true,
|
||||
})
|
||||
}, user_config.lsp.null_ls or {})
|
||||
if config_opts.default_cosmic_sources then
|
||||
local function get_user_config_sources()
|
||||
if not config_opts.add_sources then
|
||||
|
|
|
@ -3,7 +3,7 @@ local M = {}
|
|||
|
||||
M.format_disabled_override = false
|
||||
|
||||
local function can_client_format(client)
|
||||
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
|
||||
return true
|
||||
|
@ -12,12 +12,12 @@ local function can_client_format(client)
|
|||
-- check config server settings
|
||||
if user_config.lsp.servers[client.name] then
|
||||
-- default to true if no format flag on server settings is set
|
||||
if user_config.lsp.servers[client.name].format == nil then
|
||||
if user_config.lsp.servers[client.name].format_on_save == nil then
|
||||
return true
|
||||
end
|
||||
|
||||
-- check format flag on server settings
|
||||
return (user_config.lsp.servers[client.name].format == true)
|
||||
return (user_config.lsp.servers[client.name].format_on_save == true)
|
||||
end
|
||||
|
||||
return true
|
||||
|
@ -34,31 +34,27 @@ function M.force_format(bufnr, timeout)
|
|||
else
|
||||
timeout = timeout * 1000
|
||||
end
|
||||
local filter = can_client_format
|
||||
vim.lsp.buf.format({
|
||||
timeout_ms = timeout,
|
||||
filter = filter,
|
||||
bufnr = bufnr or 0,
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
||||
})
|
||||
end
|
||||
|
||||
-- format current buffer w/user settings
|
||||
function M.format(bufnr, timeout)
|
||||
if M.format_disabled_override then
|
||||
return
|
||||
end
|
||||
|
||||
if timeout == '' or timeout == nil then
|
||||
timeout = user_config.lsp.format_timeout
|
||||
else
|
||||
timeout = timeout * 1000
|
||||
end
|
||||
local filter = can_client_format
|
||||
if M.format_disabled_override then
|
||||
filter = function(client)
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
vim.lsp.buf.format({
|
||||
timeout_ms = timeout,
|
||||
filter = filter,
|
||||
bufnr = bufnr or 0,
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
||||
})
|
||||
end
|
||||
|
||||
|
@ -102,7 +98,7 @@ function M.toggle_inlay_hints()
|
|||
return function()
|
||||
enabled = not enabled
|
||||
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({
|
||||
bufnr = vim.api.nvim_get_current_buf() or 0,
|
||||
bufnr = vim.api.nvim_get_current_buf(),
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue