Skip to content

Commit 9302785

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 4d38bec commit 9302785

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
@@ -346,10 +361,8 @@ function M.get_active_clients_list_by_ft(filetype)
346361
local clients_list = {}
347362
for _, client in pairs(clients) do
348363
local filetypes = client.config.filetypes or {}
349-
for _, ft in pairs(filetypes) do
350-
if ft == filetype then
351-
table.insert(clients_list, client.name)
352-
end
364+
if M.ft_matches(filetype, filetypes) then
365+
table.insert(clients_list, client.name)
353366
end
354367
end
355368
return clients_list
@@ -362,10 +375,8 @@ function M.get_other_matching_providers(filetype)
362375
for _, config in pairs(configs) do
363376
if not vim.tbl_contains(active_clients_list, config.name) then
364377
local filetypes = config.filetypes or {}
365-
for _, ft in pairs(filetypes) do
366-
if ft == filetype then
367-
table.insert(other_matching_configs, config)
368-
end
378+
if M.ft_matches(filetype, filetypes) then
379+
table.insert(other_matching_configs, config)
369380
end
370381
end
371382
end
@@ -377,10 +388,8 @@ function M.get_config_by_ft(filetype)
377388
local matching_configs = {}
378389
for _, config in pairs(configs) do
379390
local filetypes = config.filetypes or {}
380-
for _, ft in pairs(filetypes) do
381-
if ft == filetype then
382-
table.insert(matching_configs, config)
383-
end
391+
if M.ft_matches(filetype, filetypes) then
392+
table.insert(matching_configs, config)
384393
end
385394
end
386395
return matching_configs

0 commit comments

Comments
 (0)