feat: clean up lsp utils

This commit is contained in:
Matthew Leong 2024-06-08 23:42:55 -07:00
parent 9d5a570d40
commit 374c0beccb
4 changed files with 13 additions and 19 deletions

View file

@ -34,12 +34,12 @@ function M.on_attach(client, bufnr)
}) })
vim.api.nvim_create_autocmd('BufWritePre', { vim.api.nvim_create_autocmd('BufWritePre', {
callback = function() callback = function()
if not lsp_utils.format_on_save_disabled then if lsp_utils.format_on_save_enabled then
vim.lsp.buf.format({ vim.lsp.buf.format({
timeout_ms = user_config.lsp.format_timeout, timeout_ms = user_config.lsp.format_timeout,
bufnr = bufnr, bufnr = bufnr,
filter = function() filter = function()
return lsp_utils.can_format_on_save(client) return lsp_utils.can_client_format_on_save(client)
end, end,
}) })
end end

View file

@ -1,5 +1,5 @@
local defaults = require('cosmic.lsp.servers.defaults') 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 { return {
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
defaults.on_attach(client, bufnr) defaults.on_attach(client, bufnr)

View file

@ -26,11 +26,9 @@ return {
end end
-- override options if user defines them -- override options if user defines them
if type(user_config.lsp.servers[server]) == 'table' then if type(user_config.lsp.servers[server]) == 'table' and user_config.lsp.servers[server].opts ~= nil then
if user_config.lsp.servers[server].opts ~= nil then
server_config = u.merge(server_config, user_config.lsp.servers[server].opts) server_config = u.merge(server_config, user_config.lsp.servers[server].opts)
end end
end
lspconfig[server].setup(server_config) lspconfig[server].setup(server_config)
end end

View file

@ -1,11 +1,11 @@
local user_config = require('cosmic.core.user') local user_config = require('cosmic.core.user')
local M = {} local M = {}
M.format_on_save_disabled = false M.format_on_save_enabled = true
function M.can_format_on_save(client) function M.can_client_format_on_save(client)
-- formatting enabled by default if server=true
local user_server_config = user_config.lsp.servers[client.name] local user_server_config = user_config.lsp.servers[client.name]
-- formatting enabled by default if server=true
if user_server_config == true then if user_server_config == true then
return true return true
end end
@ -16,7 +16,6 @@ function M.can_format_on_save(client)
if user_server_config.format_on_save == nil then if user_server_config.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_server_config.format_on_save == true return user_server_config.format_on_save == true
end end
@ -25,8 +24,8 @@ function M.can_format_on_save(client)
end end
function M.toggle_format_on_save() function M.toggle_format_on_save()
M.format_on_save_disabled = not M.format_on_save_disabled M.format_on_save_enabled = not M.format_on_save_enabled
vim.notify(string.format('Format on save disabled: %s', M.format_on_save_disabled)) vim.notify(string.format('Format on save: %s', M.format_on_save_enabled))
end end
function M.buf_format(bufnr, timeout) function M.buf_format(bufnr, timeout)
@ -41,11 +40,12 @@ function M.buf_format(bufnr, timeout)
}) })
end end
function M.buf_get_active_client_names(bufnr) function M.buf_get_active_clients_str()
local active_clients = vim.lsp.get_clients({ 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 = {} local client_names = {}
for _, client in pairs(active_clients or {}) do for _, client in pairs(active_clients or {}) do
table.insert(client_names, client.name) table.insert(client_names, client.name)
end end
@ -53,11 +53,7 @@ function M.buf_get_active_client_names(bufnr)
if not vim.tbl_isempty(client_names) then if not vim.tbl_isempty(client_names) then
table.sort(client_names) table.sort(client_names)
end end
return client_names
end
function M.buf_get_active_clients_str()
local client_names = M.buf_get_active_client_names()
local client_str = '' local client_str = ''
if #client_names < 1 then if #client_names < 1 then