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
|
-- Override Cosmic configuration options
|
||||||
|
|
||||||
-- You can require null-ls if needed
|
|
||||||
-- local null_ls = require('null-ls')
|
|
||||||
|
|
||||||
local config = {
|
local config = {
|
||||||
-- See :h nvim_open_win for possible border options
|
-- See :h nvim_open_win for possible border options
|
||||||
border = 'rounded',
|
border = 'rounded',
|
||||||
|
@ -10,8 +7,6 @@ local config = {
|
||||||
lsp = {
|
lsp = {
|
||||||
-- Enable/disable inlay hints
|
-- Enable/disable inlay hints
|
||||||
inlay_hint = false,
|
inlay_hint = false,
|
||||||
-- True/false or table of filetypes {'.ts', '.js',}
|
|
||||||
format_on_save = true,
|
|
||||||
-- Time in MS before format timeout
|
-- Time in MS before format timeout
|
||||||
format_timeout = 3000,
|
format_timeout = 3000,
|
||||||
-- Set to false to disable rename notification
|
-- Set to false to disable rename notification
|
||||||
|
@ -24,30 +19,13 @@ local config = {
|
||||||
'rust_analyzer',
|
'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/
|
-- 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
|
-- 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 = {
|
null_ls = {
|
||||||
-- Disable default list of sources provided by CosmicNvim
|
-- Disable default list of sources provided by CosmicNvim
|
||||||
default_cosmic_sources = false,
|
default_cosmic_sources = false,
|
||||||
--disable formatting
|
--disable formatting
|
||||||
format = false,
|
format_on_save = false,
|
||||||
-- Add additional sources here
|
-- Add additional sources here
|
||||||
get_sources = function()
|
get_sources = function()
|
||||||
local null_ls = require('null-ls')
|
local null_ls = require('null-ls')
|
||||||
|
@ -62,6 +40,25 @@ local config = {
|
||||||
}
|
}
|
||||||
end,
|
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 = {},
|
diagnostic = {},
|
||||||
-- See :h gitsigns-usage
|
-- See :h gitsigns-usage
|
||||||
gitsigns = {},
|
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
|
-- See https://github.com/nvim-lualine/lualine.nvim#default-configuration
|
||||||
lualine = {},
|
lualine = {},
|
||||||
-- See https://github.com/L3MON4D3/LuaSnip/blob/577045e9adf325e58f690f4d4b4a293f3dcec1b3/README.md#config
|
-- See https://github.com/L3MON4D3/LuaSnip/blob/577045e9adf325e58f690f4d4b4a293f3dcec1b3/README.md#config
|
||||||
|
|
|
@ -12,7 +12,6 @@ local default_config = {
|
||||||
plugins = {},
|
plugins = {},
|
||||||
lsp = {
|
lsp = {
|
||||||
inlay_hint = false,
|
inlay_hint = false,
|
||||||
format_on_save = true, -- true/false or table of filetypes {'.ts', '.js',}
|
|
||||||
format_timeout = 500,
|
format_timeout = 500,
|
||||||
rename_notification = true,
|
rename_notification = true,
|
||||||
-- table of callbacks pushed via plugins
|
-- table of callbacks pushed via plugins
|
||||||
|
@ -40,16 +39,16 @@ local default_config = {
|
||||||
tailwindcss = true,
|
tailwindcss = true,
|
||||||
eslint = true,
|
eslint = true,
|
||||||
jsonls = {
|
jsonls = {
|
||||||
format = false,
|
format_on_save = false,
|
||||||
},
|
},
|
||||||
pyright = true,
|
pyright = true,
|
||||||
lua_ls = {
|
lua_ls = {
|
||||||
format = false,
|
format_on_save = false,
|
||||||
},
|
},
|
||||||
gopls = true,
|
gopls = true,
|
||||||
html = true,
|
html = true,
|
||||||
tsserver = {
|
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' })
|
buf_map('v', '<leader>la', '<cmd>lua vim.lsp.buf.range_code_actions()<cr>', { desc = 'Range Code Actions' })
|
||||||
|
|
||||||
-- formatting
|
-- 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.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
|
||||||
|
|
||||||
-- lsp workspace
|
-- lsp workspace
|
||||||
buf_map('n', '<leader>lwa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<cr>', { desc = 'Add workspace folder' })
|
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 capabilities = require('cmp_nvim_lsp').default_capabilities()
|
||||||
local M = {}
|
local M = {}
|
||||||
local augroup_name = 'CosmicNvimLspFormat'
|
local augroup_name = 'CosmicNvimLspFormat'
|
||||||
local group = vim.api.nvim_create_augroup(augroup_name, { clear = true })
|
|
||||||
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
|
||||||
|
|
||||||
|
M.group = vim.api.nvim_create_augroup(augroup_name, {})
|
||||||
|
|
||||||
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)
|
||||||
|
@ -19,45 +21,25 @@ function M.on_attach(client, bufnr)
|
||||||
vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
|
vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
|
||||||
end
|
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
|
-- 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').force_format(%s, <q-args>)", bufnr)
|
||||||
)
|
)
|
||||||
|
|
||||||
-- set up auto format on save
|
-- 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({
|
vim.api.nvim_clear_autocmds({
|
||||||
group = group,
|
group = M.group,
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
})
|
})
|
||||||
-- autocommand for format on save with specified filetype(s)
|
|
||||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||||
callback = function(ev)
|
callback = function()
|
||||||
for _, pattern in pairs(filetype_patterns) do
|
|
||||||
if string.match(ev.file, pattern) then
|
|
||||||
filetype_allowed = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if filetype_allowed then
|
|
||||||
require('cosmic.utils.lsp').format(bufnr)
|
require('cosmic.utils.lsp').format(bufnr)
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
group = group,
|
group = M.group,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
-- set up default mappings
|
-- set up default mappings
|
||||||
require('cosmic.lsp.mappings').init(client, bufnr)
|
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 {
|
return {
|
||||||
on_attach = function(client, bufnr)
|
on_attach = function(client, bufnr)
|
||||||
default_on_attach(client, bufnr)
|
defaults.on_attach(client, bufnr)
|
||||||
vim.api.nvim_create_autocmd('BufWritePre', {
|
vim.api.nvim_create_autocmd('BufWritePre', {
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
command = 'EslintFixAll',
|
command = 'EslintFixAll',
|
||||||
|
|
|
@ -17,11 +17,6 @@ return {
|
||||||
local lspconfig = require('lspconfig')
|
local lspconfig = require('lspconfig')
|
||||||
|
|
||||||
local start_server = function(server)
|
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
|
local opts = default_config
|
||||||
|
|
||||||
-- set up default cosmic options
|
-- set up default cosmic options
|
||||||
|
|
|
@ -10,9 +10,9 @@ return {
|
||||||
config = function()
|
config = function()
|
||||||
local defaults = require('cosmic.lsp.providers.defaults')
|
local defaults = require('cosmic.lsp.providers.defaults')
|
||||||
local null_ls = require('null-ls')
|
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,
|
default_cosmic_sources = true,
|
||||||
})
|
}, user_config.lsp.null_ls or {})
|
||||||
if config_opts.default_cosmic_sources then
|
if config_opts.default_cosmic_sources then
|
||||||
local function get_user_config_sources()
|
local function get_user_config_sources()
|
||||||
if not config_opts.add_sources then
|
if not config_opts.add_sources then
|
||||||
|
|
|
@ -3,7 +3,7 @@ local M = {}
|
||||||
|
|
||||||
M.format_disabled_override = false
|
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
|
-- 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 or client.name == 'null-ls' then
|
||||||
return true
|
return true
|
||||||
|
@ -12,12 +12,12 @@ local function can_client_format(client)
|
||||||
-- check config server settings
|
-- check config server settings
|
||||||
if user_config.lsp.servers[client.name] then
|
if user_config.lsp.servers[client.name] then
|
||||||
-- default to true if no format flag on server settings is set
|
-- 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
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- check format flag on server settings
|
-- 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
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -34,31 +34,27 @@ function M.force_format(bufnr, timeout)
|
||||||
else
|
else
|
||||||
timeout = timeout * 1000
|
timeout = timeout * 1000
|
||||||
end
|
end
|
||||||
local filter = can_client_format
|
|
||||||
vim.lsp.buf.format({
|
vim.lsp.buf.format({
|
||||||
timeout_ms = timeout,
|
timeout_ms = timeout,
|
||||||
filter = filter,
|
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
||||||
bufnr = bufnr or 0,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- format current buffer w/user settings
|
-- format current buffer w/user settings
|
||||||
function M.format(bufnr, timeout)
|
function M.format(bufnr, timeout)
|
||||||
|
if M.format_disabled_override then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
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
|
||||||
timeout = timeout * 1000
|
timeout = timeout * 1000
|
||||||
end
|
end
|
||||||
local filter = can_client_format
|
|
||||||
if M.format_disabled_override then
|
|
||||||
filter = function(client)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
vim.lsp.buf.format({
|
vim.lsp.buf.format({
|
||||||
timeout_ms = timeout,
|
timeout_ms = timeout,
|
||||||
filter = filter,
|
bufnr = bufnr or vim.api.nvim_get_current_buf(),
|
||||||
bufnr = bufnr or 0,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -102,7 +98,7 @@ function M.toggle_inlay_hints()
|
||||||
return function()
|
return function()
|
||||||
enabled = not enabled
|
enabled = not enabled
|
||||||
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue