]> git.wincent.com - docvim.git/commitdiff
Filter out Empty nodes
authorGreg Hurrell <greg@hurrell.net>
Sat, 11 Jun 2016 06:51:21 +0000 (23:51 -0700)
committerGreg Hurrell <greg@hurrell.net>
Sat, 11 Jun 2016 06:51:21 +0000 (23:51 -0700)
I wanted (want) to do this in a more generic way, but I first figured I would do
this "manually" using pattern matching and explicit constructors. Will keep
looking thought to see if there is something in `Control.Lens` or `Data.Data`
that would help me do this.

Closes: https://github.com/wincent/docvim/issues/9
lib/Text/Docvim/Optimize.hs
tests/fixtures/parser/command.golden
tests/fixtures/parser/integration-docvim.golden
tests/fixtures/parser/integration-ferret-ftplugin-qf.golden
tests/fixtures/parser/integration-ferret-plugin.golden
tests/fixtures/parser/integration-ferret-private.golden
tests/fixtures/parser/integration-pinnacle.golden
tests/fixtures/parser/minimal-annotation.golden
tests/fixtures/parser/plugin-annotation.golden
tests/fixtures/parser/simple-annotations.golden

index b92c71433adf601041c2d0c147c07ed1b4d2aca7..68057f4938905aaabf47925ab97ef4e2fcf00925 100644 (file)
@@ -10,9 +10,17 @@ 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
+prune n | not (null (children n)) = checkChildren
         | container n = Empty
         | otherwise = n
+  where
+    checkChildren | all empty (children n) = Empty
+                  | any empty (children n) = filterChildren n
+                  | otherwise = n
+    filterChildren (Project cs) = Project $ filter (not . empty) cs
+    filterChildren (Unit cs) = Unit $ filter (not . empty) cs
+    filterChildren (DocBlock cs) = DocBlock $ filter (not . empty) cs
+    filterChildren _ = n
 
 -- | Returns True if the supplied node is the Empty node.
 empty :: Node -> Bool
index 1cbe19519bc66cc8e44c76212e6ace749ac1dee4..b3b75e96666b5a69e9e738ae59784c210aacb103 100644 (file)
@@ -1,6 +1,5 @@
 Project
