From 40a2839eab374cdb9aec3f26603fad19a58d3704 Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Mon, 27 Jun 2022 20:28:35 +0300 Subject: [PATCH] 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 --- README.md | 19 +++++++++++++++++ lua/gopher/config.lua | 19 +++++++++++++++++ lua/gopher/gogenerate.lua | 3 ++- lua/gopher/goget.lua | 3 ++- lua/gopher/gomod.lua | 3 ++- lua/gopher/gotests.lua | 3 ++- lua/gopher/health.lua | 9 ++++---- lua/gopher/impl.lua | 3 ++- lua/gopher/init.lua | 1 + lua/gopher/struct_tags.lua | 3 ++- spec/gopher_config_spec.lua | 41 +++++++++++++++++++++++++++++++++++++ 11 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 lua/gopher/config.lua create mode 100644 spec/gopher_config_spec.lua diff --git a/README.md b/README.md index ce1f69e..b5e0272 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lua/gopher/config.lua b/lua/gopher/config.lua new file mode 100644 index 0000000..8f77dc7 --- /dev/null +++ b/lua/gopher/config.lua @@ -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 diff --git a/lua/gopher/gogenerate.lua b/lua/gopher/gogenerate.lua index ddf756d..93f78da 100644 --- a/lua/gopher/gogenerate.lua +++ b/lua/gopher/gogenerate.lua @@ -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 diff --git a/lua/gopher/goget.lua b/lua/gopher/goget.lua index a14f796..8d79626 100644 --- a/lua/gopher/goget.lua +++ b/lua/gopher/goget.lua @@ -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 diff --git a/lua/gopher/gomod.lua b/lua/gopher/gomod.lua index e451868..61f19a2 100644 --- a/lua/gopher/gomod.lua +++ b/lua/gopher/gomod.lua @@ -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 diff --git a/lua/gopher/gotests.lua b/lua/gopher/gotests.lua index bab3afa..c71243e 100644 --- a/lua/gopher/gotests.lua +++ b/lua/gopher/gotests.lua @@ -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 diff --git a/lua/gopher/health.lua b/lua/gopher/health.lua index 30ab04f..d628ddc 100644 --- a/lua/gopher/health.lua +++ b/lua/gopher/health.lua @@ -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" }, }, }, } diff --git a/lua/gopher/impl.lua b/lua/gopher/impl.lua index 363dc5f..1c504ee 100644 --- a/lua/gopher/impl.lua +++ b/lua/gopher/impl.lua @@ -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 diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 99ac4dd..d62a04a 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -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 diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index f82a641..27cfe39 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -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 diff --git a/spec/gopher_config_spec.lua b/spec/gopher_config_spec.lua new file mode 100644 index 0000000..1d033c0 --- /dev/null +++ b/spec/gopher_config_spec.lua @@ -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)