Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure eval plugin Print class doesn't rely on Prelude being in scope #1587

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import qualified Data.List.NonEmpty as NE
import Data.String (IsString)
import qualified Data.Text as T
import Development.IDE.Types.Location (Position (..), Range (..))
import GHC (compileExpr)
import GHC (InteractiveImport (IIDecl), compileExpr)
import GHC.LanguageExtensions.Type (Extension (..))
import GhcMonad (Ghc, GhcMonad, liftIO)
import Ide.Plugin.Eval.Types (Language (Plain), Loc,
Located (..),
Section (sectionLanguage),
Test (..), Txt, locate,
locate0)
import InteractiveEval (runDecls)
import InteractiveEval (getContext, parseImportDecl, runDecls, setContext)
import Language.LSP.Types.Lens (line, start)
import Unsafe.Coerce (unsafeCoerce)

Expand Down Expand Up @@ -95,12 +95,15 @@ evalExtensions =

-- |GHC declarations required for expression evaluation
evalSetup :: Ghc ()
evalSetup =
evalSetup = do
preludeAsP <- parseImportDecl "import qualified Prelude as P"
context <- getContext
setContext (IIDecl preludeAsP : context)
mapM_
runDecls
[ "class Print f where asPrint :: f -> IO String"
, "instance Show a => Print (IO a) where asPrint io = io >>= return . show"
, "instance Show a => Print a where asPrint a = return (show a)"
[ "class Print f where asPrint :: f -> P.IO P.String"
, "instance P.Show a => Print (P.IO a) where asPrint io = io P.>>= P.return P.. P.show"
, "instance P.Show a => Print a where asPrint a = P.return (P.show a)"
]

{- |GHC declarations required to execute test properties
Expand Down
2 changes: 2 additions & 0 deletions plugins/hls-eval-plugin/test/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ tests =
$ testCase "Literate Haskell Bird Style" $ goldenTest "TLHS.lhs"
-- , testCase "Literate Haskell LaTeX Style" $ goldenTest "TLHSLateX.lhs"
]
, testCase "Works with NoImplicitPrelude"
$ goldenTest "TNoImplicitPrelude.hs"
]

goldenTest :: FilePath -> IO ()
Expand Down
10 changes: 10 additions & 0 deletions plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{-# LANGUAGE NoImplicitPrelude #-}

module TNoImplicitPrelude where

import Data.List (unwords)
import Data.String (String)

-- >>> unwords example
example :: [String]
example = ["This","is","an","example","of","evaluation"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE NoImplicitPrelude #-}

module TNoImplicitPrelude where

import Data.List (unwords)
import Data.String (String)

-- >>> unwords example
-- "This is an example of evaluation"
example :: [String]
example = ["This","is","an","example","of","evaluation"]