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

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

import Data.List (find)
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
import Prelude hiding (id)

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

id :: Issue -> Maybe String
id issue =
  (\(Tag _ v) -> T.unpack v)
    <$> ( find (\(Tag k _) -> k == "id") $
            issue.tags ++ issue.internalTags
        )

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

marker :: Text
marker = "TODO"

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