Skip to content

Commit 3b3e4c6

Browse files
committed
Enable file path highlighting in the chat input based on currently checked context files
1 parent 82550f3 commit 3b3e4c6

9 files changed

Lines changed: 89 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const Empty = () => (
2222
on_hash_sign_click={() => console.log('# clicked')}
2323
on_search_click={() => console.log('Search clicked')}
2424
on_submit_with_control={() => console.log('Submitted with control')}
25+
context_file_paths={[]}
2526
on_curly_braces_click={() => {}}
2627
/>
2728
)
@@ -43,6 +44,7 @@ export const WithText = () => (
4344
on_hash_sign_click={() => console.log('# clicked')}
4445
on_search_click={() => console.log('Search clicked')}
4546
on_submit_with_control={() => console.log('Submitted with control')}
47+
context_file_paths={[]}
4648
on_curly_braces_click={() => {}}
4749
/>
4850
)
@@ -64,6 +66,7 @@ export const LongText = () => (
6466
on_hash_sign_click={() => console.log('# clicked')}
6567
on_search_click={() => console.log('Search clicked')}
6668
on_submit_with_control={() => console.log('Submitted with control')}
69+
context_file_paths={[]}
6770
on_curly_braces_click={() => {}}
6871
/>
6972
)
@@ -85,6 +88,7 @@ export const WithPlaceholderSavedContext = () => (
8588
on_hash_sign_click={() => console.log('# clicked')}
8689
is_web_mode={false}
8790
on_submit_with_control={() => console.log('Submitted with control')}
91+
context_file_paths={[]}
8892
on_curly_braces_click={() => {}}
8993
/>
9094
)
@@ -106,6 +110,7 @@ export const WithPlaceholderSelection = () => (
106110
on_hash_sign_click={() => console.log('# clicked')}
107111
is_web_mode={false}
108112
on_submit_with_control={() => console.log('Submitted with control')}
113+
context_file_paths={[]}
109114
on_curly_braces_click={() => {}}
110115
/>
111116
)
@@ -138,6 +143,29 @@ export const WithEditFormatSelector = () => {
138143
truncated: 'Use truncated...',
139144
diff: 'Use diff...'
140145
}}
146+
context_file_paths={[]}
141147
/>
142148
)
143149
}
150+
151+
export const WithFilePaths = () => (
152+
<ChatInput
153+
value="This is about `path/to/my/file.ts` and not about `another/file.txt`"
154+
chat_history={[]}
155+
on_change={(value) => console.log('Changed:', value)}
156+
on_submit={() => console.log('Submitted')}
157+
on_copy={() => console.log('Copied')}
158+
is_connected={true}
159+
is_in_code_completions_mode={false}
160+
has_active_selection={false}
161+
has_active_editor={true}
162+
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
163+
is_web_mode={false}
164+
on_at_sign_click={() => console.log('@ clicked')}
165+
on_hash_sign_click={() => console.log('# clicked')}
166+
on_search_click={() => console.log('Search clicked')}
167+
on_submit_with_control={() => console.log('Submitted with control')}
168+
on_curly_braces_click={() => {}}
169+
context_file_paths={['path/to/my/file.ts']}
170+
/>
171+
)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type ChatInputProps = {
3636
edit_format?: EditFormat
3737
on_edit_format_change?: (format: EditFormat) => void
3838
edit_format_instructions?: Record<EditFormat, string>
39+
context_file_paths?: string[]
3940
}
4041

