Skip to content

Commit f96be4f

Browse files
committed
fix: update code to use non-deprecated methods in Neovim v0.11
1 parent f5a9668 commit f96be4f

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed

lua/astrolsp/file_operations.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
---@class astrolsp.file_operations
1313
local M = {}
1414

15+
local utils = require "astrolsp.utils"
16+
1517
local config = vim.tbl_get(require "astrolsp", "config", "file_operations") or {}
1618

1719
---@class AstroLSPFileOperationsRename
@@ -54,7 +56,8 @@ function M.didCreateFiles(fnames)
5456
local filters = did_create.filters or {}
5557
local filtered = vim.tbl_filter(function(fname) return match_filters(filters, fname) end, fnames)
5658
if next(filtered) then
57-
client.notify(
59+
utils.notify(
60+
client,
5861
"workspace/didCreateFiles",
5962
{ files = vim.tbl_map(function(fname) return { uri = vim.uri_from_fname(fname) } end, filtered) }
6063
)
@@ -74,7 +77,8 @@ function M.didDeleteFiles(fnames)
7477
local filters = did_delete.filters or {}
7578
local filtered = vim.tbl_filter(function(fname) return match_filters(filters, fname) end, fnames)
7679
if next(filtered) then
77-
client.notify(
80+
utils.notify(
81+
client,
7882
"workspace/didDeleteFiles",
7983
{ files = vim.tbl_map(function(fname) return { uri = vim.uri_from_fname(fname) } end, filtered) }
8084
)
@@ -97,7 +101,7 @@ function M.didRenameFiles(renames)
97101
renames
98102
)
99103
if next(filtered) then
100-
client.notify("workspace/didRenameFiles", {
104+
utils.notify(client, "workspace/didRenameFiles", {
101105
files = vim.tbl_map(
102106
function(rename) return { oldUri = vim.uri_from_fname(rename.from), newUri = vim.uri_from_fname(rename.to) } end,
103107
filtered
@@ -108,8 +112,11 @@ function M.didRenameFiles(renames)
108112
end
109113
end
110114

111-
local getWorkspaceEdit = function(client, req, params)
112-
local success, resp = pcall(client.request_sync, req, params, config.timeout)
115+
---@param client vim.lsp.Client
116+
---@param req string
117+
---@param params table
118+
local function getWorkspaceEdit(client, req, params)
119+
local success, resp = pcall(utils.request_sync, client, req, params, config.timeout)
113120
if success then return resp.result end
114121
end
115122

lua/astrolsp/init.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
---@class astrolsp
1010
local M = {}
1111

12+
local utils = require "astrolsp.utils"
13+
1214
local tbl_contains = vim.tbl_contains
1315
local tbl_isempty = vim.tbl_isempty
1416

@@ -27,7 +29,7 @@ local function lsp_event(name) vim.api.nvim_exec_autocmds("User", { pattern = "A
2729
local function check_cond(cond, client, bufnr)
2830
local cond_type = type(cond)
2931
if cond_type == "function" then return cond(client, bufnr) end
30-
if cond_type == "string" then return client.supports_method(cond) end
32+
if cond_type == "string" then return utils.supports_method(client, cond, bufnr) end
3133
if cond_type == "boolean" then return cond end
3234
return true
3335
end
@@ -79,13 +81,13 @@ end
7981
---@param client vim.lsp.Client The LSP client details when attaching
8082
---@param bufnr integer The buffer that the LSP client is attaching to
8183
function M.on_attach(client, bufnr)
82-
if client.supports_method "textDocument/codeLens" and M.config.features.codelens then
84+
if utils.supports_method(client, "textDocument/codeLens", bufnr) and M.config.features.codelens then
8385
vim.lsp.codelens.refresh { bufnr = bufnr }
8486
end
8587

8688
local formatting_disabled = vim.tbl_get(M.config, "formatting", "disabled")
8789
if
88-
client.supports_method "textDocument/formatting"
90+
utils.supports_method(client, "textDocument/formatting", bufnr)
8991
and (formatting_disabled ~= true and not tbl_contains(formatting_disabled, client.name))
9092
then
9193
local autoformat = assert(M.config.formatting.format_on_save)
@@ -97,7 +99,7 @@ function M.on_attach(client, bufnr)
9799
end
98100
end
99101

100-
if client.supports_method "textDocument/semanticTokens/full" and vim.lsp.semantic_tokens then
102+
if utils.supports_method(client, "textDocument/semanticTokens/full", bufnr) and vim.lsp.semantic_tokens then
101103
if M.config.features.semantic_tokens then
102104
if vim.b[bufnr].semantic_tokens == nil then vim.b[bufnr].semantic_tokens = true end
103105
else
@@ -278,7 +280,7 @@ function M.setup(opts)
278280
desc = "Add signature help triggers as language servers attach",
279281
callback = function(args)
280282
local client = vim.lsp.get_client_by_id(args.data.client_id)
281-
if client and client.supports_method "textDocument/signatureHelp" then
283+
if client and utils.supports_method(client, "textDocument/signatureHelp", args.buf) then
282284
for _, set in ipairs { "triggerCharacters", "retriggerCharacters" } do
283285
local set_var = "signature_help_" .. set
284286
local triggers = vim.b[args.buf][set_var] or {}
@@ -297,7 +299,9 @@ function M.setup(opts)
297299
if not vim.api.nvim_buf_is_valid(args.buf) then return end
298300
local triggers, retriggers = {}, {}
299301
for _, client in pairs(vim.lsp.get_clients { bufnr = args.buf }) do
300-
if client.id ~= args.data.client_id and client.supports_method "textDocument/signatureHelp" then
302+
if
303+
client.id ~= args.data.client_id and utils.supports_method(client, "textDocument/signatureHelp", args.buf)
304+
then
301305
for _, trigger in ipairs(client.server_capabilities.signatureHelpProvider.triggerCharacters or {}) do
302306
triggers[trigger] = true
303307
end

lua/astrolsp/toggles.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
---@class astrolsp.toggles
1010
local M = {}
1111

12+
local utils = require "astrolsp.utils"
13+
1214
local config = require("astrolsp").config
1315
local features = config.features --[[@as AstroLSPFeatureOpts]]
1416
local format_on_save = config.formatting.format_on_save --[[@as AstroLSPFormatOnSaveOpts]]
@@ -65,7 +67,7 @@ function M.buffer_semantic_tokens(bufnr, silent)
6567
vim.b[bufnr].semantic_tokens = not vim.b[bufnr].semantic_tokens
6668
local toggled = false
6769
for _, client in ipairs(vim.lsp.get_clients { bufnr = bufnr }) do
68-
if client.supports_method "textDocument/semanticTokens/full" then
70+
if utils.supports_method(client, "textDocument/semanticTokens/full", bufnr) then
6971
-- HACK: `semantic_tokens.start/stop` don't support 0 for current buffer
7072
local real_bufnr = bufnr == 0 and vim.api.nvim_get_current_buf() or bufnr
7173
vim.lsp.semantic_tokens[vim.b[bufnr].semantic_tokens and "start" or "stop"](real_bufnr, client.id)

lua/astrolsp/utils.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---Utilities for interacting with the Neovim LSP integration
2+
---
3+
---This module can be loaded with `local astrolsp_utils = require "astrolsp.utils"`
4+
---
5+
---copyright 2025
6+
---license GNU General Public License v3.0
7+
---@class astrolsp.utils
8+
local M = {}
9+
10+
-- TODO: remove helper functions when dropping support for Neovim v0.10
11+
12+
--- Helper function to support deprecated supports_method usage
13+
---@param client vim.lsp.Client
14+
---@param method string
15+
---@param bufnr? integer
16+
function M.supports_method(client, method, bufnr)
17+
if vim.fn.has "nvim-0.11" == 1 then
18+
return client:supports_method(method, bufnr)
19+
else
20+
---@diagnostic disable-next-line: param-type-mismatch
21+
return client.supports_method(method, { bufnr = bufnr })
22+
end
23+
end
24+
25+
--- Helper function to support deprecated request_sync usage
26+
---@param client vim.lsp.Client
27+
---@param req string
28+
---@param params table
29+
---@param timeout? integer
30+
---@param bufnr? integer
31+
function M.request_sync(client, req, params, timeout, bufnr)
32+
if vim.fn.has "nvim-0.11" == 1 then
33+
return client:request_sync(req, params, timeout, bufnr)
34+
else
35+
---@diagnostic disable-next-line: param-type-mismatch
36+
return client.request_sync(req, params, timeout, bufnr)
37+
end
38+
end
39+
40+
--- Helper function to support deprecated notify usage
41+
---@param client vim.lsp.Client
42+
---@param method string
43+
---@param params? table
44+
function M.notify(client, method, params)
45+
if vim.fn.has "nvim-0.11" == 1 then
46+
return client:notify(method, params)
47+
else
48+
---@diagnostic disable-next-line: param-type-mismatch
49+
return client.notify(method, params)
50+
end
51+
end
52+
53+
return M

0 commit comments

Comments
 (0)