mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
fix(lsp): filetype matching to documentSelector in dynamic capabilities (#25425)
Use the get_language_id client option to resolve the filetype when matching the document selector in a dynamic capability. Co-authored-by: Mathias Fussenegger <f.mathias@zignar.net>
This commit is contained in:
parent
2151e781e4
commit
031088fc0a
@ -1073,7 +1073,7 @@ function lsp.start_client(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- @param method string
|
--- @param method string
|
||||||
--- @param opts? {bufnr?: number}
|
--- @param opts? {bufnr: integer?}
|
||||||
client.supports_method = function(method, opts)
|
client.supports_method = function(method, opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
local required_capability = lsp._request_name_to_capability[method]
|
local required_capability = lsp._request_name_to_capability[method]
|
||||||
|
@ -55,7 +55,7 @@ function M:unregister(unregisterations)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- @param method string
|
--- @param method string
|
||||||
--- @param opts? {bufnr?: number}
|
--- @param opts? {bufnr: integer?}
|
||||||
--- @return lsp.Registration? (table|nil) the registration if found
|
--- @return lsp.Registration? (table|nil) the registration if found
|
||||||
--- @private
|
--- @private
|
||||||
function M:get(method, opts)
|
function M:get(method, opts)
|
||||||
@ -69,14 +69,14 @@ function M:get(method, opts)
|
|||||||
if not documentSelector then
|
if not documentSelector then
|
||||||
return reg
|
return reg
|
||||||
end
|
end
|
||||||
if M.match(opts.bufnr, documentSelector) then
|
if self:match(opts.bufnr, documentSelector) then
|
||||||
return reg
|
return reg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @param method string
|
--- @param method string
|
||||||
--- @param opts? {bufnr?: number}
|
--- @param opts? {bufnr: integer?}
|
||||||
--- @private
|
--- @private
|
||||||
function M:supports(method, opts)
|
function M:supports(method, opts)
|
||||||
return self:get(method, opts) ~= nil
|
return self:get(method, opts) ~= nil
|
||||||
@ -85,13 +85,17 @@ end
|
|||||||
--- @param bufnr number
|
--- @param bufnr number
|
||||||
--- @param documentSelector lsp.DocumentSelector
|
--- @param documentSelector lsp.DocumentSelector
|
||||||
--- @private
|
--- @private
|
||||||
function M.match(bufnr, documentSelector)
|
function M:match(bufnr, documentSelector)
|
||||||
local ft = vim.bo[bufnr].filetype
|
local client = vim.lsp.get_client_by_id(self.client_id)
|
||||||
|
if not client then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local language = client.config.get_language_id(bufnr, vim.bo[bufnr].filetype)
|
||||||
local uri = vim.uri_from_bufnr(bufnr)
|
local uri = vim.uri_from_bufnr(bufnr)
|
||||||
local fname = vim.uri_to_fname(uri)
|
local fname = vim.uri_to_fname(uri)
|
||||||
for _, filter in ipairs(documentSelector) do
|
for _, filter in ipairs(documentSelector) do
|
||||||
local matches = true
|
local matches = true
|
||||||
if filter.language and ft ~= filter.language then
|
if filter.language and language ~= filter.language then
|
||||||
matches = false
|
matches = false
|
||||||
end
|
end
|
||||||
if matches and filter.scheme and not vim.startswith(uri, filter.scheme .. ':') then
|
if matches and filter.scheme and not vim.startswith(uri, filter.scheme .. ':') then
|
||||||
|
@ -3936,12 +3936,15 @@ describe('LSP', function()
|
|||||||
|
|
||||||
describe('#dynamic vim.lsp._dynamic', function()
|
describe('#dynamic vim.lsp._dynamic', function()
|
||||||
it('supports dynamic registration', function()
|
it('supports dynamic registration', function()
|
||||||
|
---@type string
|
||||||
local root_dir = helpers.tmpname()
|
local root_dir = helpers.tmpname()
|
||||||
os.remove(root_dir)
|
os.remove(root_dir)
|
||||||
mkdir(root_dir)
|
mkdir(root_dir)
|
||||||
local tmpfile = root_dir .. '/dynamic.foo'
|
local tmpfile = root_dir .. '/dynamic.foo'
|
||||||
local file = io.open(tmpfile, 'w')
|
local file = io.open(tmpfile, 'w')
|
||||||
file:close()
|
if file then
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
|
|
||||||
exec_lua(create_server_definition)
|
exec_lua(create_server_definition)
|
||||||
local result = exec_lua([[
|
local result = exec_lua([[
|
||||||
@ -3952,6 +3955,9 @@ describe('LSP', function()
|
|||||||
name = 'dynamic-test',
|
name = 'dynamic-test',
|
||||||
cmd = server.cmd,
|
cmd = server.cmd,
|
||||||
root_dir = root_dir,
|
root_dir = root_dir,
|
||||||
|
get_language_id = function()
|
||||||
|
return "dummy-lang"
|
||||||
|
end,
|
||||||
capabilities = {
|
capabilities = {
|
||||||
textDocument = {
|
textDocument = {
|
||||||
formatting = {
|
formatting = {
|
||||||
@ -3985,6 +3991,13 @@ describe('LSP', function()
|
|||||||
{
|
{
|
||||||
id = 'range-formatting',
|
id = 'range-formatting',
|
||||||
method = 'textDocument/rangeFormatting',
|
method = 'textDocument/rangeFormatting',
|
||||||
|
registerOptions = {
|
||||||
|
documentSelector = {
|
||||||
|
{
|
||||||
|
language = "dummy-lang"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, { client_id = client_id })
|
}, { client_id = client_id })
|
||||||
@ -4002,7 +4015,11 @@ describe('LSP', function()
|
|||||||
local function check(method, fname)
|
local function check(method, fname)
|
||||||
local bufnr = fname and vim.fn.bufadd(fname) or nil
|
local bufnr = fname and vim.fn.bufadd(fname) or nil
|
||||||
local client = vim.lsp.get_client_by_id(client_id)
|
local client = vim.lsp.get_client_by_id(client_id)
|
||||||
result[#result + 1] = {method = method, fname = fname, supported = client.supports_method(method, {bufnr = bufnr})}
|
result[#result + 1] = {
|
||||||
|
method = method,
|
||||||
|
fname = fname,
|
||||||
|
supported = client.supports_method(method, {bufnr = bufnr})
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user