|
| 1 | +{-# LANGUAGE TupleSections #-} |
1 | 2 | {-# LANGUAGE CPP #-}
|
2 | 3 | {-# LANGUAGE OverloadedStrings #-}
|
3 | 4 | module Haskell.Ide.Engine.Plugin.Hoogle where
|
4 | 5 |
|
5 | 6 | import Control.Monad.IO.Class
|
6 | 7 | import Control.Monad (join)
|
7 | 8 | import Control.Exception
|
| 9 | +import Control.Applicative (liftA2) |
8 | 10 | import Data.Aeson
|
9 | 11 | import Data.Bifunctor
|
10 | 12 | import Data.Maybe
|
@@ -44,10 +46,10 @@ hoogleDescriptor plId = PluginDescriptor
|
44 | 46 |
|
45 | 47 | -- ---------------------------------------------------------------------
|
46 | 48 |
|
47 |
| -data HoogleError |
| 49 | +data HoogleError |
48 | 50 | = NoDb
|
49 | 51 | | DbFail T.Text
|
50 |
| - | NoResults |
| 52 | + | NoResults |
51 | 53 | deriving (Eq,Ord,Show)
|
52 | 54 |
|
53 | 55 | newtype HoogleDb = HoogleDb (Maybe FilePath)
|
@@ -152,15 +154,36 @@ renderTarget t = T.intercalate "\n" $
|
152 | 154 | -- If an error occurs, such as no hoogle database has been found,
|
153 | 155 | -- or the search term has no match, an empty list will be returned.
|
154 | 156 | 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 |
156 | 179 |
|
157 | 180 | -- | Search for packages that satisfy the given search text.
|
158 | 181 | -- Will return at most five, unique results.
|
159 | 182 | --
|
160 | 183 | -- If an error occurs, such as no hoogle database has been found,
|
161 | 184 | -- or the search term has no match, an empty list will be returned.
|
162 | 185 | 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) |
164 | 187 |
|
165 | 188 | -- | Search for Targets that fit to the given Text and satisfy the given predicate.
|
166 | 189 | -- Limits the amount of matches to at most ten.
|
|
0 commit comments