Skip to content

Commit d15f29d

Browse files
committed
Implement an option to attach an ASCII tree of selected context files to commit messages
- each such object [Pasted text] should include 'selected_files': string[] key that holds all the paths selected for context at the time of accepting a preview. - add new setting 'attach ascii tree of context': Ask/Always/Never (ask is default). place above 'select all prompts by default' - add missing translations [Fragment] - add missing translations [Fragment] - create util that generates ascii tree of given array of paths - while respecting codeWebChat.attachAsciiTreeOfContext include ascii file tree in a commit message (below the list of prompts). Note, do this flow only when there are some 'selected files' to attach.
1 parent 11d3aae commit d15f29d

17 files changed

Lines changed: 371 additions & 59 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,16 @@
928928
"type": "string",
929929
"scope": "resource"
930930
},
931+
"codeWebChat.attachAsciiTreeOfContext": {
932+
"type": "string",
933+
"scope": "resource",
934+
"enum": [
935+
"ask",
936+
"always",
937+
"never"
938+
],
939+
"default": "ask"
940+
},
931941
"codeWebChat.selectAllPromptsInCommitMessagesByDefault": {
932942
"type": "boolean",
933943
"scope": "resource",

‎apps/editor/src/commands/apply-response-command/apply-response-command.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ export const apply_response_command = (params: {
385385
original_states: preview_data.original_states,
386386
chat_response: preview_data.response,
387387
prompt_view_provider: params.prompt_view_provider,
388+
workspace_provider: params.workspace_provider,
388389
extension_context: params.extension_context,
389390
original_editor_state: args?.original_editor_state,
390391
raw_instructions: args?.raw_instructions,

‎apps/editor/src/commands/apply-response-command/utils/preview-handler.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode'
2+
import * as path from 'path'
23
import { Logger } from '@shared/utils/logger'
34
import { OriginalFileState } from '../types/original-file-state'
45
import { RecentApiConfiguration } from '@shared/types/response-history-item'
@@ -7,13 +8,15 @@ import { preview } from './preview'
78
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
89
import { update_undo_button_state } from './state-manager'
910
import { PromptsForCommitMessagesUtils } from '@/utils/prompts-for-commit-messages-utils'
11+
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
1012

1113
export let ongoing_preview_cleanup_promise: Promise<void> | null = null
1214

1315
export const preview_handler = async (params: {
1416
original_states: OriginalFileState[]
1517
chat_response: string
1618
prompt_view_provider: PromptViewProvider
19+
workspace_provider: WorkspaceProvider
1720
extension_context: vscode.ExtensionContext
1821
original_editor_state?: {
1922
file_path: string
@@ -135,12 +138,30 @@ export const preview_handler = async (params: {
135138
}
136139
}
137140

141+
const selected_files_by_workspace = new Map<string, string[]>()
142+
const checked_files = params.workspace_provider.get_checked_files()
143+
for (const file of checked_files) {
144+
const workspace_root =
145+
params.workspace_provider.get_workspace_root_for_file(file)
146+
if (workspace_root) {
147+
const relative_path = path
148+
.relative(workspace_root, file)
149+
.replace(/\\/g, '/')
150+
const current_selected =
151+
selected_files_by_workspace.get(workspace_root) || []
152+
current_selected.push(relative_path)
153+
selected_files_by_workspace.set(workspace_root, current_selected)
154+
}
155+
}
156+
138157
for (const [workspace_root, files] of files_by_workspace.entries()) {
139158
PromptsForCommitMessagesUtils.add({
140159
extension_context: params.extension_context,
141160
workspace_root,
142161
prompt: params.raw_instructions,
143-
files
162+
files,
163+
selected_files:
164+
selected_files_by_workspace.get(workspace_root) || []
144165
})
145166
}
146167
}

‎apps/editor/src/commands/copy-paths-command.ts‎

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,7 @@ import { dictionary } from '@shared/constants/dictionary'
44
import { t } from '../i18n'
55
import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
66
import { OpenEditorsProvider } from '../context/providers/open-editors/open-editors-provider'
7-
8-
interface TreeNode {
9-
[key: string]: TreeNode
10-
}
11-
12-
const build_tree = (paths: string[]): TreeNode => {
13-
const root: TreeNode = {}
14-
for (const path of paths) {
15-
const parts = path.split('/')
16-
let current = root
17-
for (const part of parts) {
18-
if (!current[part]) {
19-
current[part] = {}
20-
}
21-
current = current[part]
22-
}
23-
}
24-
return root
25-
}
26-
27-
const print_tree = (node: TreeNode, prefix = ''): string[] => {
28-
const keys = Object.keys(node).sort()
29-
const lines: string[] = []
30-
for (let i = 0; i < keys.length; i++) {
31-
const key = keys[i]
32-
const is_last = i == keys.length - 1
33-
lines.push(`${prefix}${is_last ? '└── ' : '├── '}${key}`)
34-
const child_prefix = prefix + (is_last ? ' ' : '│ ')
35-
lines.push(...print_tree(node[key], child_prefix))
36-
}
37-
return lines
38-
}
7+
import { generate_ascii_tree } from '../utils/ascii-tree'
398

409
const format_paths = (files: string[]) => {
4110
const config = vscode.workspace.getConfiguration('codeWebChat')
@@ -66,8 +35,7 @@ const format_paths = (files: string[]) => {
6635
})
6736

6837
if (format == 'ascii-tree') {
69-
const root = build_tree(display_paths)
70-
return print_tree(root).join('\n')
38+
return generate_ascii_tree(display_paths)
7139
} else if (format == 'comma-separated') {
7240
return display_paths.map((display_path) => `\`${display_path}\``).join(', ')
7341
}

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

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
1818
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
1919
import { create_checkpoint } from '@/features/checkpoints/actions'
2020
import { simplify_prompt_symbols } from '@shared/utils/simplify-prompt-symbols'
21+
import { generate_ascii_tree } from '../../utils/ascii-tree'
2122

2223
const truncate_prompt = (text: string): string => {
2324
if (text.length <= MAX_PROMPT_CHARS_IN_COMMIT_MESSAGE) return text
@@ -192,6 +193,50 @@ export const generate_commit_message_command = (
192193
index == self.findIndex((sp) => sp.prompt == p.prompt)
193194
)
194195

196+
const get_tree_text_if_applicable = async (
197+
selected_prompts: typeof relevant_prompts
198+
): Promise<string | undefined> => {
199+
const selected_files_set = new Set<string>()
200+
for (const p of selected_prompts) {
201+
for (const f of p.selected_files || []) {
202+
selected_files_set.add(f)
203+
}
204+
}
205+
const selected_files_to_attach = Array.from(selected_files_set)
206+
207+
if (selected_files_to_attach.length == 0) {
208+
return ''
209+
}
210+
211+
const attach_tree_setting = vscode.workspace
212+
.getConfiguration('codeWebChat')
213+
.get<string>('attachAsciiTreeOfContext', 'ask')
214+
215+
let attach_tree = false
216+
if (attach_tree_setting == 'always') {
217+
attach_tree = true
218+
} else if (attach_tree_setting == 'ask') {
219+
const answer = await vscode.window.showQuickPick(['Yes', 'No'], {
220+
title:
221+
'Attach ASCII file tree of the selected files to the commit message?',
222+
ignoreFocusOut: true
223+
})
224+
if (answer === undefined) {
225+
return undefined
226+
}
227+
attach_tree = answer == 'Yes'
228+
}
229+
230+
if (attach_tree) {
231+
const display_paths = selected_files_to_attach.map((p) =>
232+
vscode.workspace.asRelativePath(p).replace(/\\/g, '/')
233+
)
234+
return '\n\n' + generate_ascii_tree(display_paths)
235+
}
236+
237+
return ''
238+
}
239+
195240
if (params.should_commit) {
196241
const edited_message = await new Promise<string | 'back' | undefined>(
197242
(resolve) => {
@@ -293,6 +338,14 @@ export const generate_commit_message_command = (
293338
selected_prompts = picked
294339
}
295340

341+
const tree_text = await get_tree_text_if_applicable(selected_prompts)
342+
if (tree_text === undefined) {
343+
if (was_empty_stage) {
344+
await vscode.commands.executeCommand('git.unstageAll', repository)
345+
}
346+
break
347+
}
348+
296349
const selected_prompts_text =
297350
selected_prompts.length > 0
298351
? '\n\n' +
@@ -304,7 +357,8 @@ export const generate_commit_message_command = (
304357
.join('\n')
305358
: ''
306359

307-
const commit_message_value = edited_message + selected_prompts_text
360+
const commit_message_value =
361+
edited_message + selected_prompts_text + tree_text
308362
repository.inputBox.value = commit_message_value
309363
await vscode.commands.executeCommand('git.commit', repository)
310364
PromptsForCommitMessagesUtils.remove_committed_files({
@@ -326,17 +380,26 @@ export const generate_commit_message_command = (
326380
await vscode.commands.executeCommand('git.unstageAll', repository)
327381
}
328382
} else {
383+
const selected_prompts = select_prompts_setting ? relevant_prompts : []
384+
const tree_text = await get_tree_text_if_applicable(selected_prompts)
385+
if (tree_text === undefined) {
386+
if (was_empty_stage) {
387+
await vscode.commands.executeCommand('git.unstageAll', repository)
388+
}
389+
break
390+
}
391+
329392
const prompts_text =
330-
select_prompts_setting && relevant_prompts.length > 0
393+
selected_prompts.length > 0
331394
? '\n\n' +
332-
relevant_prompts
395+
selected_prompts
333396
.map(
334397
(p) =>
335398
`- ${truncate_prompt(simplify_prompt_symbols({ prompt: p.prompt }))}`
336399
)
337400
.join('\n')
338401
: ''
339-
repository.inputBox.value = commit_message + prompts_text
402+
repository.inputBox.value = commit_message + prompts_text + tree_text
340403
}
341404

342405
break
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
interface TreeNode {
2+
[key: string]: TreeNode
3+
}
4+
5+
const build_tree = (paths: string[]): TreeNode => {
6+
const root: TreeNode = {}
7+
for (const path of paths) {
8+
const parts = path.split('/')
9+
let current = root
10+
for (const part of parts) {
11+
if (!current[part]) {
12+
current[part] = {}
13+
}
14+
current = current[part]
15+
}
16+
}
17+
return root
18+
}
19+
20+
const print_tree = (node: TreeNode, prefix = ''): string[] => {
21+
const keys = Object.keys(node).sort()
22+
const lines: string[] = []
23+
for (let i = 0; i < keys.length; i++) {
24+
const key = keys[i]
25+
const is_last = i == keys.length - 1
26+
lines.push(`${prefix}${is_last ? '└── ' : '├── '}${key}`)
27+
const child_prefix = prefix + (is_last ? ' ' : '│ ')
28+
lines.push(...print_tree(node[key], child_prefix))
29+
}
30+
return lines
31+
}
32+
33+
export const generate_ascii_tree = (paths: string[]): string => {
34+
const root = build_tree(paths)
35+
return print_tree(root).join('\n')
36+
}

‎apps/editor/src/utils/prompts-for-commit-messages-utils.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export namespace PromptsForCommitMessagesUtils {
88
export type Prompt = {
99
prompt: string
1010
files: string[]
11+
selected_files: string[]
1112
}
1213

1314
export const get_file_path = (extension_context: vscode.ExtensionContext) => {
@@ -67,6 +68,7 @@ export namespace PromptsForCommitMessagesUtils {
6768
workspace_root: string
6869
prompt: string
6970
files: string[]
71+
selected_files: string[]
7072
}) => {
7173
const all_prompts = load_all(params.extension_context)
7274

@@ -80,10 +82,14 @@ export namespace PromptsForCommitMessagesUtils {
8082

8183
if (existing) {
8284
existing.files = Array.from(new Set([...existing.files, ...params.files]))
85+
existing.selected_files = Array.from(
86+
new Set([...(existing.selected_files || []), ...params.selected_files])
87+
)
8388
} else {
8489
const new_prompt: Prompt = {
8590
files: params.files,
86-
prompt: params.prompt
91+
prompt: params.prompt,
92+
selected_files: params.selected_files
8793
}
8894
all_prompts[params.workspace_root].push(new_prompt)
8995
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as vscode from 'vscode'
2+
import { SettingsViewProvider } from '@/views/settings/backend/settings-view-provider'
3+
4+
export const handle_get_attach_ascii_tree_of_context = async (
5+
provider: SettingsViewProvider
6+
): Promise<void> => {
7+
const config = vscode.workspace.getConfiguration('codeWebChat')
8+
const value = config.get<'ask' | 'always' | 'never'>(
9+
'attachAsciiTreeOfContext',
10+
'ask'
11+
)
12+
provider.postMessage({
13+
command: 'ATTACH_ASCII_TREE_OF_CONTEXT',
14+
value
15+
})
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as vscode from 'vscode'
2+
import { UpdateAttachAsciiTreeOfContextMessage } from '@/views/settings/types/messages'
3+
4+
export const handle_update_attach_ascii_tree_of_context = async (
5+
message: UpdateAttachAsciiTreeOfContextMessage
6+
): Promise<void> => {
7+
await vscode.workspace
8+
.getConfiguration('codeWebChat')
9+
.update(
10+
'attachAsciiTreeOfContext',
11+
message.value,
12+
vscode.ConfigurationTarget.Global
13+
)
14+
}

‎apps/editor/src/views/settings/backend/message-handlers/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export * from './handle-get-edit-format-instructions'
1313
export * from './handle-update-edit-format-instructions'
1414
export * from './handle-get-commit-message-instructions'
1515
export * from './handle-update-commit-message-instructions'
16+
export * from './handle-get-attach-ascii-tree-of-context'
17+
export * from './handle-update-attach-ascii-tree-of-context'
1618
export * from './handle-get-include-prompts-in-commit-messages'
1719
export * from './handle-update-include-prompts-in-commit-messages'
1820
export * from './handle-get-synchronize-edit-format-between-modes'

0 commit comments

Comments
 (0)