Skip to content

Commit a8431c3

Browse files
committed
Implement file path highlighting in the chat input, enabling users to click file paths to navigate to the file and click an icon to remove the path from the prompt
1 parent b3a7f7a commit a8431c3

8 files changed

Lines changed: 96 additions & 32 deletions

File tree

‎packages/ui/src/components/editor/panel/ChatInput/ChatInput.module.scss‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,37 @@
135135

136136
.file-keyword {
137137
font-size: var(--font-size-12px);
138-
background-color: var(--vscode-editor-selectionBackground);
138+
background-color: color-mix(
139+
in srgb,
140+
var(--vscode-charts-blue) 20%,
141+
transparent
142+
);
139143
border-radius: var(--border-radius-4px);
140144
padding: 0 var(--padding-4px) 0 var(--padding-3px);
141145
display: inline-flex;
142146
align-items: center;
143-
cursor: pointer;
147+
gap: var(--padding-3px);
148+
line-height: 1.2;
149+
transform: translateY(1.5px);
144150

145-
&::before {
146-
font-family: 'codicon';
147-
content: '\ea7b';
148-
margin-right: var(--padding-3px);
149-
font-size: var(--font-size-12px);
151+
&__icon {
152+
display: flex;
153+
154+
&::before {
155+
font-family: 'codicon';
156+
content: '\ea7b';
157+
font-size: var(--font-size-12px);
158+
transition: none;
159+
}
160+
}
161+
162+
&:hover &__icon::before {
163+
content: '\ea76';
164+
cursor: pointer;
165+
}
166+
167+
&__text {
168+
cursor: pointer;
150169
}
151170
}
152171

‎packages/ui/src/components/editor/panel/ChatInput/ChatInput.stories.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export const WithFilePaths = () => (
166166
on_search_click={() => console.log('Search clicked')}
167167
on_submit_with_control={() => console.log('Submitted with control')}
168168
on_curly_braces_click={() => {}}
169+
on_go_to_file={(path) => console.log('Go to file:', path)}
169170
context_file_paths={['path/to/my/file.ts']}
170171
/>
171172
)

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

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type ChatInputProps = {
3838
on_edit_format_change?: (format: EditFormat) => void
3939
edit_format_instructions?: Record<EditFormat, string>
4040
context_file_paths?: string[]
41+
on_go_to_file?: (file_path: string) => void
4142
}
4243

4344
const get_caret_position_from_div = (element: HTMLElement): number => {
@@ -317,6 +318,14 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
317318
}
318319
}, [props.focus_and_select_key])
319320

