aboutsummaryrefslogtreecommitdiffstats
path: root/app/TreeGrepper/FileType.hs
blob: 506cbc5760a79fdd186de5a78c411ab8abab30f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module TreeGrepper.FileType
  ( FileType (..),
    all,
    Info (..),
    BlockInfo (..),
    info,
  )
where

import Data.Aeson (FromJSON (parseJSON))
import Data.Binary (Binary)
import Data.Text (Text)
import GHC.Generics (Generic)
import Prelude hiding (all)

data FileType
  = Elm
  | Haskell
  | Nix
  | Shell
  deriving (Eq, Show, Generic, Binary)

instance FromJSON FileType where
  parseJSON v =
    parseJSON v >>= \case
      "elm" -> pure Elm
      "haskell" -> pure Haskell
      "nix" -> pure Nix
      "sh" -> pure Shell
      fileType -> fail ("parsing file_type failed, got " ++ fileType)

all :: [FileType]
all =
  [ Elm,
    Haskell,
    Nix,
    Shell
  ]

data Info = Info
  { exts :: [String],
    lineStart :: Text,
    block :: Maybe BlockInfo
  }

data BlockInfo = BlockInfo
  { blockStart :: [Text],
    blockEnd :: Text
  }

info :: FileType -> Info
info Elm =
  Info
    { exts = [".elm"],
      lineStart = "--",
      block = Just BlockInfo {blockStart = ["{-|", "{-"], blockEnd = "-}"}
    }
info Haskell =
  Info
    { exts = [".hs"],
      lineStart = "--",
      block = Just BlockInfo {blockStart = ["{-"], blockEnd = "-}"}
    }
info Nix =
  Info
    { exts = [".nix"],
      lineStart = "#",
      block = Just BlockInfo {blockStart = ["/*"], blockEnd = "*/"}
    }
info Shell =
  Info
    { exts = [".sh"],
      lineStart = "#",
      block = Nothing
    }