Skip to content

Commit

Permalink
Don't insert extra close bracket when the cursor is already on top of…
Browse files Browse the repository at this point in the history
… one (#2215)

Closes #2171.

When the cursor is on top of a close bracket (either `)`, `]`, or `}`) and the user types the same character, simply move the cursor right one position instead of inserting a new close bracket character.  This works especially well in conjunction with #1953.
  • Loading branch information
byorgey authored Nov 28, 2024
1 parent 3e05a07 commit c7678f3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/swarm-tui/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Brick hiding (Direction, Location)
import Brick.Focus
import Brick.Keybindings qualified as B
import Brick.Widgets.Dialog
import Brick.Widgets.Edit (Editor, applyEdit, handleEditorEvent)
import Brick.Widgets.Edit (Editor, applyEdit, editContentsL, handleEditorEvent)
import Brick.Widgets.List (handleListEvent)
import Brick.Widgets.List qualified as BL
import Brick.Widgets.TabularList.Mixed
Expand Down Expand Up @@ -669,7 +669,9 @@ handleREPLEventTyping = \case
-- finally if none match pass the event to the editor
ev -> do
Brick.zoom (uiState . uiGameplay . uiREPL . replPromptEditor) $ case ev of
CharKey c | c `elem` ("([{" :: String) -> insertMatchingPair c
CharKey c
| c `elem` ("([{" :: String) -> insertMatchingPair c
| c `elem` (")]}" :: String) -> insertOrMovePast c
_ -> handleEditorEvent ev
uiState . uiGameplay . uiREPL . replPromptType %= \case
CmdPrompt _ -> CmdPrompt [] -- reset completions on any event passed to editor
Expand All @@ -685,6 +687,16 @@ insertMatchingPair c = modify . applyEdit $ TZ.insertChar c >>> TZ.insertChar (c
'{' -> '}'
_ -> c

-- | Insert a character in an editor unless it matches the character
-- already at the cursor, in which case we just move past it
-- instead, without inserting an extra copy.
insertOrMovePast :: Char -> EventM Name (Editor Text Name) ()
insertOrMovePast c = do
e <- get
modify . applyEdit $ case TZ.currentChar (e ^. editContentsL) of
Just c' | c' == c -> TZ.moveRight
_ -> TZ.insertChar c

data CompletionType
= FunctionName
| EntityName
Expand Down

0 comments on commit c7678f3

Please sign in to comment.