diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-10-05 09:46:55 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-10-05 09:46:55 +0200 |
commit | 1890844c731b4c5c648b1871193947144aefa3dd (patch) | |
tree | d4d44e7369d039d3b18e91b9696004a51a01ea87 /app | |
parent | ed2a9d64f07656956b76442ed91b41981b912a60 (diff) |
refactor issue text extraction
Diffstat (limited to 'app')
-rw-r--r-- | app/Issue.hs | 55 | ||||
-rw-r--r-- | app/Issue/Text.hs | 53 | ||||
-rw-r--r-- | app/Main.hs | 4 |
3 files changed, 64 insertions, 48 deletions
diff --git a/app/Issue.hs b/app/Issue.hs index 0d9f6ad..249ce92 100644 --- a/app/Issue.hs +++ b/app/Issue.hs @@ -5,13 +5,11 @@ module Issue (Issue (..), fromMatch) where -import Data.List (find) -import Data.Maybe (fromMaybe) import Data.Text (Text) import Data.Text qualified as T import Issue.Tag (Tag) import Issue.Tag qualified as I -import TreeGrepper.FileType qualified as G +import Issue.Text qualified as I import TreeGrepper.Match (Match (..)) import TreeGrepper.Match qualified as G import TreeGrepper.Result (Result (..)) @@ -19,7 +17,7 @@ import TreeGrepper.Result qualified as G data Issue = Issue { title :: Text, - description :: Text, + description :: Maybe Text, start :: G.Position, end :: G.Position, tags :: [Tag] @@ -27,59 +25,26 @@ data Issue = Issue fromMatch :: G.Result -> G.Match -> Maybe Issue fromMatch result match = - if T.isPrefixOf marker (T.unlines (take 1 lns)) + if T.isPrefixOf marker title then Just Issue - { title = stripMarker (T.strip (T.unlines title)), - description = stripTags (T.strip (T.unlines description)), + { title = stripMarker title, + description = stripTags <$> description, start = match.start, end = match.end, - tags = I.extract text + tags = maybe [] I.extract description } else Nothing where - text = stripComments result.file_type (T.strip match.text) - lns = T.lines text - title = takeWhile (not . isEmpty) lns - description = drop (length title + 1) lns - isEmpty = T.null . T.strip + (title, description) = I.extractText result.file_type match.text marker :: Text marker = "TODO" -stripTags :: Text -> Text -stripTags text = - T.strip (T.unlines (filter (not . T.isPrefixOf "@") (T.lines text))) - stripMarker :: Text -> Text stripMarker text = maybe text T.stripStart (T.stripPrefix marker text) -stripComments :: G.FileType -> Text -> Text -stripComments fileType text = - maybe - (stripLineComments (G.info fileType).lineStart text) - ( \(blockInfo, blockStart) -> - stripBlockComment blockStart blockInfo.blockEnd text - ) - $ do - blockInfo <- (G.info fileType).block - (,) blockInfo <$> find (`T.isPrefixOf` text) blockInfo.blockStart - -stripLineComments :: Text -> Text -> Text -stripLineComments lineStart text = - onLines - ( \line -> - fromMaybe line . fmap T.stripStart $ - T.stripPrefix lineStart line - ) - text - where - onLines f = T.unlines . map f . T.lines - -stripBlockComment :: Text -> Text -> Text -> Text -stripBlockComment blockStart blockEnd text = - T.strip - . (fromMaybe text . T.stripSuffix blockEnd) - . (fromMaybe text . T.stripPrefix blockStart) - $ text +stripTags :: Text -> Text +stripTags text = + T.strip (T.unlines (filter (not . T.isPrefixOf "@") (T.lines text))) diff --git a/app/Issue/Text.hs b/app/Issue/Text.hs new file mode 100644 index 0000000..190e1c7 --- /dev/null +++ b/app/Issue/Text.hs @@ -0,0 +1,53 @@ +{-# LANGUAGE OverloadedRecordDot #-} +{-# LANGUAGE OverloadedStrings #-} + +module Issue.Text (extractText) where + +import Control.Arrow (second) +import Data.List (find) +import Data.Maybe (fromMaybe) +import Data.Text (Text) +import Data.Text qualified as T +import TreeGrepper.FileType (FileType) +import TreeGrepper.FileType qualified as G + +extractText :: FileType -> Text -> (Text, Maybe Text) +extractText fileType rawText = (title, description) + where + text = stripComments fileType $ stripLines rawText + stripLines = T.unlines . map T.strip . T.lines + (title, description') = + second T.stripStart $ + T.breakOn "\n\n" text + description + | T.null description' = Nothing + | otherwise = Just description' + +stripComments :: G.FileType -> Text -> Text +stripComments fileType text = + maybe + (stripLineComments (G.info fileType).lineStart text) + ( \(blockInfo, blockStart) -> + stripBlockComment blockStart blockInfo.blockEnd text + ) + $ do + blockInfo <- (G.info fileType).block + (,) blockInfo <$> find (`T.isPrefixOf` text) blockInfo.blockStart + +stripLineComments :: Text -> Text -> Text +stripLineComments lineStart text = + onLines + ( \line -> + fromMaybe line . fmap T.stripStart $ + T.stripPrefix lineStart line + ) + text + where + onLines f = T.unlines . map f . T.lines + +stripBlockComment :: Text -> Text -> Text -> Text +stripBlockComment blockStart blockEnd text = + T.strip + . (fromMaybe text . T.stripSuffix blockEnd) + . (fromMaybe text . T.stripPrefix blockStart) + $ text diff --git a/app/Main.hs b/app/Main.hs index cc8bde3..f48d824 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -166,9 +166,7 @@ showMatches issues = do P.vsep ( concat [ [P.annotate P.bold (P.pretty issue.title)], - if not (T.null issue.description) - then [P.pretty issue.description] - else [], + maybe [] ((: []) . P.pretty) issue.description, map ( \(I.Tag k v) -> P.annotate (P.colorDull P.Yellow) $ |