Skip to content

Commit

Permalink
feat: merge duplicate namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil committed Jul 6, 2024
1 parent 0bca481 commit 2de85e2
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions lua/neotest-golang/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 2de85e2

Please sign in to comment.