Skip to content

Commit a9430e9

Browse files
committed
fix: support compound file-types
Vim supports compound filetypes (see |'filetype'|): > When a dot appears in the value then this separates two filetype names. > This works both for filetype plugins and for syntax files. More than one dot may appear. nvim-lspconfig currently always compares the full 'filetype' option against the configured filetypes. So buffers set to multiple filetypes will not match at all. Fix utility functions to match against each individual filetype
1 parent 056f569 commit a9430e9

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

lua/lspconfig/server_configurations/elmls.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ return {
1010
filetypes = { 'elm' },
1111
root_dir = function(fname)
1212
local filetype = api.nvim_buf_get_option(0, 'filetype')
13-
if filetype == 'elm' or (filetype == 'json' and fname:match 'elm%.json$') then
13+
if util.ft_matches(filetype, 'elm') or (util.ft_matches(filetype, 'json') and fname:match 'elm%.json$') then
1414
return elm_root_pattern(fname)
1515
end
1616
end,

lua/lspconfig/util.lua

+21-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ M.default_config = {
2222
-- global on_setup hook
2323
M.on_setup = nil
2424

25+
---@param filetype string the filetype to check (can be a compound, dot-separated filetype; see |'filetype'|)
26+
---@param expected string|string[] the filetype(s) to match against
27+
---@return boolean
28+
function M.ft_matches(filetype, expected)
29+
expected = type(expected) == 'table' and expected or { expected }
30+
for ft in filetype:gmatch '([^.]+)' do
31+
for _, expected_ft in ipairs(expected) do
32+
if ft == expected_ft then
33+
return true
34+
end
35+
end
36+
end
37+
return false
38+
end
39+
2540
function M.bufname_valid(bufname)
2641
if bufname:match '^/' or bufname:match '^[a-zA-Z]:' or bufname:match '^zipfile://' or bufname:match '^tarfile:' then
2742
return true
@@ -348,10 +363,8 @@ function M.get_active_clients_list_by_ft(filetype)
348363
local clients_list = {}
349364
for _, client in pairs(clients) do
350365
local filetypes = client.config.filetypes or {}
351-
for _, ft in pairs(filetypes) do
352-
if ft == filetype then
353-
table.insert(clients_list, client.name)
354-
end
366+
if M.ft_matches(filetype, filetypes) then
367+
table.insert(clients_list, client.name)
355368
end
356369
end
357370
return clients_list
@@ -364,10 +377,8 @@ function M.get_other_matching_providers(filetype)
364377
for _, config in pairs(configs) do
365378
if not vim.tbl_contains(active_clients_list, config.name) then
366379
local filetypes = config.filetypes or {}
367-
for _, ft in pairs(filetypes) do
368-
if ft == filetype then
369-
table.insert(other_matching_configs, config)
370-
end
380+
if M.ft_matches(filetype, filetypes) then
381+
table.insert(other_matching_configs, config)
371382
end
372383
end
373384
end
@@ -379,10 +390,8 @@ function M.get_config_by_ft(filetype)
379390
local matching_configs = {}
380391
for _, config in pairs(configs) do
381392
local filetypes = config.filetypes or {}
382-
for _, ft in pairs(filetypes) do
383-
if ft == filetype then
384-
table.insert(matching_configs, config)
385-
end
393+
if M.ft_matches(filetype, filetypes) then
394+
table.insert(matching_configs, config)
386395
end
387396
end
388397
return matching_configs

0 commit comments

Comments
 (0)