321+
useEffect(() => {
322+
const input_element = input_ref.current
323+
if (input_element) {
324+
const is_focused = document.activeElement === input_element
325+
if (!is_focused) input_element.focus()
326+
}
327+
}, [props.value])
328+
320329
useEffect(() => {
321330
const on_selection_change = () => {
322331
if (document.activeElement === input_ref.current && input_ref.current) {
@@ -357,28 +366,27 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
357366

358367
const custom_handle_key_down = (e: React.KeyboardEvent<HTMLDivElement>) => {
359368
if (e.key == 'Tab' && !e.shiftKey) {
360-
const value = e.currentTarget.innerText
369+
const value = e.currentTarget.textContent ?? ''
361370
const selection_start = caret_position
371+
const text_before_cursor = value.substring(0, selection_start)
362372

363-
if (selection_start > 0) {
364-
const text_before_cursor = value.substring(0, selection_start)
365-
if (
366-
text_before_cursor.trim() != '' &&
367-
!/\s$/.test(text_before_cursor)
368-
) {
369-
e.preventDefault()
370-
const match = text_before_cursor.match(/(\S+)$/)
371-
if (match) {
372-
props.on_at_sign_click(match[1])
373-
return
374-
}
375-
}
373+
if (selection_start == 0 || /\s$/.test(text_before_cursor)) {
374+
e.preventDefault()
375+
props.on_at_sign_click()
376+
return
377+
}
378+
379+
e.preventDefault()
380+
const match = text_before_cursor.match(/(\S+)$/)
381+
if (match) {
382+
props.on_at_sign_click(match[1])
383+
return
376384
}
377385
}
378386

379387
// Handle backspace on shortened filenames
380388
if (e.key == 'Backspace' && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
381-
const display_value = e.currentTarget.innerText
389+
const display_value = e.currentTarget.textContent ?? ''
382390
const cursor_pos = caret_position
383391

384392
// Check if we're at the end of a word that looks like a filename
@@ -409,13 +417,17 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
409417

410418
const handle_input_click = (e: React.MouseEvent<HTMLDivElement>) => {
411419
const target = e.target as HTMLElement
412-
if (target.classList.contains(styles['file-keyword'])) {
420+
const icon_element = target.closest(`.${styles['file-keyword__icon']}`)
421+
const text_element = target.closest(`.${styles['file-keyword__text']}`)
422+
423+
if (icon_element) {
413424
e.preventDefault()
414425
e.stopPropagation()
415426

416427
const input_element = input_ref.current
417428
if (!input_element) return
418429

430+
// Logic for deletion based on caret position
419431
const display_pos = get_caret_position_from_div(input_element)
420432
const raw_pos = map_display_pos_to_raw_pos(
421433
display_pos,
@@ -426,20 +438,20 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
426438
let start_of_path = -1
427439
let end_of_path = -1
428440

429-
if (props.value[raw_pos] === '`') {
441+
if (props.value[raw_pos] == '`') {
430442
// case: raw_pos at start
431443
start_of_path = raw_pos
432444
end_of_path = props.value.indexOf('`', start_of_path + 1)
433-
} else if (raw_pos > 0 && props.value[raw_pos - 1] === '`') {
445+
} else if (raw_pos > 0 && props.value[raw_pos - 1] == '`') {
434446
// case: raw_pos at end
435447
end_of_path = raw_pos - 1
436448
const text_before = props.value.substring(0, end_of_path)
437449
start_of_path = text_before.lastIndexOf('`')
438450
}
439451

440452
if (
441-
start_of_path !== -1 &&
442-
end_of_path !== -1 &&
453+
start_of_path != -1 &&
454+
end_of_path != -1 &&
443455
start_of_path < end_of_path
444456
) {
445457
const path_in_backticks = props.value.substring(
@@ -473,6 +485,25 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
473485
}, 0)
474486
}
475487
}
488+
} else if (text_element) {
489+
// Clicking on file name text should open the file
490+
e.preventDefault()
491+
e.stopPropagation()
492+
493+
const filename = text_element.textContent
494+
if (filename && props.on_go_to_file && props.context_file_paths) {
495+
const file_path = props.context_file_paths.find(
496+
(p) => p.endsWith('/' + filename) || p == filename
497+
)
498+
if (file_path) {
499+
props.on_go_to_file(file_path)
500+
}
501+
}
502+
503+
// Keep focus on input
504+
if (input_ref.current) {
505+
input_ref.current.focus()
506+
}
476507
}
477508
}
478509

@@ -580,6 +611,7 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
580611
>
581612
<Icon variant="CURLY_BRACES" />
582613
</button>
614+
<span className={styles.icon}></span>
583615
</div>
584616
<div
585617
className={styles.footer__right}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const use_handlers = (props: ChatInputProps) => {
6868
}
6969

7070
const handle_input_change = (e: React.FormEvent<HTMLDivElement>) => {
71-
let new_display_value = e.currentTarget.innerText
71+
let new_display_value = e.currentTarget.textContent ?? ''
7272
if (new_display_value == '\n') {
7373
new_display_value = ''
7474
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ export const get_highlighted_text = (params: {
6868
(path) => path.endsWith('/' + sub_part) || path == sub_part
6969
)
7070
if (is_context_file) {
71-
return `<span class="${styles['file-keyword']}">${escape_html(
72-
sub_part
73-
)}</span>`
71+
return `<span class="${styles['file-keyword']}"><span class="${
72+
styles['file-keyword__icon']
73+
}"></span><span class="${
74+
styles['file-keyword__text']
75+
}">${escape_html(sub_part)}</span></span>`
7476
}
7577
}
7678
return escape_html(sub_part)

‎packages/vscode/src/views/panel/backend/message-handlers/handle-go-to-file.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const handle_go_to_file = async (
2222
target_workspace = workspace_folders.find(
2323
(folder) => folder.name == workspace_name
2424
)
25-
} else if (workspace_folders.length === 1) {
25+
} else if (workspace_folders.length == 1) {
2626
target_workspace = workspace_folders[0]
2727
} else {
2828
for (const folder of workspace_folders) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ export const Main: React.FC<Props> = (props) => {
596596
})
597597
}
598598

599+
const handle_go_to_file = (file_path: string) => {
600+
post_message(props.vscode, {
601+
command: 'GO_TO_FILE',
602+
file_path
603+
})
604+
}
605+
599606
const handle_response_history_item_click = (item: {
600607
response: string
601608
raw_instructions?: string
@@ -733,6 +740,7 @@ export const Main: React.FC<Props> = (props) => {
733740
on_configurations_collapsed_change={
734741
props.on_configurations_collapsed_change
735742
}
743+
on_go_to_file={handle_go_to_file}
736744
/>
737745
)
738746
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type Props = {
9797
on_presets_collapsed_change: (is_collapsed: boolean) => void
9898
configurations_collapsed: boolean
9999
on_configurations_collapsed_change: (is_collapsed: boolean) => void
100+
on_go_to_file: (file_path: string) => void
100101
}
101102

102103
export const MainView: React.FC<Props> = (props) => {
@@ -232,6 +233,7 @@ export const MainView: React.FC<Props> = (props) => {
232233
}
233234
edit_format_instructions={props.edit_format_instructions}
234235
context_file_paths={props.context_file_paths}
236+
on_go_to_file={props.on_go_to_file}
235237
/>
236238
<UiContextUtilisation
237239
current_context_size={props.token_count}

0 commit comments

Comments
 (0)