Skip to content

Commit

Permalink
Improve / fix arrow keys for Cody history (#51586)
Browse files Browse the repository at this point in the history
Closes #51575.

Some issues that were fixed:
- Down arrow did not navigate history forward
- You couldn't navigate the WIP prompt with the up arrow as it would
navigate history always; now we only navigate to backwards when the up
arrow is pressed at the start of the prompt and forwards then the down
arrow is pressed at the end of the prompt
- There was a typo that prevented the history index from being set
properly in VSCode
- Input history wasn't populated on the web

## Test plan

Tested history navigation in VSCode and web.
  • Loading branch information
SuperAuguste authored and cesrjimenez committed May 17, 2023
1 parent 550e9ba commit 357b7dc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
21 changes: 15 additions & 6 deletions client/cody-ui/src/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface ChatUITextAreaProps {
value: string
required: boolean
onInput: React.FormEventHandler<HTMLElement>
onKeyDown: React.KeyboardEventHandler<HTMLElement>
onKeyDown?: (event: React.KeyboardEvent<HTMLElement>, caretPosition: number | null) => void
}

export interface ChatUISubmitButtonProps {
Expand Down Expand Up @@ -145,7 +145,7 @@ export const Chat: React.FunctionComponent<ChatProps> = ({

onSubmit(input, submitType)
setSuggestions?.(undefined)
setHistoryIndex(input.length + 1)
setHistoryIndex(inputHistory.length + 1)
setInputHistory([...inputHistory, input])
},
[inputHistory, messageInProgress, onSubmit, setInputHistory, setSuggestions]
Expand All @@ -161,7 +161,7 @@ export const Chat: React.FunctionComponent<ChatProps> = ({
}, [formInput, messageInProgress, setFormInput, submitInput])

const onChatKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLDivElement>): void => {
(event: React.KeyboardEvent<HTMLElement>, caretPosition: number | null): void => {
// Submit input on Enter press (without shift) and
// trim the formInput to make sure input value is not empty.
if (
Expand All @@ -176,16 +176,25 @@ export const Chat: React.FunctionComponent<ChatProps> = ({
setMessageBeingEdited(false)
onChatSubmit()
}

// Loop through input history on up arrow press
if (event.key === 'ArrowUp' && inputHistory.length) {
if (formInput === inputHistory[historyIndex] || !formInput) {
if (!inputHistory.length) {
return
}

if (formInput === inputHistory[historyIndex] || !formInput) {
if (event.key === 'ArrowUp' && caretPosition === 0) {
const newIndex = historyIndex - 1 < 0 ? inputHistory.length - 1 : historyIndex - 1
setHistoryIndex(newIndex)
setFormInput(inputHistory[newIndex])
} else if (event.key === 'ArrowDown' && caretPosition === formInput.length) {
const newIndex = historyIndex + 1 >= inputHistory.length ? 0 : historyIndex + 1
setHistoryIndex(newIndex)
setFormInput(inputHistory[newIndex])
}
}
},
[inputHistory, onChatSubmit, formInput, historyIndex, setFormInput, setMessageBeingEdited]
[inputHistory, historyIndex, setFormInput, onChatSubmit, formInput, setMessageBeingEdited]
)

const transcriptWithWelcome = useMemo<ChatMessage[]>(
Expand Down
35 changes: 23 additions & 12 deletions client/cody-web/src/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useRef } from 'react'

import { noop } from 'lodash'

Expand Down Expand Up @@ -60,17 +60,28 @@ const TextArea: React.FunctionComponent<ChatUITextAreaProps> = ({
required,
onInput,
onKeyDown,
}) => (
<textarea
className={className}
rows={rows}
value={value}
autoFocus={autoFocus}
required={required}
onInput={onInput}
onKeyDown={onKeyDown}
/>
)
}) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null)

const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>): void => {
if (onKeyDown) {
onKeyDown(event, textAreaRef.current?.selectionStart ?? null)
}
}

return (
<textarea
ref={textAreaRef}
className={className}
rows={rows}
value={value}
autoFocus={autoFocus}
required={required}
onInput={onInput}
onKeyDown={handleKeyDown}
/>
)
}

const SubmitButton: React.FunctionComponent<ChatUISubmitButtonProps> = ({ className, disabled, onClick }) => (
<button className={className} type="submit" disabled={disabled} onClick={onClick}>
Expand Down
2 changes: 2 additions & 0 deletions client/cody/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to Sourcegraph Cody will be documented in this file.

### Changed

- Arrow key behavior: you can now navigate forwards through messages with the down arrow; additionally the up and down arrows will navigate backwards and forwards only if you're at the start or end of the drafted text, respectively. [pull/51586](https://github.com/sourcegraph/sourcegraph/pull/51586)

## [0.1.2]

### Added
Expand Down
10 changes: 8 additions & 2 deletions client/cody/webviews/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const TextArea: React.FunctionComponent<ChatUITextAreaProps> = ({
// Focus the textarea when the webview gains focus (unless there is text selected). This makes
// it so that the user can immediately start typing to Cody after invoking `Cody: Focus on Chat
// View` with the keyboard.
const inputRef = useRef<HTMLElement>(null)
const inputRef = useRef<HTMLTextAreaElement>(null)
useEffect(() => {
const handleFocus = (): void => {
if (document.getSelection()?.isCollapsed) {
Expand All @@ -145,6 +145,12 @@ const TextArea: React.FunctionComponent<ChatUITextAreaProps> = ({
}
}, [autoFocus])

const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>): void => {
if (onKeyDown) {
onKeyDown(event, (inputRef.current as any)?.control.selectionStart)
}
}

return (
<VSCodeTextArea
className={classNames(styles.chatInput, className)}
Expand All @@ -159,7 +165,7 @@ const TextArea: React.FunctionComponent<ChatUITextAreaProps> = ({
autofocus={autoFocus}
required={required}
onInput={e => onInput(e as React.FormEvent<HTMLTextAreaElement>)}
onKeyDown={onKeyDown}
onKeyDown={handleKeyDown}
/>
)
}
Expand Down
27 changes: 23 additions & 4 deletions client/web/src/cody/components/ChatUI/ChatUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@ export const SCROLL_THRESHOLD = 100
const onFeedbackSubmit = (feedback: string): void => eventLogger.log(`web:cody:feedbackSubmit:${feedback}`)

export const ChatUI = (): JSX.Element => {
const { submitMessage, editMessage, messageInProgress, transcript, getChatContext, transcriptId } =
useChatStoreState()
const {
submitMessage,
editMessage,
messageInProgress,
transcript,
getChatContext,
transcriptId,
transcriptHistory,
} = useChatStoreState()

const [formInput, setFormInput] = useState('')
const [inputHistory, setInputHistory] = useState<string[] | []>([])
const [inputHistory, setInputHistory] = useState<string[] | []>(() =>
transcriptHistory
.flatMap(entry => entry.interactions)
.sort((entryA, entryB) => +new Date(entryA.timestamp) - +new Date(entryB.timestamp))
.filter(interaction => interaction.humanMessage.displayText !== undefined)
.map(interaction => interaction.humanMessage.displayText!)
)
const [messageBeingEdited, setMessageBeingEdited] = useState<boolean>(false)

return (
Expand Down Expand Up @@ -172,6 +185,12 @@ export const AutoResizableTextArea: React.FC<AutoResizableTextAreaProps> = ({
adjustTextAreaHeight()
}, [adjustTextAreaHeight, value, width])

const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>): void => {
if (onKeyDown) {
onKeyDown(event, textAreaRef.current?.selectionStart ?? null)
}
}

return (
<TextArea
ref={textAreaRef}
Expand All @@ -181,7 +200,7 @@ export const AutoResizableTextArea: React.FC<AutoResizableTextAreaProps> = ({
rows={1}
autoFocus={false}
required={true}
onKeyDown={onKeyDown}
onKeyDown={handleKeyDown}
onInput={onInput}
/>
)
Expand Down

0 comments on commit 357b7dc

Please sign in to comment.