Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 27 additions & 13 deletions src/Haskell/Ide/Engine/Plugin/Hoogle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ infoCmd' expr = do
if null res then
Left NoResults
else
return $ T.pack $ targetInfo $ head res
return $ renderTargetInfo $ head res

renderTargetInfo :: Target -> T.Text
renderTargetInfo t =
T.intercalate "\n"
$ ["```haskell\n" <> unHTML (T.pack $ targetItem t) <> "\n```"]
++ [renderDocs $ targetDocs t]
++ [T.pack $ curry annotate "More info" $ targetURL t]

-- | Command to get the prettified documentation of an hoogle identifier.
-- Identifier should be understandable for hoogle.
Expand Down Expand Up @@ -133,18 +140,25 @@ renderTarget t = T.intercalate "\n" $
++ [renderDocs $ targetDocs t]
++ [T.pack $ curry annotate "More info" $ targetURL t]
where mdl = map annotate $ catMaybes [targetPackage t, targetModule t]
annotate (thing,url) = "["<>thing++"]"++"("++url++")"
unHTML = T.replace "<0>" "" . innerText . parseTags
renderDocs = T.concat . map htmlToMarkDown . parseTree . T.pack
htmlToMarkDown :: TagTree T.Text -> T.Text
htmlToMarkDown (TagLeaf x) = fromMaybe "" $ maybeTagText x
htmlToMarkDown (TagBranch "i" _ tree) = "*" <> T.concat (map htmlToMarkDown tree) <> "*"
htmlToMarkDown (TagBranch "b" _ tree) = "**" <> T.concat (map htmlToMarkDown tree) <> "**"
htmlToMarkDown (TagBranch "a" _ tree) = "`" <> T.concat (map htmlToMarkDown tree) <> "`"
htmlToMarkDown (TagBranch "li" _ tree) = "- " <> T.concat (map htmlToMarkDown tree)
htmlToMarkDown (TagBranch "tt" _ tree) = "`" <> innerText (flattenTree tree) <> "`"
htmlToMarkDown (TagBranch "pre" _ tree) = "```haskell\n" <> T.concat (map htmlToMarkDown tree) <> "```"
htmlToMarkDown (TagBranch _ _ tree) = T.concat $ map htmlToMarkDown tree

annotate :: (String, String) -> String
annotate (thing,url) = "["<>thing<>"]"<>"("<>url<>")"

unHTML :: T.Text -> T.Text
unHTML = T.replace "<0>" "" . innerText . parseTags

renderDocs :: String -> T.Text
renderDocs = T.concat . map htmlToMarkDown . parseTree . T.pack

htmlToMarkDown :: TagTree T.Text -> T.Text
htmlToMarkDown (TagLeaf x) = fromMaybe "" $ maybeTagText x
htmlToMarkDown (TagBranch "i" _ tree) = "*" <> T.concat (map htmlToMarkDown tree) <> "*"
htmlToMarkDown (TagBranch "b" _ tree) = "**" <> T.concat (map htmlToMarkDown tree) <> "**"
htmlToMarkDown (TagBranch "a" _ tree) = "`" <> T.concat (map htmlToMarkDown tree) <> "`"
htmlToMarkDown (TagBranch "li" _ tree) = "- " <> T.concat (map htmlToMarkDown tree)
htmlToMarkDown (TagBranch "tt" _ tree) = "`" <> innerText (flattenTree tree) <> "`"
htmlToMarkDown (TagBranch "pre" _ tree) = "```haskell\n" <> T.concat (map htmlToMarkDown tree) <> "```"
htmlToMarkDown (TagBranch _ _ tree) = T.concat $ map htmlToMarkDown tree

------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion test/unit/HooglePluginSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ hoogleSpec = do
it "runs the info command" $ do
let req = liftToGhc $ infoCmd' "head"
r <- dispatchRequestP $ initializeHoogleDb >> req
r `shouldBe` Right "head :: [a] -> a\nbase Prelude\nExtract the first element of a list, which must be non-empty.\n\n"
r `shouldBe` Right "```haskell\nhead :: [a] -> a\n```\nExtract the first element of a list, which must be non-empty.\n\n[More info](https://hackage.haskell.org/package/base/docs/Prelude.html#v:head)"

-- ---------------------------------

Expand Down