diff --git a/src/base/tree.lua b/src/base/tree.lua index 464b6b17..d3dc3f97 100644 --- a/src/base/tree.lua +++ b/src/base/tree.lua @@ -31,11 +31,13 @@ -- The tree to contain the new node. -- @param p -- The path of the new node. +-- @param extraFields +-- A table containing key-value pairs to be added to any new nodes. -- @returns -- The new tree node. -- - function tree.add(tr, p) + function tree.add(tr, p, extraFields) -- Special case "." refers to the current node if p == "." then return tr @@ -43,7 +45,7 @@ -- Look for the immediate parent for this new node, creating it if necessary. -- Recurses to create as much of the tree as necessary. - local parentnode = tree.add(tr, path.getdirectory(p), onaddfunc) + local parentnode = tree.add(tr, path.getdirectory(p), extraFields) -- Create the child if necessary local childname = path.getname(p) @@ -51,6 +53,11 @@ if not childnode or childnode.path ~= p then childnode = tree.insert(parentnode, tree.new(childname)) childnode.path = p + if extraFields then + for k,v in pairs(extraFields) do + childnode[k] = v + end + end end return childnode @@ -275,7 +282,7 @@ local node = tr.children[1] -- if this node has no children (it is the last node in the tree) I'm done - if #node.children == 0 then + if #node.children == 0 or node.trim == false then break end diff --git a/src/project/project.lua b/src/project/project.lua index b3e3c0de..5c7b2ddb 100755 --- a/src/project/project.lua +++ b/src/project/project.lua @@ -722,13 +722,22 @@ -- in the IDE, not the physical organization of the file system. So -- virtual paths are used when adding nodes. + -- If the project script specifies a virtual path for a file, disable + -- the logic that could trim out empty root nodes from that path. If + -- the script writer wants an empty root node they should get it. + + local flags + if fcfg.vpath ~= fcfg.relpath then + flags = { trim = false } + end + -- Virtual paths can overlap, potentially putting files with the same -- name in the same folder, even though they have different paths on -- the underlying filesystem. The tree.add() call won't overwrite -- existing nodes, so provide the extra logic here. Start by getting -- the parent folder node, creating it if necessary. - local parent = tree.add(tr, path.getdirectory(fcfg.vpath)) + local parent = tree.add(tr, path.getdirectory(fcfg.vpath), flags) local node = tree.insert(parent, tree.new(path.getname(fcfg.vpath))) -- Pass through value fetches to the file configuration diff --git a/tests/base/test_tree.lua b/tests/base/test_tree.lua index af5e4e65..bb0cac45 100644 --- a/tests/base/test_tree.lua +++ b/tests/base/test_tree.lua @@ -267,3 +267,20 @@ tree.trimroot(tr) test.isequal("tests", tr.children[1].path) end + + +-- +-- Nodes with the key "trim" set to false should be removed. +-- + + function suite.trimroot_respectsTrimFlag() + local n = tree.add(tr, "A") + tree.add(tr, "A/1") + n.trim = false + tree.trimroot(tr) + prepare() + test.capture [[ + A + 1 + ]] + end