module Yi.Hoogle where
import Control.Applicative ((<$>))
import Control.Arrow ((&&&))
import Data.Char (isUpper)
import Data.List (nub)
import qualified Data.Text as T (isInfixOf, lines, pack)
import System.Exit (ExitCode (ExitFailure))
import Yi.Buffer (readRegionB, regionOfB, replaceRegionB, unitWord)
import Yi.Editor (printMsgs, withCurrentBuffer)
import Yi.Keymap (YiM)
import Yi.Process (runProgCommand)
import qualified Yi.Rope as R (YiString, fromText, head, null, toString, toText, words)
import Yi.String (showT)
import Yi.Utils (io)
caseSensitize :: [R.YiString] -> [R.YiString]
caseSensitize = filter p
where
p :: R.YiString -> Bool
p t = case R.head t of
Nothing -> False
Just c -> not $ isUpper c
gv :: [R.YiString] -> [R.YiString]
gv = filter f
where
ks = ["module ", " type ", "package ", " data ", " keyword "]
f x = not $ any (`T.isInfixOf` R.toText x) ks
hoogleRaw :: R.YiString -> R.YiString -> IO [R.YiString]
hoogleRaw srch opts = do
let options = filter (not . R.null) [opts, srch]
outp@(_status, out, _err) <- runProgCommand "hoogle" (R.toString <$> options)
case outp of
(ExitFailure 1, "", "") ->
fail "Error running hoogle command. Is hoogle on path?"
(ExitFailure 1, xs, _) -> fail $ "hoogle failed with: " ++ xs
_ -> return ()
let results = fmap R.fromText . T.lines $ T.pack out
if results == ["No results found"]
then fail "No Hoogle results"
else return results
hoogleFunctions :: R.YiString -> IO [R.YiString]
hoogleFunctions a =
caseSensitize . gv . nub . map ((!!1) . R.words) <$> hoogleRaw a ""
hoogleFunModule :: R.YiString -> IO [(R.YiString, R.YiString)]
hoogleFunModule a = map ((head &&& (!! 1)) . R.words) . gv <$> hoogleRaw a ""
hoogle :: YiM R.YiString
hoogle = do
(wordRegion,word) <- withCurrentBuffer $ do
wordRegion <- regionOfB unitWord
word <- readRegionB wordRegion
return (wordRegion, word)
((modl,fun):_) <- io $ hoogleFunModule word
withCurrentBuffer $ replaceRegionB wordRegion fun
return modl
hoogleSearch :: YiM ()
hoogleSearch = do
word <- withCurrentBuffer $ do
wordRegion <- regionOfB unitWord
readRegionB wordRegion
results <- io $ hoogleRaw word ""
printMsgs $ map showT results