Skip to content

Commit

Permalink
fix+feat: [FIXES] null-ls (by ignoring it). [FEATS] new option `exclu…
Browse files Browse the repository at this point in the history
…ded_lsp_clients` [FEATS] config is not exposed through a global so it can be changed on the fly.
  • Loading branch information
Zeioth committed Nov 7, 2023
1 parent 0478d9a commit c1eb312
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions lua/garbage-day/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local M = {}
function M.set(opts)
M.grace_period = opts.grace_period or (60*15) -- seconds
M.excluded_filetypes = opts.excluded_filetypes or { "java", "markdown" }
M.excluded_lsp_clients = opts.excluded_lsp_clients or { "null-ls" }
M.stop_invisible = opts.stop_invisible or false
M.notifications = opts.notifications or false
end
Expand Down
5 changes: 4 additions & 1 deletion lua/garbage-day/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ local M = {}
--- Entry point of the program
function M.setup(opts)
config.set(opts)
vim.g.garbage_day_config = config

-- Focus lost?
vim.api.nvim_create_autocmd({ "FocusLost" }, {
Expand Down Expand Up @@ -84,7 +85,9 @@ function M.setup(opts)
if config.notifications then utils.notify("lsp_has_stopped") end

-- fix for null-ls
pcall(function() require("null-ls").enable({}) end)
if not vim.tbl_contains(config.excluded_lsp_clients, 'null-ls') then
pcall(function() require("null-ls").enable({}) end)
end
end
end
})
Expand Down
28 changes: 18 additions & 10 deletions lua/garbage-day/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ local M = {}
-- ----------------------------------------------------------------------------

---Stop all lsp clients, including the ones in other tabs.
---@param excluded_filetypes table Languages where we don't want to stop LSP.
---@return table stopped_lsp_clients A table like { [client] = buf, ... }
---So we can start them again on FocusGaind.
function M.stop_lsp(excluded_filetypes)
function M.stop_lsp()
local config = vim.g.garbage_day_config

local stopped_lsp_clients = {}
for _, client in pairs(vim.lsp.get_active_clients()) do
for buf, _ in pairs(client.attached_buffers) do

-- If all conditions pass
local filetype = vim.api.nvim_get_option_value("filetype", { buf = buf })
local is_filetype_excluded = vim.tbl_contains(excluded_filetypes, filetype)
if not is_filetype_excluded then
local is_filetype_excluded = vim.tbl_contains(config.excluded_filetypes, filetype)
local is_lsp_client_excluded = vim.tbl_contains(config.excluded_lsp_clients, client.config.name)
if not is_filetype_excluded and
not is_lsp_client_excluded
then
-- save client+buf for later
stopped_lsp_clients[client] = buf

Expand Down Expand Up @@ -62,15 +66,16 @@ end

---Stop all lsp clients, including the ones in other tabs.
--Except the ones currently asociated to a nvim window in the current tab.
---@param excluded_filetypes table Languages where we don't want to stop LSP.
---@return table stopped_lsp_clients A table like { [client] = buf, ... }
---So we can start them again on BufEnter.
function M.stop_invisible(excluded_filetypes)
function M.stop_invisible()
-- get config
local config = vim.g.garbage_day_config

-- Get all visible filetypes in the current tab.
local stopped_lsp_clients = {}
local visible_buffers = {}
local visible_filetypes = {}

-- Get all visible filetypes in the current tab.
local current_tab = vim.api.nvim_get_current_tabpage()
local visible_windows = vim.api.nvim_tabpage_list_wins(current_tab)
for _, win in ipairs(visible_windows) do
Expand All @@ -87,11 +92,13 @@ function M.stop_invisible(excluded_filetypes)
local filetype = vim.api.nvim_get_option_value("filetype", { buf = buf })
local is_buf_visible = vim.tbl_contains(visible_buffers, buf)
local is_filetype_visible = visible_filetypes[filetype]
local is_filetype_excluded = vim.tbl_contains(excluded_filetypes, filetype)
local is_filetype_excluded = vim.tbl_contains(config.excluded_filetypes, filetype)
local is_lsp_client_excluded = vim.tbl_contains(config.excluded_lsp_clients, client.config.name)
-- If all conditions pass
if not is_buf_visible and
not is_filetype_visible and
not is_filetype_excluded
not is_filetype_excluded and
not is_lsp_client_excluded
then
-- save client+buf for later
stopped_lsp_clients[client] = buf
Expand Down Expand Up @@ -129,3 +136,4 @@ end


return M

0 comments on commit c1eb312

Please sign in to comment.