Haskell (haskeline) 单词补全

发布于 2024-11-10 04:08:47 字数 972 浏览 0 评论 0原文

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)中的函数推荐也很有帮助。

Haskeline makes it very easy to get filename tab-completion functionality:

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)

It also provides functions like completeWord and completeQuotedWord, which should be able to be used in the same way that completeFilename is used to make the above functionality.
(In other words, have tab-completion based on a list of words (say, function names), instead of based on the contents of a folder)

Can anyone provide a working example - or working code - of this?

Recommendations for functions from other packages (like HCL) are helpful also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-11-17 04:08:47

这就是你所追求的东西吗?

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 的定义替换为该定义,它应该可以工作。

Is this the sort of thing you're after?

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
                      }

Replace the definition of mySettings in your snippet with that and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文