Skip to content

Commit 413e875

Browse files
committed
Refactor file matching hint logic into a dedicated utility function and use it consistently across prompt field components to handle file path auto-completion
1 parent 9ef4737 commit 413e875

4 files changed

Lines changed: 127 additions & 133 deletions

File tree

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

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { use_ghost_text } from './hooks/use-ghost-text'
99
import { use_drag_drop } from './hooks/use-drag-drop'
1010
import { DropdownMenu } from '../../common/DropdownMenu'
1111
import { use_is_narrow_viewport, use_is_mac } from '@shared/hooks'
12-
import { search_paths } from '@shared/utils/search-paths'
1312
import { get_display_text } from './utils/get-display-text'
1413
import {
1514
get_caret_position_from_div,
@@ -20,6 +19,7 @@ import {
2019
map_raw_pos_to_display_pos
2120
} from './utils/position-mapping'
2221
import { dictionary } from '@shared/constants/dictionary'
22+
import { get_file_match_hint_data } from './utils/get-file-match-hint-data'
2323

2424
const Tooltip: React.FC<{
2525
message: string
@@ -83,6 +83,11 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
8383
const [show_submit_tooltip, set_show_submit_tooltip] = useState(false)
8484
const [is_text_selecting, set_is_text_selecting] = useState(false)
8585
const [is_focused, set_is_focused] = useState(false)
86+
const [show_file_match_hint, set_show_file_match_hint] = useState(false)
87+
const file_match_hint_timer_ref = useRef<ReturnType<typeof setTimeout> | null>(null)
88+
const has_shown_hint_once_ref = useRef(false)
89+
const last_matching_word_ref = useRef<string | null>(null)
90+
8691
const is_narrow_viewport = use_is_narrow_viewport(268)
8792
const {
8893
is_dropdown_open,
@@ -151,46 +156,47 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
151156
return get_display_text(props.value, props.context_file_paths ?? [])
152157
}, [props.value, props.context_file_paths])
153158

154-
const show_file_match_hint = useMemo(() => {
155-
const value = display_text
156-
if (
157-
!value ||
158-
caret_position != value.length ||
159-
value.endsWith(' ') ||
160-
value.endsWith('\n') ||
161-
!props.context_file_paths
162-
) {
163-
return false
164-
}
165-
166-
// Check if cursor is at the end of a shortened filename
167-
const text_before_cursor = value.substring(0, caret_position)
168-
const filename_match = text_before_cursor.match(
169-
/([^\s,;:!?`]*\.[^\s,;:!?`]+)$/
159+
const should_show_hint = useMemo(() => {
160+
const hint_data = get_file_match_hint_data(
161+
display_text,
162+
caret_position,
163+
props.context_file_paths
170164
)
165+
return hint_data ? hint_data.word : false
166+
}, [display_text, caret_position, props.context_file_paths])
171167

172-
if (filename_match) {
173-
const filename = filename_match[1]
174-
const is_shortened_filename = props.context_file_paths.some(
175-
(path) => path.endsWith('/' + filename) || path === filename
176-
)
177-
if (is_shortened_filename) {
178-
return false
179-
}
168+
useEffect(() => {
169+
if (file_match_hint_timer_ref.current) {
170+
clearTimeout(file_match_hint_timer_ref.current)
171+
file_match_hint_timer_ref.current = null
180172
}
181173

182-
const last_word = value.trim().split(/\s+/).pop()
183-
184-
if (last_word && last_word.length >= 3) {
185-
const matching_paths = search_paths({
186-
paths: props.context_file_paths,
187-
search_value: last_word
188-
})
189-
return matching_paths.length == 1
174+
if (should_show_hint) {
175+
const matching_word = should_show_hint as string
176+
const is_same_word = matching_word === last_matching_word_ref.current
177+
if (has_shown_hint_once_ref.current && is_same_word) {
178+
set_show_file_match_hint(true)
179+
} else {
180+
file_match_hint_timer_ref.current = setTimeout(() => {
181+
set_show_file_match_hint(true)
182+
has_shown_hint_once_ref.current = true
183+
last_matching_word_ref.current = matching_word
184+
}, 300)
185+
}
186+
} else {
187+
set_show_file_match_hint(false)
188+
if (last_matching_word_ref.current !== null) {
189+
has_shown_hint_once_ref.current = false
190+
last_matching_word_ref.current = null
191+
}
190192
}
191193

192-
return false
193-
}, [display_text, caret_position, props.context_file_paths])
194+
return () => {
195+
if (file_match_hint_timer_ref.current) {
196+
clearTimeout(file_match_hint_timer_ref.current)
197+
}
198+
}
199+
}, [should_show_hint])
194200

195201
const highlighted_html = useMemo(() => {
196202
return get_highlighted_text({

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const use_drag_drop = ({
3434
const input_element = input_ref.current
3535
if (!input_element || !input_element.contains(range.startContainer)) return
3636

37-
// Calculate display positions
3837
const pre_selection_range = document.createRange()
3938
pre_selection_range.selectNodeContents(input_element)
4039
pre_selection_range.setEnd(range.startContainer, range.startOffset)
@@ -43,7 +42,6 @@ export const use_drag_drop = ({
4342
pre_selection_range.setEnd(range.endContainer, range.endOffset)
4443
const display_end = pre_selection_range.toString().length
4544

46-
// Map to raw positions
4745
const raw_start = map_display_pos_to_raw_pos(
4846
display_start,
4947
value,
@@ -89,7 +87,6 @@ export const use_drag_drop = ({
8987
const dragged_range = dragged_text_range_ref.current
9088
dragged_text_range_ref.current = null
9189

92-
// Find drop position
9390
const range = document.caretRangeFromPoint(e.clientX, e.clientY)
9491
if (!range) return
9592

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

Lines changed: 33 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
map_display_pos_to_raw_pos,
99
map_raw_pos_to_display_pos
1010
} from '../utils/position-mapping'
11-
import { search_paths } from '@shared/utils/search-paths'
11+
import { get_file_match_hint_data } from '../utils/get-file-match-hint-data'
1212

1313
type HistoryEntry = {
1414
value: string
@@ -40,50 +40,6 @@ const getKeywordRanges = (
4040
return ranges
4141
}
4242

43-
const should_show_file_match_hint = (
44-
value: string,
45-
caret_position: number,
46-
context_file_paths: string[] | undefined
47-
): boolean => {
48-
if (
49-
!value ||
50-
caret_position !== value.length ||
51-
value.endsWith(' ') ||
52-
value.endsWith('\n') ||
53-
!context_file_paths
54-
) {
55-
return false
56-
}
57-
58-
// Check if cursor is at the end of a shortened filename
59-
const text_before_cursor = value.substring(0, caret_position)
60-
const filename_match = text_before_cursor.match(
61-
/([^\s,;:!?`]*\.[^\s,;:!?`]+)$/
62-
)
63-
64-
if (filename_match) {
65-
const filename = filename_match[1]
66-
const is_shortened_filename = context_file_paths.some(
67-
(path) => path.endsWith('/' + filename) || path === filename
68-
)
69-
if (is_shortened_filename) {
70-
return false
71-
}
72-
}
73-
74-
const last_word = value.trim().split(/\s+/).pop()
75-
76-
if (last_word && last_word.length >= 3) {
77-
const matching_paths = search_paths({
78-
paths: context_file_paths,
79-
search_value: last_word
80-
})
81-
return matching_paths.length === 1
82-
}
83-
84-
return false
85-
}
86-
8743
const reconstruct_raw_value_from_node = (node: Node): string => {
8844
if (node.nodeType == Node.TEXT_NODE) {
8945
return node.textContent || ''
@@ -239,7 +195,6 @@ export const use_handlers = (
239195
let leading_part = props.value.substring(0, start_pos)
240196
let trailing_part = props.value.substring(end_pos)
241197

242-
// Handle spacing
243198
if (leading_part.endsWith(' ')) {
244199
leading_part = leading_part.slice(0, -1)
245200
} else if (trailing_part.startsWith(' ')) {
@@ -424,38 +379,29 @@ export const use_handlers = (
424379
0,
425380
caret_position - 1
426381
)
427-
const show_hint = should_show_file_match_hint(
382+
const hint_data = get_file_match_hint_data(
428383
display_value_before_backslash,
429384
caret_position - 1,
430385
props.context_file_paths
431386
)
432387

433-
if (show_hint) {
434-
const last_word = display_value_before_backslash.trim().split(/\s+/).pop()
435-
if (last_word) {
436-
const matching_paths = search_paths({
437-
paths: props.context_file_paths ?? [],
438-
search_value: last_word
439-
})
440-
if (matching_paths.length === 1) {
441-
const matched_path = matching_paths[0]
442-
const old_raw_value = props.value
443-
444-
const last_word_in_raw_index = old_raw_value.lastIndexOf(last_word)
445-
if (
446-
last_word_in_raw_index !== -1 &&
447-
old_raw_value.endsWith(last_word)
448-
) {
449-
const value_before = old_raw_value.substring(
450-
0,
451-
last_word_in_raw_index
452-
)
453-
const final_new_value = `${value_before}\`${matched_path}\` `
454-
update_value(final_new_value, final_new_value.length)
455-
set_history_index(-1)
456-
return
457-
}
458-
}
388+
if (hint_data) {
389+
const { word: last_word, path: matched_path } = hint_data
390+
const old_raw_value = props.value
391+
392+
const last_word_in_raw_index = old_raw_value.lastIndexOf(last_word)
393+
if (
394+
last_word_in_raw_index !== -1 &&
395+
old_raw_value.endsWith(last_word)
396+
) {
397+
const value_before = old_raw_value.substring(
398+
0,
399+
last_word_in_raw_index
400+
)
401+
const final_new_value = `${value_before}\`${matched_path}\` `
402+
update_value(final_new_value, final_new_value.length)
403+
set_history_index(-1)
404+
return
459405
}
460406
}
461407
}
@@ -468,15 +414,14 @@ export const use_handlers = (
468414
let is_after_non_word_char = false
469415
if (caret_position > 1) {
470416
const char_before_at = new_display_value.charAt(caret_position - 2)
471-
// Check if the character before '@' is not a word character (a-z, A-Z, 0-9, _)
472417
is_after_non_word_char = !/\w/.test(char_before_at)
473418
}
474419

475420
if (is_at_start || is_after_non_word_char) {
476-
props.on_at_sign_click()
421+
props.on_at_sign_click()
477422
}
478423
} else if (char_before_caret == '#') {
479-
props.on_hash_sign_click()
424+
props.on_hash_sign_click()
480425
}
481426
}
482427

@@ -641,12 +586,6 @@ export const use_handlers = (
641586
return false
642587
}
643588

644-
const handle_tab_key = (e: React.KeyboardEvent<HTMLDivElement>) => {
645-
if (!input_ref.current) return
646-
e.preventDefault()
647-
props.on_at_sign_click()
648-
}
649-
650589
const handle_backspace_key = (e: React.KeyboardEvent<HTMLDivElement>) => {
651590
const selection = window.getSelection()
652591
if (
@@ -662,10 +601,7 @@ export const use_handlers = (
662601
const { startContainer, startOffset } = range
663602
let node_before_cursor: Node | null = null
664603

665-
if (
666-
startContainer.nodeType == Node.TEXT_NODE &&
667-
startOffset == 0
668-
) {
604+
if (startContainer.nodeType == Node.TEXT_NODE && startOffset == 0) {
669605
node_before_cursor = startContainer.previousSibling
670606
} else if (
671607
startContainer.nodeType == Node.ELEMENT_NODE &&
@@ -769,15 +705,17 @@ export const use_handlers = (
769705
if (ghost_node) {
770706
const { startContainer, startOffset } = range
771707

772-
// Check if we're at the position just before ghost text
773708
let is_before_ghost = false
774709

775710
if (startContainer === ghost_node.previousSibling) {
776711
if (startContainer.nodeType === Node.TEXT_NODE) {
777-
is_before_ghost = startOffset === startContainer.textContent?.length
712+
is_before_ghost =
713+
startOffset === startContainer.textContent?.length
778714
}
779715
} else if (startContainer === ghost_node.parentNode) {
780-
const ghost_index = Array.from(startContainer.childNodes).indexOf(ghost_node as ChildNode)
716+
const ghost_index = Array.from(startContainer.childNodes).indexOf(
717+
ghost_node as ChildNode
718+
)
781719
is_before_ghost = startOffset === ghost_index
782720
}
783721

@@ -799,12 +737,16 @@ export const use_handlers = (
799737
const { value, context_file_paths = [] } = props
800738
let i = raw_pos - 1
801739

802-
// First, skip any whitespace characters before the cursor.
803740
while (i >= 0 && /\s/.test(value[i])) {
804741
i--
805742
}
806743
if (i < 0) {
807-
set_caret_position_after_change(input_ref, 0, value, context_file_paths)
744+
set_caret_position_after_change(
745+
input_ref,
746+
0,
747+
value,
748+
context_file_paths
749+
)
808750
return
809751
}
810752

@@ -849,7 +791,6 @@ export const use_handlers = (
849791

850792
let i = raw_pos
851793

852-
// Skip any whitespace characters after the cursor.
853794
while (i < value.length && /\s/.test(value[i])) {
854795
i++
855796
}
@@ -935,9 +876,7 @@ export const use_handlers = (
935876
if (ghost_text) {
936877
e.preventDefault()
937878
on_accept_ghost_text()
938-
return
939879
}
940-
handle_tab_key(e)
941880
return
942881
}
943882

@@ -950,7 +889,6 @@ export const use_handlers = (
950889
const value = props.value
951890
let i = raw_pos - 1
952891

953-
// First, skip any whitespace characters before the cursor.
954892
while (i >= 0 && /\s/.test(value[i])) {
955893
i--
956894
}

0 commit comments

Comments
 (0)