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', {
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

View file

@ -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)

View file

@ -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)

View file

@ -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