Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit 08c1425

Browse files
committed
feat(builtins): add gotests support in code actions
1 parent 33cfeb7 commit 08c1425

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
local h = require("null-ls.helpers")
2+
local log = require("null-ls.logger")
3+
local methods = require("null-ls.methods")
4+
5+
local CODE_ACTION = methods.internal.CODE_ACTION
6+
7+
return h.make_builtin({
8+
name = "gotests",
9+
meta = {
10+
url = "https://github.com/cweill/gotests",
11+
description = "Go tool to generate tests",
12+
notes = { "Requires installing the Go tree-sitter parser." },
13+
},
14+
method = CODE_ACTION,
15+
filetypes = { "go" },
16+
can_run = function()
17+
return require("null-ls.utils").is_executable("gotests")
18+
end,
19+
generator_opts = {
20+
command = "gotests",
21+
args = { "-w" },
22+
},
23+
factory = function(opts)
24+
return {
25+
fn = function(params)
26+
local bufnr = params.bufnr
27+
local bufname = params.bufname
28+
local row = params.range.row
29+
local col = params.range.col
30+
print(vim.inspect(vim.fn.expand("%:p:h")))
31+
32+
local exec = function(cmd_opts)
33+
print(vim.inspect(cmd_opts))
34+
local cmd = table.concat(cmd_opts, " ")
35+
local output = vim.fn.system(cmd)
36+
if vim.api.nvim_get_vvar("shell_error") == 0 then
37+
vim.fn.execute(":e " .. bufname) -- reload the file
38+
else
39+
log:error(output) -- print the error message
40+
end
41+
end
42+
local add_function_test = function(fn_name)
43+
return {
44+
title = "gtests: add test for function: " .. fn_name,
45+
action = function()
46+
local cmd_opts = vim.list_extend({ opts.command }, opts.args or {})
47+
vim.list_extend(cmd_opts, { "--only", fn_name })
48+
vim.list_extend(cmd_opts, { bufname })
49+
return exec(cmd_opts)
50+
end,
51+
}
52+
end
53+
local add_package_test = function(pkg_name)
54+
return {
55+
title = "gotests: add tests for package: " .. pkg_name,
56+
action = function()
57+
local cmd_opts = vim.list_extend({ opts.command }, opts.args or {})
58+
vim.list_extend(cmd_opts, { "--all" })
59+
vim.list_extend(cmd_opts, { vim.fn.expand("%:p:h") })
60+
return exec(cmd_opts)
61+
end,
62+
}
63+
end
64+
65+
-- Main
66+
local tsnode = vim.treesitter.get_node_at_pos(bufnr, row - 1, col - 1, {})
67+
local actions = {}
68+
local package_name
69+
local function_name
70+
71+
-- Ops on function name
72+
if (tsnode:type()) == "identifier" then
73+
function_name = vim.treesitter.query.get_node_text(tsnode, 0)
74+
if function_name == nil then
75+
return
76+
end
77+
78+
table.insert(actions, add_function_test(function_name))
79+
end
80+
if (tsnode:type()) == "package_clause" then
81+
local tscnode = tsnode:child(1)
82+
if tscnode ~= nil and (tscnode:type() == "package_identifier") then
83+
package_name = vim.treesitter.query.get_node_text(tscnode, 0)
84+
end
85+
table.insert(actions, add_package_test(package_name))
86+
end
87+
return actions
88+
end,
89+
}
90+
end,
91+
})

0 commit comments

Comments
 (0)