Skip to content

Commit 6649df5

Browse files
committed
feat: move to new vim.lsp.semantic_tokens API
1 parent 8f03a7f commit 6649df5

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

lua/astrolsp/init.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ function M.on_attach(client, bufnr)
9494
end
9595
end
9696

97-
if client:supports_method("textDocument/semanticTokens/full", bufnr) and vim.lsp.semantic_tokens then
97+
-- TODO: remove when dropping support for Neovim v0.11
98+
if client:supports_method("textDocument/semanticTokens/full", bufnr) and not vim.lsp.semantic_tokens.enable then
9899
if M.config.features.semantic_tokens then
99100
if vim.b[bufnr].semantic_tokens == nil then vim.b[bufnr].semantic_tokens = true end
100101
else
@@ -283,6 +284,8 @@ function M.setup(opts)
283284
end
284285

285286
vim.lsp.inlay_hint.enable(M.config.features.inlay_hints ~= false)
287+
-- TODO: remove check when dropping support for Neovim v0.11
288+
if vim.lsp.semantic_tokens.enable then vim.lsp.semantic_tokens.enable(M.config.features.semantic_tokens ~= false) end
286289

287290
-- Set up tracking of signature help trigger characters
288291
local augroup = vim.api.nvim_create_augroup("track_signature_help_triggers", { clear = true })

lua/astrolsp/toggles.lua

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,37 @@ end
6262
---@param silent? boolean if true then don't sent a notification
6363
function M.buffer_semantic_tokens(bufnr, silent)
6464
bufnr = bufnr or 0
65-
vim.b[bufnr].semantic_tokens = not vim.b[bufnr].semantic_tokens
66-
local toggled = false
67-
for _, client in ipairs(vim.lsp.get_clients { bufnr = bufnr }) do
68-
if client:supports_method("textDocument/semanticTokens/full", bufnr) then
69-
vim.lsp.semantic_tokens[vim.b[bufnr].semantic_tokens and "start" or "stop"](bufnr, client.id)
70-
vim.lsp.semantic_tokens.force_refresh(bufnr)
71-
toggled = true
65+
if vim.lsp.semantic_tokens.enable then
66+
vim.lsp.semantic_tokens.enable(not vim.lsp.semantic_tokens.is_enabled { bufnr = bufnr }, { bufnr = bufnr })
67+
vim.lsp.semantic_tokens.force_refresh(bufnr)
68+
ui_notify(
69+
silent,
70+
("Buffer lsp semantic highlighting %s"):format(bool2str(vim.lsp.semantic_tokens.is_enabled { bufnr = bufnr }))
71+
)
72+
else -- TODO: remove when dropping support for Neovim v0.11
73+
vim.b[bufnr].semantic_tokens = not vim.b[bufnr].semantic_tokens
74+
local toggled = false
75+
for _, client in ipairs(vim.lsp.get_clients { bufnr = bufnr }) do
76+
if client:supports_method("textDocument/semanticTokens/full", bufnr) then
77+
vim.lsp.semantic_tokens[vim.b[bufnr].semantic_tokens and "start" or "stop"](bufnr, client.id)
78+
toggled = true
79+
end
7280
end
81+
if toggled then vim.lsp.semantic_tokens.force_refresh(bufnr) end
82+
ui_notify(silent, ("Buffer lsp semantic highlighting %s"):format(bool2str(vim.b[bufnr].semantic_tokens)))
7383
end
74-
ui_notify(
75-
not toggled or silent,
76-
("Buffer lsp semantic highlighting %s"):format(bool2str(vim.b[bufnr].semantic_tokens))
77-
)
84+
end
85+
86+
--- Toggle global semantic token highlighting for all language servers that support it
87+
---@param silent? boolean if true then don't sent a notification
88+
function M.semantic_tokens(silent)
89+
if not vim.lsp.semantic_tokens.enable then
90+
ui_notify(silent, "Only available in Neovim v0.12+", vim.log.levels.WARN)
91+
return
92+
end
93+
vim.lsp.semantic_tokens.enable(not vim.lsp.semantic_tokens.is_enabled())
94+
vim.lsp.semantic_tokens.force_refresh()
95+
ui_notify(silent, ("Global lsp semantic highlighting %s"):format(bool2str(vim.lsp.semantic_tokens.is_enabled())))
7896
end
7997

8098
--- Toggle codelens

0 commit comments

Comments
 (0)