Commit 45664753 authored by Andrew Gibiansky's avatar Andrew Gibiansky

Changing :set to be useful. :set [no]lint, :set no[svg].

parent 2de73f6e
......@@ -36,12 +36,7 @@ eval string = do
outputAccum <- newIORef []
let publish final displayDatas = when final $ modifyIORef outputAccum (displayDatas :)
getTemporaryDirectory >>= setCurrentDirectory
<<<<<<< HEAD:Hspec.hs
let state = KernelState 1 LintOff "."
=======
let state :: KernelState
state = mempty { getLintStatus = LintOff }
>>>>>>> 63ecc797eb66565e4bb6ed04d503b3884b37cb4e:src/Hspec.hs
let state = defaultKernelState { getLintStatus = LintOff }
interpret $ Eval.evaluate state string publish
out <- readIORef outputAccum
return $ reverse out
......@@ -279,7 +274,7 @@ parseStringTests = describe "Parser" $ do
it "parses :set x" $
parses ":set x" `like` [
Directive HelpForSet "x"
Directive SetOpt "x"
]
it "parses :extension x" $
......
......@@ -2,7 +2,8 @@
module IHaskell.Display (
IHaskellDisplay(..),
plain, html, png, jpg, svg, latex,
serializeDisplay
serializeDisplay,
Width, Height, Base64Data
) where
import ClassyPrelude
......@@ -12,6 +13,8 @@ import Data.String.Utils (rstrip)
import IHaskell.Types
type Base64Data = String
-- | A class for displayable Haskell types.
--
-- IHaskell's displaying of results behaves as if these two
......@@ -20,7 +23,7 @@ import IHaskell.Types
-- > instance (Show a) => IHaskellDisplay a
-- > instance Show a where shows _ = id
class IHaskellDisplay a where
display :: a -> [DisplayData]
display :: a -> IO [DisplayData]
-- | Generate a plain text display.
plain :: String -> DisplayData
......@@ -30,11 +33,11 @@ plain = Display PlainText . rstrip
html :: String -> DisplayData
html = Display MimeHtml
png :: String -> DisplayData
png = Display MimePng
png :: Width -> Height -> Base64Data -> DisplayData
png width height = Display (MimePng width height)
jpg :: String -> DisplayData
jpg = Display MimeJpg
jpg :: Width -> Height -> Base64Data -> DisplayData
jpg width height = Display (MimeJpg width height)
svg :: String -> DisplayData
svg = Display MimeSvg
......
......@@ -332,17 +332,6 @@ evalCommand _ (Directive SetExtension exts) state = wrapExecution state $ do
-- In that case, we disable the extension.
flagMatchesNo ext (name, _, _) = ext == "No" ++ name
evalCommand _ (Directive SetLint status) state = do
let isOn = "on" == strip status
let isOff = "off" == strip status
return $ if isOn
then EvalOut Success [] (state { getLintStatus = LintOn })
else if isOff
then EvalOut Success [] (state { getLintStatus = LintOff })
else EvalOut Failure err state
where
err = displayError $ "Unknown hlint command: " ++ status
evalCommand _ (Directive GetType expr) state = wrapExecution state $ do
write $ "Type: " ++ expr
result <- exprType expr
......@@ -366,18 +355,33 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do
-- This is taken largely from GHCi's info section in InteractiveUI.
evalCommand _ (Directive HelpForSet _) state = do
write "Help for :set."
evalCommand _ (Directive SetOpt option) state = do
let opt = strip option
newState = setOpt opt state
out = case newState of
Nothing -> displayError $ "Unknown option: " ++ opt
Just _ -> []
return EvalOut {
evalStatus = Success,
evalResult = [out],
evalState = state
evalStatus = if isJust newState then Success else Failure,
evalResult = out,
evalState = fromMaybe state newState
}
where out = plain $ intercalate "\n"
[":set is not implemented in IHaskell."
," Use :extension <Extension> to enable a GHC extension."
," Use :extension No<Extension> to disable a GHC extension."
]
where
setOpt :: String -> KernelState -> Maybe KernelState
setOpt "lint" state = Just $
state { getLintStatus = LintOn }
setOpt "nolint" state = Just $
state { getLintStatus = LintOff }
setOpt "svg" state = Just $
state { useSvg = True }
setOpt "nosvg" state = Just $
state { useSvg = False }
setOpt _ _ = Nothing
-- This is taken largely from GHCi's info section in InteractiveUI.
evalCommand _ (Directive GetHelp _) state = do
......@@ -393,9 +397,15 @@ evalCommand _ (Directive GetHelp _) state = do
," :extension No<Extension> - disable a GHC extension."
," :type <expression> - Print expression type."
," :info <name> - Print all info for a name."
," :set <opt> - Set an option."
," :set no<opt> - Unset an option."
," :?, :help - Show this help text."
,""
,"Any prefix of the commands will also suffice, e.g. use :ty for :type."
,""
,"Options:"
," lint - enable or disable linting."
," svg - use svg output (cannot be resized)."
]
-- This is taken largely from GHCi's info section in InteractiveUI.
......@@ -490,6 +500,9 @@ evalCommand output (Expression expr) state = do
Nothing -> False
where isPlain (Display mime _) = mime == PlainText
isSvg (Display MimeSvg _) = True
isSvg _ = False
useDisplay displayExpr = wrapExecution state $ do
-- If there are instance matches, convert the object into
-- a [DisplayData]. We also serialize it into a bytestring. We get
......@@ -509,7 +522,10 @@ evalCommand output (Expression expr) state = do
Left err -> error err
Right displayData -> do
write $ show displayData
return displayData
return $
if useSvg state
then displayData
else filter (not . isSvg) displayData
evalCommand _ (Declaration decl) state = wrapExecution state $ do
......
......@@ -61,8 +61,7 @@ data DirectiveType
| GetInfo -- ^ Get info about the identifier via ':info' (or unique prefixes)
| SetExtension -- ^ Enable or disable an extension via ':extension' (or prefixes)
| LoadFile -- ^ Load a Haskell module.
| SetLint -- ^ Enable or disable a hlint via ':hlint on' or ':hlint off'
| HelpForSet -- ^ Provide useful info if people try ':set'.
| SetOpt -- ^ Set various options.
| GetHelp -- ^ General help via ':?' or ':help'.
deriving (Show, Eq)
......@@ -238,8 +237,7 @@ parseDirective (':':directive) line = case find rightDirective directives of
,(GetInfo, "info")
,(SetExtension, "extension")
,(LoadFile, "load")
,(SetLint, "hlint")
,(HelpForSet, "set")
,(SetOpt, "set")
,(GetHelp, "?")
,(GetHelp, "help")
]
......
......@@ -18,6 +18,8 @@ module IHaskell.Types (
InitInfo(..),
KernelState(..),
LintStatus(..),
Width, Height,
defaultKernelState
) where
import ClassyPrelude
......@@ -72,15 +74,17 @@ instance ToJSON Profile where
data KernelState = KernelState
{ getExecutionCounter :: Int,
getLintStatus :: LintStatus, -- Whether to use hlint, and what arguments to pass it.
getCwd :: String
getCwd :: String,
useSvg :: Bool
}
-- | like 'First', except also add up the execution counter
instance Monoid KernelState where
mempty = KernelState 1 LintOn "."
KernelState na sa cwda `mappend` KernelState nb sb cwdb =
KernelState (na+nb) sa cwda
defaultKernelState :: KernelState
defaultKernelState = KernelState
{ getExecutionCounter = 1,
getLintStatus = LintOn,
getCwd = ".",
useSvg = True
}
-- | Initialization information for the kernel.
data InitInfo = InitInfo {
......@@ -314,10 +318,12 @@ instance Serialize DisplayData
instance Serialize MimeType
-- | Possible MIME types for the display data.
type Width = Int
type Height = Int
data MimeType = PlainText
| MimeHtml
| MimePng
| MimeJpg
| MimePng Width Height
| MimeJpg Width Height
| MimeSvg
| MimeLatex
deriving (Eq, Typeable, Generic)
......@@ -326,8 +332,8 @@ data MimeType = PlainText
instance Show MimeType where
show PlainText = "text/plain"
show MimeHtml = "text/html"
show MimePng = "image/png"
show MimeJpg = "image/jpeg"
show (MimePng _ _) = "image/png"
show (MimeJpg _ _) = "image/jpeg"
show MimeSvg = "image/svg+xml"
show MimeLatex = "text/latex"
......
......@@ -260,7 +260,7 @@ runKernel profileSrc initInfo = do
-- Initial kernel state.
initialKernelState :: IO (MVar KernelState)
initialKernelState =
newMVar mempty
newMVar defaultKernelState
-- | Duplicate a message header, giving it a new UUID and message type.
dupHeader :: MessageHeader -> MessageType -> IO MessageHeader
......
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