From f6917e32959591386e7b5967d33484f11ca74182 Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Mon, 4 Jul 2022 21:47:21 -0500 Subject: [PATCH] don't tab complete when there are no identifier chars typed (#543) When the prompt was blank, it didn't tab complete; however, when there were any characters typed at all, it found the "last word" (all identifier characters at the end) and tried to match on it. However, if there were no identifier chars, the resulting empty "last word" matched everything. This fix now has a special case to check whether the `lastWord` is blank, which also takes care of the original special case when the prompt is completely empty. Fixes #518. --- src/Swarm/TUI/Controller.hs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Swarm/TUI/Controller.hs b/src/Swarm/TUI/Controller.hs index ca4520f2..10258053 100644 --- a/src/Swarm/TUI/Controller.hs +++ b/src/Swarm/TUI/Controller.hs @@ -656,18 +656,19 @@ handleREPLEvent s = \case -- things reserved words and names in scope. tabComplete :: AppState -> REPLPrompt -> REPLPrompt tabComplete _ p@(SearchPrompt {}) = p -tabComplete _ (CmdPrompt "" []) = CmdPrompt "" [] -tabComplete s (CmdPrompt t []) = case matches of - [] -> CmdPrompt t [] - [m] -> CmdPrompt (completeWith m) [] - (m : ms) -> CmdPrompt (completeWith m) (ms ++ [m]) +tabComplete s (CmdPrompt t mms) + | (m : ms) <- mms = CmdPrompt (replaceLast m t) (ms ++ [m]) + | T.null lastWord = CmdPrompt t [] + | otherwise = case matches of + [] -> CmdPrompt t [] + [m] -> CmdPrompt (completeWith m) [] + (m : ms) -> CmdPrompt (completeWith m) (ms ++ [m]) where completeWith m = T.append t (T.drop (T.length lastWord) m) lastWord = T.takeWhileEnd isIdentChar t names = s ^.. gameState . robotMap . ix 0 . robotContext . defTypes . to assocs . traverse . _1 possibleWords = reservedWords ++ names matches = filter (lastWord `T.isPrefixOf`) possibleWords -tabComplete _ (CmdPrompt t (m : ms)) = CmdPrompt (replaceLast m t) (ms ++ [m]) -- | Validate the REPL input when it changes: see if it parses and -- typechecks, and set the color accordingly.