feat(config): add base implementation

* feat(config): naive implementation

feat(config): get command for a run from config

* docs: add config

* fix typo [skip ci]

* test: add config
This commit is contained in:
Smirnov Oleksandr 2022-06-27 20:28:35 +03:00 committed by GitHub
parent 1db0914cfc
commit 40a2839eab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 97 additions and 10 deletions

View file

@ -20,6 +20,25 @@ use {
Also, run `TSInstall go` if install the `go` parser if not installed yet.
## Config
By `.setup` function you can configure the plugin.
Note:
- Installer does not install the tool in user set path
```lua
require("gopher").setup {
commands = {
go = "go",
gomodifytags = "gomodifytags",
gotests = "~/go/bin/gotests", -- also you can set custom command path
impl = "impl",
},
}
```
## Features
1. Install requires go tools:

19
lua/gopher/config.lua Normal file
View file

@ -0,0 +1,19 @@
local M = {
config = {
---set custom commands for tools
commands = {
go = "go",
gomodifytags = "gomodifytags",
gotests = "gotests",
impl = "impl",
},
},
}
---Plugin setup function
---@param opts table user options
function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts)
end
return M

View file

@ -1,4 +1,5 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---run "go generate"
@ -12,7 +13,7 @@ return function(...)
Job
:new({
command = "go",
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then

View file

@ -1,4 +1,5 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---run "go get"
@ -19,7 +20,7 @@ return function(...)
Job
:new({
command = "go",
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then

View file

@ -1,4 +1,5 @@
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---run "go mod"
@ -13,7 +14,7 @@ return function(...)
Job
:new({
command = "go",
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then

View file

@ -1,5 +1,6 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local M = {}
@ -7,7 +8,7 @@ local M = {}
local function run(cmd_args)
Job
:new({
command = "gotests",
command = c.gotests,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then

View file

@ -1,4 +1,5 @@
local utils = require "gopher._utils"
local c = require("gopher.config").config.commands
local M = {
_required = {
plugins = {
@ -6,10 +7,10 @@ local M = {
{ lib = "nvim-treesitter" },
},
binarys = {
{ bin = "go", help = "required for GoMod, GoGet, GoGenerate command" },
{ bin = "gomodifytags", help = "required for modify struct tags" },
{ bin = "impl", help = "required for interface implementing" },
{ bin = "gotests", help = "required for test(s) generation" },
{ bin = c.go, help = "required for GoMod, GoGet, GoGenerate command" },
{ bin = c.gomodifytags, help = "required for modify struct tags" },
{ bin = c.impl, help = "required for interface implementing" },
{ bin = c.gotests, help = "required for test(s) generation" },
},
},
}

View file

@ -1,5 +1,6 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
---@return string
@ -55,7 +56,7 @@ return function(...)
local res_data
Job
:new({
command = "impl",
command = c.impl,
args = cmd_args,
on_exit = function(data, retval)
if retval ~= 0 then

View file

@ -12,5 +12,6 @@ gopher.generate = require "gopher.gogenerate"
gopher.test_add = gotests.func_test
gopher.test_exported = gotests.all_exported_tests
gopher.tests_all = gotests.all_tests
gopher.setup = require("gopher.config").setup
return gopher

View file

@ -1,6 +1,7 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils"
local c = require("gopher.config").config.commands
local M = {}
local function modify(...)
@ -42,7 +43,7 @@ local function modify(...)
local res_data
Job
:new({
command = "gomodifytags",
command = c.gomodifytags,
args = cmd_args,
on_exit = function(data, retval)
if retval ~= 0 then

View file

@ -0,0 +1,41 @@
describe("gopher.config", function()
it("can be required", function()
require "gopher.config"
end)
it(".setup() when gets empty table not edit config", function()
local c = require "gopher.config"
c.setup {}
assert.are.same(c.config.commands.go, "go")
assert.are.same(c.config.commands.gomodifytags, "gomodifytags")
assert.are.same(c.config.commands.gotests, "gotests")
assert.are.same(c.config.commands.impl, "impl")
end)
it(".setup() when get one custom value sets that", function()
local c = require "gopher.config"
c.setup { commands = {
go = "custom_go",
} }
assert.are.same(c.config.commands.go, "custom_go")
end)
it(".setup() when get all custom values sets it", function()
local c = require "gopher.config"
c.setup {
commands = {
go = "go1.18",
gomodifytags = "user-gomodifytags",
gotests = "gotests",
impl = "goimpl",
},
}
assert.are.same(c.config.commands.go, "go1.18")
assert.are.same(c.config.commands.gomodifytags, "user-gomodifytags")
assert.are.same(c.config.commands.gotests, "gotests")
assert.are.same(c.config.commands.impl, "goimpl")
end)
end)