diff --git a/lua/cosmic/lsp/servers/defaults.lua b/lua/cosmic/lsp/servers/defaults.lua index 0f9037f..082f2f8 100644 --- a/lua/cosmic/lsp/servers/defaults.lua +++ b/lua/cosmic/lsp/servers/defaults.lua @@ -34,12 +34,12 @@ function M.on_attach(client, bufnr) }) vim.api.nvim_create_autocmd('BufWritePre', { callback = function() - if not lsp_utils.format_on_save_disabled then + if lsp_utils.format_on_save_enabled then vim.lsp.buf.format({ timeout_ms = user_config.lsp.format_timeout, bufnr = bufnr, filter = function() - return lsp_utils.can_format_on_save(client) + return lsp_utils.can_client_format_on_save(client) end, }) end diff --git a/lua/cosmic/lsp/servers/eslint.lua b/lua/cosmic/lsp/servers/eslint.lua index cb0eb91..9a304a8 100644 --- a/lua/cosmic/lsp/servers/eslint.lua +++ b/lua/cosmic/lsp/servers/eslint.lua @@ -1,5 +1,5 @@ local defaults = require('cosmic.lsp.servers.defaults') -local can_format_on_save = require('cosmic.utils.lsp').can_format_on_save +local can_format_on_save = require('cosmic.utils.lsp').can_client_format_on_save return { on_attach = function(client, bufnr) defaults.on_attach(client, bufnr) diff --git a/lua/cosmic/plugins/mason-lspconfig/init.lua b/lua/cosmic/plugins/mason-lspconfig/init.lua index f232693..a682cce 100644 --- a/lua/cosmic/plugins/mason-lspconfig/init.lua +++ b/lua/cosmic/plugins/mason-lspconfig/init.lua @@ -26,10 +26,8 @@ return { end -- override options if user defines them - if type(user_config.lsp.servers[server]) == 'table' then - if user_config.lsp.servers[server].opts ~= nil then - server_config = u.merge(server_config, user_config.lsp.servers[server].opts) - end + if type(user_config.lsp.servers[server]) == 'table' and user_config.lsp.servers[server].opts ~= nil then + server_config = u.merge(server_config, user_config.lsp.servers[server].opts) end lspconfig[server].setup(server_config) diff --git a/lua/cosmic/utils/lsp.lua b/lua/cosmic/utils/lsp.lua index 9e0f51d..ea70737 100644 --- a/lua/cosmic/utils/lsp.lua +++ b/lua/cosmic/utils/lsp.lua @@ -1,11 +1,11 @@ local user_config = require('cosmic.core.user') local M = {} -M.format_on_save_disabled = false +M.format_on_save_enabled = true -function M.can_format_on_save(client) - -- formatting enabled by default if server=true +function M.can_client_format_on_save(client) local user_server_config = user_config.lsp.servers[client.name] + -- formatting enabled by default if server=true if user_server_config == true then return true end @@ -16,7 +16,6 @@ function M.can_format_on_save(client) if user_server_config.format_on_save == nil then return true end - -- check format flag on server settings return user_server_config.format_on_save == true end @@ -25,8 +24,8 @@ function M.can_format_on_save(client) end function M.toggle_format_on_save() - 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)) + M.format_on_save_enabled = not M.format_on_save_enabled + vim.notify(string.format('Format on save: %s', M.format_on_save_enabled)) end function M.buf_format(bufnr, timeout) @@ -41,11 +40,12 @@ function M.buf_format(bufnr, timeout) }) end -function M.buf_get_active_client_names(bufnr) +function M.buf_get_active_clients_str() local active_clients = vim.lsp.get_clients({ - bufnr = bufnr or vim.api.nvim_get_current_buf(), + bufnr = vim.api.nvim_get_current_buf(), }) local client_names = {} + for _, client in pairs(active_clients or {}) do table.insert(client_names, client.name) end @@ -53,11 +53,7 @@ function M.buf_get_active_client_names(bufnr) if not vim.tbl_isempty(client_names) then table.sort(client_names) end - return client_names -end -function M.buf_get_active_clients_str() - local client_names = M.buf_get_active_client_names() local client_str = '' if #client_names < 1 then