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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Issue
( Issue (..),
Provenance (..),
id,
internalTags,
replaceText,
)
where
import CMark qualified as D
import CMark.Extra ()
import Comment qualified as G
import Comment.Language qualified as G
import Data.Binary (Binary (..))
import Data.ByteString.Lazy qualified as LB
import Data.Digest.Pure.SHA qualified as S
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as N
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.IO qualified as T
import Data.Time.Clock (UTCTime (utctDay))
import GHC.Generics (Generic)
import GHC.Records (HasField (..))
import Git (Author (..), Commit (..))
import Git qualified
import Issue.Parser qualified as I
import Issue.Provenance (Provenance (..))
import Issue.Tag (Tag (..))
import Issue.Text qualified as I
import Prelude hiding (id)
data Issue = Issue
{ commitHash :: Git.CommitHash,
language :: G.Language,
rawTextHash :: S.Digest S.SHA1State,
title :: T.Text,
file :: FilePath,
provenance :: Provenance,
startByte :: Int,
endByte :: Int,
startPoint :: G.Point,
endPoint :: G.Point,
tags :: [Tag],
markers :: [T.Text],
commentStyle :: G.CommentStyle,
closed :: Bool
}
deriving (Show, Binary, Generic, Eq)
instance HasField "description" Issue (IO (Maybe (NonEmpty D.Node))) where
getField issue = do
rawText <- issue.rawText
let node = D.commonmarkToNode [] rawText
case I.parse I.issueMarkers node of
Just parseResult -> pure (N.nonEmpty parseResult.paragraphs)
Nothing -> pure Nothing
instance HasField "rawText" Issue (IO T.Text) where
getField issue = do
text <- getText issue
let (_, rawText) = G.uncomment issue.language text
pure rawText
instance HasField "comments" Issue (IO (Maybe (NonEmpty [D.Node]))) where
getField issue = do
rawText <- issue.rawText
let node = D.commonmarkToNode [] rawText
case I.parse I.issueMarkers node of
Just parseResult -> pure (N.nonEmpty parseResult.comments)
Nothing -> pure Nothing
id :: Issue -> T.Text
id issue = toSpinalCase issue.title
where
toSpinalCase = T.replace " " "-" . T.filter keep . T.toLower
keep = (`elem` (concat [[' ', '-'], ['a' .. 'z'], ['0' .. '9']]))
internalTags :: Issue -> [Tag]
internalTags issue@(Issue {..}) =
concat
[ [ Tag "id" $ Just issue.id,
Tag "createdAt" $ Just $ T.pack $ show $ utctDay provenance.first.date,
Tag "modifiedAt" $ Just $ T.pack $ show $ utctDay provenance.last.date,
Tag "author" $ Just provenance.first.author.name,
Tag "editor" $ Just provenance.last.author.name,
Tag "state" $ Just $ if closed then "closed" else "open"
],
map (Tag "type" . Just) markers
]
instance HasField "internalTags" Issue [Tag] where
getField issue = internalTags issue
instance HasField "id" Issue T.Text where
getField issue = id issue
getText :: Issue -> IO T.Text
getText (Issue {..}) =
T.decodeUtf8 . LB.toStrict . LB.take (fromIntegral (endByte - startByte)) . LB.drop (fromIntegral startByte)
<$> Git.readTextFileOfBS commitHash file
replaceText :: Issue -> T.Text -> IO ()
replaceText issue s' =
T.writeFile issue.file . replace (indent (comment s')) =<< T.readFile issue.file
where
comment = T.intercalate "\n" . map T.strip . T.lines . G.comment issue.commentStyle
indent = T.intercalate "\n" . mapButFirst (T.replicate (issue.startPoint.column - 1) " " <>) . T.lines
replace s t = before <> s <> after
where
t' = T.lines t
before = T.intercalate "\n" (mapLast (T.take (issue.startPoint.column - 1)) (take issue.startPoint.row t'))
after = T.unlines (mapFirst (T.drop issue.endPoint.column) (drop (issue.endPoint.row - 1) t'))
mapFirst _ [] = []
mapFirst f (x : xs) = f x : xs
mapLast _ [] = []
mapLast f [x] = [f x]
mapLast f (x : xs) = x : mapLast f xs
mapButFirst _ [] = []
mapButFirst f (x : xs) = x : map f xs
|