From ddba25fde2253d2efad4ae0ac96eaf638c9ba2a7 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Mon, 17 Jun 2024 19:57:50 +0200 Subject: [PATCH] refactor: adapter options --- lua/neotest-golang/init.lua | 2 +- lua/neotest-golang/options.lua | 66 +++++++++++++---------------- lua/neotest-golang/runspec_dir.lua | 8 ++-- lua/neotest-golang/runspec_test.lua | 12 +++--- 4 files changed, 42 insertions(+), 46 deletions(-) diff --git a/lua/neotest-golang/init.lua b/lua/neotest-golang/init.lua index 1f1cce82..706a73fb 100644 --- a/lua/neotest-golang/init.lua +++ b/lua/neotest-golang/init.lua @@ -178,7 +178,7 @@ end --- Adapter options. setmetatable(M.Adapter, { __call = function(_, opts) - return options.setup(opts) + M.Adapter.options = options.setup(opts) end, }) diff --git a/lua/neotest-golang/options.lua b/lua/neotest-golang/options.lua index 943252c5..e6b1b4ba 100644 --- a/lua/neotest-golang/options.lua +++ b/lua/neotest-golang/options.lua @@ -2,50 +2,42 @@ --- providing them as arguments to the Adapter function. See the README for mode --- details and examples. -local M = {} +local Opts = {} ---- Arguments to pass into `go test`. Will be combined with arguments required ---- for neotest-golang to work and execute the expected test(s). ---- @type table -M._go_test_args = { - "-v", - "-race", - "-count=1", - "-timeout=60s", -} +--- Create a new options object. +function Opts:new(opts) + self.go_test_args = opts.go_test_args + or { + "-v", + "-race", + "-count=1", + "-timeout=60s", + } + self.dap_go_enabled = opts.dap_go_enabled or false + self.dap_go_opts = opts.dap_go_opts or {} +end ---- Whether to enable nvim-dap-go. ---- @type boolean -M._dap_go_enabled = false +--- A convenience function to get the current options. +function Opts:get() + return { + go_test_args = self.go_test_args, + dap_go_enabled = self.dap_go_enabled, + dap_go_opts = self.dap_go_opts, + } +end ---- Options to pass into dap-go.setup. ---- @type table -M._dap_go_opts = {} +local M = {} ---- Option setup function. This is what you call when setting up the adapter. ---- @param opts table +--- Set up the adapter. function M.setup(opts) opts = opts or {} - if opts.args or opts.dap_go_args then - -- temporary warning - vim.notify( - "Please update your config, the arguments/opts have changed for neotest-golang.", - vim.log.levels.WARN - ) - end - if opts.go_test_args then - if opts.go_test_args then - M._go_test_args = opts.go_test_args - end - if opts.dap_go_enabled then - M._dap_go_enabled = opts.dap_go_enabled - if opts.dap_go_opts then - M._dap_go_opts = opts.dap_go_opts - end - end - end + Opts:new(opts) + return M.options +end - return M.Adapter +--- Get the adapter configuration. +function M.get() + return Opts:get() end return M diff --git a/lua/neotest-golang/runspec_dir.lua b/lua/neotest-golang/runspec_dir.lua index 8b5bd792..7053cea1 100644 --- a/lua/neotest-golang/runspec_dir.lua +++ b/lua/neotest-golang/runspec_dir.lua @@ -86,12 +86,14 @@ function M.build_dir_test_runspec(pos, cwd, test_pattern) } --- @type table - local go_test_args = { + local required_go_test_args = { test_pattern, } - local combined_args = - vim.list_extend(vim.deepcopy(options._go_test_args), go_test_args) + local combined_args = vim.list_extend( + vim.deepcopy(options.get().go_test_args), + required_go_test_args + ) local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args) --- @type neotest.RunSpec diff --git a/lua/neotest-golang/runspec_test.lua b/lua/neotest-golang/runspec_test.lua index 9ba34012..0f2f62d0 100644 --- a/lua/neotest-golang/runspec_test.lua +++ b/lua/neotest-golang/runspec_test.lua @@ -20,14 +20,16 @@ function M.build(pos, strategy) } --- @type table - local go_test_args = { + local required_go_test_args = { test_folder_absolute_path, "-run", "^" .. test_name .. "$", } - local combined_args = - vim.list_extend(vim.deepcopy(options._go_test_args), go_test_args) + local combined_args = vim.list_extend( + vim.deepcopy(options.get().go_test_args), + required_go_test_args + ) local gotest_command = vim.list_extend(vim.deepcopy(gotest), combined_args) --- @type neotest.RunSpec @@ -47,8 +49,8 @@ function M.build(pos, strategy) run_spec.context.skip = true -- do not attempt to parse test output -- nvim-dap and nvim-dap-go cwd - if options._dap_go_enabled then - local dap_go_opts = options._dap_go_opts or {} + if options.get().dap_go_enabled then + local dap_go_opts = options.get().dap_go_opts or {} local dap_go_opts_original = vim.deepcopy(dap_go_opts) if dap_go_opts.delve == nil then dap_go_opts.delve = {}