diff --git a/.hlint.yaml b/.hlint.yaml index b105cab6..1ec15f18 100644 --- a/.hlint.yaml +++ b/.hlint.yaml @@ -60,3 +60,6 @@ name: Avoid return lhs: return rhs: pure + +# hlint fails to account for ViewPatterns usage inside PatternSynonyms +- ignore: {name: Unused LANGUAGE pragma, within: Prettyprinter.Internal} diff --git a/aux/version-compatibility-macros.h b/aux/version-compatibility-macros.h index f4d593d2..571a0f02 100644 --- a/aux/version-compatibility-macros.h +++ b/aux/version-compatibility-macros.h @@ -20,4 +20,8 @@ #define FAIL_IN_MONAD !(MIN_VERSION_base(4,13,0)) +#define BIDI_PATTERN_SYNONYMS MIN_VERSION_base(4,8,0) +#define PATTERN_SYNONYM_SIGNATURES MIN_VERSION_base(4,9,0) +#define COMPLETE_PRAGMA MIN_VERSION_base(4,10,0) + #endif diff --git a/prettyprinter/src/Prettyprinter/Internal.hs b/prettyprinter/src/Prettyprinter/Internal.hs index 010c537e..ad626a5f 100755 --- a/prettyprinter/src/Prettyprinter/Internal.hs +++ b/prettyprinter/src/Prettyprinter/Internal.hs @@ -10,6 +10,14 @@ #include "version-compatibility-macros.h" +#if BIDI_PATTERN_SYNONYMS +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ViewPatterns #-} +#if PATTERN_SYNONYM_SIGNATURES +{-# OPTIONS_GHC -fno-warn-missing-pattern-synonym-signatures #-} +#endif +#endif + -- | __Warning: internal module!__ This means that the API may change -- arbitrarily between versions without notice. Depending on this module may -- lead to unexpected breakages, so proceed with caution! @@ -68,6 +76,9 @@ module Prettyprinter.Internal ( -- * Layout SimpleDocStream(..), +#if BIDI_PATTERN_SYNONYMS + pattern SString, +#endif PageWidth(..), defaultPageWidth, LayoutOptions(..), defaultLayoutOptions, layoutPretty, layoutCompact, layoutSmart, @@ -1596,6 +1607,23 @@ data SimpleDocStream ann = | SAnnPop (SimpleDocStream ann) deriving (Eq, Ord, Show, Generic, Typeable) +#if BIDI_PATTERN_SYNONYMS + +#ifdef MIN_VERSION_text +-- | 'length' is /O(n)/, so we cache it in the 'Int' field. +pattern SString a b c <- SText a (T.unpack -> b) c where + SString a b c = SText a (T.pack b) c +#else +-- | 'length' is /O(n)/, so we cache it in the 'Int' field. +pattern SString a b c = SText a b c +#endif + +#if COMPLETE_PRAGMA +{-# COMPLETE SFail, SEmpty, SChar, SString, SLine, SAnnPush, SAnnPop #-} +#endif + +#endif + -- | Remove all trailing space characters. -- -- This has some performance impact, because it does an entire additional pass