Skip to content

Commit 81c41fe

Browse files
committed
Add support for #Commit context keyword in the chat input, enabling users to reference specific Git commits for context
1 parent 63aad52 commit 81c41fe

16 files changed

Lines changed: 666 additions & 210 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@
204204
content: '\ebdb';
205205
}
206206

207+
&--commit &__icon::before {
208+
content: '\eafc';
209+
}
210+
207211
&--saved-context &__icon::before {
208212
content: '\eab3';
209213
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,28 @@ export const WithPlaceholderSelection = () => (
115115
/>
116116
)
117117

118+
export const WithCommit = () => (
119+
<ChatInput
120+
value='Ask about #Commit:my-repo:a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 "Initial commit"'
121+
chat_history={[]}
122+
on_change={(value) => console.log('Changed:', value)}
123+
on_submit={() => console.log('Submitted')}
124+
on_copy={() => console.log('Copied')}
125+
is_connected={true}
126+
is_in_code_completions_mode={false}
127+
has_active_editor={true}
128+
has_active_selection={false}
129+
on_caret_position_change={(pos) => console.log('Caret position:', pos)}
130+
on_search_click={() => console.log('Search clicked')}
131+
on_at_sign_click={() => console.log('@ clicked')}
132+
on_hash_sign_click={() => console.log('# clicked')}
133+
is_web_view_type={false}
134+
on_submit_with_control={() => console.log('Submitted with control')}
135+
context_file_paths={[]}
136+
on_curly_braces_click={() => {}}
137+
/>
138+
)
139+
118140
export const WithEditFormatSelector = () => {
119141
const [edit_format, set_edit_format] = useState<EditFormat>('diff')
120142
return (

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
347347
const search_pattern = '#Selection'
348348
const start_index = props.value.indexOf(search_pattern)
349349

350+
if (start_index !== -1) {
351+
apply_deletion(start_index, start_index + search_pattern.length)
352+
}
353+
}
354+
// Check if it's a commit keyword
355+
else if (keyword_element.classList.contains(styles['keyword--commit'])) {
356+
const repo_name = keyword_element.dataset.repoName
357+
const commit_hash = keyword_element.dataset.commitHash
358+
const commit_message = keyword_element.dataset.commitMessage
359+
if (!repo_name || !commit_hash || commit_message === undefined) return
360+
361+
const search_pattern = `#Commit:${repo_name}:${commit_hash} "${commit_message}"`
362+
const start_index = props.value.indexOf(search_pattern)
363+
350364
if (start_index !== -1) {
351365
apply_deletion(start_index, start_index + search_pattern.length)
352366
}

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
3636
}
3737
case 'selection-keyword':
3838
return '#Selection'
39+
case 'commit-keyword': {
40+
const repo_name = el.dataset.repoName
41+
const commit_hash = el.dataset.commitHash
42+
const commit_message = el.dataset.commitMessage
43+
return repo_name && commit_hash && commit_message !== undefined
44+
? `#Commit:${repo_name}:${commit_hash} "${commit_message}"`
45+
: ''
46+
}
3947
}
4048

4149
if (el.childNodes.length > 0) {
@@ -255,6 +263,32 @@ export const use_handlers = (
255263
return false
256264
}
257265

266+
const handle_commit_keyword_deletion = (
267+
raw_pos: number,
268+
context_file_paths: string[]
269+
): boolean => {
270+
const text_before_cursor = props.value.substring(0, raw_pos)
271+
const match = text_before_cursor.match(/#Commit:[^:]+:[^\s"]+\s+"[^"]*"$/)
272+
273+
if (match) {
274+
const start_of_match = raw_pos - match[0].length
275+
const new_value =
276+
props.value.substring(0, start_of_match) +
277+
props.value.substring(raw_pos)
278+
const new_raw_cursor_pos = start_of_match
279+
props.on_change(new_value)
280+
281+
set_caret_position_after_change(
282+
input_ref,
283+
new_raw_cursor_pos,
284+
new_value,
285+
context_file_paths
286+
)
287+
return true
288+
}
289+
return false
290+
}
291+
258292
const handle_keyword_deletion_by_backspace = (
259293
el: HTMLElement,
260294
display_pos: number
@@ -282,6 +316,10 @@ export const use_handlers = (
282316
return handle_selection_keyword_deletion(raw_pos, context_file_paths)
283317
}
284318

319+
if (el.dataset.type === 'commit-keyword') {
320+
return handle_commit_keyword_deletion(raw_pos, context_file_paths)
321+
}
322+
285323
return false
286324
}
287325

@@ -341,7 +379,8 @@ export const use_handlers = (
341379
el.dataset.type === 'file-keyword' ||
342380
el.dataset.type === 'changes-keyword' ||
343381
el.dataset.type === 'selection-keyword' ||
344-
el.dataset.type === 'saved-context-keyword'
382+
el.dataset.type === 'saved-context-keyword' ||
383+
el.dataset.type === 'commit-keyword'
345384
) {
346385
const display_pos = get_caret_position_from_div(input_ref.current)
347386
if (handle_keyword_deletion_by_backspace(el, display_pos)) {

‎packages/ui/src/components/editor/panel/ChatInput/utils/get-display-text.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ export const get_display_text = (
33
context_file_paths: string[]
44
): string => {
55
const regex =
6-
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#SavedContext:(?:WorkspaceState|JSON)\s+"([^"]+)")/g
6+
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#SavedContext:(?:WorkspaceState|JSON)\s+"([^"]+)")|(#Commit:[^:]+:([^\s"]+)\s+"[^"]*")/g
77
return text.replace(
88
regex,
99
(
1010
match,
1111
file_path,
1212
changes_keyword,
1313
saved_context_keyword,
14-
context_name
14+
context_name,
15+
commit_keyword,
16+
commit_hash
1517
) => {
1618
if (file_path) {
1719
if (context_file_paths.includes(file_path)) {
@@ -27,6 +29,10 @@ export const get_display_text = (
2729
if (saved_context_keyword) {
2830
return `Context "${context_name}"`
2931
}
32+
if (commit_keyword) {
33+
const short_hash = commit_hash.substring(0, 7)
34+
return short_hash
35+
}
3036
return match
3137
}
3238
)

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ export const get_highlighted_text = (params: {
6666
.join('')
6767
}
6868

69+
const commit_regex_part = '#Commit:[^:]+:[^\\s"]+\\s+"[^"]*"'
6970
const regex = new RegExp(
70-
`(#Selection|#Changes:[^\\s,;:!?]+|${saved_context_regex_part})`,
71+
`(#Selection|#Changes:[^\\s,;:!?]+|${saved_context_regex_part}|${commit_regex_part})`,
7172
'g'
7273
)
7374
const parts = params.text.split(regex)
@@ -109,6 +110,21 @@ export const get_highlighted_text = (params: {
109110
styles['keyword__text']
110111
}">Context "${context_name}"</span></span>`
111112
}
113+
const commit_match = part.match(/^#Commit:([^:]+):([^\s"]+)\s+"([^"]*)"$/)
114+
if (part && commit_match) {
115+
const repo_name = commit_match[1]
116+
const commit_hash = commit_match[2]
117+
const commit_message = commit_match[3]
118+
const short_hash = commit_hash.substring(0, 7)
119+
return `<span class="${cn(
120+
styles['keyword'],
121+
styles['keyword--commit']
122+
)}" contentEditable="false" data-type="commit-keyword" data-repo-name="${repo_name}" data-commit-hash="${commit_hash}" data-commit-message="${commit_message}" title="${commit_message}"><span class="${
123+
styles['keyword__icon']
124+
}"></span><span class="${
125+
styles['keyword__text']
126+
}">${short_hash}</span></span>`
127+
}
112128

113129
return process_text_part_for_files(part, params.context_file_paths)
114130
})

‎packages/ui/src/components/editor/panel/ChatInput/utils/position-mapping.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const map_display_pos_to_raw_pos = (
88
let last_raw_index = 0
99

1010
const regex =
11-
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#Selection)|(#SavedContext:(?:WorkspaceState|JSON)\s+"([^"]+)")/g
11+
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#Selection)|(#SavedContext:(?:WorkspaceState|JSON)\s+"([^"]+)")|(#Commit:[^:]+:([^\s"]+)\s+"[^"]*")/g
1212
let match
1313

1414
while ((match = regex.exec(raw_text)) !== null) {
@@ -17,6 +17,8 @@ export const map_display_pos_to_raw_pos = (
1717
const selection_keyword = match[3]
1818
const saved_context_keyword = match[4]
1919
const context_name = match[5]
20+
const commit_keyword = match[6]
21+
const commit_hash = match[7]
2022

2123
let is_replacement_match = false
2224
let display_match_length = 0
@@ -35,6 +37,10 @@ export const map_display_pos_to_raw_pos = (
3537
} else if (saved_context_keyword) {
3638
display_match_length = `Context "${context_name}"`.length
3739
is_replacement_match = true
40+
} else if (commit_keyword) {
41+
const short_hash = commit_hash.substring(0, 7)
42+
display_match_length = short_hash.length
43+
is_replacement_match = true
3844
}
3945

4046
if (!is_replacement_match) {
@@ -79,7 +85,7 @@ export const map_raw_pos_to_display_pos = (
7985
let last_raw_index = 0
8086

8187
const regex =
82-
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#Selection)|(#SavedContext:(?:WorkspaceState|JSON)\s+"([^"]+)")/g
88+
/`([^\s`]*\.[^\s`]+)`|(#Changes:[^\s,;:!?]+)|(#Selection)|(#SavedContext:(?:WorkspaceState|JSON)\s+"([^"]+)")|(#Commit:[^:]+:([^\s"]+)\s+"[^"]*")/g
8389
let match
8490

8591
while ((match = regex.exec(raw_text)) !== null) {
@@ -88,6 +94,8 @@ export const map_raw_pos_to_display_pos = (
8894
const selection_keyword = match[3]
8995
const saved_context_keyword = match[4]
9096
const context_name = match[5]
97+
const commit_keyword = match[6]
98+
const commit_hash = match[7]
9199

92100
let is_replacement_match = false
93101
let display_match_length = 0
@@ -106,6 +114,10 @@ export const map_raw_pos_to_display_pos = (
106114
} else if (saved_context_keyword) {
107115
display_match_length = `Context "${context_name}"`.length
108116
is_replacement_match = true
117+
} else if (commit_keyword) {
118+
const short_hash = commit_hash.substring(0, 7)
119+
display_match_length = short_hash.length
120+
is_replacement_match = true
109121
}
110122

111123
if (!is_replacement_match) {

‎packages/vscode/src/utils/replace-saved-context-placeholder.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SavedContext } from '@/types/context'
55
import { SAVED_CONTEXTS_STATE_KEY } from '@/constants/state-keys'
66
import { WorkspaceProvider } from '@/context/providers/workspace-provider'
77
import { dictionary } from '@shared/constants/dictionary'
8-
import { resolve_glob_patterns } from '@/commands/apply-context-command/apply-context-command'
8+
import { resolve_glob_patterns } from '@/commands/apply-context-command/utils'
99

1010
async function get_file_content_as_xml(
1111
file_path: string,

‎packages/vscode/src/views/panel/backend/message-handlers/handle-at-sign-quick-pick/handle-at-sign-quick-pick.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const at_sign_quick_pick = async (params: {
6868
})
6969

7070
if (filtered_paths.length == 1) {
71-
return `\`${filtered_paths[0]}\``
71+
return `\`${filtered_paths[0]}\` `
7272
} else if (filtered_paths.length > 1) {
7373
const filtered_paths_set = new Set(filtered_paths)
7474
quick_pick_items_to_show = all_quick_pick_items.filter((item) =>
@@ -94,7 +94,7 @@ const at_sign_quick_pick = async (params: {
9494
)
9595

9696
if (selected_path_item) {
97-
return `\`${selected_path_item.fullPath}\``
97+
return `\`${selected_path_item.fullPath}\` `
9898
}
9999
return
100100
}

‎packages/vscode/src/views/panel/backend/message-handlers/handle-copy-prompt.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { PanelProvider } from '@/views/panel/backend/panel-provider'
22
import * as vscode from 'vscode'
33
import { FilesCollector } from '@/utils/files-collector'
44
import { replace_selection_placeholder } from '@/views/panel/backend/utils/replace-selection-placeholder'
5-
import { replace_changes_placeholder } from '@/views/panel/backend/utils/replace-changes-placeholder'
5+
import {
6+
replace_changes_placeholder,
7+
replace_commit_placeholder
8+
} from '@/views/panel/backend/utils/replace-git-placeholders'
69
import { replace_saved_context_placeholder } from '@/utils/replace-saved-context-placeholder'
710
import { code_completion_instructions_for_panel } from '@/constants/instructions'
811
import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixes'
@@ -105,6 +108,16 @@ export const handle_copy_prompt = async (params: {
105108
})
106109
}
107110

111+
if (instructions.includes('#Commit:')) {
112+
pre_context_instructions = await replace_commit_placeholder({
113+
instruction: pre_context_instructions
114+
})
115+
post_context_instructions = await replace_commit_placeholder({
116+
instruction: post_context_instructions,
117+
after_context: true
118+
})
119+
}
120+
108121
if (instructions.includes('#SavedContext:')) {
109122
pre_context_instructions = await replace_saved_context_placeholder({
110123
instruction: pre_context_instructions,

0 commit comments

Comments
 (0)