Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit e300e64

Browse files
authored
Merge pull request #1290 from Avi-D-coder/master
Render completion documentation to markdown
2 parents d679eb5 + c25ab7f commit e300e64

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/Haskell/Ide/Engine/Plugin/Hoogle.hs

+33-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE TupleSections #-}
2+
{-# LANGUAGE LambdaCase #-}
23
{-# LANGUAGE CPP #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
module Haskell.Ide.Engine.Plugin.Hoogle where
@@ -99,11 +100,16 @@ infoCmd = CmdSync $ \expr -> do
99100
infoCmd' :: T.Text -> IdeM (Either HoogleError T.Text)
100101
infoCmd' expr = do
101102
HoogleDb mdb <- get
102-
liftIO $ runHoogleQuery mdb expr $ \res ->
103-
if null res then
104-
Left NoResults
105-
else
106-
return $ T.pack $ targetInfo $ head res
103+
liftIO $ runHoogleQuery mdb expr $ \case
104+
[] -> Left NoResults
105+
h:_ -> return $ renderTargetInfo h
106+
107+
renderTargetInfo :: Target -> T.Text
108+
renderTargetInfo t =
109+
T.intercalate "\n"
110+
$ ["```haskell\n" <> unHTML (T.pack $ targetItem t) <> "\n```"]
111+
++ [renderDocs $ targetDocs t]
112+
++ [T.pack $ curry annotate "More info" $ targetURL t]
107113

108114
-- | Command to get the prettified documentation of an hoogle identifier.
109115
-- Identifier should be understandable for hoogle.
@@ -117,11 +123,9 @@ infoCmd' expr = do
117123
infoCmdFancyRender :: T.Text -> IdeM (Either HoogleError T.Text)
118124
infoCmdFancyRender expr = do
119125
HoogleDb mdb <- get
120-
liftIO $ runHoogleQuery mdb expr $ \res ->
121-
if null res then
122-
Left NoResults
123-
else
124-
return $ renderTarget $ head res
126+
liftIO $ runHoogleQuery mdb expr $ \case
127+
[] -> Left NoResults
128+
h:_ -> return $ renderTarget h
125129

126130
-- | Render the target in valid markdown.
127131
-- Transform haddock documentation into markdown.
@@ -133,18 +137,25 @@ renderTarget t = T.intercalate "\n" $
133137
++ [renderDocs $ targetDocs t]
134138
++ [T.pack $ curry annotate "More info" $ targetURL t]
135139
where mdl = map annotate $ catMaybes [targetPackage t, targetModule t]
136-
annotate (thing,url) = "["<>thing++"]"++"("++url++")"
137-
unHTML = T.replace "<0>" "" . innerText . parseTags
138-
renderDocs = T.concat . map htmlToMarkDown . parseTree . T.pack
139-
htmlToMarkDown :: TagTree T.Text -> T.Text
140-
htmlToMarkDown (TagLeaf x) = fromMaybe "" $ maybeTagText x
141-
htmlToMarkDown (TagBranch "i" _ tree) = "*" <> T.concat (map htmlToMarkDown tree) <> "*"
142-
htmlToMarkDown (TagBranch "b" _ tree) = "**" <> T.concat (map htmlToMarkDown tree) <> "**"
143-
htmlToMarkDown (TagBranch "a" _ tree) = "`" <> T.concat (map htmlToMarkDown tree) <> "`"
144-
htmlToMarkDown (TagBranch "li" _ tree) = "- " <> T.concat (map htmlToMarkDown tree)
145-
htmlToMarkDown (TagBranch "tt" _ tree) = "`" <> innerText (flattenTree tree) <> "`"
146-
htmlToMarkDown (TagBranch "pre" _ tree) = "```haskell\n" <> T.concat (map htmlToMarkDown tree) <> "```"
147-
htmlToMarkDown (TagBranch _ _ tree) = T.concat $ map htmlToMarkDown tree
140+
141+
annotate :: (String, String) -> String
142+
annotate (thing,url) = "["<>thing<>"]"<>"("<>url<>")"
143+
144+
unHTML :: T.Text -> T.Text
145+
unHTML = T.replace "<0>" "" . innerText . parseTags
146+
147+
renderDocs :: String -> T.Text
148+
renderDocs = T.concat . map htmlToMarkDown . parseTree . T.pack
149+
150+
htmlToMarkDown :: TagTree T.Text -> T.Text
151+
htmlToMarkDown (TagLeaf x) = fromMaybe "" $ maybeTagText x
152+
htmlToMarkDown (TagBranch "i" _ tree) = "*" <> T.concat (map htmlToMarkDown tree) <> "*"
153+
htmlToMarkDown (TagBranch "b" _ tree) = "**" <> T.concat (map htmlToMarkDown tree) <> "**"
154+
htmlToMarkDown (TagBranch "a" _ tree) = "`" <> T.concat (map htmlToMarkDown tree) <> "`"
155+
htmlToMarkDown (TagBranch "li" _ tree) = "- " <> T.concat (map htmlToMarkDown tree)
156+
htmlToMarkDown (TagBranch "tt" _ tree) = "`" <> innerText (flattenTree tree) <> "`"
157+
htmlToMarkDown (TagBranch "pre" _ tree) = "```haskell\n" <> T.concat (map htmlToMarkDown tree) <> "```"
158+
htmlToMarkDown (TagBranch _ _ tree) = T.concat $ map htmlToMarkDown tree
148159

149160
------------------------------------------------------------------------
150161

test/unit/HooglePluginSpec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ hoogleSpec = do
4848
it "runs the info command" $ do
4949
let req = liftToGhc $ infoCmd' "head"
5050
r <- dispatchRequestP $ initializeHoogleDb >> req
51-
r `shouldBe` Right "head :: [a] -> a\nbase Prelude\nExtract the first element of a list, which must be non-empty.\n\n"
51+
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)"
5252

5353
-- ---------------------------------
5454

0 commit comments

Comments
 (0)