Skip to content

Commit f5fd6c8

Browse files
committed
Enhance commit message generation with full file content and update the staging UI with token counts
1 parent 65f45a5 commit f5fd6c8

6 files changed

Lines changed: 157 additions & 31 deletions

File tree

‎apps/editor/src/commands/generate-commit-message-command/generate-commit-message-command.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const generate_commit_message_command = (
2323
const is_single_change = was_empty_stage && working_tree_changes.length == 1
2424
const diff = await prepare_staged_changes(repository, !!source_control)
2525
if (!diff) return null
26-
const message_prompt = build_commit_message_prompt(diff)
26+
const message_prompt = await build_commit_message_prompt(diff, repository)
2727
return { repository, was_empty_stage, message_prompt, is_single_change }
2828
}
2929

‎apps/editor/src/commands/generate-commit-message-command/utils/build-commit-message-prompt.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as vscode from 'vscode'
22
import { commit_message_instructions } from '@/constants/instructions'
3+
import type { GitRepository } from '@/utils/git-repository-utils'
34

4-
export const build_commit_message_prompt = (diff: string): string => {
5+
export const build_commit_message_prompt = async (
6+
diff: string,
7+
repository: GitRepository
8+
): Promise<string> => {
59
const config = vscode.workspace.getConfiguration('codeWebChat')
610
const instructions = config.get<string>('commitMessageInstructions')
711
const commit_message_prompt = instructions || commit_message_instructions
@@ -70,12 +74,36 @@ export const build_commit_message_prompt = (diff: string): string => {
7074
}
7175
}
7276

77+
const hunk_start_index = final_diff_content.indexOf('\n@@ ')
78+
if (hunk_start_index != -1) {
79+
final_diff_content = final_diff_content.substring(hunk_start_index + 1)
80+
} else if (!final_diff_content.startsWith('@@ ')) {
81+
final_diff_content = ''
82+
}
83+
7384
changes_content += `<file path="${file_path}" status="${status}"`
7485
if (old_path_attr) {
7586
changes_content += ` old_path="${old_path_attr}"`
7687
}
7788
changes_content += '>\n'
78-
changes_content += `<![CDATA[\n${final_diff_content}\n]]>\n`
89+
changes_content += `<![CDATA[\n${final_diff_content.trimEnd()}\n]]>\n`
90+
91+
if (!is_deleted && file_path) {
92+
try {
93+
let full_content = ''
94+
try {
95+
full_content = await repository.show('', file_path)
96+
} catch (e) {
97+
const uri = vscode.Uri.joinPath(repository.rootUri, file_path)
98+
const content = await vscode.workspace.fs.readFile(uri)
99+
full_content = Buffer.from(content).toString('utf8')
100+
}
101+
if (full_content) {
102+
changes_content += `<![CDATA[\n${full_content.trimEnd()}\n]]>\n`
103+
}
104+
} catch (err) {}
105+
}
106+
79107
changes_content += `</file>\n`
80108
}
81109
}

‎apps/editor/src/i18n/index.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import * as vscode from 'vscode'
2-
import { translations } from './translations'
2+
import { commands } from './translations/commands'
3+
import { common } from './translations/common'
34

45
export type TranslationKey = keyof typeof translations
56

7+
const translations = {
8+
...commands,
9+
...common
10+
}
11+
612
export const t = (
713
key: TranslationKey,
814
placeholders?: Record<string, string | number>

‎apps/editor/src/i18n/translations/index.ts‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎apps/editor/src/utils/git-repository-utils.ts‎

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { execSync } from 'child_process'
33
import { dictionary } from '@shared/constants/dictionary'
44
import * as path from 'path'
55
import { t } from '@/i18n'
6+
import { display_token_count } from './display-token-count'
67

7-
type GitRepository = {
8+
export type GitRepository = {
89
rootUri: vscode.Uri
910
state: {
1011
indexChanges: any[]
@@ -107,26 +108,90 @@ export const prepare_staged_changes = async (
107108
} else if (repository.state.workingTreeChanges.length == 1) {
108109
files_to_stage = [repository.state.workingTreeChanges[0].uri.fsPath]
109110
} else {
110-
const items = repository.state.workingTreeChanges.map((change: any) => {
111-
const relative_path = path.relative(
112-
repository.rootUri.fsPath,
113-
change.uri.fsPath
114-
)
115-
const dir_name = path.dirname(relative_path)
116-
117-
return {
118-
label: path.basename(relative_path),
119-
description: dir_name == '.' ? '' : dir_name,
120-
picked: true,
121-
fsPath: change.uri.fsPath,
122-
buttons: [
123-
{
124-
iconPath: new vscode.ThemeIcon('go-to-file'),
125-
tooltip: t('common.go-to-file')
111+
const items = await Promise.all(
112+
repository.state.workingTreeChanges.map(async (change: any) => {
113+
const relative_path = path.relative(
114+
repository.rootUri.fsPath,
115+
change.uri.fsPath
116+
)
117+
const dir_name = path.dirname(relative_path)
118+
119+
let status = 'updated'
120+
let is_deleted = false
121+
let final_diff_content = ''
122+
let full_content = ''
123+
124+
try {
125+
if (change.status == 7) {
126+
// UNTRACKED
127+
status = 'created'
128+
const content = await vscode.workspace.fs.readFile(change.uri)
129+
full_content = Buffer.from(content).toString('utf8')
130+
const lines = full_content.split('\n')
131+
final_diff_content =
132+
`@@ -0,0 +1,${lines.length} @@\n` +
133+
lines.map((l: string) => '+' + l).join('\n')
134+
} else {
135+
const raw_diff = execSync(`git diff -- "${change.uri.fsPath}"`, {
136+
cwd: repository.rootUri.fsPath
137+
}).toString()
138+
139+
if (
140+
raw_diff.includes('\n+++ /dev/null') ||
141+
raw_diff.startsWith('+++ /dev/null')
142+
) {
143+
status = 'deleted'
144+
is_deleted = true
145+
} else {
146+
if (
147+
raw_diff.includes('\nnew file mode ') ||
148+
raw_diff.startsWith('new file mode ')
149+
) {
150+
status = 'created'
151+
}
152+
153+
const hunk_start_index = raw_diff.indexOf('\n@@ ')
154+
if (hunk_start_index !== -1) {
155+
final_diff_content = raw_diff.substring(hunk_start_index + 1)
156+
} else if (raw_diff.startsWith('@@ ')) {
157+
final_diff_content = raw_diff
158+
}
159+
160+
try {
161+
const content = await vscode.workspace.fs.readFile(change.uri)
162+
full_content = Buffer.from(content).toString('utf8')
163+
} catch (e) {}
164+
}
126165
}
127-
]
128-
}
129-
})
166+
} catch (e) {}
167+
168+
let file_xml = `<file path="${relative_path}" status="${status}">\n`
169+
file_xml += `<![CDATA[\n${final_diff_content.trimEnd()}\n]]>\n`
170+
if (!is_deleted && full_content) {
171+
file_xml += `<![CDATA[\n${full_content.trimEnd()}\n]]>\n`
172+
}
173+
file_xml += `</file>\n`
174+
175+
const token_count = Math.ceil(file_xml.length / 4)
176+
const tokens_str = display_token_count(token_count)
177+
const description_parts = []
178+
description_parts.push(tokens_str)
179+
if (dir_name !== '.') description_parts.push(dir_name)
180+
181+
return {
182+
label: path.basename(relative_path),
183+
description: description_parts.join(' · '),
184+
picked: true,
185+
fsPath: change.uri.fsPath,
186+
buttons: [
187+
{
188+
iconPath: new vscode.ThemeIcon('go-to-file'),
189+
tooltip: t('common.go-to-file')
190+
}
191+
]
192+
}
193+
})
194+
)
130195

131196
const selected = await new Promise<any[] | undefined>((resolve) => {
132197
const quick_pick = vscode.window.createQuickPick<any>()

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,40 @@ export const use_handlers = (
10251025

10261026
const range = selection.getRangeAt(0)
10271027
const { startContainer, startOffset } = range
1028+
1029+
let current_node: Node | null = startContainer
1030+
let symbol_node: HTMLElement | null = null
1031+
1032+
while (current_node && current_node !== params.input_ref.current) {
1033+
if (
1034+
current_node.nodeType === Node.ELEMENT_NODE &&
1035+
(current_node as HTMLElement).dataset.type?.endsWith('-symbol')
1036+
) {
1037+
symbol_node = current_node as HTMLElement
1038+
break
1039+
}
1040+
current_node = current_node.parentNode
1041+
}
1042+
1043+
if (symbol_node) {
1044+
const range_after = document.createRange()
1045+
range_after.selectNodeContents(symbol_node)
1046+
range_after.collapse(false)
1047+
1048+
const pre_caret_range = document.createRange()
1049+
pre_caret_range.selectNodeContents(params.input_ref.current)
1050+
pre_caret_range.setEnd(range_after.endContainer, range_after.endOffset)
1051+
1052+
const display_pos = pre_caret_range.toString().length
1053+
1054+
if (
1055+
handle_symbol_deletion_by_backspace({ el: symbol_node, display_pos })
1056+
) {
1057+
e.preventDefault()
1058+
return
1059+
}
1060+
}
1061+
10281062
let node_before_cursor: Node | null = null
10291063

10301064
if (startContainer.nodeType == Node.TEXT_NODE && startOffset == 0) {

0 commit comments

Comments
 (0)