Skip to content

Commit 075d867

Browse files
committed
Implement Ctrl/Cmd + Arrow key navigation to enable word-jumping behavior that correctly respects keyword boundaries in the prompt field
1 parent 424e424 commit 075d867

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ type HistoryEntry = {
1515
raw_caret_pos: number
1616
}
1717

18+
const getKeywordRanges = (
19+
text: string,
20+
context_file_paths: string[]
21+
): { start: number; end: number }[] => {
22+
const ranges: { start: number; end: number }[] = []
23+
// This regex is a combination of all keyword types
24+
const regex =
25+
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#Selection)|(#SavedContext:(?:WorkspaceState|JSON)\s+"[^"]+")|(#Commit:[^:]+:[^\s"]+\s+"[^"]*")/g
26+
27+
let match
28+
while ((match = regex.exec(text)) !== null) {
29+
const file_path = match[1]
30+
31+
if (file_path) {
32+
// Only treat file paths as keywords if they are in the context
33+
if (context_file_paths.includes(file_path)) {
34+
ranges.push({ start: match.index, end: match.index + match[0].length })
35+
}
36+
} else {
37+
ranges.push({ start: match.index, end: match.index + match[0].length })
38+
}
39+
}
40+
return ranges
41+
}
42+
1843
const should_show_file_match_hint = (
1944
value: string,
2045
caret_position: number,
@@ -763,6 +788,104 @@ export const use_handlers = (
763788
}
764789
}
765790

791+
if ((e.ctrlKey || e.metaKey) && !shiftKey) {
792+
if (key == 'ArrowLeft') {
793+
e.preventDefault()
794+
const raw_pos = raw_caret_pos_ref.current
795+
if (raw_pos === 0) return
796+
797+
const { value, context_file_paths = [] } = props
798+
let i = raw_pos - 1
799+
800+
// First, skip any whitespace characters before the cursor.
801+
while (i >= 0 && /\s/.test(value[i])) {
802+
i--
803+
}
804+
if (i < 0) {
805+
set_caret_position_after_change(input_ref, 0, value, context_file_paths)
806+
return
807+
}
808+
809+
const keyword_ranges = getKeywordRanges(value, context_file_paths)
810+
let new_raw_pos: number | undefined
811+
812+
// Check if cursor is within a keyword; if so, jump to its start.
813+
for (const range of keyword_ranges) {
814+
if (i >= range.start && i < range.end) {
815+
new_raw_pos = range.start
816+
break
817+
}
818+
}
819+
820+
if (new_raw_pos === undefined) {
821+
// Default word-jump behavior.
822+
const is_word_char = /\w/.test(value[i])
823+
while (i >= 0) {
824+
const current_is_word = /\w/.test(value[i])
825+
if (/\s/.test(value[i]) || current_is_word !== is_word_char) {
826+
break
827+
}
828+
i--
829+
}
830+
new_raw_pos = i + 1
831+
}
832+
833+
set_caret_position_after_change(
834+
input_ref,
835+
new_raw_pos,
836+
value,
837+
context_file_paths
838+
)
839+
return
840+
}
841+
842+
if (key == 'ArrowRight') {
843+
e.preventDefault()
844+
const { value, context_file_paths = [] } = props
845+
const raw_pos = raw_caret_pos_ref.current
846+
if (raw_pos === value.length) return
847+
848+
let i = raw_pos
849+
850+
// Skip any whitespace characters after the cursor.
851+
while (i < value.length && /\s/.test(value[i])) {
852+
i++
853+
}
854+
855+
const keyword_ranges = getKeywordRanges(value, context_file_paths)
856+
let new_raw_pos: number | undefined
857+
858+
// Check if cursor is within a keyword; if so, jump to its end.
859+
for (const range of keyword_ranges) {
860+
if (i >= range.start && i < range.end) {
861+
new_raw_pos = range.end
862+
break
863+
}
864+
}
865+
866+
if (new_raw_pos === undefined) {
867+
// Default word-jump behavior.
868+
const is_word_char = /\w/.test(value[i])
869+
while (i < value.length) {
870+
const current_is_word = /\w/.test(value[i])
871+
if (/\s/.test(value[i]) || current_is_word !== is_word_char) {
872+
break
873+
}
874+
i++
875+
}
876+
new_raw_pos = i
877+
}
878+
879+
set_caret_position_after_change(
880+
input_ref,
881+
new_raw_pos,
882+
value,
883+
context_file_paths
884+
)
885+
return
886+
}
887+
}
888+
766889
if ((e.ctrlKey || e.metaKey) && !shiftKey && key.toLowerCase() === 'z') {
767890
e.preventDefault()
768891
if (undo_stack.length > 0) {

0 commit comments

Comments
 (0)