Fix bug in simpleCompletion

pull/5/head
Lennart Spitzner 2018-10-02 16:03:05 +02:00
parent f756d4fb55
commit 131216d4f4
2 changed files with 11 additions and 4 deletions

View File

@ -121,7 +121,7 @@ simpleRunTest = do
describe "completions" $ do describe "completions" $ do
it "case 1" $ testCompletion completionTestCmd "" `shouldBe` "" it "case 1" $ testCompletion completionTestCmd "" `shouldBe` ""
it "case 2" $ testCompletion completionTestCmd "a" `shouldBe` "bc" it "case 2" $ testCompletion completionTestCmd "a" `shouldBe` "bc"
it "case 3" $ testCompletion completionTestCmd "abc" `shouldBe` "def" it "case 3" $ testCompletion completionTestCmd "abc" `shouldBe` ""
it "case 4" $ testCompletion completionTestCmd "abc " `shouldBe` "-" it "case 4" $ testCompletion completionTestCmd "abc " `shouldBe` "-"
it "case 5" $ testCompletion completionTestCmd "abc -" `shouldBe` "" it "case 5" $ testCompletion completionTestCmd "abc -" `shouldBe` ""
it "case 6" $ testCompletion completionTestCmd "abc --" `shouldBe` "flag" it "case 6" $ testCompletion completionTestCmd "abc --" `shouldBe` "flag"

View File

@ -30,11 +30,18 @@ simpleCompletion
-- subcommand. See 'UI.Butcher.Monadic.runCmdParserExt'. -- subcommand. See 'UI.Butcher.Monadic.runCmdParserExt'.
-> String -- ^ completion, i.e. a string that might be appended -> String -- ^ completion, i.e. a string that might be appended
-- to the current prompt when user presses tab. -- to the current prompt when user presses tab.
simpleCompletion line cdesc pcRest = List.drop (List.length lastWord) simpleCompletion line cdesc pcRest = case reverse line of
$ longestCommonPrefix choices [] -> compl
' ' : _ -> compl
_ | null pcRest -> "" -- necessary to prevent subcommand completion
-- appearing before space that is, if you have command
-- "aaa" with subcommand "sss", we want completion
-- "sss" on "aaa " but not on "aaa".
_ -> compl
where where
compl = List.drop (List.length lastWord) (longestCommonPrefix choices)
longestCommonPrefix [] = "" longestCommonPrefix [] = ""
longestCommonPrefix (c1:cr) = longestCommonPrefix (c1 : cr) =
case find (\s -> List.all (s `isPrefixOf`) cr) $ reverse $ List.inits c1 of case find (\s -> List.all (s `isPrefixOf`) cr) $ reverse $ List.inits c1 of
Nothing -> "" Nothing -> ""
Just x -> x Just x -> x