Unverified Commit ef698157 authored by Vaibhav Sagar's avatar Vaibhav Sagar Committed by GitHub

Merge pull request #1191 from MMesch/mmesch/codemirror

add codemirror command line flag
parents c3ae2d21 64f5a217
...@@ -88,6 +88,8 @@ parseKernelArgs = foldl' addFlag defaultKernelSpecOptions ...@@ -88,6 +88,8 @@ parseKernelArgs = foldl' addFlag defaultKernelSpecOptions
kernelSpecOpts { kernelSpecConfFile = return (Just filename) } kernelSpecOpts { kernelSpecConfFile = return (Just filename) }
addFlag kernelSpecOpts KernelDebug = addFlag kernelSpecOpts KernelDebug =
kernelSpecOpts { kernelSpecDebug = True } kernelSpecOpts { kernelSpecDebug = True }
addFlag kernelSpecOpts (CodeMirror codemirror) =
kernelSpecOpts { kernelSpecCodeMirror = codemirror }
addFlag kernelSpecOpts (GhcLibDir libdir) = addFlag kernelSpecOpts (GhcLibDir libdir) =
kernelSpecOpts { kernelSpecGhcLibdir = libdir } kernelSpecOpts { kernelSpecGhcLibdir = libdir }
addFlag kernelSpecOpts (RTSFlags rts) = addFlag kernelSpecOpts (RTSFlags rts) =
...@@ -191,7 +193,7 @@ runKernel kOpts profileSrc = do ...@@ -191,7 +193,7 @@ runKernel kOpts profileSrc = do
else do else do
-- Create the reply, possibly modifying kernel state. -- Create the reply, possibly modifying kernel state.
oldState <- liftIO $ takeMVar state oldState <- liftIO $ takeMVar state
(newState, reply) <- replyTo interface request replyHeader oldState (newState, reply) <- replyTo kOpts interface request replyHeader oldState
liftIO $ putMVar state newState liftIO $ putMVar state newState
-- Write the reply to the reply channel. -- Write the reply to the reply channel.
...@@ -224,11 +226,11 @@ createReplyHeader parent = do ...@@ -224,11 +226,11 @@ createReplyHeader parent = do
newMessageId (mhSessionId parent) (mhUsername parent) repType [] newMessageId (mhSessionId parent) (mhUsername parent) repType []
-- | Compute a reply to a message. -- | Compute a reply to a message.
replyTo :: ZeroMQInterface -> Message -> MessageHeader -> KernelState -> Interpreter (KernelState, Message) replyTo :: KernelSpecOptions -> ZeroMQInterface -> Message -> MessageHeader -> KernelState -> Interpreter (KernelState, Message)
-- Reply to kernel info requests with a kernel info reply. No computation needs to be done, as a -- Reply to kernel info requests with a kernel info reply. No computation needs to be done, as a
-- kernel info reply is a static object (all info is hard coded into the representation of that -- kernel info reply is a static object (all info is hard coded into the representation of that
-- message type). -- message type).
replyTo interface KernelInfoRequest{} replyHeader state = do replyTo kOpts interface KernelInfoRequest{} replyHeader state = do
let send msg = liftIO $ writeChan (iopubChannel interface) msg let send msg = liftIO $ writeChan (iopubChannel interface) msg
-- Notify the frontend that the Kernel is idle -- Notify the frontend that the Kernel is idle
...@@ -246,14 +248,14 @@ replyTo interface KernelInfoRequest{} replyHeader state = do ...@@ -246,14 +248,14 @@ replyTo interface KernelInfoRequest{} replyHeader state = do
{ languageName = "haskell" { languageName = "haskell"
, languageVersion = VERSION_ghc , languageVersion = VERSION_ghc
, languageFileExtension = ".hs" , languageFileExtension = ".hs"
, languageCodeMirrorMode = "ihaskell" , languageCodeMirrorMode = kernelSpecCodeMirror kOpts
, languagePygmentsLexer = "Haskell" , languagePygmentsLexer = "Haskell"
, languageMimeType = "text/x-haskell" -- https://jupyter-client.readthedocs.io/en/stable/wrapperkernels.html#MyKernel.language_info , languageMimeType = "text/x-haskell" -- https://jupyter-client.readthedocs.io/en/stable/wrapperkernels.html#MyKernel.language_info
} }
, status = Ok , status = Ok
}) })
replyTo _ CommInfoRequest{} replyHeader state = replyTo _ _ CommInfoRequest{} replyHeader state =
let comms = Map.mapKeys (UUID.uuidToString) (openComms state) in let comms = Map.mapKeys (UUID.uuidToString) (openComms state) in
return return
(state, CommInfoReply (state, CommInfoReply
...@@ -263,13 +265,13 @@ replyTo _ CommInfoRequest{} replyHeader state = ...@@ -263,13 +265,13 @@ replyTo _ CommInfoRequest{} replyHeader state =
-- Reply to a shutdown request by exiting the main thread. Before shutdown, reply to the request to -- Reply to a shutdown request by exiting the main thread. Before shutdown, reply to the request to
-- let the frontend know shutdown is happening. -- let the frontend know shutdown is happening.
replyTo interface ShutdownRequest { restartPending = pending } replyHeader _ = liftIO $ do replyTo _ interface ShutdownRequest { restartPending = pending } replyHeader _ = liftIO $ do
writeChan (shellReplyChannel interface) $ ShutdownReply replyHeader pending writeChan (shellReplyChannel interface) $ ShutdownReply replyHeader pending
exitSuccess exitSuccess
-- Reply to an execution request. The reply itself does not require computation, but this causes -- Reply to an execution request. The reply itself does not require computation, but this causes
-- messages to be sent to the IOPub socket with the output of the code in the execution request. -- messages to be sent to the IOPub socket with the output of the code in the execution request.
replyTo interface req@ExecuteRequest { getCode = code } replyHeader state = do replyTo _ interface req@ExecuteRequest { getCode = code } replyHeader state = do
-- Convenience function to send a message to the IOPub socket. -- Convenience function to send a message to the IOPub socket.
let send msg = liftIO $ writeChan (iopubChannel interface) msg let send msg = liftIO $ writeChan (iopubChannel interface) msg
...@@ -310,7 +312,7 @@ replyTo interface req@ExecuteRequest { getCode = code } replyHeader state = do ...@@ -310,7 +312,7 @@ replyTo interface req@ExecuteRequest { getCode = code } replyHeader state = do
-- Check for a trailing empty line. If it doesn't exist, we assume the code is incomplete, -- Check for a trailing empty line. If it doesn't exist, we assume the code is incomplete,
-- otherwise we assume the code is complete. Todo: Implement a mechanism that only requests -- otherwise we assume the code is complete. Todo: Implement a mechanism that only requests
-- a trailing empty line, when multiline code is entered. -- a trailing empty line, when multiline code is entered.
replyTo _ req@IsCompleteRequest{} replyHeader state = do replyTo _ _ req@IsCompleteRequest{} replyHeader state = do
isComplete <- isInputComplete isComplete <- isInputComplete
let reply = IsCompleteReply { header = replyHeader, reviewResult = isComplete } let reply = IsCompleteReply { header = replyHeader, reviewResult = isComplete }
return (state, reply) return (state, reply)
...@@ -323,7 +325,7 @@ replyTo _ req@IsCompleteRequest{} replyHeader state = do ...@@ -323,7 +325,7 @@ replyTo _ req@IsCompleteRequest{} replyHeader state = do
else return $ CodeIncomplete $ indent 4 else return $ CodeIncomplete $ indent 4
indent n = take n $ repeat ' ' indent n = take n $ repeat ' '
replyTo _ req@CompleteRequest{} replyHeader state = do replyTo _ _ req@CompleteRequest{} replyHeader state = do
let code = getCode req let code = getCode req
pos = getCursorPos req pos = getCursorPos req
(matchedText, completions) <- complete (T.unpack code) pos (matchedText, completions) <- complete (T.unpack code) pos
...@@ -333,7 +335,7 @@ replyTo _ req@CompleteRequest{} replyHeader state = do ...@@ -333,7 +335,7 @@ replyTo _ req@CompleteRequest{} replyHeader state = do
reply = CompleteReply replyHeader (map T.pack completions) start end (Metadata HashMap.empty) True reply = CompleteReply replyHeader (map T.pack completions) start end (Metadata HashMap.empty) True
return (state, reply) return (state, reply)
replyTo _ req@InspectRequest{} replyHeader state = do replyTo _ _ req@InspectRequest{} replyHeader state = do
result <- inspect (T.unpack $ inspectCode req) (inspectCursorPos req) result <- inspect (T.unpack $ inspectCode req) (inspectCursorPos req)
let reply = let reply =
case result of case result of
...@@ -346,7 +348,7 @@ replyTo _ req@InspectRequest{} replyHeader state = do ...@@ -346,7 +348,7 @@ replyTo _ req@InspectRequest{} replyHeader state = do
return (state, reply) return (state, reply)
-- TODO: Implement history_reply. -- TODO: Implement history_reply.
replyTo _ HistoryRequest{} replyHeader state = do replyTo _ _ HistoryRequest{} replyHeader state = do
let reply = HistoryReply let reply = HistoryReply
{ header = replyHeader { header = replyHeader
-- FIXME -- FIXME
...@@ -365,7 +367,7 @@ replyTo _ HistoryRequest{} replyHeader state = do ...@@ -365,7 +367,7 @@ replyTo _ HistoryRequest{} replyHeader state = do
-- --
-- Sending the message only on the shell_reply channel doesn't work, so we send it as a comm message -- Sending the message only on the shell_reply channel doesn't work, so we send it as a comm message
-- on the iopub channel and return the SendNothing message. -- on the iopub channel and return the SendNothing message.
replyTo interface ocomm@CommOpen{} replyHeader state = do replyTo _ interface ocomm@CommOpen{} replyHeader state = do
let send = liftIO . writeChan (iopubChannel interface) let send = liftIO . writeChan (iopubChannel interface)
incomingUuid = commUuid ocomm incomingUuid = commUuid ocomm
...@@ -393,7 +395,7 @@ replyTo interface ocomm@CommOpen{} replyHeader state = do ...@@ -393,7 +395,7 @@ replyTo interface ocomm@CommOpen{} replyHeader state = do
return (state, SendNothing) return (state, SendNothing)
-- TODO: What else can be implemented? -- TODO: What else can be implemented?
replyTo _ message _ state = do replyTo _ _ message _ state = do
liftIO $ hPutStrLn stderr $ "Unimplemented message: " ++ show message liftIO $ hPutStrLn stderr $ "Unimplemented message: " ++ show message
return (state, SendNothing) return (state, SendNothing)
......
...@@ -31,6 +31,7 @@ data Argument = ConfFile String -- ^ A file with commands to load at startup ...@@ -31,6 +31,7 @@ data Argument = ConfFile String -- ^ A file with commands to load at startup
| KernelDebug -- ^ Spew debugging output from the kernel. | KernelDebug -- ^ Spew debugging output from the kernel.
| Help -- ^ Display help text. | Help -- ^ Display help text.
| Version -- ^ Display version text. | Version -- ^ Display version text.
| CodeMirror String -- ^ change codemirror mode (default=ihaskell)
| ConvertFrom String | ConvertFrom String
| ConvertTo String | ConvertTo String
| ConvertFromFormat NotebookFormat | ConvertFromFormat NotebookFormat
...@@ -113,6 +114,10 @@ kernelDebugFlag = flagNone ["debug"] addDebug "Print debugging output from the k ...@@ -113,6 +114,10 @@ kernelDebugFlag = flagNone ["debug"] addDebug "Print debugging output from the k
where where
addDebug (Args md prev) = Args md (KernelDebug : prev) addDebug (Args md prev) = Args md (KernelDebug : prev)
kernelCodeMirrorFlag :: Flag Args
kernelCodeMirrorFlag = flagReq ["codemirror"] (store CodeMirror) "<codemirror>"
"Specify codemirror mode that is used for syntax highlighting (default: ihaskell)."
kernelStackFlag :: Flag Args kernelStackFlag :: Flag Args
kernelStackFlag = flagNone ["stack"] addStack kernelStackFlag = flagNone ["stack"] addStack
"Inherit environment from `stack` when it is installed" "Inherit environment from `stack` when it is installed"
...@@ -143,7 +148,7 @@ installKernelSpec = ...@@ -143,7 +148,7 @@ installKernelSpec =
kernel :: Mode Args kernel :: Mode Args
kernel = mode "kernel" (Args (Kernel Nothing) []) "Invoke the IHaskell kernel." kernelArg kernel = mode "kernel" (Args (Kernel Nothing) []) "Invoke the IHaskell kernel." kernelArg
[ghcLibFlag, kernelDebugFlag, confFlag, kernelStackFlag] [ghcLibFlag, kernelDebugFlag, confFlag, kernelStackFlag, kernelCodeMirrorFlag]
where where
kernelArg = flagArg update "<json-kernel-file>" kernelArg = flagArg update "<json-kernel-file>"
update filename (Args _ flags) = Right $ Args (Kernel $ Just filename) flags update filename (Args _ flags) = Right $ Args (Kernel $ Just filename) flags
......
...@@ -39,6 +39,7 @@ data KernelSpecOptions = ...@@ -39,6 +39,7 @@ data KernelSpecOptions =
{ kernelSpecGhcLibdir :: String -- ^ GHC libdir. { kernelSpecGhcLibdir :: String -- ^ GHC libdir.
, kernelSpecRTSOptions :: [String] -- ^ Runtime options to use. , kernelSpecRTSOptions :: [String] -- ^ Runtime options to use.
, kernelSpecDebug :: Bool -- ^ Spew debugging output? , kernelSpecDebug :: Bool -- ^ Spew debugging output?
, kernelSpecCodeMirror :: String -- ^ CodeMirror mode
, kernelSpecConfFile :: IO (Maybe String) -- ^ Filename of profile JSON file. , kernelSpecConfFile :: IO (Maybe String) -- ^ Filename of profile JSON file.
, kernelSpecInstallPrefix :: Maybe String , kernelSpecInstallPrefix :: Maybe String
, kernelSpecUseStack :: Bool -- ^ Whether to use @stack@ environments. , kernelSpecUseStack :: Bool -- ^ Whether to use @stack@ environments.
...@@ -50,6 +51,7 @@ defaultKernelSpecOptions = KernelSpecOptions ...@@ -50,6 +51,7 @@ defaultKernelSpecOptions = KernelSpecOptions
, kernelSpecRTSOptions = ["-M3g", "-N2"] -- Memory cap 3 GiB, , kernelSpecRTSOptions = ["-M3g", "-N2"] -- Memory cap 3 GiB,
-- multithreading on two processors. -- multithreading on two processors.
, kernelSpecDebug = False , kernelSpecDebug = False
, kernelSpecCodeMirror = "ihaskell"
, kernelSpecConfFile = defaultConfFile , kernelSpecConfFile = defaultConfFile
, kernelSpecInstallPrefix = Nothing , kernelSpecInstallPrefix = Nothing
, kernelSpecUseStack = False , kernelSpecUseStack = False
......
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