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

Adds :kind and :kind! commands to Eval Plugin #345

Merged
merged 7 commits into from
Aug 26, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Ide/Plugin/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import GHC (DynFlags, ExecResult (..), Gene
setInteractiveDynFlags,
setLogAction,
setSessionDynFlags, setTargets,
simpleImportDecl, ways)
simpleImportDecl, typeKind, ways)
import GHC.Generics (Generic)
import GhcMonad (modifySession)
import GhcPlugins (defaultLogActionHPutStrDoc,
Expand All @@ -86,6 +86,8 @@ import qualified Control.Exception as E
import Control.DeepSeq ( NFData
, deepseq
)
import Outputable (Outputable(ppr), showSDoc)
import Control.Applicative ((<|>))

descriptor :: PluginId -> PluginDescriptor
descriptor plId =
Expand Down Expand Up @@ -245,6 +247,18 @@ done, we want to switch back to GhcSessionDeps:

df <- liftIO $ evalGhcEnv hscEnv' getSessionDynFlags
let eval (stmt, l)
| let stmt0 = T.strip $ T.pack stmt -- For stripping and de-prefixing
, Just (reduce, type_) <-
(True,) <$> T.stripPrefix ":kind! " stmt0
<|> (False,) <$> T.stripPrefix ":kind " stmt0
= do
let input = T.strip type_
(ty, kind) <- typeKind reduce $ T.unpack input
pure $ Just
$ T.unlines
$ map ("-- " <>)
$ (input <> " :: " <> T.pack (showSDoc df $ ppr kind))
: [ "= " <> T.pack (showSDoc df $ ppr ty) | reduce]
| isStmt df stmt = do
-- set up a custom interactive print function
liftIO $ writeFile temp ""
Expand Down
6 changes: 6 additions & 0 deletions test/functional/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ tests = testGroup
, testCase "Refresh a multiline evaluation" $ goldenTest "T7.hs"
, testCase "Evaluate incorrect expressions" $ goldenTest "T8.hs"
, testCase "Applies file LANGUAGE extensions" $ goldenTest "T9.hs"
, testCase "Evaluate a type with :kind!" $ goldenTest "T10.hs"
, testCase "Reports an error for an incorrect type with :kind!"
$ goldenTest "T11.hs"
, testCase "Shows a kind with :kind" $ goldenTest "T12.hs"
, testCase "Reports an error for an incorrect type with :kind"
$ goldenTest "T13.hs"
]

goldenTest :: FilePath -> IO ()
Expand Down
9 changes: 9 additions & 0 deletions test/testdata/eval/T10.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE DataKinds, TypeOperators #-}
module T10 where
import GHC.TypeNats ( type (+) )

type Dummy = 1 + 1

-- >>> type N = 1
-- >>> type M = 40
-- >>> :kind! N + M + 1
11 changes: 11 additions & 0 deletions test/testdata/eval/T10.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE DataKinds, TypeOperators #-}
module T10 where
import GHC.TypeNats ( type (+) )

type Dummy = 1 + 1

-- >>> type N = 1
-- >>> type M = 40
-- >>> :kind! N + M + 1
-- N + M + 1 :: Nat
-- = 42
3 changes: 3 additions & 0 deletions test/testdata/eval/T11.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module T11 where

-- >>> :kind! a
4 changes: 4 additions & 0 deletions test/testdata/eval/T11.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module T11 where

-- >>> :kind! a
-- Not in scope: type variable ‘a’
9 changes: 9 additions & 0 deletions test/testdata/eval/T12.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE DataKinds, TypeOperators #-}
module T12 where
import GHC.TypeNats ( type (+) )

type Dummy = 1 + 1

-- >>> type N = 1
-- >>> type M = 40
-- >>> :kind N + M + 1
10 changes: 10 additions & 0 deletions test/testdata/eval/T12.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{-# LANGUAGE DataKinds, TypeOperators #-}
module T12 where
import GHC.TypeNats ( type (+) )

type Dummy = 1 + 1

-- >>> type N = 1
-- >>> type M = 40
-- >>> :kind N + M + 1
-- N + M + 1 :: Nat
3 changes: 3 additions & 0 deletions test/testdata/eval/T13.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module T13 where

-- >>> :kind a
4 changes: 4 additions & 0 deletions test/testdata/eval/T13.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module T13 where

-- >>> :kind a
-- Not in scope: type variable ‘a’
4 changes: 3 additions & 1 deletion test/testdata/eval/T9.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# LANGUAGE DataKinds #-}
module T9 where
import Data.Proxy
import Data.Proxy (Proxy(..))

type P = Proxy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you let me know why have you changed this test? It seems irrelevant to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that change is not directly related to my change; but without this hack, at least on my laptop, Applies file LANGUAGE extensions test case fails due to the interference with import lens.
This issue seems already existent in the master branch and I wanted to avoid such errors to make my pull-request get merged as soon as possible. Indeed, I once wondered if I should make this change in this pull-request but I decided to include it since it's a minor change and somewhat related to Eval plugin.

But, yes, this change is semantically separate from this pull-request. Should I file this workaround as another pull-request?

Copy link
Member

@Ailrun Ailrun Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worthwhile to post that as an issue, but for the separated PR, it's OK to stay this change as-is.


-- >>> Proxy :: Proxy 3
4 changes: 3 additions & 1 deletion test/testdata/eval/T9.hs.expected
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE DataKinds #-}
module T9 where
import Data.Proxy
import Data.Proxy (Proxy(..))

type P = Proxy

-- >>> Proxy :: Proxy 3
-- Proxy