Commit 73469b7c authored by Andrew Gibiansky's avatar Andrew Gibiansky

Add support for type inspection with shift tab (inspect_reply)

parent c8ee3d8e
...@@ -106,6 +106,7 @@ library ...@@ -106,6 +106,7 @@ library
IHaskell.Convert.IpynbToLhs IHaskell.Convert.IpynbToLhs
IHaskell.Convert.LhsToIpynb IHaskell.Convert.LhsToIpynb
IHaskell.Eval.Completion IHaskell.Eval.Completion
IHaskell.Eval.Inspect
IHaskell.Eval.Evaluate IHaskell.Eval.Evaluate
IHaskell.Eval.Info IHaskell.Eval.Info
IHaskell.Eval.Lint IHaskell.Eval.Lint
......
...@@ -72,6 +72,7 @@ instance ToJSON Message where ...@@ -72,6 +72,7 @@ instance ToJSON Message where
else "error" else "error"
, "data" .= object (map displayDataToJson . inspectData $ i) , "data" .= object (map displayDataToJson . inspectData $ i)
, "metadata" .= object [] , "metadata" .= object []
, "found" .= inspectStatus i
] ]
toJSON ShutdownReply { restartPending = restart } = toJSON ShutdownReply { restartPending = restart } =
...@@ -128,4 +129,4 @@ ints :: [Int] -> [Int] ...@@ -128,4 +129,4 @@ ints :: [Int] -> [Int]
ints = id ints = id
string :: String -> String string :: String -> String
string = id string = id
\ No newline at end of file
...@@ -194,7 +194,11 @@ receiveMessage debug socket = do ...@@ -194,7 +194,11 @@ receiveMessage debug socket = do
sendMessage :: Sender a => Bool -> ByteString -> Socket a -> Message -> IO () sendMessage :: Sender a => Bool -> ByteString -> Socket a -> Message -> IO ()
sendMessage _ _ _ SendNothing = return () sendMessage _ _ _ SendNothing = return ()
sendMessage debug hmacKey socket message = do sendMessage debug hmacKey socket message = do
when debug $ print message when debug $ do
putStr "Message: "
print message
putStr "Sent: "
print content
-- Send all pieces of the message. -- Send all pieces of the message.
mapM_ sendPiece idents mapM_ sendPiece idents
......
...@@ -12,6 +12,7 @@ module IHaskell.Eval.Evaluate ( ...@@ -12,6 +12,7 @@ module IHaskell.Eval.Evaluate (
liftIO, liftIO,
typeCleaner, typeCleaner,
globalImports, globalImports,
formatType,
) where ) where
import ClassyPrelude hiding (init, last, liftIO, head, hGetContents, tail, try) import ClassyPrelude hiding (init, last, liftIO, head, hGetContents, tail, try)
......
{-# LANGUAGE CPP, NoImplicitPrelude, OverloadedStrings, DoAndIfThenElse, FlexibleContexts #-}
{- |
Description: Generates inspections when asked for by the frontend.
-}
module IHaskell.Eval.Inspect (inspect) where
import ClassyPrelude
import qualified Prelude as P
import Data.List.Split (splitOn)
import Exception (ghandle)
import IHaskell.Eval.Evaluate (Interpreter)
import IHaskell.Display
import IHaskell.Eval.Util (getType)
import IHaskell.Types
-- | Characters used in Haskell operators.
operatorChars :: String
operatorChars = "!#$%&*+./<=>?@\\^|-~:"
-- | Whitespace characters.
whitespace :: String
whitespace = " \t\n"
-- | Compute the identifier that is being queried.
getIdentifier :: String -> Int -> String
getIdentifier code pos = identifier
where
chunks = splitOn whitespace code
lastChunk = P.last chunks :: String
identifier =
if all (`elem` operatorChars) lastChunk
then "(" ++ lastChunk ++ ")"
else lastChunk
inspect :: String -- ^ Code in the cell
-> Int -- ^ Cursor position in the cell
-> Interpreter (Maybe Display)
inspect code pos = do
let identifier = getIdentifier code pos
handler :: SomeException -> Interpreter (Maybe a)
handler _ = return Nothing
response <- ghandle handler (Just <$> getType identifier)
let prefix = identifier ++ " :: "
fmt str = Display [plain $ prefix ++ str]
return $ fmt <$> response
...@@ -24,6 +24,7 @@ import qualified Data.Text as T ...@@ -24,6 +24,7 @@ import qualified Data.Text as T
-- IHaskell imports. -- IHaskell imports.
import IHaskell.Convert (convert) import IHaskell.Convert (convert)
import IHaskell.Eval.Completion (complete) import IHaskell.Eval.Completion (complete)
import IHaskell.Eval.Inspect (inspect)
import IHaskell.Eval.Evaluate import IHaskell.Eval.Evaluate
import IHaskell.Display import IHaskell.Display
import IHaskell.Eval.Info import IHaskell.Eval.Info
...@@ -335,10 +336,16 @@ replyTo _ req@CompleteRequest{} replyHeader state = do ...@@ -335,10 +336,16 @@ replyTo _ req@CompleteRequest{} replyHeader state = do
reply = CompleteReply replyHeader (map pack completions) start end Map.empty True reply = CompleteReply replyHeader (map pack completions) start end Map.empty True
return (state, reply) return (state, reply)
-- TODO: Implement inspect_reply replyTo _ req@InspectRequest{} replyHeader state = do
replyTo _ InspectRequest{} replyHeader state = do result <- inspect (unpack $ inspectCode req) (inspectCursorPos req)
-- FIXME let reply =
let reply = InspectReply { header = replyHeader, inspectStatus = False, inspectData = [] } case result of
Just (Display datas) -> InspectReply
{ header = replyHeader
, inspectStatus = True
, inspectData = datas
}
_ -> InspectReply { header = replyHeader, inspectStatus = False, inspectData = [] }
return (state, reply) return (state, reply)
-- TODO: Implement history_reply. -- TODO: Implement history_reply.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment