From 49ed8057505f4af272247b757397ac0336efae39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3n=C3=A1n=20Carrigan?= Date: Sun, 6 Feb 2022 21:37:21 +0000 Subject: [PATCH] feat: add dap args --- lua/neotest-python/base.lua | 23 +-------------------- lua/neotest-python/init.lua | 41 ++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/lua/neotest-python/base.lua b/lua/neotest-python/base.lua index c2e11cd..064d46e 100644 --- a/lua/neotest-python/base.lua +++ b/lua/neotest-python/base.lua @@ -17,7 +17,7 @@ M.module_exists = function(module, python_command) return lib.process.run(vim.tbl_flatten({ python_command, "-c", - "import imp; imp.find_module('"..module.."')", + "import imp; imp.find_module('" .. module .. "')", })) == 0 end @@ -56,25 +56,4 @@ function M.parse_positions(file_path) return lib.treesitter.parse_positions(file_path, query) end -function M.get_strategy_config(strategy, python, python_script, args) - local config = { - dap = function() - return { - type = "python", - name = "Neotest Debugger", - request = "launch", - python = python, - program = python_script, - cwd = async.fn.getcwd(), - args = args, - justMyCode= false, - } - end, - } - if config[strategy] then - return config[strategy]() - end -end - return M - diff --git a/lua/neotest-python/init.lua b/lua/neotest-python/init.lua index 1c79eea..49bfddf 100644 --- a/lua/neotest-python/init.lua +++ b/lua/neotest-python/init.lua @@ -10,6 +10,27 @@ end local python_script = (Path.new(script_path()):parent():parent() / "neotest.py").filename +local dap_args + +local function get_strategy_config(strategy, python, python_script, args) + local config = { + dap = function() + return vim.tbl_extend("keep", { + type = "python", + name = "Neotest Debugger", + request = "launch", + python = python, + program = python_script, + cwd = async.fn.getcwd(), + args = args, + }, dap_args or {}) + end, + } + if config[strategy] then + return config[strategy]() + end +end + local get_args = function(runner, position) if runner == "unittest" then runner = "pyunit" @@ -97,12 +118,18 @@ function PythonNeotestAdapter.build_spec(args) python_script, script_args, }) + local strategy_config = get_strategy_config( + args.strategy, + python, + python_script, + script_args + ) return { command = command, context = { results_path = results_path, }, - strategy = base.get_strategy_config(args.strategy, python, python_script, script_args), + strategy = strategy_config, } end @@ -111,11 +138,11 @@ end ---@param result NeotestStrategyResult ---@return NeotestResult[] function PythonNeotestAdapter.results(spec, result) - -- TODO: Find out if this JSON option is supported in future local success, data = pcall(lib.files.read, spec.context.results_path) if not success then data = "{}" end + -- TODO: Find out if this JSON option is supported in future local results = vim.json.decode(data, { luanil = { object = true } }) for _, pos_result in pairs(results) do result.output_path = pos_result.output_path @@ -123,6 +150,10 @@ function PythonNeotestAdapter.results(spec, result) return results end +local is_callable = function(obj) + return type(obj) == "function" or (type(obj) == "table" and obj.__call) +end + setmetatable(PythonNeotestAdapter, { __call = function(_, opts) if type(opts.args) == "function" or (type(opts.args) == "table" and opts.args.__call) then @@ -132,13 +163,17 @@ setmetatable(PythonNeotestAdapter, { return opts.args end end - if type(opts.runner) == "function" or (type(opts.runner) == "table" and opts.runner.__call) then + if is_callable(opts.runner) then get_runner = opts.runner elseif opts.runner then get_runner = function() return opts.runner end end + if type(opts.dap) == "table" then + dap_args = opts.dap + end + return PythonNeotestAdapter end, })