aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue.hs
blob: 249ce921c1e93f43859399e422241630b8f98c71 (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
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Issue (Issue (..), fromMatch) where

import Data.Text (Text)
import Data.Text qualified as T
import Issue.Tag (Tag)
import Issue.Tag qualified as I
import Issue.Text qualified as I
import TreeGrepper.Match (Match (..))
import TreeGrepper.Match qualified as G
import TreeGrepper.Result (Result (..))
import TreeGrepper.Result qualified as G

data Issue = Issue
  { title :: Text,
    description :: Maybe Text,
    start :: G.Position,
    end :: G.Position,
    tags :: [Tag]
  }

fromMatch :: G.Result -> G.Match -> Maybe Issue
fromMatch result match =
  if T.isPrefixOf marker title
    then
      Just
        Issue
          { title = stripMarker title,
            description = stripTags <$> description,
            start = match.start,
            end = match.end,
            tags = maybe [] I.extract description
          }
    else Nothing
  where
    (title, description) = I.extractText result.file_type match.text

marker :: Text
marker = "TODO"

stripMarker :: Text -> Text
stripMarker text = maybe text T.stripStart (T.stripPrefix marker text)

stripTags :: Text -> Text
stripTags text =
  T.strip (T.unlines (filter (not . T.isPrefixOf "@") (T.lines text)))