From 11e7a914cb848045e7e099a248cf254d64f2fb49 Mon Sep 17 00:00:00 2001 From: Matthew Leong Date: Sat, 8 Jun 2024 18:10:29 -0700 Subject: [PATCH] feat: clean up format on save --- lua/cosmic/config/examples/config.lua | 59 +++++++++------------ lua/cosmic/core/user.lua | 7 ++- lua/cosmic/lsp/mappings.lua | 6 ++- lua/cosmic/lsp/providers/defaults.lua | 48 ++++++----------- lua/cosmic/lsp/providers/eslint.lua | 4 +- lua/cosmic/plugins/mason-lspconfig/init.lua | 5 -- lua/cosmic/plugins/null-ls/init.lua | 4 +- lua/cosmic/utils/lsp.lua | 26 ++++----- 8 files changed, 61 insertions(+), 98 deletions(-) diff --git a/lua/cosmic/config/examples/config.lua b/lua/cosmic/config/examples/config.lua index 8936ded..a267b50 100644 --- a/lua/cosmic/config/examples/config.lua +++ b/lua/cosmic/config/examples/config.lua @@ -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,6 +19,28 @@ local config = { 'rust_analyzer', }, + -- 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_on_save = false, + -- Add additional sources here + get_sources = function() + local null_ls = require('null-ls') + return { + null_ls.builtins.diagnostics.shellcheck, + null_ls.builtins.diagnostics.actionlint.with({ + condition = function() + local cwd = vim.fn.expand('%:p:.') + return cwd:find('.github/workflows') + end, + }), + } + end, + }, + -- lsp servers that should be enabled servers = { -- Enable rust_analyzer @@ -32,36 +49,16 @@ local config = { -- Enable tsserver w/custom settings tsserver = { -- Disable formatting (defaults to true) - format = false, + format_on_save = false, -- OR add/override server options opts = { on_attach = function(client, bufnr) end, flags = { debounce_text_changes = 150, }, + settings = {}, }, }, - -- 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, - -- Add additional sources here - get_sources = function() - local null_ls = require('null-ls') - return { - null_ls.builtins.diagnostics.shellcheck, - null_ls.builtins.diagnostics.actionlint.with({ - condition = function() - local cwd = vim.fn.expand('%:p:.') - return cwd:find('.github/workflows') - end, - }), - } - end, - }, }, }, @@ -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 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 diff --git a/lua/cosmic/core/user.lua b/lua/cosmic/core/user.lua index 9d5f081..a2d0b52 100644 --- a/lua/cosmic/core/user.lua +++ b/lua/cosmic/core/user.lua @@ -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, }, }, }, diff --git a/lua/cosmic/lsp/mappings.lua b/lua/cosmic/lsp/mappings.lua index ac03c9c..aefe03a 100644 --- a/lua/cosmic/lsp/mappings.lua +++ b/lua/cosmic/lsp/mappings.lua @@ -44,8 +44,10 @@ function M.init(client, bufnr) buf_map('v', 'la', 'lua vim.lsp.buf.range_code_actions()', { desc = 'Range Code Actions' }) -- formatting - buf_map('n', 'lf', '', { desc = 'Format', callback = lsp_utils.format }) - buf_map('v', 'lf', 'lua vim.lsp.buf.range_formatting()', { desc = 'Range Format' }) + if client.supports_method('textDocument/formatting') then + buf_map('n', 'lf', '', { desc = 'Format', callback = lsp_utils.format }) + buf_map('v', 'lf', 'lua vim.lsp.buf.range_formatting()', { desc = 'Range Format' }) + end -- lsp workspace buf_map('n', 'lwa', 'lua vim.lsp.buf.add_workspace_folder()', { desc = 'Add workspace folder' }) diff --git a/lua/cosmic/lsp/providers/defaults.lua b/lua/cosmic/lsp/providers/defaults.lua index 12a7f08..6ed6132 100644 --- a/lua/cosmic/lsp/providers/defaults.lua +++ b/lua/cosmic/lsp/providers/defaults.lua @@ -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,44 +21,24 @@ 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, )", 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, - 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 - require('cosmic.utils.lsp').format(bufnr) - end - end, - buffer = bufnr, - group = group, - }) - end + vim.api.nvim_clear_autocmds({ + group = M.group, + buffer = bufnr, + }) + vim.api.nvim_create_autocmd('BufWritePre', { + callback = function() + require('cosmic.utils.lsp').format(bufnr) + end, + buffer = bufnr, + group = M.group, + }) end -- set up default mappings diff --git a/lua/cosmic/lsp/providers/eslint.lua b/lua/cosmic/lsp/providers/eslint.lua index b8b1c79..fb0f2c3 100644 --- a/lua/cosmic/lsp/providers/eslint.lua +++ b/lua/cosmic/lsp/providers/eslint.lua @@ -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', diff --git a/lua/cosmic/plugins/mason-lspconfig/init.lua b/lua/cosmic/plugins/mason-lspconfig/init.lua index 1bfd0da..3be2a3d 100644 --- a/lua/cosmic/plugins/mason-lspconfig/init.lua +++ b/lua/cosmic/plugins/mason-lspconfig/init.lua @@ -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 diff --git a/lua/cosmic/plugins/null-ls/init.lua b/lua/cosmic/plugins/null-ls/init.lua index b2cc4fb..24366b7 100644 --- a/lua/cosmic/plugins/null-ls/init.lua +++ b/lua/cosmic/plugins/null-ls/init.lua @@ -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 diff --git a/lua/cosmic/utils/lsp.lua b/lua/cosmic/utils/lsp.lua index c9ea92a..e6b9c15 100644 --- a/lua/cosmic/utils/lsp.lua +++ b/lua/cosmic/utils/lsp.lua @@ -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