all repos

gopher.nvim @ v0.6.0

Minimalistic plugin for Go development

gopher.nvim/lua/gopher/go.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
local c = require "gopher.config"
local u = require "gopher._utils"
local r = require "gopher._utils.runner"
local go = {}

local function run(subcmd, args)
  local rs = r.sync { c.commands.go, subcmd, unpack(args) }
  if rs.code ~= 0 then
    error("go " .. subcmd .. " failed: " .. rs.stderr)
  end

  u.notify(c.commands.go .. " " .. subcmd .. " ran successful")
  return rs.stdout
end

---@param args string[]
function go.get(args)
  for i, arg in ipairs(args) do
    local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
    table.remove(args, i)
    table.insert(args, i, m)
  end

  run("get", args)
end

---@param args string[]
function go.mod(args)
  run("mod", args)
end

---@param args string[]
function go.work(args)
  -- TODO: use `gopls.tidy`

  run("work", args)
end

---Executes `go generate`
---If only argument is `%` it's going to be equivalent to `go generate <path to current file>`
---@param args string[]
function go.generate(args)
  -- TODO: use `gopls.generate`

  if #args == 0 then
    error "please provide arguments"
  end

  if #args == 1 and args[1] == "%" then
    args[1] = vim.fn.expand "%"
  end

  run("generate", args)
end

return go