Skip to content

Commit f02a3ae

Browse files
committed
Refactor prompt field history handling to correctly manage the modification state and update default instructions for Edit Context formats
1 parent b51e0c1 commit f02a3ae

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,15 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
289289
}, [props.on_caret_position_change, props.value, props.context_file_paths])
290290

291291
const placeholder = useMemo(() => {
292-
const active_history = props.chat_history
293-
294292
if (props.is_in_code_completions_mode) {
295-
if (active_history.length > 0 && is_history_enabled) {
293+
if (props.chat_history.length > 0 && is_history_enabled) {
296294
return 'Completion instructions (⇅ for history)'
297295
} else {
298296
return 'Completion instructions'
299297
}
300298
}
301299

302-
return active_history.length > 0 && is_history_enabled
300+
return props.chat_history.length > 0 && is_history_enabled
303301
? 'Type something (⇅ for history)'
304302
: 'Type something'
305303
}, [

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ export const use_handlers = (
194194

195195
const update_value = (new_value: string, caret_pos?: number) => {
196196
if (new_value === props.value) return
197-
// Mark that we've modified the current entry (not from history navigation)
198-
has_modified_current_entry_ref.current = true
199197
set_undo_stack((prev) => [
200198
...prev,
201199
{ value: props.value, raw_caret_pos: raw_caret_pos_ref.current }
@@ -224,6 +222,7 @@ export const use_handlers = (
224222

225223
const new_value = leading_part + trailing_part
226224
const new_raw_cursor_pos = leading_part.length
225+
has_modified_current_entry_ref.current = new_value !== ''
227226
update_value(new_value, new_raw_cursor_pos)
228227
}
229228

@@ -394,8 +393,8 @@ export const use_handlers = (
394393
}
395394

396395
const handle_clear = () => {
397-
update_value('')
398396
has_modified_current_entry_ref.current = false
397+
update_value('')
399398
set_history_index(-1)
400399
}
401400

@@ -436,16 +435,16 @@ export const use_handlers = (
436435
last_word_in_raw_index
437436
)
438437
const final_new_value = `${value_before}\`${matched_path}\` `
438+
has_modified_current_entry_ref.current = true
439439
update_value(final_new_value, final_new_value.length)
440440
set_history_index(-1)
441441
return
442442
}
443443
}
444444
}
445445

446+
has_modified_current_entry_ref.current = new_raw_value !== ''
446447
update_value(new_raw_value)
447-
has_modified_current_entry_ref.current = true
448-
set_history_index(-1)
449448

450449
if (char_before_caret == '@') {
451450
const is_at_start = caret_position == 1
@@ -461,6 +460,8 @@ export const use_handlers = (
461460
} else if (char_before_caret == '#') {
462461
props.on_hash_sign_click()
463462
}
463+
464+
set_history_index(-1)
464465
}
465466

466467
const handle_submit = (
@@ -708,9 +709,9 @@ export const use_handlers = (
708709
e.preventDefault()
709710

710711
const update_and_set_caret = (value: string) => {
711-
update_value(value, value.length)
712712
// Reset the modification flag when navigating history
713713
has_modified_current_entry_ref.current = false
714+
update_value(value, value.length)
714715
}
715716

716717
if (e.key === 'ArrowUp') {
@@ -725,7 +726,6 @@ export const use_handlers = (
725726
set_history_index(new_index)
726727
update_and_set_caret(active_history[new_index])
727728
} else if (history_index === 0) {
728-
has_modified_current_entry_ref.current = false
729729
set_history_index(-1)
730730
update_and_set_caret('')
731731
}
@@ -967,6 +967,7 @@ export const use_handlers = (
967967
const new_start_pos = i + 1
968968
const new_value =
969969
value.substring(0, new_start_pos) + value.substring(raw_pos)
970+
has_modified_current_entry_ref.current = new_value !== ''
970971
update_value(new_value, new_start_pos)
971972
return
972973
}

‎packages/vscode/package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@
582582
"scope": "resource",
583583
"description": "Style of generated code blocks in \"Edit context\" mode.",
584584
"default": {
585-
"whole": "Whenever showing a new or updated file, include a brief explanation of changes, the path and a Markdown Code Block with full contents—because I have a disability which means I can't type and need to be able to just copy and paste.",
586-
"truncated": "Whenever showing a new or updated file, include a brief explanation of changes, the path and a Markdown Code Block with truncated contents—unchanged parts should be replaced by ellipsis comments, e.g. \"// ...\".",
587-
"diff": "Whenever showing a new or updated file, include a brief explanation of changes, and print them in the Unified Diff format (that's compatible with the git apply command)."
585+
"whole": "Whenever showing a new or updated file, print the path and a Markdown Code Block with file's full contents. I have a disability which means I can't type and need to be able to just copy and paste. Briefly explain your reasoning, when you deem appropriate.",
586+
"truncated": "Whenever showing a new or updated file, print the path and a Markdown Code Block with truncated contents. To save space, unchanged parts should be replaced by ellipsis comments, e.g. \"// ...\". Briefly explain your reasoning, when you deem appropriate.",
587+
"diff": "Whenever showing a new or updated file, use the the Unified Diff format (that's compatible with the git apply command). Each diff should be placed in a Markdown Code Block. Briefly explain your reasoning, when you deem appropriate."
588588
},
589589
"properties": {
590590
"whole": {

0 commit comments

Comments
 (0)