diff options
| author | Luc Sinet <[email protected]> | 2021-10-09 13:45:34 +0200 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-09 13:45:34 +0200 | 
| commit | b524100f016de6b934894547d48f9ef811902397 (patch) | |
| tree | a810e47fededc67da39252d29f2438ab71cf2881 /lua | |
| parent | efd82c87568a791b2f7fb9c94b763f2c1950dc8e (diff) | |
feat: support wildcard filetypes for null-ls providers (#1447)
Co-authored-by: kylo252 <[email protected]>
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/core/info.lua | 6 | ||||
| -rw-r--r-- | lua/lsp/null-ls/formatters.lua | 9 | ||||
| -rw-r--r-- | lua/lsp/null-ls/linters.lua | 9 | ||||
| -rw-r--r-- | lua/lsp/null-ls/services.lua | 4 | ||||
| -rw-r--r-- | lua/lsp/utils.lua | 15 | ||||
| -rw-r--r-- | lua/utils/table.lua | 24 | 
6 files changed, 48 insertions, 19 deletions
| diff --git a/lua/core/info.lua b/lua/core/info.lua index ff024fa5..16001d07 100644 --- a/lua/core/info.lua +++ b/lua/core/info.lua @@ -11,6 +11,7 @@ local M = {  local fmt = string.format  local text = require "interface.text" +local lsp_utils = require "lsp.utils"  local function str_list(list)    return fmt("[ %s ]", table.concat(list, ", ")) @@ -67,7 +68,7 @@ local function tbl_set_highlight(terms, highlight_group)  end  local function make_client_info(client) -  local client_enabled_caps = require("lsp.utils").get_ls_capabilities(client.id) +  local client_enabled_caps = lsp_utils.get_client_capabilities(client.id)    local name = client.name    local id = client.id    local document_formatting = client.resolved_capabilities.document_formatting @@ -89,8 +90,7 @@ local function make_client_info(client)  end  function M.toggle_popup(ft) -  local lsp_utils = require "lsp.utils" -  local clients = lsp_utils.get_active_client_by_ft(ft) +  local clients = lsp_utils.get_active_clients_by_ft(ft)    local client_names = {}    local header = { diff --git a/lua/lsp/null-ls/formatters.lua b/lua/lsp/null-ls/formatters.lua index d6455d5f..4728b908 100644 --- a/lua/lsp/null-ls/formatters.lua +++ b/lua/lsp/null-ls/formatters.lua @@ -13,9 +13,11 @@ end  function M.list_available(filetype)    local formatters = {} +  local tbl = require "utils.table"    for _, provider in pairs(null_ls.builtins.formatting) do -    -- TODO: Add support for wildcard filetypes -    if vim.tbl_contains(provider.filetypes or {}, filetype) then +    if tbl.contains(provider.filetypes or {}, function(ft) +      return ft == "*" or ft == filetype +    end) then        table.insert(formatters, provider.name)      end    end @@ -27,7 +29,8 @@ function M.list_configured(formatter_configs)    local formatters, errors = {}, {}    for _, fmt_config in ipairs(formatter_configs) do -    local formatter = null_ls.builtins.formatting[fmt_config.exe] +    local formatter_name = fmt_config.exe:gsub("-", "_") +    local formatter = null_ls.builtins.formatting[formatter_name]      if not formatter then        Log:error("Not a valid formatter: " .. fmt_config.exe) diff --git a/lua/lsp/null-ls/linters.lua b/lua/lsp/null-ls/linters.lua index e60411e6..549c6cdd 100644 --- a/lua/lsp/null-ls/linters.lua +++ b/lua/lsp/null-ls/linters.lua @@ -13,9 +13,11 @@ end  function M.list_available(filetype)    local linters = {} +  local tbl = require "utils.table"    for _, provider in pairs(null_ls.builtins.diagnostics) do -    -- TODO: Add support for wildcard filetypes -    if vim.tbl_contains(provider.filetypes or {}, filetype) then +    if tbl.contains(provider.filetypes or {}, function(ft) +      return ft == "*" or ft == filetype +    end) then        table.insert(linters, provider.name)      end    end @@ -27,7 +29,8 @@ function M.list_configured(linter_configs)    local linters, errors = {}, {}    for _, lnt_config in pairs(linter_configs) do -    local linter = null_ls.builtins.diagnostics[lnt_config.exe] +    local linter_name = lnt_config.exe:gsub("-", "_") +    local linter = null_ls.builtins.diagnostics[linter_name]      if not linter then        Log:error("Not a valid linter: " .. lnt_config.exe) diff --git a/lua/lsp/null-ls/services.lua b/lua/lsp/null-ls/services.lua index c62fc709..ef9e7d22 100644 --- a/lua/lsp/null-ls/services.lua +++ b/lua/lsp/null-ls/services.lua @@ -4,8 +4,8 @@ local function find_root_dir()    local util = require "lspconfig/util"    local lsp_utils = require "lsp.utils" -  local status_ok, ts_client = lsp_utils.is_client_active "typescript" -  if status_ok then +  local ts_client = lsp_utils.is_client_active "typescript" +  if ts_client then      return ts_client.config.root_dir    end    local dirname = vim.fn.expand "%:p:h" diff --git a/lua/lsp/utils.lua b/lua/lsp/utils.lua index 87ba2337..59003406 100644 --- a/lua/lsp/utils.lua +++ b/lua/lsp/utils.lua @@ -1,16 +1,15 @@  local M = {} +local tbl = require "utils.table" +  function M.is_client_active(name)    local clients = vim.lsp.get_active_clients() -  for _, client in pairs(clients) do -    if client.name == name then -      return true, client -    end -  end -  return false +  return tbl.find_first(clients, function(client) +    return client.name == name +  end)  end -function M.get_active_client_by_ft(filetype) +function M.get_active_clients_by_ft(filetype)    local matches = {}    local clients = vim.lsp.get_active_clients()    for _, client in pairs(clients) do @@ -22,7 +21,7 @@ function M.get_active_client_by_ft(filetype)    return matches  end -function M.get_ls_capabilities(client_id) +function M.get_client_capabilities(client_id)    if not client_id then      local buf_clients = vim.lsp.buf_get_clients()      for _, buf_client in ipairs(buf_clients) do diff --git a/lua/utils/table.lua b/lua/utils/table.lua new file mode 100644 index 00000000..1ac5949e --- /dev/null +++ b/lua/utils/table.lua @@ -0,0 +1,24 @@ +local Table = {} + +--- Find the first entry for which the predicate returns true. +-- @param t The table +-- @param predicate The function called for each entry of t +-- @return The entry for which the predicate returned True or nil +function Table.find_first(t, predicate) +  for _, entry in pairs(t) do +    if predicate(entry) then +      return entry +    end +  end +  return nil +end + +--- Check if the predicate returns True for at least one entry of the table. +-- @param t The table +-- @param predicate The function called for each entry of t +-- @return True if predicate returned True at least once, false otherwise +function Table.contains(t, predicate) +  return Table.find_first(t, predicate) ~= nil +end + +return Table | 
