From c138fb9910c661f7efd00cc7dceb6fc68dc790a9 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Tue, 28 Nov 2023 12:46:59 +0100 Subject: move `replaceText` to `Issue` --- app/Issue.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'app/Issue.hs') diff --git a/app/Issue.hs b/app/Issue.hs index 9d5a102..411e910 100644 --- a/app/Issue.hs +++ b/app/Issue.hs @@ -3,12 +3,14 @@ module Issue Provenance (..), id, internalTags, + replaceText, ) where import Data.Binary (Binary) import Data.List (find) import Data.Text qualified as T +import Data.Text.IO qualified as T import Data.Time.Clock (UTCTime (utctDay)) import GHC.Generics (Generic) import GHC.Records (HasField (..)) @@ -55,3 +57,24 @@ toSpinalCase :: T.Text -> T.Text toSpinalCase = T.replace " " "-" . T.filter keep . T.toLower where keep = (`elem` (concat [[' ', '-'], ['a' .. 'z'], ['0' .. '9']])) + +-- TODO `replaceFile` hardcodes comment +-- +-- @difficulty easy +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 . map ("-- " <>) . T.lines + indent = T.intercalate "\n" . mapButFirst (T.replicate (issue.start.column - 1) " " <>) . T.lines + replace s t = before <> s <> after + where + t' = T.lines t + before = T.intercalate "\n" (mapLast (T.take (issue.start.column - 1)) (take issue.start.row t')) + after = T.unlines (mapFirst (T.drop issue.end.column) (drop (issue.end.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 -- cgit v1.2.3