Skip to content

Commit 1834758

Browse files
committed
Add support for restoring closed prompt tabs
- add ability to restore closed tab with its instructions via a shortcut (identical to the one used by google chrome) [Accuracy boost]
1 parent dda9160 commit 1834758

7 files changed

Lines changed: 129 additions & 20 deletions

File tree

‎apps/editor/src/views/prompt/frontend/Main/Main.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type Props = {
7474
on_new_tab: () => void
7575
on_tab_delete: (index: number) => void
7676
on_tabs_reorder: (new_order: number[]) => void
77+
on_tab_restore: () => void
7778
missing_web_configuration?: boolean
7879
voice_input_push_to_talk: boolean
7980
selected_files_token_count: number
@@ -716,6 +717,7 @@ export const Main: React.FC<Props> = (props) => {
716717
on_new_tab={props.on_new_tab}
717718
on_tab_delete={props.on_tab_delete}
718719
on_tabs_reorder={props.on_tabs_reorder}
720+
on_tab_restore={props.on_tab_restore}
719721
voice_input_push_to_talk={props.voice_input_push_to_talk}
720722
on_preview_prompt={props.on_preview_prompt}
721723
on_changes_click={props.on_changes_click}

‎apps/editor/src/views/prompt/frontend/Main/MainView/MainView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ type Props = {
135135
on_new_tab: () => void
136136
on_tab_delete: (index: number) => void
137137
on_tabs_reorder: (new_order: number[]) => void
138+
on_tab_restore: () => void
138139
voice_input_push_to_talk: boolean
139140
on_preview_prompt: () => void
140141
on_changes_click?: (branch_name: string) => void
@@ -520,6 +521,7 @@ export const MainView: React.FC<Props> = (props) => {
520521
on_new_tab={props.on_new_tab}
521522
on_tab_delete={props.on_tab_delete}
522523
on_tabs_reorder={props.on_tabs_reorder}
524+
on_tab_restore={props.on_tab_restore}
523525
voice_input_push_to_talk={props.voice_input_push_to_talk}
524526
currently_open_file_path={props.currently_open_file_path}
525527
are_keyboard_shortcuts_disabled={

‎apps/editor/src/views/prompt/frontend/Prompt.tsx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const Prompt = () => {
7474
handle_new_tab,
7575
handle_tab_delete,
7676
handle_tabs_reorder,
77+
handle_tab_restore,
7778
voice_input_push_to_talk,
7879
is_modern_ui,
7980
selected_files_token_count,
@@ -386,6 +387,7 @@ export const Prompt = () => {
386387
on_new_tab={handle_new_tab}
387388
on_tab_delete={handle_tab_delete}
388389
on_tabs_reorder={handle_tabs_reorder}
390+
on_tab_restore={handle_tab_restore}
389391
voice_input_push_to_talk={voice_input_push_to_talk}
390392
selected_files_token_count={selected_files_token_count}
391393
edit_instructions_token_count={edit_instructions_token_count}
@@ -647,13 +649,13 @@ export const Prompt = () => {
647649
/>
648650
}
649651
>
650-
<UiResponsePreview
651-
items={items_in_preview}
652-
raw_instructions={raw_instructions}
653-
api_configuration_details={api_configuration_details}
654-
cli_configuration_details={cli_configuration_details}
655-
web_configuration_details={web_configuration_details}
656-
has_multiple_workspaces={workspace_folder_count > 1}
652+
<UiResponsePreview
653+
items={items_in_preview}
654+
raw_instructions={raw_instructions}
655+
api_configuration_details={api_configuration_details}
656+
cli_configuration_details={cli_configuration_details}
657+
web_configuration_details={web_configuration_details}
658+
has_multiple_workspaces={workspace_folder_count > 1}
657659
is_from_agentic_cli={is_from_agentic_cli}
658660
on_focus_file={(file) => {
659661
post_message(vscode, {

‎apps/editor/src/views/prompt/frontend/hooks/use-prompt/hooks/use-instructions.ts‎

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react'
1+
import { useState, useEffect, useRef } from 'react'
22
import { BackendMessage, InstructionsState } from '../../../../types/messages'
33
import { post_message } from '../../../utils/post-message'
44
import { Target } from '@shared/types/target'
@@ -21,6 +21,9 @@ export const use_instructions = (
2121
InstructionsState | undefined
2222
>()
2323

24+
const closed_ask_tabs = useRef<string[]>([])
25+
const closed_edit_tabs = useRef<string[]>([])
26+
2427
const handle_instructions_change = (
2528
value: string,
2629
prompt_type: 'ask' | 'edit'
@@ -133,10 +136,14 @@ export const use_instructions = (
133136
state: InstructionsState | undefined,
134137
setter: React.Dispatch<
135138
React.SetStateAction<InstructionsState | undefined>
136-
>
139+
>,
140+
closed_tabs_ref: React.MutableRefObject<string[]>
137141
) => {
138142
if (!state || state.instructions.length <= 1) return
139143

144+
const deleted_instruction = state.instructions[index]
145+
closed_tabs_ref.current.push(deleted_instruction)
146+
140147
const new_instructions = state.instructions.filter((_, i) => i !== index)
141148
let new_active_index = state.active_index
142149

@@ -157,9 +164,64 @@ export const use_instructions = (
157164
}
158165

159166
if (prompt_type === 'ask') {
160-
update(ask_about_context_instructions, set_ask_about_context_instructions)
167+
update(
168+
ask_about_context_instructions,
169+
set_ask_about_context_instructions,
170+
closed_ask_tabs
171+
)
161172
} else if (prompt_type === 'edit') {
162-
update(edit_files_instructions, set_edit_files_instructions)
173+
update(
174+
edit_files_instructions,
175+
set_edit_files_instructions,
176+
closed_edit_tabs
177+
)
178+
}
179+
}
180+
181+
const handle_tab_restore = () => {
182+
const prompt_type = (
183+
target === 'WEB'
184+
? web_prompt_type
185+
: target === 'API'
186+
? api_prompt_type
187+
: cli_prompt_type
188+
) as any
189+
190+
const update = (
191+
state: InstructionsState | undefined,
192+
setter: React.Dispatch<
193+
React.SetStateAction<InstructionsState | undefined>
194+
>,
195+
closed_tabs_ref: React.MutableRefObject<string[]>
196+
) => {
197+
if (closed_tabs_ref.current.length === 0) return
198+
199+
const restored_instruction = closed_tabs_ref.current.pop()!
200+
const current_instructions = state?.instructions ?? ['']
201+
const new_state = {
202+
instructions: [...current_instructions, restored_instruction],
203+
active_index: current_instructions.length
204+
}
205+
setter(new_state)
206+
post_message(vscode, {
207+
command: 'SAVE_INSTRUCTIONS',
208+
instruction: new_state,
209+
prompt_type
210+
})
211+
}
212+
213+
if (prompt_type === 'ask') {
214+
update(
215+
ask_about_context_instructions,
216+
set_ask_about_context_instructions,
217+
closed_ask_tabs
218+
)
219+
} else if (prompt_type === 'edit') {
220+
update(
221+
edit_files_instructions,
222+
set_edit_files_instructions,
223+
closed_edit_tabs
224+
)
163225
}
164226
}
165227

@@ -222,6 +284,7 @@ export const use_instructions = (
222284
handle_tab_change,
223285
handle_new_tab,
224286
handle_tab_delete,
225-
handle_tabs_reorder
287+
handle_tabs_reorder,
288+
handle_tab_restore
226289
}
227290
}

‎apps/editor/src/views/prompt/frontend/hooks/use-prompt/use-prompt.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export const use_prompt = (vscode: any) => {
4747
handle_tab_change,
4848
handle_new_tab,
4949
handle_tab_delete,
50-
handle_tabs_reorder
50+
handle_tabs_reorder,
51+
handle_tab_restore
5152
} = use_instructions(
5253
vscode,
5354
target,
@@ -337,6 +338,7 @@ export const use_prompt = (vscode: any) => {
337338
handle_new_tab,
338339
handle_tab_delete,
339340
handle_tabs_reorder,
341+
handle_tab_restore,
340342
voice_input_push_to_talk,
341343
is_modern_ui,
342344
selected_files_token_count,

‎packages/ui/src/components/editor/common/prompts/PromptField/PromptField.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export namespace PromptField {
9090
on_new_tab: () => void
9191
on_tab_delete: (index: number) => void
9292
on_tabs_reorder?: (new_order: number[]) => void
93+
on_tab_restore?: () => void
9394
voice_input_push_to_talk?: boolean
9495
prompt_token_count: number
9596
is_copy_only?: boolean

‎packages/ui/src/components/editor/common/prompts/PromptField/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,44 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
141141
if (props.are_keyboard_shortcuts_disabled) return
142142

143143
const is_next_tab =
144-
(e.ctrlKey && e.key === 'Tab' && !e.shiftKey && !e.altKey && !e.metaKey) ||
145-
(e.ctrlKey && e.key === 'PageDown' && !e.shiftKey && !e.altKey && !e.metaKey) ||
146-
(e.metaKey && e.altKey && e.key === 'ArrowRight' && !e.shiftKey && !e.ctrlKey) ||
147-
(e.metaKey && e.shiftKey && (e.key === ']' || e.key === '}' || e.code === 'BracketRight') && !e.altKey && !e.ctrlKey)
144+
(e.ctrlKey &&
145+
e.key === 'Tab' &&
146+
!e.shiftKey &&
147+
!e.altKey &&
148+
!e.metaKey) ||
149+
(e.ctrlKey &&
150+
e.key === 'PageDown' &&
151+
!e.shiftKey &&
152+
!e.altKey &&
153+
!e.metaKey) ||
154+
(e.metaKey &&
155+
e.altKey &&
156+
e.key === 'ArrowRight' &&
157+
!e.shiftKey &&
158+
!e.ctrlKey) ||
159+
(e.metaKey &&
160+
e.shiftKey &&
161+
(e.key === ']' || e.key === '}' || e.code === 'BracketRight') &&
162+
!e.altKey &&
163+
!e.ctrlKey)
148164

149165
const is_prev_tab =
150166
(e.ctrlKey && e.key === 'Tab' && e.shiftKey && !e.altKey && !e.metaKey) ||
151-
(e.ctrlKey && e.key === 'PageUp' && !e.shiftKey && !e.altKey && !e.metaKey) ||
152-
(e.metaKey && e.altKey && e.key === 'ArrowLeft' && !e.shiftKey && !e.ctrlKey) ||
153-
(e.metaKey && e.shiftKey && (e.key === '[' || e.key === '{' || e.code === 'BracketLeft') && !e.altKey && !e.ctrlKey)
167+
(e.ctrlKey &&
168+
e.key === 'PageUp' &&
169+
!e.shiftKey &&
170+
!e.altKey &&
171+
!e.metaKey) ||
172+
(e.metaKey &&
173+
e.altKey &&
174+
e.key === 'ArrowLeft' &&
175+
!e.shiftKey &&
176+
!e.ctrlKey) ||
177+
(e.metaKey &&
178+
e.shiftKey &&
179+
(e.key === '[' || e.key === '{' || e.code === 'BracketLeft') &&
180+
!e.altKey &&
181+
!e.ctrlKey)
154182

155183
if (is_next_tab) {
156184
e.preventDefault()
@@ -172,6 +200,15 @@ export const use_keyboard_shortcuts = (props: PromptField.Props) => {
172200
return
173201
}
174202

203+
if ((e.ctrlKey || e.metaKey) && e.shiftKey && !e.altKey) {
204+
if (e.key.toLowerCase() === 't') {
205+
e.preventDefault()
206+
e.stopPropagation()
207+
props.on_tab_restore?.()
208+
return
209+
}
210+
}
211+
175212
if ((e.ctrlKey || e.metaKey) && !e.shiftKey && !e.altKey) {
176213
if (e.key.toLowerCase() === 'w') {
177214
if (props.tabs_count > 1) {

0 commit comments

Comments
 (0)