2023-03-21 02:58:01 +00:00
|
|
|
local runner = require('go.runner')
|
|
|
|
local utils = require('go.utils')
|
2022-11-03 01:53:45 +00:00
|
|
|
local vfn = vim.fn
|
2022-02-02 04:49:54 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.run(args)
|
2022-08-06 12:00:49 +00:00
|
|
|
args = args or {}
|
2022-02-02 04:49:54 +00:00
|
|
|
for i, arg in ipairs(args) do
|
2023-03-21 02:58:01 +00:00
|
|
|
local m = string.match(arg, '^https?://(.*)$') or arg
|
2022-02-02 04:49:54 +00:00
|
|
|
table.remove(args, i)
|
|
|
|
table.insert(args, i, m)
|
|
|
|
end
|
2022-02-09 04:33:19 +00:00
|
|
|
|
|
|
|
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
|
|
|
row, col = row - 1, col
|
|
|
|
|
|
|
|
local line = vim.api.nvim_buf_get_lines(0, row, row + 1, true)[1]
|
2023-03-21 02:58:01 +00:00
|
|
|
line = line:gsub('^%s+', '') -- lstrip
|
|
|
|
line = line:gsub('%s+', ' ') -- combine spaces
|
2022-11-22 22:31:02 +00:00
|
|
|
utils.log(line)
|
2023-03-21 02:58:01 +00:00
|
|
|
line = vim.split(line, ' ')
|
2022-11-22 22:31:02 +00:00
|
|
|
utils.log(line)
|
2023-03-21 02:58:01 +00:00
|
|
|
local cmd = { 'go', 'get' }
|
2022-02-02 04:49:54 +00:00
|
|
|
vim.list_extend(cmd, args)
|
2023-03-21 02:58:01 +00:00
|
|
|
local pkg1 = line[1]:gsub('"', '')
|
|
|
|
local pkg2 = ''
|
|
|
|
if line[2] then
|
|
|
|
pkg2 = line[2]:gsub('"', '')
|
|
|
|
end
|
|
|
|
utils.log(pkg1, pkg2)
|
|
|
|
if string.find(pkg1, '%a+%.%a+/%a+/%a+') or string.find(pkg1, '%a+%.%a+/%a+') then
|
2022-02-09 04:33:19 +00:00
|
|
|
-- the cursor is on line of package URL e.g. github.com/abc/pkg
|
2023-03-21 02:58:01 +00:00
|
|
|
table.insert(cmd, pkg1)
|
|
|
|
elseif string.find(pkg2, '%a+%.%a+/%a+/%a+') or string.find(pkg2, '%a+%.%a+/%a+') then
|
|
|
|
table.insert(cmd, pkg2)
|
2022-02-09 04:33:19 +00:00
|
|
|
else
|
|
|
|
if #args == 0 then
|
2023-03-21 02:58:01 +00:00
|
|
|
table.insert(cmd, './...')
|
2022-02-09 04:33:19 +00:00
|
|
|
end
|
|
|
|
end
|
2022-11-03 01:53:45 +00:00
|
|
|
|
2023-03-21 02:58:01 +00:00
|
|
|
utils.log(cmd)
|
|
|
|
|
2022-11-03 01:53:45 +00:00
|
|
|
local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd()
|
2023-03-21 02:58:01 +00:00
|
|
|
local modfile = workfolder .. utils.sep() .. 'go.mod'
|
2022-02-21 22:46:59 +00:00
|
|
|
local opts = {
|
2022-06-28 04:36:30 +00:00
|
|
|
update_buffer = true,
|
|
|
|
on_exit = function()
|
2022-02-21 22:46:59 +00:00
|
|
|
vim.schedule(function()
|
2022-11-03 01:53:45 +00:00
|
|
|
-- utils.restart()
|
|
|
|
require('go.lsp').watchFileChanged(modfile)
|
2022-02-21 22:46:59 +00:00
|
|
|
end)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
runner.run(cmd, opts)
|
2022-05-22 10:11:14 +00:00
|
|
|
return cmd, opts
|
2022-02-02 04:49:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|