Skip to content

Commit

Permalink
Enforce utf8 in eval plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
berberman committed Mar 23, 2021
1 parent 2a1cefa commit 3a8d949
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs
Expand Up @@ -21,10 +21,11 @@ import Ide.Plugin.Eval.Types (Language (Plain), Loc,
Section (sectionLanguage),
Test (..), Txt, locate,
locate0)
import InteractiveEval (getContext, parseImportDecl,
runDecls, setContext)
import InteractiveEval (execOptions, getContext,
parseImportDecl, runDecls,
setContext)
import Language.LSP.Types.Lens (line, start)
import System.IO.Extra (newTempFile, readFile')
import System.IO.Extra (newTempFile, readFileUTF8')

-- | Return the ranges of the expression and result parts of the given test
testRanges :: Test -> (Range, Range)
Expand Down Expand Up @@ -95,8 +96,13 @@ evalExtensions =
evalSetup :: Ghc ()
evalSetup = do
preludeAsP <- parseImportDecl "import qualified Prelude as P"
encodingAsP <- parseImportDecl "import qualified GHC.IO.Encoding as P"
context <- getContext
setContext (IIDecl preludeAsP : context)
setContext (IIDecl encodingAsP : IIDecl preludeAsP : context)
execStmt "P.setLocaleEncoding P.utf8" execOptions >>= \case
ExecComplete (Left err) _ -> error $ "failed to set encoding in eval setup: " <> show err
ExecComplete (Right _) _ -> pure ()
ExecBreak{} -> error "breakpoints are not supported"

-- | A wrapper of 'InteractiveEval.execStmt', capturing the execution result
myExecStmt :: String -> ExecOptions -> Ghc (Either String (Maybe String))
Expand All @@ -106,7 +112,7 @@ myExecStmt stmt opts = do
modifySession $ \hsc -> hsc {hsc_IC = setInteractivePrintName (hsc_IC hsc) evalPrint}
result <- execStmt stmt opts >>= \case
ExecComplete (Left err) _ -> pure $ Left $ show err
ExecComplete (Right _) _ -> liftIO $ Right . (\x -> if null x then Nothing else Just x) <$> readFile' temp
ExecComplete (Right _) _ -> liftIO $ Right . (\x -> if null x then Nothing else Just x) <$> readFileUTF8' temp
ExecBreak{} -> pure $ Right $ Just "breakpoints are not supported"
liftIO purge
pure result
Expand Down

0 comments on commit 3a8d949

Please sign in to comment.