diff --git a/hindent.cabal b/hindent.cabal index e3120ed4b..a7062f7c6 100644 --- a/hindent.cabal +++ b/hindent.cabal @@ -46,6 +46,7 @@ library HIndent.Pretty.Imports.Sort HIndent.Pretty.ModuleDeclaration HIndent.Pretty.Pragma + HIndent.Read HIndent.RelocateComments HIndent.SrcSpan HIndent.Types diff --git a/src/HIndent/CabalFile.hs b/src/HIndent/CabalFile.hs index 7ab8481b5..3c34abea3 100644 --- a/src/HIndent/CabalFile.hs +++ b/src/HIndent/CabalFile.hs @@ -25,10 +25,10 @@ import GHC.Driver.Session (impliedXFlags, import qualified GHC.Driver.Session as GLP import qualified GHC.LanguageExtensions.Type as GLP import qualified HIndent.ExtensionConversion as EC +import HIndent.Read import Language.Haskell.Extension import System.Directory import System.FilePath -import Text.Read data Stanza = MkStanza @@ -166,9 +166,6 @@ getCabalExtensionsForSourcePath srcpath = do implicitExtensions :: GLP.Language -> [Extension] implicitExtensions = fmap (EnableExtension . readOrFail . show) . languageExtensions . Just - where - readOrFail x = - fromMaybe (error $ "Unsupported extension: " ++ show x) $ readMaybe x extensionImplies :: Extension -> [Extension] extensionImplies (EnableExtension e) = diff --git a/src/HIndent/ExtensionConversion.hs b/src/HIndent/ExtensionConversion.hs index 1ca30697f..ba9feacfe 100644 --- a/src/HIndent/ExtensionConversion.hs +++ b/src/HIndent/ExtensionConversion.hs @@ -6,16 +6,12 @@ module HIndent.ExtensionConversion , convertExtension ) where -import Data.Maybe import qualified GHC.LanguageExtensions as GLP +import HIndent.Read import qualified Language.Haskell.Extension as Cabal -import Text.Read gleExtensionToCabalExtension :: GLP.Extension -> Cabal.Extension gleExtensionToCabalExtension = readOrFail . show - where - readOrFail x = - fromMaybe (error $ "Unsupported extension: " ++ show x) $ readMaybe x uniqueExtensions :: [Cabal.Extension] -> [GLP.Extension] uniqueExtensions [] = [] @@ -23,9 +19,6 @@ uniqueExtensions ((Cabal.EnableExtension e):xs) = convertExtension e : uniqueExtensions xs uniqueExtensions ((Cabal.DisableExtension e):xs) = uniqueExtensions $ filter (/= readOrFail (show e)) xs - where - readOrFail x = - fromMaybe (error $ "Unsupported extension: " ++ show x) $ readMaybe x uniqueExtensions ((Cabal.UnknownExtension s):_) = error $ "Unknown extension: " ++ s diff --git a/src/HIndent/Read.hs b/src/HIndent/Read.hs new file mode 100644 index 000000000..30ae4430b --- /dev/null +++ b/src/HIndent/Read.hs @@ -0,0 +1,14 @@ +module HIndent.Read + ( readOrFail + ) where + +import Data.Maybe +import GHC.Stack +import Text.Read + +-- | `read` but dumps a stack trace if reading a value fails. +readOrFail :: + HasCallStack + => Read a => + String -> a +readOrFail x = fromMaybe (error $ "`read` failed: " ++ x) $ readMaybe x