4142
export const ChatInput: React.FC<ChatInputProps> = (props) => {
@@ -196,7 +197,8 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
196197
{get_highlighted_text({
197198
text: props.value,
198199
is_in_code_completions_mode: props.is_in_code_completions_mode,
199-
has_active_selection: props.has_active_selection
200+
has_active_selection: props.has_active_selection,
201+
context_file_paths: props.context_file_paths ?? []
200202
})}
201203
</div>
202204
<TextareaAutosize

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const get_highlighted_text = (params: {
55
text: string
66
is_in_code_completions_mode: boolean
77
has_active_selection: boolean
8+
context_file_paths: string[]
89
}) => {
910
if (params.is_in_code_completions_mode) {
1011
const regex = /(#SavedContext:(?:WorkspaceState|JSON)\s+"[^"]+")/g
@@ -58,8 +59,15 @@ export const get_highlighted_text = (params: {
5859
)
5960
}
6061
if (part && /^`[^\s`]*\.[^\s`]+`$/.test(part)) {
62+
const file_path = part.slice(1, -1)
6163
return (
62-
<span key={index} className={styles['selection-keyword']}>
64+
<span
65+
key={index}
66+
className={cn({
67+
[styles['selection-keyword']]:
68+
params.context_file_paths.includes(file_path)
69+
})}
70+
>
6371
{part}
6472
</span>
6573
)

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ export class PanelProvider implements vscode.WebviewViewProvider {
216216
this.calculate_token_count()
217217
}
218218
})
219+
this.workspace_provider.onDidChangeCheckedFiles(() =>
220+
this.send_context_files()
221+
)
219222

220223
this.context.subscriptions.push(this._config_listener)
221224

@@ -308,6 +311,35 @@ export class PanelProvider implements vscode.WebviewViewProvider {
308311
this.intelligent_update_cancel_token_sources = []
309312
}
310313

314+
public send_context_files() {
315+
const workspace_files = this.workspace_provider.get_checked_files()
316+
317+
const is_multi_root = this.workspace_provider.getWorkspaceRoots().length > 1
318+
319+
const file_paths = workspace_files.map((file_path) => {
320+
const workspace_root =
321+
this.workspace_provider.get_workspace_root_for_file(file_path)
322+
if (!workspace_root) {
323+
return file_path.replace(/\\/g, '/') // Should not happen for context files
324+
}
325+
const relative_path = path
326+
.relative(workspace_root, file_path)
327+
.replace(/\\/g, '/')
328+
329+
if (is_multi_root) {
330+
const workspace_name =
331+
this.workspace_provider.get_workspace_name(workspace_root)
332+
return `${workspace_name}/${relative_path}`
333+
}
334+
return relative_path
335+
})
336+
337+
this.send_message({
338+
command: 'CONTEXT_FILES',
339+
file_paths
340+
})
341+
}
342+
311343
public send_message(message: BackendMessage) {
312344
if (this._webview_view) {
313345
this._webview_view.webview.postMessage(message)
@@ -520,6 +552,7 @@ export class PanelProvider implements vscode.WebviewViewProvider {
520552
command: 'CAN_UNDO_CHANGED',
521553
can_undo: this._can_undo()
522554
})
555+
this.send_context_files()
523556
}, 1000)
524557
}
525558

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Props = {
5353
chat_input_focus_and_select_key: number
5454
chat_input_focus_key: number
5555
context_size_warning_threshold: number
56+
context_file_paths: string[]
5657
}
5758

5859
export const Main: React.FC<Props> = (props) => {
@@ -673,6 +674,7 @@ export const Main: React.FC<Props> = (props) => {
673674
on_response_history_item_click={handle_response_history_item_click}
674675
selected_history_item_created_at={props.selected_history_item_created_at}
675676
on_selected_history_item_change={props.on_selected_history_item_change}
677+
context_file_paths={props.context_file_paths}
676678
/>
677679
)
678680
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ type Props = {
100100
}) => void
101101
selected_history_item_created_at?: number
102102
on_selected_history_item_change: (created_at: number) => void
103+
context_file_paths: string[]
103104
}
104105

105106
export const MainView: React.FC<Props> = (props) => {
@@ -280,6 +281,7 @@ export const MainView: React.FC<Props> = (props) => {
280281
: props.on_api_edit_format_change
281282
}
282283
edit_format_instructions={props.edit_format_instructions}
284+
context_file_paths={props.context_file_paths}
283285
/>
284286
<UiContextUtilisation
285287
current_context_size={props.token_count}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const Panel = () => {
6767
context_size_warning_threshold,
6868
has_changes_to_commit,
6969
can_undo,
70+
context_file_paths,
7071
handle_instructions_change,
7172
edit_preset_back_click_handler,
7273
edit_preset_save_handler,
@@ -213,6 +214,7 @@ export const Panel = () => {
213214
}
214215
chat_input_focus_key={chat_input_focus_key}
215216
context_size_warning_threshold={context_size_warning_threshold}
217+
context_file_paths={context_file_paths}
216218
/>
217219
</div>
218220
<div

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const use_panel = (vscode: any) => {
8686
const [has_changes_to_commit, set_has_changes_to_commit] =
8787
useState<boolean>(false)
8888
const [can_undo, set_can_undo] = useState<boolean>(false)
89+
const [context_file_paths, set_context_file_paths] = useState<string[]>([])
8990

9091
const handle_instructions_change = (
9192
value: string,
@@ -131,6 +132,8 @@ export const use_panel = (vscode: any) => {
131132
set_has_active_editor(message.has_active_editor)
132133
} else if (message.command == 'EDITOR_SELECTION_CHANGED') {
133134
set_has_active_selection(message.has_selection)
135+
} else if (message.command == 'CONTEXT_FILES') {
136+
set_context_file_paths(message.file_paths)
134137
} else if (message.command == 'RESPONSE_PREVIEW_STARTED') {
135138
set_items_to_review(message.items)
136139
set_raw_instructions(message.raw_instructions)
@@ -413,6 +416,7 @@ export const use_panel = (vscode: any) => {
413416
context_size_warning_threshold,
414417
has_changes_to_commit,
415418
can_undo,
419+
context_file_paths,
416420
handle_instructions_change,
417421
edit_preset_back_click_handler,
418422
edit_preset_save_handler,

‎packages/vscode/src/views/panel/types/messages.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@ export interface CanUndoChangedMessage extends BaseMessage {
495495
can_undo: boolean
496496
}
497497

498+
export interface ContextFilesMessage extends BaseMessage {
499+
command: 'CONTEXT_FILES'
500+
file_paths: string[]
501+
}
502+
498503
export interface ResponsePreviewStartedMessage extends BaseMessage {
499504
command: 'RESPONSE_PREVIEW_STARTED'
500505
items: ItemInPreview[]
@@ -599,6 +604,7 @@ export type BackendMessage =
599604
| WebModeMessage
600605
| ApiModeMessage
601606
| VersionMessage
607+
| ContextFilesMessage
602608
| CanUndoChangedMessage
603609
| ResponsePreviewStartedMessage
604610
| ResponsePreviewFinishedMessage

0 commit comments

Comments
 (0)