]> git.wincent.com - docvim.git/commitdiff
Add file meant to be included in previous commit
authorGreg Hurrell <greg@hurrell.net>
Sat, 11 Jun 2016 06:28:47 +0000 (23:28 -0700)
committerGreg Hurrell <greg@hurrell.net>
Sat, 11 Jun 2016 06:28:47 +0000 (23:28 -0700)
lib/Text/Docvim/Optimize.hs [new file with mode: 0644]

diff --git a/lib/Text/Docvim/Optimize.hs b/lib/Text/Docvim/Optimize.hs
new file mode 100644 (file)
index 0000000..b92c714
--- /dev/null
@@ -0,0 +1,27 @@
+module Text.Docvim.Optimize (optimize) where
+
+import Control.Lens hiding (Empty)
+import Text.Docvim.AST
+
+-- | "Optimize" a Project's AST by eliminating empty paths.
+optimize :: Node -> Node
+optimize = transform prune
+
+-- | Marks a node for pruning by returning Empty if it has no non-Empty
+-- children.
+prune :: Node -> Node
+prune n | not (null (children n)) = if all empty (children n) then Empty else n
+        | container n = Empty
+        | otherwise = n
+
+-- | Returns True if the supplied node is the Empty node.
+empty :: Node -> Bool
+empty Empty = True
+empty _     = False
+
+-- | Returns True if the supplied node is a (potentially) prunable container.
+container :: Node -> Bool
+container (Project _)  = True
+container (Unit _)     = True
+container (DocBlock _) = True
+container _            = False