Skip to content

Commit bde0efa

Browse files
committed
Enforce utf8 in eval plugin
1 parent 2a1cefa commit bde0efa

File tree

1 file changed

+11
-5
lines changed
  • plugins/hls-eval-plugin/src/Ide/Plugin/Eval

1 file changed

+11
-5
lines changed

Diff for: plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import Ide.Plugin.Eval.Types (Language (Plain), Loc,
2121
Section (sectionLanguage),
2222
Test (..), Txt, locate,
2323
locate0)
24-
import InteractiveEval (getContext, parseImportDecl,
25-
runDecls, setContext)
24+
import InteractiveEval (execOptions, getContext,
25+
parseImportDecl, runDecls,
26+
setContext)
2627
import Language.LSP.Types.Lens (line, start)
27-
import System.IO.Extra (newTempFile, readFile')
28+
import System.IO.Extra (newTempFile, readFileUTF8')
2829

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

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

0 commit comments

Comments
 (0)