summaryrefslogtreecommitdiff
path: root/lua/lsp/null-ls.lua
blob: 099a6dc137c4a79f62d7f1e413fd731cf4cb84d9 (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
local M = {}

local null_ls = require "null-ls"
local sources = {}

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

local function is_table(t)
  return type(t) == "table"
end

local function is_string(t)
  return type(t) == "string"
end

local function has_value(tab, val)
  for index, value in ipairs(tab) do
    if value == val then
      return true
    end
  end

  return false
end

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

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

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 is_table(executables) then
    for _, exe in pairs(executables) do
      setup_ls(exe, type)
    end
  end
  if is_string(executables) then
    setup_ls(executables, type)
  end
end

function M.setup(filetype)
  setup(filetype, "formatting")
  setup(filetype, "diagnostics")
end

return M