Skip to content

Commit d55b283

Browse files
committed
Centralize path normalization into a shared normalize_path utility and improve commit message generation to include context files from previously accepted prompts
- when generating commit message, are selected files from CommitMessageDetails included for better the generated commit message accuracy? they should - assess correctness of CommitMessageDetails.remove_committed_files - are paths handled correctly? - should plcae a global util in `apps/editor/src/utils` for path normalizations - Place 'additional context files' without distinction inside '# Files', each file like by `apps/editor/src/utils/files-collector.ts` "- File: ``" - place updated files below selected files - don't place 'task' and 'output formatting' sections on top. - the files collected from 'commit message details' utility are ordered in the same way as those got from `apps/editor/src/utils/files-collector.ts`? considering these are all the same files - All full files should live in "# Files" section. Diffs should be placed together in "# Changes" section. - if a file includes markdown code block (triple backticks), it should be wrapped in four backticks ├── apps │ └── editor │ ├── src │ │ ├── commands │ │ │ ├── apply-response-command │ │ │ │ └── utils │ │ │ │ ├── preview │ │ │ │ │ └── workspace-listener.ts │ │ │ │ ├── preview-handler.ts │ │ │ │ └── response-parser │ │ │ │ ├── parsers │ │ │ │ │ └── diff-parser │ │ │ │ │ └── helpers │ │ │ │ │ └── path-utils.ts │ │ │ │ └── response-parser.ts │ │ │ ├── code-at-cursor-command │ │ │ │ ├── code-at-cursor-commands.ts │ │ │ │ └── utils │ │ │ │ └── perform-code-at-cursor.ts │ │ │ ├── copy-paths-command.ts │ │ │ ├── generate-commit-message-commands │ │ │ │ ├── actions │ │ │ │ │ ├── get-prompt-data.ts │ │ │ │ │ └── run-generate-action.ts │ │ │ │ ├── generate-commit-message-commands.ts │ │ │ │ ├── index.ts │ │ │ │ └── utils │ │ │ │ ├── build-commit-message-prompt.ts │ │ │ │ ├── generate-commit-message-with-api.ts │ │ │ │ ├── get-commit-message-config.ts │ │ │ │ └── strip-wrapping-quotes.ts │ │ │ ├── restore-file-selection-command │ │ │ │ └── actions │ │ │ │ ├── restore-from-json-file.ts │ │ │ │ └── restore-from-workspace-state.ts │ │ │ ├── save-file-selection-command │ │ │ │ └── actions │ │ │ │ ├── save-to-json-file.ts │ │ │ │ └── save-to-workspace-state.ts │ │ │ └── set-ranges-command.ts │ │ ├── constants │ │ │ └── instructions.ts │ │ ├── context │ │ │ ├── helpers │ │ │ │ ├── get-all-workspace-files.ts │ │ │ │ └── get-selected-files.ts │ │ │ └── providers │ │ │ └── workspace │ │ │ ├── modules │ │ │ │ └── token-calculator.ts │ │ │ └── workspace-provider.ts │ │ ├── features │ │ │ └── checkpoints │ │ │ └── actions │ │ │ └── restore-checkpoint.ts │ │ ├── services │ │ │ └── git-discard-file-watcher.ts │ │ ├── utils │ │ │ ├── commit-message-details.ts │ │ │ ├── files-collector.ts │ │ │ ├── is-binary.ts │ │ │ ├── normalize-path.ts │ │ │ ├── path-sanitizer.ts │ │ │ └── prompt-builder.ts │ │ └── views │ │ └── prompt │ │ └── backend │ │ ├── message-handlers │ │ │ └── handle-at-sign-quick-pick │ │ │ └── handle-at-sign-quick-pick.ts │ │ ├── prompt-view-provider.ts │ │ └── utils │ │ └── symbols │ │ └── skill │ │ └── replace-skill-symbol.ts │ └── webpack.config.ts └── packages └── shared └── src └── utils └── extract-path-from-line-of-code └── extract-path-from-line-of-code.ts
1 parent 270fd39 commit d55b283

28 files changed

Lines changed: 294 additions & 113 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
99
import { update_undo_button_state } from './state-manager'
1010
import { CommitMessageDetails } from '@/utils/commit-message-details'
1111
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
12+
import { normalize_path } from '@/utils/normalize-path'
1213

