Skip to content

Commit e88f7d6

Browse files
committed
Add Shift+Enter to send messages and Enter for new lines in prompt field
1 parent 713a0b6 commit e88f7d6

20 files changed

Lines changed: 187 additions & 10 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type PromptFieldProps = {
4141
on_at_sign_click: () => void
4242
on_hash_sign_click: () => void
4343
on_curly_braces_click: () => void
44+
send_with_shift_enter?: boolean
4445
caret_position_to_set?: number
4546
on_caret_position_set?: () => void
4647
focus_key?: number
@@ -58,6 +59,7 @@ export type PromptFieldProps = {
5859
export const PromptField: React.FC<PromptFieldProps> = (props) => {
5960
const input_ref = useRef<HTMLDivElement>(null)
6061
const [caret_position, set_caret_position] = useState(0)
62+
const [should_show_ghost_text, set_should_show_ghost_text] = useState(false)
6163
const [show_at_sign_tooltip, set_show_at_sign_tooltip] = useState(false)
6264
const [show_submit_tooltip, set_show_submit_tooltip] = useState(false)
6365
const [is_text_selecting, set_is_text_selecting] = useState(false)
@@ -80,7 +82,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
8082
const { ghost_text, handle_accept_ghost_text } = use_ghost_text({
8183
value: props.value,
8284
input_ref,
83-
is_focused,
85+
is_focused: is_focused && should_show_ghost_text,
8486
currently_open_file_text: props.currently_open_file_text,
8587
caret_position
8688
})
@@ -105,6 +107,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
105107

106108
const handle_mouse_down = useCallback(
107109
(e: React.MouseEvent<HTMLDivElement>) => {
110+
set_should_show_ghost_text(false)
108111
mouse_down_pos_ref.current = { x: e.clientX, y: e.clientY }
109112

110113
const handle_mouse_move = (move_e: MouseEvent) => {
@@ -310,6 +313,7 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
310313
<button
311314
className={cn(styles['clear-button'], 'codicon', 'codicon-close')}
312315
onClick={() => {
316+
set_should_show_ghost_text(false)
313317
handle_clear()
314318
input_ref.current?.focus()
315319
}}
@@ -320,8 +324,14 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
320324
ref={input_ref}
321325
contentEditable={true}
322326
suppressContentEditableWarning={true}
323-
onInput={handle_input_change}
324-
onKeyDown={handle_key_down}
327+
onInput={(e) => {
328+
set_should_show_ghost_text(true)
329+
handle_input_change(e)
330+
}}
331+
onKeyDown={(e) => {
332+
set_should_show_ghost_text(true)
333+
handle_key_down(e)
334+
}}
325335
onCopy={handle_copy}
326336
onPaste={handle_paste}
327337
onClick={handle_input_click}

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-ghost-text.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const use_ghost_text = ({
104104
} else {
105105
ghost_text_debounce_timer_ref.current = setTimeout(() => {
106106
set_ghost_text(potential_ghost_text)
107-
}, 500)
107+
}, 200)
108108
}
109109
} else {
110110
set_ghost_text('')

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-handlers.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,30 @@ export const use_handlers = (
963963
return
964964
}
965965

966-
if (key == 'Enter' && !shiftKey) {
967-
e.preventDefault()
968-
handle_submit(e)
966+
if (key == 'Enter') {
967+
if (props.send_with_shift_enter) {
968+
if (shiftKey) {
969+
e.preventDefault()
970+
handle_submit(e)
971+
} else {
972+
e.preventDefault()
973+
const selection = window.getSelection()
974+
if (!selection || !selection.rangeCount || !input_ref.current) return
975+
976+
const range = selection.getRangeAt(0)
977+
range.deleteContents()
978+
const text_node = document.createTextNode('\n')
979+
range.insertNode(text_node)
980+
range.setStartAfter(text_node)
981+
range.setEndAfter(text_node)
982+
selection.removeAllRanges()
983+
selection.addRange(range)
984+
input_ref.current.dispatchEvent(new Event('input', { bubbles: true }))
985+
}
986+
} else if (!shiftKey) {
987+
e.preventDefault()
988+
handle_submit(e)
989+
}
969990
return
970991
}
971992

‎packages/ui/src/components/editor/panel/PromptField/utils/get-highlighted-text.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const get_highlighted_text = (params: {
5555
if (params.is_in_code_completions_mode) {
5656
const regex = new RegExp(`(${saved_context_regex_part})`, 'g')
5757
const parts = params.text.split(regex)
58-
return parts
58+
const result = parts
5959
.map((part) => {
6060
const saved_context_match = part.match(
6161
/^#SavedContext:(WorkspaceState|JSON)\s+"([^"]+)"$/
@@ -79,6 +79,10 @@ export const get_highlighted_text = (params: {
7979
return process_text_part_for_files(part, params.context_file_paths)
8080
})
8181
.join('')
82+
if (params.text.endsWith('\n')) {
83+
return result + '<br data-role="trailing-break">'
84+
}
85+
return result
8286
}
8387

8488
const commit_regex_part =
@@ -89,7 +93,7 @@ export const get_highlighted_text = (params: {
8993
)
9094
const parts = params.text.split(regex)
9195

92-
return parts
96+
const result = parts
9397
.map((part) => {
9498
if (part == '#Selection') {
9599
const className = cn(styles['keyword'], styles['keyword--selection'], {
@@ -162,4 +166,9 @@ export const get_highlighted_text = (params: {
162166
return process_text_part_for_files(part, params.context_file_paths)
163167
})
164168
.join('')
169+
170+
if (params.text.endsWith('\n')) {
171+
return result + '<br data-role="trailing-break">'
172+
}
173+
return result
165174
}

‎packages/vscode/package.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,12 @@
633633
"scope": "resource",
634634
"default": false
635635
},
636+
"codeWebChat.sendWithShiftEnter": {
637+
"type": "boolean",
638+
"scope": "resource",
639+
"default": false,
640+
"description": "If enabled, use Shift+Enter to send messages and Enter to insert a new line."
641+
},
636642
"codeWebChat.clearChecksInWorkspaceBehavior": {
637643
"type": "string",
638644
"enum": [

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ export class PanelProvider implements vscode.WebviewViewProvider {
151151
})
152152
}
153153

154+
private _send_send_with_shift_enter() {
155+
const config = vscode.workspace.getConfiguration('codeWebChat')
156+
const enabled = config.get<boolean>('sendWithShiftEnter', false)
157+
this.send_message({
158+
command: 'SEND_WITH_SHIFT_ENTER',
159+
enabled
160+
})
161+
}
162+
154163
public send_currently_open_file_text() {
155164
const active_editor = vscode.window.activeTextEditor
156165
this.send_message({
@@ -266,6 +275,10 @@ export class PanelProvider implements vscode.WebviewViewProvider {
266275
if (event.affectsConfiguration('codeWebChat.isTimelineCollapsed')) {
267276
handle_get_collapsed_states(this)
268277
}
278+
279+
if (event.affectsConfiguration('codeWebChat.sendWithShiftEnter')) {
280+
this._send_send_with_shift_enter()
281+
}
269282
}
270283
)
271284

@@ -502,6 +515,8 @@ export class PanelProvider implements vscode.WebviewViewProvider {
502515
this.send_currently_open_file_text()
503516
} else if (message.command == 'REPLACE_PRESETS') {
504517
await handle_replace_presets(this, message)
518+
} else if (message.command == 'GET_SEND_WITH_SHIFT_ENTER') {
519+
this._send_send_with_shift_enter()
505520
} else if (message.command == 'UPDATE_PRESET') {
506521
await handle_update_preset(this, message, webview_view)
507522
} else if (message.command == 'DELETE_PRESET_GROUP_OR_SEPARATOR') {

‎packages/vscode/src/views/panel/frontend/Main/Main.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Props = {
4848
context_file_paths: string[]
4949
presets_collapsed: boolean
5050
on_presets_collapsed_change: (is_collapsed: boolean) => void
51+
send_with_shift_enter: boolean
5152
configurations_collapsed: boolean
5253
on_configurations_collapsed_change: (is_collapsed: boolean) => void
5354
currently_open_file_text?: string
@@ -728,6 +729,7 @@ export const Main: React.FC<Props> = (props) => {
728729
on_response_history_item_remove={props.on_response_history_item_remove}
729730
context_file_paths={props.context_file_paths}
730731
presets_collapsed={props.presets_collapsed}
732+
send_with_shift_enter={props.send_with_shift_enter}
731733
on_presets_collapsed_change={props.on_presets_collapsed_change}
732734
configurations_collapsed={props.configurations_collapsed}
733735
on_configurations_collapsed_change={

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type Props = {
8686
context_file_paths: string[]
8787
presets_collapsed: boolean
8888
on_presets_collapsed_change: (is_collapsed: boolean) => void
89+
send_with_shift_enter: boolean
8990
configurations_collapsed: boolean
9091
on_configurations_collapsed_change: (is_collapsed: boolean) => void
9192
currently_open_file_text?: string
@@ -199,6 +200,7 @@ export const MainView: React.FC<Props> = (props) => {
199200
is_connected={props.is_connected}
200201
is_in_code_completions_mode={is_in_code_completions_prompt_type}
201202
has_active_selection={props.has_active_selection}
203+
send_with_shift_enter={props.send_with_shift_enter}
202204
has_active_editor={props.has_active_editor}
203205
on_caret_position_change={props.on_caret_position_change}
204206
caret_position_to_set={props.caret_position_to_set}

‎packages/vscode/src/views/panel/frontend/Panel.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const Panel = () => {
8383
can_undo,
8484
context_file_paths,
8585
presets_collapsed,
86+
send_with_shift_enter,
8687
is_preview_ongoing_modal_visible,
8788
set_is_preview_ongoing_modal_visible,
8889
configurations_collapsed,
@@ -280,6 +281,7 @@ export const Panel = () => {
280281
context_size_warning_threshold={context_size_warning_threshold}
281282
context_file_paths={context_file_paths}
282283
presets_collapsed={presets_collapsed}
284+
send_with_shift_enter={send_with_shift_enter}
283285
on_presets_collapsed_change={handle_presets_collapsed_change}
284286
configurations_collapsed={configurations_collapsed}
285287
on_configurations_collapsed_change={

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export const use_panel = (vscode: any) => {
109109
const [context_file_paths, set_context_file_paths] = useState<string[]>([])
110110
const [presets_collapsed_by_web_mode, set_presets_collapsed_by_web_mode] =
111111
useState<{ [mode in WebPromptType]?: boolean }>({})
112+
const [send_with_shift_enter, set_send_with_shift_enter] = useState(false)
112113
const [is_timeline_collapsed, set_is_timeline_collapsed] = useState(false)
113114
const [are_tasks_collapsed, set_are_tasks_collapsed] = useState(false)
114115
const [
@@ -211,6 +212,8 @@ export const use_panel = (vscode: any) => {
211212
set_has_active_selection(message.has_selection)
212213
} else if (message.command == 'CONTEXT_FILES') {
213214
set_context_file_paths(message.file_paths)
215+
} else if (message.command == 'SEND_WITH_SHIFT_ENTER') {
216+
set_send_with_shift_enter(message.enabled)
214217
} else if (message.command == 'CURRENTLY_OPEN_FILE_TEXT') {
215218
set_currently_open_file_text(message.text)
216219
} else if (message.command == 'RESPONSE_PREVIEW_STARTED') {
@@ -361,6 +364,7 @@ export const use_panel = (vscode: any) => {
361364
{ command: 'GET_WORKSPACE_STATE' },
362365
{ command: 'GET_CONTEXT_SIZE_WARNING_THRESHOLD' },
363366
{ command: 'REQUEST_GIT_STATE' },
367+
{ command: 'GET_SEND_WITH_SHIFT_ENTER' },
364368
{ command: 'GET_COLLAPSED_STATES' },
365369
{ command: 'GET_CHECKPOINTS' },
366370
{ command: 'REQUEST_CURRENTLY_OPEN_FILE_TEXT' },
@@ -556,6 +560,7 @@ export const use_panel = (vscode: any) => {
556560
presets_collapsed: web_prompt_type
557561
? (presets_collapsed_by_web_mode[web_prompt_type] ?? false)
558562
: false,
563+
send_with_shift_enter,
559564
configurations_collapsed: api_prompt_type
560565
? (configurations_collapsed_by_api_mode[api_prompt_type] ?? false)
561566
: false,

0 commit comments

Comments
 (0)