Skip to content

Commit

Permalink
Treefmt
Browse files Browse the repository at this point in the history
  • Loading branch information
simmsb committed Mar 27, 2024
1 parent f11c765 commit 82ba567
Show file tree
Hide file tree
Showing 47 changed files with 620 additions and 433 deletions.
2 changes: 1 addition & 1 deletion calamity-commands/CalamityCommands/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ buildCheck name cb = do
pure $ MkCheck name cb''

-- | Given the name of a check and a pure callback function, build a check.
buildCheckPure :: Monad m => T.Text -> (c -> Maybe T.Text) -> Check m c
buildCheckPure :: (Monad m) => T.Text -> (c -> Maybe T.Text) -> Check m c
buildCheckPure name cb = MkCheck name (pure . cb)

{- | Given an invokation context @c@, run a check and transform the result into an
Expand Down
4 changes: 2 additions & 2 deletions calamity-commands/CalamityCommands/CommandUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ApplyTup a b where

applyTup :: ApplyTupRes a b -> a -> b

instance ApplyTup as b => ApplyTup (a, as) b where
instance (ApplyTup as b) => ApplyTup (a, as) b where
type ApplyTupRes (a, as) b = a -> ApplyTupRes as b

applyTup f (a, as) = applyTup (f a) as
Expand All @@ -201,7 +201,7 @@ instance ApplyTup () b where

buildTypedCommandParser ::
forall (ps :: [Type]) c r.
ParameterParser (ListToTup ps) c r =>
(ParameterParser (ListToTup ps) c r) =>
c ->
S.Text ->
P.Sem r (Either CommandError (ParserResult (ListToTup ps)))
Expand Down
10 changes: 5 additions & 5 deletions calamity-commands/CalamityCommands/Dsl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ commandA name aliases cmd = do
action to hidden.
-}
hide ::
P.Member (P.Tagged "hidden" (P.Reader Bool)) r =>
(P.Member (P.Tagged "hidden" (P.Reader Bool)) r) =>
P.Sem r x ->
P.Sem r x
hide = P.tag @"hidden" . P.local @Bool (const True) . P.raise
Expand All @@ -253,7 +253,7 @@ hide = P.tag @"hidden" . P.local @Bool (const True) . P.raise
@
-}
help ::
P.Member (P.Reader (c -> T.Text)) r =>
(P.Member (P.Reader (c -> T.Text)) r) =>
(c -> T.Text) ->
P.Sem r a ->
P.Sem r a
Expand All @@ -263,7 +263,7 @@ help = P.local . const
action.
-}
requires ::
DSLC m c a r =>
(DSLC m c a r) =>
[Check m c] ->
P.Sem r x ->
P.Sem r x
Expand Down Expand Up @@ -355,7 +355,7 @@ groupA name aliases m = mdo
ltell $ LH.fromList [(name, (group', Alias)) | name <- aliases]
pure res

fetchOrigHelp :: P.Member (P.Tagged "original-help" (P.Reader (c -> T.Text))) r => P.Sem r (c -> T.Text)
fetchOrigHelp :: (P.Member (P.Tagged "original-help" (P.Reader (c -> T.Text))) r) => P.Sem r (c -> T.Text)
fetchOrigHelp = P.tag P.ask

{- | Construct a group and place any commands registered in the given action
Expand Down Expand Up @@ -408,5 +408,5 @@ groupA' name aliases m = mdo
pure res

-- | Retrieve the final command handler for this block
fetchHandler :: DSLC m c a r => P.Sem r (CommandHandler m c a)
fetchHandler :: (DSLC m c a r) => P.Sem r (CommandHandler m c a)
fetchHandler = P.ask
12 changes: 6 additions & 6 deletions calamity-commands/CalamityCommands/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import TextShow.TH (deriveTextShow)

data CommandError
= ParseError
-- | The type of the parser
T.Text
-- ^ The type of the parser
-- | The reason that parsing failed
T.Text
-- ^ The reason that parsing failed
| CheckError
-- | The name of the check that failed
T.Text
-- ^ The name of the check that failed
-- | The reason for the check failing
T.Text
-- ^ The reason for the check failing
| InvokeError
-- | The name of the command that failed
T.Text
-- ^ The name of the command that failed
-- | The reason for failing
T.Text
-- ^ The reason for failing
deriving (Show)

$(deriveTextShow ''CommandError)
6 changes: 3 additions & 3 deletions calamity-commands/CalamityCommands/Help.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ parameterTypeHelp pinfo =
helpCommandHelp :: c -> T.Text
helpCommandHelp _ = "Show help for a command or group."

helpForCommand :: CommandContext m c a => c -> Command m c a -> T.Text
helpForCommand :: (CommandContext m c a) => c -> Command m c a -> T.Text
helpForCommand ctx cmd@Command {names, checks, help, params} =
"Usage: "
<> prefix'
Expand Down Expand Up @@ -91,7 +91,7 @@ onlyVisibleC = mapMaybe notHiddenC
onlyVisibleG :: [Group m c a] -> [Group m c a]
onlyVisibleG = mapMaybe notHiddenG

helpForGroup :: CommandContext m c a => c -> Group m c a -> T.Text
helpForGroup :: (CommandContext m c a) => c -> Group m c a -> T.Text
helpForGroup ctx grp =
"Group: "
<> path'
Expand Down Expand Up @@ -141,7 +141,7 @@ rootHelp handler = groupsMsg <> commandsMsg
then ""
else "\nThe following commands exist:\n" <> (T.unlines . map (("- " <>) . fmtCommandWithParams) $ commands)

renderHelp :: CommandContext m c a => CommandHandler m c a -> c -> [T.Text] -> T.Text
renderHelp :: (CommandContext m c a) => CommandHandler m c a -> c -> [T.Text] -> T.Text
renderHelp handler ctx path =
case findCommandOrGroup handler path of
Just (Command' cmd@Command {names}) ->
Expand Down
2 changes: 1 addition & 1 deletion calamity-commands/CalamityCommands/Internal/LocalWriter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data LocalWriter o m a where

P.makeSem ''LocalWriter

runLocalWriter :: Monoid o => P.Sem (LocalWriter o ': r) a -> P.Sem r (o, a)
runLocalWriter :: (Monoid o) => P.Sem (LocalWriter o ': r) a -> P.Sem r (o, a)
runLocalWriter =
P.runState mempty
. P.reinterpretH
Expand Down
8 changes: 4 additions & 4 deletions calamity-commands/CalamityCommands/Internal/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ import Data.Semigroup (Last (..))
-- _ ->
-- go action'

whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
whenJust :: (Applicative m) => Maybe a -> (a -> m ()) -> m ()
whenJust = flip $ maybe (pure ())

whenM :: Monad m => m Bool -> m () -> m ()
whenM :: (Monad m) => m Bool -> m () -> m ()
whenM p m =
p >>= \case
True -> m
False -> pure ()

unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: (Monad m) => m Bool -> m () -> m ()
unlessM = whenM . (not <$>)

lastMaybe :: Maybe a -> Maybe a -> Maybe a
Expand Down Expand Up @@ -98,7 +98,7 @@ infixl 4 <<$>>

infixl 4 <<*>>

(<.>) :: Functor f => (a -> b) -> (c -> f a) -> (c -> f b)
(<.>) :: (Functor f) => (a -> b) -> (c -> f a) -> (c -> f b)
(<.>) f g x = f <$> g x

infixl 4 <.>
20 changes: 10 additions & 10 deletions calamity-commands/CalamityCommands/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ runCommandParser ctx t = P.runReader ctx . P.runError . P.evalState (ParserState
parameter @ps@ of 'CalamityCommands.Dsl.command',
'CalamityCommands.CommandUtils.buildCommand', etc.
-}
class Typeable a => ParameterParser (a :: Type) c r where
class (Typeable a) => ParameterParser (a :: Type) c r where
type ParserResult a

type ParserResult a = a
Expand Down Expand Up @@ -104,12 +104,12 @@ instance (KnownSymbol s, ParameterParser a c r) => ParameterParser (Named s a) c

parse = mapE (_1 .~ parserName @(Named s a) @c @r) $ parse @a @c @r

parserName :: forall a c r. ParameterParser a c r => T.Text
parserName :: forall a c r. (ParameterParser a c r) => T.Text
parserName =
let ParameterInfo (fromMaybe "" -> name) type_ _ = parameterInfo @a @c @r
in name <> ":" <> T.pack (show type_)

mapE :: P.Member (P.Error e) r => (e -> e) -> P.Sem r a -> P.Sem r a
mapE :: (P.Member (P.Error e) r) => (e -> e) -> P.Sem r a -> P.Sem r a
mapE f m = P.catch m (P.throw . f)

{- | Parse a paremeter using a MegaParsec parser.
Expand Down Expand Up @@ -159,7 +159,7 @@ instance ParameterParser Float c r where
parse = parseMP (parserName @Float) (signed mempty (try float <|> decimal))
parameterDescription = "number"

instance ParameterParser a c r => ParameterParser (Maybe a) c r where
instance (ParameterParser a c r) => ParameterParser (Maybe a) c r where
type ParserResult (Maybe a) = Maybe (ParserResult a)

parse = P.catch (Just <$> parse @a) (const $ pure Nothing)
Expand All @@ -176,7 +176,7 @@ instance (ParameterParser a c r, ParameterParser b c r) => ParameterParser (Eith
Right <$> parse @b @c @r
parameterDescription = "either '" <> parameterDescription @a @c @r <> "' or '" <> parameterDescription @b @c @r <> "'"

instance ParameterParser a c r => ParameterParser [a] c r where
instance (ParameterParser a c r) => ParameterParser [a] c r where
type ParserResult [a] = [ParserResult a]

parse = go []
Expand Down Expand Up @@ -271,23 +271,23 @@ instance ShowErrorComponent SpannedError where
skipN :: (Stream s, Ord e) => Int -> ParsecT e s m ()
skipN n = void $ takeP Nothing n

trackOffsets :: MonadParsec e s m => m a -> m (a, Int)
trackOffsets :: (MonadParsec e s m) => m a -> m (a, Int)
trackOffsets m = do
offs <- getOffset
a <- m
offe <- getOffset
pure (a, offe - offs)

item :: MonadParsec e T.Text m => m T.Text
item :: (MonadParsec e T.Text m) => m T.Text
item = try quotedString <|> someNonWS

manySingle :: MonadParsec e s m => m (Tokens s)
manySingle :: (MonadParsec e s m) => m (Tokens s)
manySingle = takeWhileP (Just "Any character") (const True)

someSingle :: MonadParsec e s m => m (Tokens s)
someSingle :: (MonadParsec e s m) => m (Tokens s)
someSingle = takeWhile1P (Just "any character") (const True)

quotedString :: MonadParsec e T.Text m => m T.Text
quotedString :: (MonadParsec e T.Text m) => m T.Text
quotedString =
try (between (chunk "'") (chunk "'") (takeWhileP (Just "any character") (/= '\'')))
<|> between (chunk "\"") (chunk "\"") (takeWhileP (Just "any character") (/= '"'))
Expand Down
7 changes: 2 additions & 5 deletions calamity-commands/calamity-commands.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ library

hs-source-dirs: ./
default-extensions:
NoMonomorphismRestriction
AllowAmbiguousTypes
BangPatterns
BinaryLiterals
Expand Down Expand Up @@ -82,6 +81,7 @@ library
MultiParamTypeClasses
MultiWayIf
NamedFieldPuns
NoMonomorphismRestriction
OverloadedLabels
OverloadedStrings
PartialTypeSignatures
Expand All @@ -101,10 +101,7 @@ library
UndecidableInstances
ViewPatterns

ghc-options:
-funbox-strict-fields -Wall
-fno-warn-name-shadowing

ghc-options: -funbox-strict-fields -Wall -fno-warn-name-shadowing
build-depends:
base >=4.13 && <5
, megaparsec >=8 && <10
Expand Down
10 changes: 5 additions & 5 deletions calamity/Calamity/Cache/Eff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ data CacheEff m a where

makeSem ''CacheEff

updateBotUser :: P.Member CacheEff r => (User -> User) -> Sem r ()
updateBotUser :: (P.Member CacheEff r) => (User -> User) -> Sem r ()
updateBotUser f = getBotUser >>= flip whenJust (setBotUser . f)

updateGuild :: P.Member CacheEff r => Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild :: (P.Member CacheEff r) => Snowflake Guild -> (Guild -> Guild) -> Sem r ()
updateGuild id f = getGuild id >>= flip whenJust (setGuild . f)

updateDM :: P.Member CacheEff r => Snowflake DMChannel -> (DMChannel -> DMChannel) -> Sem r ()
updateDM :: (P.Member CacheEff r) => Snowflake DMChannel -> (DMChannel -> DMChannel) -> Sem r ()
updateDM id f = getDM id >>= flip whenJust (setDM . f)

updateUser :: P.Member CacheEff r => Snowflake User -> (User -> User) -> Sem r ()
updateUser :: (P.Member CacheEff r) => Snowflake User -> (User -> User) -> Sem r ()
updateUser id f = getUser id >>= flip whenJust (setUser . f)

updateMessage :: P.Member CacheEff r => Snowflake Message -> (Message -> Message) -> Sem r ()
updateMessage :: (P.Member CacheEff r) => Snowflake Message -> (Message -> Message) -> Sem r ()
updateMessage id f = getMessage id >>= flip whenJust (setMessage . f)
10 changes: 5 additions & 5 deletions calamity/Calamity/Cache/InMemory.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
{-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-}

-- | A 'Cache' handler that operates in memory
module Calamity.Cache.InMemory (
Expand Down Expand Up @@ -54,19 +54,19 @@ emptyCache' :: Int -> CacheWithMsg
emptyCache' msgLimit = Cache Nothing SM.empty SM.empty SH.empty SM.empty HS.empty (Identity $ BS.empty msgLimit)

-- | Run the cache in memory with a default message cache size of 1000
runCacheInMemory :: P.Member (P.Embed IO) r => P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemory :: (P.Member (P.Embed IO) r) => P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemory m = do
var <- P.embed $ newIORef emptyCache
P.runAtomicStateIORef var $ P.reinterpret runCache' m

-- | Run the cache in memory with no messages being cached
runCacheInMemoryNoMsg :: P.Member (P.Embed IO) r => P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemoryNoMsg :: (P.Member (P.Embed IO) r) => P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemoryNoMsg m = do
var <- P.embed $ newIORef emptyCacheNoMsg
P.runAtomicStateIORef var $ P.reinterpret runCache' m

-- | Run the cache in memory with a configurable message cache limit
runCacheInMemory' :: P.Member (P.Embed IO) r => Int -> P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemory' :: (P.Member (P.Embed IO) r) => Int -> P.Sem (CacheEff ': r) a -> P.Sem r a
runCacheInMemory' msgLimit m = do
var <- P.embed $ newIORef (emptyCache' msgLimit)
P.runAtomicStateIORef var $ P.reinterpret runCache' m
Expand All @@ -92,7 +92,7 @@ instance MessageMod CacheNoMsg where
getMessages' = pure []
delMessage' !_ = pure ()

runCache :: MessageMod (Cache t) => CacheEff m a -> State (Cache t) a
runCache :: (MessageMod (Cache t)) => CacheEff m a -> State (Cache t) a
runCache (SetBotUser u) = #user ?= u
runCache GetBotUser = use #user
runCache (SetGuild g) = do
Expand Down
Loading

0 comments on commit 82ba567

Please sign in to comment.