Haskell(haskeline)一词完成

时间:2011-05-27 02:23:40

标签: haskell command-line haskeline word-completion

Haskeline使得获取文件名标签完成功能变得非常容易:

module Main where

import System.Console.Haskeline
import System.Environment

mySettings :: Settings IO
mySettings = defaultSettings {historyFile = Just "myhist"}

main :: IO ()
main = do
        args <- getArgs
        let inputFunc = getInputLine
        runInputT mySettings $ withInterrupt $ loop inputFunc 0
    where
        loop inputFunc n = do
            minput <-  handleInterrupt (return (Just "Caught interrupted"))
                        $ inputFunc (show n ++ ":")
            case minput of
                Nothing -> return ()
                Just s -> do
                            outputStrLn ("line " ++ show n ++ ":" ++ s)
                            loop inputFunc (n+1)

它还提供了类似completeWord和completeQuotedWord的函数,它们应该能够以与使用completeFilename来实现上述功能相同的方式使用。
(换句话说,根据单词列表(例如,函数名称)而不是基于文件夹的内容来完成制表符完成)

任何人都可以提供一个工作示例 - 或工作代码 - 吗?

其他软件包(如HCL)的功能建议也很有帮助。

1 个答案:

答案 0 :(得分:21)

这是你想要的事吗?

import Data.List

wordList = [ "Apple", "Pear", "Peach", "Grape", "Grapefruit", "Slime Mold"]

searchFunc :: String -> [Completion]
searchFunc str = map simpleCompletion $ filter (str `isPrefixOf`) wordList

mySettings :: Settings IO
mySettings = Settings { historyFile = Just "myhist"
                      , complete = completeWord Nothing " \t" $ return . searchFunc
                      , autoAddHistory = True
                      }

将代码段中的mySettings的定义替换为该代码,它应该可以正常工作。