1314
export let ongoing_preview_cleanup_promise: Promise<void> | null = null
1415

@@ -143,9 +144,9 @@ export const preview_handler = async (params: {
143144
const workspace_root =
144145
params.workspace_provider.get_workspace_root_for_file(file)
145146
if (workspace_root) {
146-
const relative_path = path
147-
.relative(workspace_root, file)
148-
.replace(/\\/g, '/')
147+
const relative_path = path.isAbsolute(file)
148+
? normalize_path(path.relative(workspace_root, file))
149+
: normalize_path(file)
149150
const current_selected =
150151
selected_files_by_workspace.get(workspace_root) || []
151152
current_selected.push(relative_path)

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { get_diff_stats } from './diff-utils'
99
import { remove_directory_if_empty } from '../file-operations'
1010
import { ResponseHistoryItem } from '@shared/types/response-history-item'
1111
import { create_temp_files_with_original_content } from './temp-file-manager'
12+
import { normalize_path } from '@/utils/normalize-path'
1213

1314
export let toggle_file_preview_state:
1415
| ((file: {
@@ -165,9 +166,9 @@ export const setup_workspace_listeners = (params: {
165166
}
166167

167168
const new_content = doc.getText()
168-
const relative_path = vscode.workspace
169-
.asRelativePath(doc.uri, false)
170-
.replace(/\\/g, '/')
169+
const relative_path = normalize_path(
170+
vscode.workspace.asRelativePath(doc.uri, false)
171+
)
171172

172173
let original_content = ''
173174
let is_new = false
@@ -285,9 +286,9 @@ export const setup_workspace_listeners = (params: {
285286
}
286287

287288
const new_content = ''
288-
const relative_path = vscode.workspace
289-
.asRelativePath(uri, false)
290-
.replace(/\\/g, '/')
289+
const relative_path = normalize_path(
290+
vscode.workspace.asRelativePath(uri, false)
291+
)
291292

292293
const original_content_for_undo =
293294
deleted_files_content_cache.get(uri.fsPath) ?? ''
@@ -392,9 +393,9 @@ export const setup_workspace_listeners = (params: {
392393
new_content = ''
393394
}
394395

395-
const relative_path = vscode.workspace
396-
.asRelativePath(uri, false)
397-
.replace(/\\/g, '/')
396+
const relative_path = normalize_path(
397+
vscode.workspace.asRelativePath(uri, false)
398+
)
398399

399400
const original_content = ''
400401
const is_new = true
@@ -480,12 +481,12 @@ export const setup_workspace_listeners = (params: {
480481
continue
481482
}
482483

483-
const old_relative = vscode.workspace
484-
.asRelativePath(oldUri, false)
485-
.replace(/\\/g, '/')
486-
const new_relative = vscode.workspace
487-
.asRelativePath(newUri, false)
488-
.replace(/\\/g, '/')
484+
const old_relative = normalize_path(
485+
vscode.workspace.asRelativePath(oldUri, false)
486+
)
487+
const new_relative = normalize_path(
488+
vscode.workspace.asRelativePath(newUri, false)
489+
)
489490

490491
// Find existing tracked file by old path
491492
const existing = params.prepared_files.find(

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/diff-parser/diff-parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {
66
extract_workspace_and_path
77
} from '../../response-parser'
88
import {
9-
normalize_path,
109
find_file_path_before_block,
1110
remove_path_line_from_text_block,
1211
normalize_header_line,
1312
extract_paths_from_lines,
1413
find_patch_start_index,
1514
format_hunk_headers
1615
} from './helpers'
16+
import { normalize_path } from '@/utils/normalize-path'
1717

1818
type DiffParserItem = DiffItem | TextItem | InlineFileItem
1919

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/diff-parser/helpers/path-utils.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
22

3-
export const normalize_path = (path: string): string => {
4-
return path.trim().replace(/\\/g, '/')
5-
}
6-
73
export const strip_quotes = (path: string): string => {
84
const trimmed = path.trim()
95
if (trimmed.startsWith('"') && trimmed.endsWith('"')) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
parse_multiple_files,
55
parse_relevant_files
66
} from './parsers'
7+
import { normalize_path } from '@/utils/normalize-path'
78

89
export type FileItem = {
910
type: 'file'
@@ -61,7 +62,7 @@ export const extract_workspace_and_path = (params: {
6162
raw_file_path: string
6263
is_single_root_folder_workspace: boolean
6364
}): { workspace_name?: string; relative_path: string } => {
64-
let file_path = params.raw_file_path.replace(/\\/g, '/')
65+
let file_path = normalize_path(params.raw_file_path)
6566
if (file_path.startsWith('/')) {
6667
file_path = file_path.substring(1)
6768
}

‎apps/editor/src/commands/code-at-cursor-command/utils/perform-code-at-cursor.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { show_ghost_text } from './show-ghost-text'
1717
import { PromptBuilder } from '../../../utils/prompt-builder'
1818
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
1919
import { OpenEditorsProvider } from '@/context/providers/open-editors/open-editors-provider'
20+
import { normalize_path } from '@/utils/normalize-path'
2021

2122
export const perform_code_at_cursor = async (params: {
2223
workspace_provider: WorkspaceProvider
@@ -269,9 +270,9 @@ export const perform_code_at_cursor = async (params: {
269270
const file_workspace_root =
270271
params.workspace_provider.get_workspace_root_for_file(file)
271272
if (file_workspace_root === workspace_root) {
272-
const relative_path = path
273-
.relative(workspace_root, file)
274-
.replace(/\\/g, '/')
273+
const relative_path = normalize_path(
274+
path.relative(workspace_root, file)
275+
)
275276
selected_files.push(relative_path)
276277
}
277278
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
3-
import { dictionary } from '@shared/constants/dictionary'
43
import { t } from '../i18n'
54
import { WorkspaceProvider } from '../context/providers/workspace/workspace-provider'
65
import { OpenEditorsProvider } from '../context/providers/open-editors/open-editors-provider'
76
import { AsciiTree } from '../utils/ascii-tree'
87
import { LAST_COPY_PATHS_FORMAT_STATE_KEY } from '../constants/state-keys'
8+
import { normalize_path } from '../utils/normalize-path'
99

1010
type Format = 'bullet-list' | 'comma-separated' | 'ascii-tree'
1111

@@ -28,7 +28,7 @@ const format_paths = (files: string[], format: Format) => {
2828
display_path = vscode.workspace.asRelativePath(file_path)
2929
}
3030

31-
return display_path.replace(/\\/g, '/')
31+
return normalize_path(display_path)
3232
})
3333

3434
if (format == 'ascii-tree') {

‎apps/editor/src/commands/generate-commit-message-commands/actions/get-prompt-data.ts‎

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import * as vscode from 'vscode'
2+
import * as path from 'path'
23
import {
34
prepare_staged_changes,
45
GitRepository
56
} from '../../../utils/git-repository-utils'
67
import { build_commit_message_prompt } from '../utils/build-commit-message-prompt'
8+
import { CommitMessageDetails } from '../../../utils/commit-message-details'
9+
import { normalize_path } from '@/utils/normalize-path'
10+
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
11+
import { FilesCollector } from '@/utils/files-collector'
712

813
export const get_prompt_data = async (params: {
914
repository: GitRepository
1015
stage_all_if_none_staged: boolean
1116
selection_state?: { files?: string[] }
17+
extension_context: vscode.ExtensionContext
18+
workspace_provider: WorkspaceProvider
1219
}) => {
1320
await vscode.workspace.saveAll()
1421
await params.repository.status()
@@ -22,10 +29,6 @@ export const get_prompt_data = async (params: {
2229
selection_state: params.selection_state
2330
})
2431
if (!diff) return null
25-
const { api_prompt, chatbot_prompt } = await build_commit_message_prompt(
26-
diff,
27-
params.repository
28-
)
2932

3033
const diff_file_paths: string[] = []
3134
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
@@ -58,6 +61,61 @@ export const get_prompt_data = async (params: {
5861
}
5962
}
6063

64+
const workspace_root = params.repository.rootUri.fsPath
65+
const all_prompts =
66+
CommitMessageDetails.load_all(params.extension_context)[workspace_root] ||
67+
[]
68+
69+
const relevant_prompts = all_prompts.filter((p) =>
70+
p.files.some((file) => {
71+
const rel_path = path.isAbsolute(file)
72+
? normalize_path(path.relative(workspace_root, file))
73+
: normalize_path(file)
74+
return (
75+
diff_file_paths.includes(rel_path) || diff_file_paths.includes(file)
76+
)
77+
})
78+
)
79+
80+
const context_files_set = new Set<string>()
81+
for (const p of relevant_prompts) {
82+
for (const f of p.selected_files || []) {
83+
const rel_path = path.isAbsolute(f)
84+
? normalize_path(path.relative(workspace_root, f))
85+
: normalize_path(f)
86+
if (!diff_file_paths.includes(rel_path) && !diff_file_paths.includes(f)) {
87+
context_files_set.add(rel_path)
88+
}
89+
}
90+
}
91+
92+
const context_files = Array.from(context_files_set)
93+
94+
const files_collector = new FilesCollector({
95+
workspace_provider: params.workspace_provider
96+
})
97+
98+
const absolute_context_files = context_files.map((f) =>
99+
path.join(workspace_root, f)
100+
)
101+
102+
const sorted_files = files_collector.sort_context_files({
103+
files: absolute_context_files
104+
})
105+
106+
const ordered_context_files = [
107+
...sorted_files.other_files,
108+
...sorted_files.recent_files
109+
].map((f) => {
110+
return normalize_path(path.relative(workspace_root, f))
111+
})
112+
113+
const { api_prompt, chatbot_prompt } = await build_commit_message_prompt(
114+
diff,
115+
params.repository,
116+
ordered_context_files
117+
)
118+
61119
return {
62120
repository: params.repository,
63121
was_empty_stage,

‎apps/editor/src/commands/generate-commit-message-commands/actions/run-generate-action.ts‎

Lines changed: 15 additions & 4 deletions
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 { get_repository_for_commit } from '../../../utils/git-repository-utils'
34
import { get_commit_message_api_configuration } from '../utils/get-commit-message-config'
45
import { generate_commit_message_with_api } from '../utils/generate-commit-message-with-api'
@@ -23,6 +24,7 @@ import { dictionary } from '@shared/constants/dictionary'
2324
import { WebSocketManager } from '@/services/websocket-manager'
2425
import { ModelProvidersManager } from '@/services/model-providers-manager'
2526
import { get_response_preview_promise_resolve } from '@/commands/apply-response-command/utils/preview'
27+
import { normalize_path } from '@/utils/normalize-path'
2628

2729
const truncate_prompt = (text: string): string => {
2830
if (text.length <= MAX_PROMPT_CHARS_IN_COMMIT_MESSAGE) return text
@@ -57,7 +59,9 @@ export const run_generate_action = async (params: {
5759
const data = await get_prompt_data({
5860
repository,
5961
stage_all_if_none_staged: !!params.source_control,
60-
selection_state
62+
selection_state,
63+
extension_context: params.extension_context,
64+
workspace_provider: params.workspace_provider
6165
})
6266
if (!data) return
6367
const { api_prompt, chatbot_prompt, is_single_change, staged_files } = data
@@ -381,7 +385,14 @@ export const run_generate_action = async (params: {
381385
.get<boolean>('selectAllPromptsInCommitMessagesByDefault', true)
382386

383387
const relevant_prompts = all_prompts
384-
.filter((p) => p.files.some((file) => staged_files.includes(file)))
388+
.filter((p) =>
389+
p.files.some((file) => {
390+
const rel_path = path.isAbsolute(file)
391+
? normalize_path(path.relative(workspace_root, file))
392+
: normalize_path(file)
393+
return staged_files.includes(rel_path) || staged_files.includes(file)
394+
})
395+
)
385396
.filter(
386397
(p, index, self) =>
387398
index == self.findIndex((sp) => sp.prompt == p.prompt)
@@ -405,7 +416,7 @@ export const run_generate_action = async (params: {
405416
}
406417

407418
const all_files_touched = selected_files_to_attach.every((f) => {
408-
const rel_path = vscode.workspace.asRelativePath(f).replace(/\\/g, '/')
419+
const rel_path = normalize_path(vscode.workspace.asRelativePath(f))
409420
return staged_files.includes(rel_path) || staged_files.includes(f)
410421
})
411422

@@ -493,7 +504,7 @@ export const run_generate_action = async (params: {
493504

494505
if (attach_tree) {
495506
const display_paths = selected_files_to_attach.map((p) =>
496-
vscode.workspace.asRelativePath(p).replace(/\\/g, '/')
507+
normalize_path(vscode.workspace.asRelativePath(p))
497508
)
498509
return '\n\n' + AsciiTree.generate(display_paths)
499510
}

0 commit comments

Comments
 (0)