1
1
{-# LANGUAGE TupleSections #-}
2
+ {-# LANGUAGE LambdaCase #-}
2
3
{-# LANGUAGE CPP #-}
3
4
{-# LANGUAGE OverloadedStrings #-}
4
5
module Haskell.Ide.Engine.Plugin.Hoogle where
@@ -99,11 +100,16 @@ infoCmd = CmdSync $ \expr -> do
99
100
infoCmd' :: T. Text -> IdeM (Either HoogleError T. Text )
100
101
infoCmd' expr = do
101
102
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]
107
113
108
114
-- | Command to get the prettified documentation of an hoogle identifier.
109
115
-- Identifier should be understandable for hoogle.
@@ -117,11 +123,9 @@ infoCmd' expr = do
117
123
infoCmdFancyRender :: T. Text -> IdeM (Either HoogleError T. Text )
118
124
infoCmdFancyRender expr = do
119
125
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
125
129
126
130
-- | Render the target in valid markdown.
127
131
-- Transform haddock documentation into markdown.
@@ -133,18 +137,25 @@ renderTarget t = T.intercalate "\n" $
133
137
++ [renderDocs $ targetDocs t]
134
138
++ [T. pack $ curry annotate " More info" $ targetURL t]
135
139
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
148
159
149
160
------------------------------------------------------------------------
150
161
0 commit comments