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

Commit d1e4c1c

Browse files
authored
Merge pull request #1284 from fendor/hsimport-support-constructors
Improve import action of hsimport
2 parents 83c28a9 + 4091895 commit d1e4c1c

File tree

5 files changed

+431
-91
lines changed

5 files changed

+431
-91
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
{-# LANGUAGE TupleSections #-}
12
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
module Haskell.Ide.Engine.Plugin.Hoogle where
45

56
import Control.Monad.IO.Class
67
import Control.Monad (join)
78
import Control.Exception
9+
import Control.Applicative (liftA2)
810
import Data.Aeson
911
import Data.Bifunctor
1012
import Data.Maybe
@@ -44,10 +46,10 @@ hoogleDescriptor plId = PluginDescriptor
4446

4547
-- ---------------------------------------------------------------------
4648

47-
data HoogleError
49+
data HoogleError
4850
= NoDb
4951
| DbFail T.Text
50-
| NoResults
52+
| NoResults
5153
deriving (Eq,Ord,Show)
5254

5355
newtype HoogleDb = HoogleDb (Maybe FilePath)
@@ -152,15 +154,36 @@ renderTarget t = T.intercalate "\n" $
152154
-- If an error occurs, such as no hoogle database has been found,
153155
-- or the search term has no match, an empty list will be returned.
154156
searchModules :: T.Text -> IdeM [T.Text]
155-
searchModules = fmap (nub . take 5) . searchTargets (fmap (T.pack . fst) . targetModule)
157+
searchModules = fmap (map fst) . searchModules'
158+
159+
-- | Just like 'searchModules', but includes the signature of the search term
160+
-- that has been found in the module.
161+
searchModules' :: T.Text -> IdeM [(T.Text, T.Text)]
162+
searchModules' = fmap (take 5 . nub) . searchTargets retrieveModuleAndSignature
163+
where
164+
-- | Hoogle results contain html like tags.
165+
-- We remove them with `tagsoup` here.
166+
-- So, if something hoogle related shows html tags,
167+
-- then maybe this function is responsible.
168+
normaliseItem :: T.Text -> T.Text
169+
normaliseItem = innerText . parseTags
170+
171+
retrieveModuleAndSignature :: Target -> Maybe (T.Text, T.Text)
172+
retrieveModuleAndSignature target = liftA2 (,) (packModuleName target) (packSymbolSignature target)
173+
174+
packModuleName :: Target -> Maybe T.Text
175+
packModuleName = fmap (T.pack . fst) . targetModule
176+
177+
packSymbolSignature :: Target -> Maybe T.Text
178+
packSymbolSignature = Just . normaliseItem . T.pack . targetItem
156179

157180
-- | Search for packages that satisfy the given search text.
158181
-- Will return at most five, unique results.
159182
--
160183
-- If an error occurs, such as no hoogle database has been found,
161184
-- or the search term has no match, an empty list will be returned.
162185
searchPackages :: T.Text -> IdeM [T.Text]
163-
searchPackages = fmap (nub . take 5) . searchTargets (fmap (T.pack . fst) . targetPackage)
186+
searchPackages = fmap (take 5 . nub) . searchTargets (fmap (T.pack . fst) . targetPackage)
164187

165188
-- | Search for Targets that fit to the given Text and satisfy the given predicate.
166189
-- Limits the amount of matches to at most ten.

0 commit comments

Comments
 (0)