-  [ Empty
-  , CommandsAnnotation
+  [ CommandsAnnotation
   , CommandAnnotation "Ack" (Just "{pattern} {options}")
   , Paragraph [ Plaintext "Info." ]
   , CommandAnnotation "Qargs" Nothing
index 5a68d50b3251e242a5e277550303c6cb03d93bba..559325ed580b2131a37db7e9dd67fa55f23c0a43 100644 (file)
@@ -935,5 +935,4 @@ Project
       [ ListItem
           [ Plaintext "Initial" , Whitespace , Plaintext "release." ]
       ]
-  , Empty
   ]
index 8c6174e3d238506421a3df9da1f4f1c745eb7a68..0ae56a7704855a32cf9e9d5db01b849b63474bef 100644 (file)
@@ -1,8 +1,7 @@
 Project
   [ Project
       [ Unit
-          [ Empty
-          , LetStatement
+          [ LetStatement
               { letLexpr = "s:options"
               , letValue = "get(g:, 'FerretQFOptions', 1)"
               }
@@ -26,7 +25,6 @@ Project
           , GenericStatement "augroup END"
           , GenericStatement "endif"
           , GenericStatement "endif"
-          , Empty
           , LetStatement
               { letLexpr = "s:map" , letValue = "get(g:, 'FerretQFMap', 1)" }
           , GenericStatement "if s:map"
@@ -38,7 +36,6 @@ Project
               "vnoremap <buffer> <silent> d :call ferret#private#qf_delete()<CR>"
           , GenericStatement "endif"
           ]
-      , Empty
       ]
   , OptionsAnnotation
   , OptionAnnotation "g:FerretQFOptions" "boolean" (Just "1")
index f68d84391fcbb2cb0d00936fb3a482288936285d..e6c951a777f2827cfae10ee613edf2c091d4da79 100644 (file)
@@ -829,9 +829,7 @@ Project
   , Fenced [ ":call pathogen#helptags()" ]
   , Project
       [ Unit
-          [ Empty
-          , Empty
-          , GenericStatement
+          [ GenericStatement
               "if exists('g:FerretLoaded') || &compatible || v:version < 700"
           , GenericStatement "finish"
           , GenericStatement "endif"
@@ -867,50 +865,40 @@ Project
           , GenericStatement "autocmd QuickFixCmdPost l* nested lwindow"
           , GenericStatement "augroup END"
           , GenericStatement "endif"
-          , Empty
           , GenericStatement
               "command! -nargs=+ -complete=file Ack call ferret#private#ack(<q-args>)"
-          , Empty
           , GenericStatement
               "command! -nargs=+ -complete=file Lack call ferret#private#lack(<q-args>)"
-          , Empty
           , GenericStatement
               "command! -nargs=1 Acks call ferret#private#acks(<q-args>)"
-          , Empty
           , LetStatement
               { letLexpr = "s:map" , letValue = "get(g:, 'FerretMap', 1)" }
           , GenericStatement "if s:map"
           , GenericStatement
               "if !hasmapto('<Plug>(FerretAck)') && maparg('<leader>a', 'n') ==# ''"
-          , Empty
           , GenericStatement "nmap <unique> <leader>a <Plug>(FerretAck)"
           , GenericStatement "endif"
           , GenericStatement "nnoremap <Plug>(FerretAck) :Ack<space>"
           , GenericStatement
               "if !hasmapto('<Plug>FerretLack') && maparg('<leader>l', 'n') ==# ''"
-          , Empty
           , GenericStatement "nmap <unique> <leader>l <Plug>(FerretLack)"
           , GenericStatement "endif"
           , GenericStatement "nnoremap <Plug>(FerretLack) :Lack<space>"
           , GenericStatement
               "if !hasmapto('<Plug>(FerretAckWord)') && maparg('<leader>s', 'n') ==# ''"
-          , Empty
           , GenericStatement "nmap <unique> <leader>s <Plug>(FerretAckWord)"
           , GenericStatement "endif"
           , GenericStatement
               "nnoremap <Plug>(FerretAckWord) :Ack <C-r><C-w><CR>"
           , GenericStatement
               "if !hasmapto('<Plug>(FerretAcks)') && maparg('<leader>r', 'n') ==# ''"
-          , Empty
           , GenericStatement "nmap <unique> <leader>r <Plug>(FerretAcks)"
           , GenericStatement "endif"
           , GenericStatement
               "nnoremap <Plug>(FerretAcks) :Acks <c-r>=(exists('g:ferret_lastsearch') ? '/' . g:ferret_lastsearch . '//' : ' ')<CR><Left>"
           , GenericStatement "endif"
-          , Empty
           , GenericStatement
               "command! -bar Qargs execute 'args' ferret#private#qargs()"
-          , Empty
           , LetStatement
               { letLexpr = "s:commands"
               , letValue = "get(g:, 'FerretQFCommands', 1)"
@@ -929,8 +917,6 @@ Project
               { letLexpr = "&cpoptions " , letValue = "s:cpoptions" }
           , UnletStatement { unletBang = False , unletBody = "s:cpoptions" }
           ]
-      , Empty
-      , Empty
       ]
   , CommandsAnnotation
   , CommandAnnotation "Ack" (Just "{pattern} {options}")
index f01965a44412cd993a2376b007718cc7c0268490..33692b79294449af5139f27679b5179fb1dd60b9 100644 (file)
@@ -345,7 +345,6 @@ Project
                   ]
               }
           ]
-      , Empty
       ]
   , OptionsAnnotation
   , OptionAnnotation "g:FerretDispatch" "boolean" (Just "1")
index a3e72572997bcb6ab71d6207157966f901ed5d47..b2b66fac8e1b4d551d3d9cc27fe7ff904f1dcf35 100644 (file)
@@ -774,5 +774,4 @@ Project
       [ ListItem
           [ Plaintext "Initial" , Whitespace , Plaintext "release." ]
       ]
-  , Empty
   ]
index f0d9c2723a61ad9d44346868f1cf9c7165a72ba5..1820ac0234848c3431177ef7fb2035bb83ca8298 100644 (file)
@@ -1,2 +1 @@
-Project
-  [ DocBlock [ PluginAnnotation "Foo" "Bar" , TOC [] ] , Empty ]
+Project [ DocBlock [ PluginAnnotation "Foo" "Bar" , TOC [] ] ]
index 7e2d34f8d6a3345f0b0555c5d76d98d48fb84b12..d99c8eaac729d1b037a5e69234184a3d2322abb4 100644 (file)
@@ -3,5 +3,4 @@ Project
       [ PluginAnnotation "Foo" "Description with trailing whitespace"
       , TOC []
       ]
-  , Empty
   ]
index c070693a7fb8f6228e8b02d69b7ba5b20b7b5205..a9878bd206a240c98d212241674f798bebb7b6d8 100644 (file)
@@ -4,7 +4,6 @@ Project
   , Project
       [ Unit
           [ LetStatement { letLexpr = "g:x" , letValue = "1 \"Unset!" }
-          , Empty
           , LetStatement { letLexpr = "g:y" , letValue = "1" }
           ]
       ]