From 652c2030c5ef39bf1dd34d26064e1059431898f0 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Tue, 9 Nov 2021 20:24:17 +0100 Subject: first example of a map adjustment this also includes some more monad plumbing, and an option for the linter to actually write things out again. Some of the previous commit was reverted a bit since it turned out to be stupid, but overall it was suprisingly easy once I got around to it, so yay! i guess Also includes a fairly silly example of how to use it. --- lib/LintWriter.hs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib/LintWriter.hs') diff --git a/lib/LintWriter.hs b/lib/LintWriter.hs index 8d91948..54a5954 100644 --- a/lib/LintWriter.hs +++ b/lib/LintWriter.hs @@ -84,9 +84,12 @@ resultToOffers (LintResult a) = mapMaybe lintToOffer $ snd a resultToLints :: LintResult a -> [Lint] resultToLints (LintResult res) = snd res --- | run a linter +resultToAdjusted :: LintResult a -> a +resultToAdjusted (LintResult res) = fst res + +-- | run a linter. Returns the adjusted context, and a list of lints runLintWriter :: ctxt -> Context -> LintWriter ctxt -> LintResult ctxt -runLintWriter c c' linter = LintResult (c, fst $ fromLinterState lints) +runLintWriter c c' linter = LintResult (snd $ fromLinterState lints,fst $ fromLinterState lints) where lints = snd $ runReader ranstate (c',c) ranstate = runStateT linter (LinterState ([], c)) @@ -104,6 +107,8 @@ dependsOn dep = tell' $ Depends dep offersEntrypoint :: Text -> LintWriter a offersEntrypoint text = tell' $ Offers text +-- | adjusts the context. Gets a copy of the /current/ context, i.e. one which might +-- have already been changed by other lints adjust :: (a -> a) -> LintWriter a adjust f = modify $ LinterState . second f . fromLinterState @@ -114,6 +119,8 @@ warn = lint Warning forbid = lint Forbidden complain = lint Error + +-- | get the context as it was originally, without any modifications askContext :: LintWriter' a a askContext = lift $ asks snd -- cgit v1.2.3