【问题标题】:Haskell (haskeline) word completionHaskell(haskell)单词补全
【发布时间】:2011-05-27 02:23:39
【问题描述】:

Haskline 让获取文件名制表符补全功能变得非常容易:

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)中的功能建议也很有帮助。

【问题讨论】:

  • 我从未使用过这些函数,但如果您正在寻找具有特定函数或模块的示例代码,您可以随时尝试Google Code Search。它并不总是有帮助,但它通常会在各种情况下为您提供一些很好的示例。
  • 我没有找到任何功能参考,但感谢您的建议。
  • 提供我 62% 的声誉来回答这个问题 :)

标签: haskell command-line haskeline word-completion


【解决方案1】:

这是你所追求的吗?

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
                      }

用那个替换你的sn-p中mySettings的定义,它应该可以工作。

【讨论】:

  • @amindfv:不客气!并不是说我真的需要更多代表,但我通常不会看到有关 Haskell 问题的赏金,所以我想它达到了吸引注意力的目的?我确实很快对其进行了测试以验证它是否有效,但如果您有任何问题,我会很乐意提供帮助(因为那感觉不像是 +50 的努力,哈哈哈)。
  • 似乎当我使用不在当前目录中的历史文件时(例如“~/.myhist”),haskline 历史不再起作用。对此有什么想法吗?
  • @Carson:你有没有试过给出一个不依赖扩展的绝对路径~?除此之外,我不知道。
  • 是的,我有。也不行。我猜这只是我这边的一个简单错误。
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多