summaryrefslogtreecommitdiff
path: root/lua/lsp/null-ls.lua
blob: 6a31de2688666782fe6246197926dc8068be0cf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local M = {}

local _, null_ls = pcall(require, "null-ls")
local utils = require "utils"
local sources = {}

local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" }

local find_local_exe = function(exe)
  vim.cmd "let root_dir = FindRootDirectory()"
  local root_dir = vim.api.nvim_get_var "root_dir"
  local local_exe = root_dir .. "/node_modules/.bin/" .. exe
  return local_exe
end

-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint
local get_normalized_exe = function(exe, type)
  if type == "diagnostics" and exe == "eslint_d" then
    return "eslint"
  end
  return exe
end

local function setup_ls(exe, type)
  if utils.has_value(local_executables, exe) then
    local normalized_exe = get_normalized_exe(exe, type)
    local smart_executable = null_ls.builtins[type][normalized_exe]
    local local_executable = find_local_exe(exe)
    if vim.fn.executable(local_executable) == 1 then
      smart_executable._opts.command = local_executable
      table.insert(sources, smart_executable)
    else
      if vim.fn.executable(exe) == 1 then
        smart_executable._opts.command = exe
        table.insert(sources, smart_executable)
      end
    end
  else
    if null_ls.builtins[type][exe] and vim.fn.executable(null_ls.builtins[type][exe]._opts.command) then
      table.insert(sources, null_ls.builtins[type][exe])
    end
  end
  null_ls.register { sources = sources }
end

-- TODO: for linters and formatters with spaces and '-' replace with '_'
local function setup(filetype, type)
  local executables = nil
  if type == "diagnostics" then
    executables = lvim.lang[filetype].linters
  end
  if type == "formatting" then
    executables = lvim.lang[filetype].formatter.exe
  end

  if utils.is_table(executables) then
    for _, exe in pairs(executables) do
      if exe ~= "" then
        setup_ls(exe, type)
      end
    end
  end
  if utils.is_string(executables) and executables ~= "" then
    setup_ls(executables, type)
  end
end

-- TODO: return the formatter if one was registered, then turn off the builtin formatter
function M.setup(filetype)
  setup(filetype, "formatting")
  setup(filetype, "diagnostics")
  lvim.sources = sources
  return sources
end

return M