diff --git a/lua/neotest-golang/ast.lua b/lua/neotest-golang/ast.lua index a1fdc286..78a59403 100644 --- a/lua/neotest-golang/ast.lua +++ b/lua/neotest-golang/ast.lua @@ -141,7 +141,8 @@ function M.detect_tests(file_path) local opts = { nested_tests = true } ---@type neotest.Tree - local positions = lib.treesitter.parse_positions(file_path, query, opts) + local tree = lib.treesitter.parse_positions(file_path, query, opts) + local tree_with_merged_namespaces = M.merge_duplicate_namespaces(tree:root()) -- vim.notify(vim.inspect(positions), vim.log.levels.DEBUG) -- FIXME: remove after finishing debugging/implementation @@ -159,7 +160,42 @@ function M.detect_tests(file_path) -- assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) -- } - return positions + return tree_with_merged_namespaces +end + +function M.merge_duplicate_namespaces(node) + if not node._children or #node._children == 0 then + return node + end + + local namespaces = {} + local new_children = {} + + for _, child in ipairs(node._children) do + if child._data.type == "namespace" then + local existing = namespaces[child._data.name] + if existing then + -- Merge children of duplicate namespace + for _, grandchild in ipairs(child._children) do + table.insert(existing._children, grandchild) + grandchild._parent = existing + end + else + namespaces[child._data.name] = child + table.insert(new_children, child) + end + else + table.insert(new_children, child) + end + end + + -- Recursively process children + for _, child in ipairs(new_children) do + M.merge_duplicate_namespaces(child) + end + + node._children = new_children + return node end return M