Skip to content

Commit

Permalink
Add type tooltip to function in result
Browse files Browse the repository at this point in the history
  • Loading branch information
runeksvendsen committed Oct 11, 2023
1 parent 087e617 commit 1f44e52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/lib/MyLib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module MyLib
, runPrintQueryAll
, spTreeToPaths
, renderComposedFunctions, renderComposedFunctionsStr, parseComposedFunctions
, renderFunction, parseFunction
, renderFunction, parseFunction, renderTypedFunction
, Function(..), TypedFunction, UntypedFunction
, FullyQualifiedType(..), textToFullyQualifiedType, fullyQualifiedTypeToText
, Graph
Expand Down Expand Up @@ -251,6 +251,18 @@ renderFunction :: Function typeSig -> BS.ByteString
renderFunction fn =
_function_package fn <> ":" <> _function_module fn <> "." <> _function_name fn

-- | Render a function's name (output of 'renderFunction') and its FROM and TO type.
renderTypedFunction
:: TypedFunction
-> (BS.ByteString, (FullyQualifiedType, FullyQualifiedType))
-- ^ (output of 'renderFunction', (FROM type, TO type))
renderTypedFunction fn =
( renderFunction fn
, (Json.functionType_arg sig, Json.functionType_ret sig)
)
where
sig = _function_typeSig fn

-- | Parse e.g. "text-2.0.2:Data.Text.Encoding.encodeUtf16BE" to an untyped 'Function'
parseFunction :: BS.ByteString -> Maybe UntypedFunction
parseFunction bs =
Expand Down
33 changes: 28 additions & 5 deletions src/server/Server/Pages/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,37 @@ import qualified Data.Text as T
import Servant.Server (Handler)
import qualified Server.Monad as MyLib
import qualified MyLib
import Data.List (intersperse)

page :: MyLib.Graph -> T.Text -> T.Text -> Handler (Html ())
page graph src dst = pure $ do
p_ $ "Hi there, you entered src=" <> mono (toHtml src) <> ", dst=" <> mono (toHtml dst)
forM_ (map MyLib.renderComposedFunctions results) $ \result -> do
mono (toHtml result)
br_ []
forM_ results $ \result -> renderResult result >> br_ []
where
mono = span_ [style_ "font-family: monospace, monospace; background-color: rgb(200, 200, 200);"]
maxCount = 20
results = take maxCount $ MyLib.runQueryAll maxCount (MyLib.textToFullyQualifiedType src, MyLib.textToFullyQualifiedType dst) graph

results =
take maxCount $ MyLib.runQueryAll
maxCount
(MyLib.textToFullyQualifiedType src, MyLib.textToFullyQualifiedType dst)
graph

renderResult :: [MyLib.TypedFunction] -> Html ()
renderResult fns =
let nameWithTypeLst = map MyLib.renderTypedFunction (reverse fns)
renderSingleFn (name, (fromTy, toTy)) =
let typeSig = T.unwords $
[ "::"
, MyLib.fullyQualifiedTypeToText fromTy
, "->"
, MyLib.fullyQualifiedTypeToText toTy
]
in mono (toHtml name) `with` [title_ typeSig]
in mconcat $ intersperse (mono " . ") $ map renderSingleFn nameWithTypeLst

mono =
let style = style_ $ T.intercalate "; " $
[ "font-family: monospace, monospace"
, "background-color: rgb(200, 200, 200)"
]
in span_ [style]

0 comments on commit 1f44e52

Please sign in to comment.