Skip to content

Commit 87ca803

Browse files
committed
Trim extracted file paths across all clipboard parser utilities
- how is such scenario handled with a space in path,e.g. ` .prettierrc` - is it `.prettierrc` or ` .prettierrc`? Should be `.prettierrc`
1 parent b00e310 commit 87ca803

7 files changed

Lines changed: 27 additions & 22 deletions

File tree

‎apps/editor/src/commands/apply-response-command/utils/clipboard-parser/parsers/code-at-cursor-parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const extract_path_and_position = (
1313
const match = line.match(path_pos_regex)
1414

1515
if (match) {
16-
const path = match[1]
16+
const path = match[1].trim()
1717
const line_num_str = match[2]
1818
const char_num_str = match[3]
1919

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ export const extract_paths_from_lines = (
5454
for (const line of lines) {
5555
const git_diff_match = line.match(/^diff --git a\/(.+?) b\/(.+)$/)
5656
if (git_diff_match) {
57-
if (git_diff_match[2]) to_path = git_diff_match[2]
58-
if (git_diff_match[1]) from_path = git_diff_match[1]
57+
if (git_diff_match[2]) to_path = git_diff_match[2].trim()
58+
if (git_diff_match[1]) from_path = git_diff_match[1].trim()
5959
}
6060
const from_match = line.match(/^--- (?:a\/|"a\/)?([^\t"]+)"?(?:\t.*)?$/)
61-
if (from_match && from_match[1]) from_path = from_match[1]
61+
if (from_match && from_match[1]) from_path = from_match[1].trim()
6262
const to_match = line.match(/^\+\+\+ (?:b\/|"b\/)?([^\t"]+)"?(?:\t.*)?$/)
63-
if (to_match && to_match[1]) to_path = to_match[1]
63+
if (to_match && to_match[1]) to_path = to_match[1].trim()
6464
}
6565
return { from_path, to_path }
6666
}

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
22

33
export const normalize_path = (path: string): string => {
4-
return path.replace(/\\/g, '/')
4+
return path.trim().replace(/\\/g, '/')
55
}
66

77
export const strip_quotes = (path: string): string => {
8-
if (path.startsWith('"') && path.endsWith('"')) {
9-
return path.substring(1, path.length - 1)
8+
const trimmed = path.trim()
9+
if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
10+
return trimmed.substring(1, trimmed.length - 1).trim()
1011
}
11-
return path
12+
return trimmed
1213
}
1314

1415
export const is_valid_file_path = (potential_path: string): boolean => {
16+
const trimmed = potential_path.trim()
1517
return (
16-
!potential_path.endsWith('/') &&
17-
!potential_path.endsWith('\\') &&
18-
(potential_path.includes('.') || potential_path.includes('/')) &&
19-
!potential_path.includes(' ') &&
20-
/[a-zA-Z0-9]/.test(potential_path)
18+
!trimmed.endsWith('/') &&
19+
!trimmed.endsWith('\\') &&
20+
(trimmed.includes('.') || trimmed.includes('/')) &&
21+
!trimmed.includes(' ') &&
22+
/[a-zA-Z0-9]/.test(trimmed)
2123
)
2224
}
2325

2426
export const extract_path_from_potential_string = (line: string) => {
2527
let extracted = extract_path_from_line_of_code(line)
28+
if (extracted) extracted = extracted.trim()
2629

2730
if (!extracted) {
2831
const xml_match = line.match(/^<[^>]+>([^<]+)<\/[^>]+>$/)
@@ -41,13 +44,13 @@ export const extract_path_from_potential_string = (line: string) => {
4144
}
4245

4346
if (!extracted) {
44-
let potential_path = line
47+
let potential_path = line.trim()
4548
if (potential_path.endsWith(':')) {
4649
potential_path = potential_path.slice(0, -1).trim()
4750
}
4851
const backtick_match = potential_path.match(/`([^`]+)`/)
4952
if (backtick_match && backtick_match[1]) {
50-
potential_path = backtick_match[1]
53+
potential_path = backtick_match[1].trim()
5154
}
5255

5356
if (
@@ -62,11 +65,12 @@ export const extract_path_from_potential_string = (line: string) => {
6265
}
6366
}
6467

65-
return extracted
68+
return extracted ? extracted.trim() : extracted
6669
}
6770

6871
export const extract_path_with_xml_fallback = (line: string) => {
6972
let extracted = extract_path_from_line_of_code(line)
73+
if (extracted) extracted = extracted.trim()
7074

7175
if (!extracted) {
7276
const xml_match = line.match(/^<[^>]+>([^<]+)<\/[^>]+>$/)
@@ -87,7 +91,7 @@ export const extract_path_with_xml_fallback = (line: string) => {
8791
if (!extracted) {
8892
const match = line.match(/`([^`]+)`/)
8993
if (match && match[1]) {
90-
const potential_path = match[1]
94+
const potential_path = match[1].trim()
9195
if (
9296
potential_path.includes('/') ||
9397
potential_path.includes('\\') ||
@@ -98,5 +102,5 @@ export const extract_path_with_xml_fallback = (line: string) => {
98102
}
99103
}
100104

101-
return extracted
105+
return extracted ? extracted.trim() : extracted
102106
}

‎apps/editor/src/commands/apply-response-command/utils/clipboard-parser/parsers/file-content-only-parser.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const parse_file_content_only = (params: {
3434

3535
extracted_filename = extract_path_from_line_of_code(line)
3636
if (extracted_filename) {
37+
extracted_filename = extracted_filename.trim()
3738
path_line_idx = i
3839
break
3940
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const extract_and_set_workspace_path = (params: {
55
is_single_root_folder_workspace: boolean
66
}): { workspace_name: string | undefined; relative_path: string } => {
77
const { workspace_name, relative_path } = extract_workspace_and_path({
8-
raw_file_path: params.raw_file_path,
8+
raw_file_path: params.raw_file_path.trim(),
99
is_single_root_folder_workspace: params.is_single_root_folder_workspace
1010
})
1111
return { workspace_name, relative_path }

‎apps/editor/src/commands/apply-response-command/utils/clipboard-parser/parsers/multiple-files-parser/helpers/xml-utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const extract_file_path_from_xml = (
33
): { tagName: string; path: string } | null => {
44
const match = line.match(/<([\w-]+)\s+path=["']([^"']+)["']/)
55
if (match && match[1] && match[2]) {
6-
return { tagName: match[1], path: match[2] }
6+
return { tagName: match[1], path: match[2].trim() }
77
}
88
return null
99
}

‎apps/editor/src/commands/apply-response-command/utils/clipboard-parser/parsers/relevant-files-parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const parse_relevant_files_from_response = (params: {
2828
if (trimmed.startsWith('-') || trimmed.startsWith('*')) {
2929
let path = trimmed.substring(1).trim()
3030
if (path.startsWith('`') && path.endsWith('`')) {
31-
path = path.substring(1, path.length - 1)
31+
path = path.substring(1, path.length - 1).trim()
3232
}
3333
if (path) {
3434
found_paths.add(path.replace(/[.?!]+$/, ''))

0 commit comments

Comments
 (0)