Browse Source

feat: support wildcard filetypes for null-ls providers (#1447)

Co-authored-by: kylo252 <59826753+kylo252@users.noreply.github.com>
Luc Sinet 3 years ago
parent
commit
b524100f01

+ 3 - 3
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 = {

+ 6 - 3
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)

+ 6 - 3
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)

+ 2 - 2
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"

+ 7 - 8
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

+ 24 - 0
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