all repos

gopher.nvim @ e8d53840d40d67f38b801e964f4a179e72c86aa8

Minimalistic plugin for Go development

gopher.nvim/lua/gopher/health.lua(view raw)

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
local c = require("gopher.config").commands
local health = {}

local deps = {
  vim_version = "nvim-0.10",
  bin = {
    {
      bin = c.go,
      msg = "required for `:GoGet`, `:GoMod`, `:GoGenerate`, `:GoWork`, `:GoInstallDeps`, `:GoInstallDepsSync`",
    },
    { bin = c.gomodifytags, msg = "required for `:GoTagAdd`, `:GoTagRm`" },
    { bin = c.impl, msg = "required for `:GoImpl`" },
    { bin = c.iferr, msg = "required for `:GoIfErr`" },
    { bin = c.gotests, msg = "required for `:GoTestAdd`, `:GoTestsAll`, `:GoTestsExp`" },
  },
  treesitter = {
    { parser = "go", msg = "required for most of the parts of `gopher.nvim`" },
  },
}

---@param bin {bin:string, msg:string, optional:boolean}
local function check_binary(bin)
  if vim.fn.executable(bin.bin) == 1 then
    vim.health.ok(bin.bin .. " is found oh PATH: `" .. vim.fn.exepath(bin.bin) .. "`")
  else
    vim.health.error(bin.bin .. " not found on PATH, " .. bin.msg)
  end
end

---@param ts {parser:string, msg:string}
local function check_treesitter(ts)
  local ok, parser = pcall(vim.treesitter.get_parser, 0, ts.parser)
  if ok and parser ~= nil then
    vim.health.ok("`" .. ts.parser .. "` parser is installed")
  else
    vim.health.error("`" .. ts.parser .. "` parser not found")
  end
end

function health.check()
  vim.health.start "Neovim version"
  if vim.fn.has(deps.vim_version) == 1 then
    vim.health.ok "Neovim version is compatible"
  else
    vim.health.error(deps.vim_version .. " or newer is required")
  end

  vim.health.start "Required binaries (those can be installed with `:GoInstallDeps`)"
  for _, bin in ipairs(deps.bin) do
    check_binary(bin)
  end

  vim.health.start "Treesitter"
  for _, parser in ipairs(deps.treesitter) do
    check_treesitter(parser)
  end
end

return health