<commit_before>module Docvim.Visitor.Footer (getFooter) where

import Control.Monad.State
import Debug.Trace
import Docvim.AST

-- | State monad where the state is a Bool indicating whether we are
-- accumulating nodes after a `@footer` annotation, and the result is a list of
-- the accumulated nodes.
type Env = State (Bool, [Node]) [Node]

-- | Gets a list of nodes (if any exist) from the `@footer` section of the
-- source code.
--
-- If multiple footers (potentially across multiple translation units) no
-- guarantees about order but they just get concatenated in the order we see
-- them
getFooter :: Node -> [Node]
getFooter n = evalState (node n) (False, [])

nodes :: [Node] -> Env
nodes ns = concat <$> mapM node ns

node :: Node -> Env
node n = case n of
  DocBlock d -> do
    (_, acc) <- get
    ns <- nodes d
    put (False, acc) -- Make sure we reset state on exiting docblock.
    return $ acc ++ ns

  FooterAnnotation -> do
    (_, acc) <- get
    put (True, acc)
    return $ acc

  Unit u -> nodes u

  _ -> do
    (capture, acc) <- get
    return $ if capture
             then acc ++ [n]
             else acc<commit_msg>Further flesh out footer visitor<commit_after>module Docvim.Visitor.Footer (getFooter) where

import Control.Monad.State
import Debug.Trace
import Docvim.AST

-- | State monad where the state is a Bool indicating whether we are
-- accumulating nodes after a `@footer` annotation, and the result is a list of
-- the accumulated nodes.
type Env = State (Bool, [Node]) [Node]

-- | Gets a list of nodes (if any exist) from the `@footer` section of the
-- source code.
--
-- If multiple footers (potentially across multiple translation units) no
-- guarantees about order but they just get concatenated in the order we see
-- them
getFooter :: Node -> [Node]
getFooter n = evalState (node n) (False, [])

nodes :: [Node] -> Env
nodes ns = concat <$> mapM node ns

node :: Node -> Env
node n = case n of
  CommandAnnotation _ -> do
    (_, acc) <- get
    put (False, acc)
    return $ acc

  DocBlock d -> do
    (_, acc) <- get
    ns <- nodes d
    put (False, acc) -- Make sure we reset state on exiting docblock.
    return $ acc ++ ns

  FooterAnnotation -> do
    (_, acc) <- get
    put (True, acc)
    return $ acc

  MappingAnnotation _ -> do
    (_, acc) <- get
    put (False, acc)
    return $ acc

  MappingsAnnotation -> do
    (_, acc) <- get
    put (False, acc)
    return $ acc

  OptionAnnotation _ _ _ -> do
    (_, acc) <- get
    put (False, acc)
    return $ acc

  PluginAnnotation _ _ -> do
    (_, acc) <- get
    put (False, acc)
    return $ acc

  Unit u -> nodes u

  _ -> do
    (capture, acc) <- get
    return $ if capture
             then acc ++ [n]
             else acc
{'fraction_non_alphanumeric': 0.11030214779759738, 'fraction_numerical': 0.0, 'mean_word_length': 2.487309644670051, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974384', 'n_tokens_mistral': 873, 'n_tokens_neox': 825, 'n_words': 360}