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.
This commit is contained in:
Brent Yorgey 2022-07-04 21:47:21 -05:00 committed by GitHub
parent da4e0cec6c
commit f6917e3295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.