Skip to content
This repository has been archived by the owner on Apr 25, 2020. It is now read-only.

Fix stdin encoding 5.5 #749

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Language/Haskell/GhcMod/FileMapping.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ loadMappedFileSource :: IOish m
-> GhcModT m ()
loadMappedFileSource from src = do
tmpdir <- cradleTempDir `fmap` cradle
enc <- liftIO . mkTextEncoding . optEncoding =<< options
to <- liftIO $ do
(fn, h) <- openTempFile tmpdir (takeFileName from)
hSetEncoding h enc
hPutStr h src
hClose h
return fn
Expand Down
2 changes: 2 additions & 0 deletions Language/Haskell/GhcMod/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ data Options = Options {
-- | GHC command line options set on the @ghc-mod@ command line
, optGhcUserOptions :: [GHCOption]
, optFileMappings :: [(FilePath, Maybe FilePath)]
, optEncoding :: String
} deriving (Show)

-- | A default 'Options'.
Expand All @@ -124,6 +125,7 @@ defaultOptions = Options {
}
, optGhcUserOptions = []
, optFileMappings = []
, optEncoding = "UTF-8"
}

----------------------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions src/GHCMod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ handler = flip gcatches
]

main :: IO ()
main = do
hSetEncoding stdout utf8
parseArgs >>= \res@(globalOptions, _) ->
main =
parseArgs >>= \res@(globalOptions, _) -> do
enc <- mkTextEncoding $ optEncoding globalOptions
hSetEncoding stdout enc
hSetEncoding stderr enc
hSetEncoding stdin enc
catches (progMain res) [
Handler $ \(e :: GhcModError) ->
runGmOutT globalOptions $ exitError $ renderStyle ghcModStyle (gmeDoc e)
Expand Down Expand Up @@ -107,7 +110,6 @@ getFileSourceFromStdin = do
then fmap (x:) readStdin'
else return []

-- Someone please already rewrite the cmdline parsing code *weep* :'(
wrapGhcCommands :: (IOish m, GmOut m) => Options -> GhcModCommands -> m ()
wrapGhcCommands _opts CmdRoot = gmPutStr =<< rootInfo
wrapGhcCommands opts cmd =
Expand Down
5 changes: 5 additions & 0 deletions src/GHCMod/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ globalArgSpec = Options
<=> metavar "OPT"
<=> help "Option to be passed to GHC"
<*> many fileMappingSpec
<*> strOption
$$ long "encoding"
<=> value "UTF-8"
<=> showDefault
<=> help "I/O encoding"
where
fileMappingSpec =
getFileMapping . splitOn '=' <$> strOption
Expand Down