Skip to content

Commit efcc935

Browse files
committed
feat(toggles): add silent options to toggle functions
1 parent 9cb0e24 commit efcc935

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

lua/astrolsp/toggles.lua

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
local M = {}
22

3-
local config = require("astrolsp").config
4-
local features = config.features
3+
local features = require("astrolsp").config.features
54

6-
local notify = vim.notify
5+
local function ui_notify(silent, ...) return not silent and vim.notify(...) end
76
local function bool2str(bool) return bool and "on" or "off" end
87

98
--- Toggle auto format
@@ -14,58 +13,63 @@ end
1413

1514
--- Toggle buffer local auto format
1615
---@param bufnr? number The buffer to toggle the autoformatting of, default the current buffer
17-
function M.buffer_autoformat(bufnr)
16+
---@param silent? boolean if true then don't sent a notification
17+
function M.buffer_autoformat(bufnr, silent)
1818
bufnr = bufnr or 0
1919
local old_val = vim.b[bufnr].autoformat
2020
if old_val == nil then old_val = features.autoformat end
2121
vim.b[bufnr].autoformat = not old_val
22-
notify(string.format("Buffer autoformatting %s", bool2str(vim.b[bufnr].autoformat)))
22+
ui_notify(silent, string.format("Buffer autoformatting %s", bool2str(vim.b[bufnr].autoformat)))
2323
end
2424

2525
--- Toggle buffer LSP inlay hints
2626
---@param bufnr? number the buffer to toggle the clients on
27-
function M.buffer_inlay_hints(bufnr)
27+
---@param silent? boolean if true then don't sent a notification
28+
function M.buffer_inlay_hints(bufnr, silent)
2829
bufnr = bufnr or 0
2930
vim.b[bufnr].inlay_hints = not vim.b[bufnr].inlay_hints
3031
-- TODO: remove check after dropping support for Neovim v0.9
3132
if vim.lsp.inlay_hint then
3233
vim.lsp.inlay_hint(bufnr, vim.b[bufnr].inlay_hints)
33-
notify(string.format("Inlay hints %s", bool2str(vim.b[bufnr].inlay_hints)))
34+
ui_notify(silent, string.format("Inlay hints %s", bool2str(vim.b[bufnr].inlay_hints)))
3435
end
3536
end
3637

3738
--- Toggle buffer semantic token highlighting for all language servers that support it
3839
---@param bufnr? number the buffer to toggle the clients on
39-
function M.buffer_semantic_tokens(bufnr)
40+
---@param silent? boolean if true then don't sent a notification
41+
function M.buffer_semantic_tokens(bufnr, silent)
4042
bufnr = bufnr or 0
4143
vim.b[bufnr].semantic_tokens = not vim.b[bufnr].semantic_tokens
42-
for _, client in ipairs((vim.lsp.get_clients or vim.lsp.get_active_clients)()) do
44+
for _, client in ipairs(vim.lsp.get_clients()) do
4345
if client.server_capabilities.semanticTokensProvider then
4446
vim.lsp.semantic_tokens[vim.b[bufnr].semantic_tokens and "start" or "stop"](bufnr, client.id)
45-
notify(string.format("Buffer lsp semantic highlighting %s", bool2str(vim.b[bufnr].semantic_tokens)))
47+
ui_notify(silent, string.format("Buffer lsp semantic highlighting %s", bool2str(vim.b[bufnr].semantic_tokens)))
4648
end
4749
end
4850
end
4951

5052
--- Toggle codelens
51-
function M.codelens()
53+
---@param silent? boolean if true then don't sent a notification
54+
function M.codelens(silent)
5255
features.codelens = not features.codelens
5356
if not features.codelens then vim.lsp.codelens.clear() end
54-
notify(string.format("CodeLens %s", bool2str(features.codelens)))
57+
ui_notify(silent, string.format("CodeLens %s", bool2str(features.codelens)))
5558
end
5659

5760
--- Toggle diagnostics
58-
function M.diagnostics()
61+
---@param silent? boolean if true then don't sent a notification
62+
function M.diagnostics(silent)
5963
features.diagnostics_mode = (features.diagnostics_mode - 1) % 4
6064
vim.diagnostic.config(require("astrolsp").diagnostics[features.diagnostics_mode])
6165
if features.diagnostics_mode == 0 then
62-
notify "diagnostics off"
66+
ui_notify(silent, "diagnostics off")
6367
elseif features.diagnostics_mode == 1 then
64-
notify "only status diagnostics"
68+
ui_notify(silent, "only status diagnostics")
6569
elseif features.diagnostics_mode == 2 then
66-
notify "virtual text off"
70+
ui_notify(silent, "virtual text off")
6771
else
68-
notify "all diagnostics on"
72+
ui_notify(silent, "all diagnostics on")
6973
end
7074
end
7175

0 commit comments

Comments
 (0)