|
1 | 1 | import { cleanup_api_response } from '@/utils/cleanup-api-response' |
2 | | -import { CompletionItem, extract_workspace_and_path } from '../clipboard-parser' |
| 2 | +import { |
| 3 | + CompletionItem, |
| 4 | + TextItem, |
| 5 | + extract_workspace_and_path |
| 6 | +} from '../clipboard-parser' |
3 | 7 |
|
4 | 8 | const extract_path_and_position = ( |
5 | 9 | line: string |
@@ -28,61 +32,71 @@ const extract_path_and_position = ( |
28 | 32 | export const parse_code_completion = (params: { |
29 | 33 | response: string |
30 | 34 | is_single_root_folder_workspace: boolean |
31 | | -}): CompletionItem | null => { |
| 35 | +}): (CompletionItem | TextItem)[] | null => { |
32 | 36 | const lines = params.response.split('\n') |
33 | | - let in_code_block = false |
34 | | - let first_line_of_block: string | null = null |
35 | | - let content_lines: string[] = [] |
36 | | - |
37 | | - for (const line of lines) { |
38 | | - if (line.trim().startsWith('```')) { |
39 | | - if (in_code_block) { |
40 | | - if (first_line_of_block) { |
41 | | - const completion_info = extract_path_and_position(first_line_of_block) |
42 | | - if (completion_info) { |
43 | | - const { workspace_name, relative_path } = |
44 | | - extract_workspace_and_path({ |
45 | | - raw_file_path: completion_info.path, |
46 | | - is_single_root_folder_workspace: |
47 | | - params.is_single_root_folder_workspace |
48 | | - }) |
49 | | - return { |
50 | | - type: 'completion', |
51 | | - file_path: relative_path, |
52 | | - content: cleanup_api_response({ |
53 | | - content: content_lines.join('\n') |
54 | | - }), |
55 | | - line: completion_info.line, |
56 | | - character: completion_info.character, |
57 | | - workspace_name |
58 | | - } |
59 | | - } |
| 37 | + let code_block_start_index = -1 |
| 38 | + let code_block_end_index = -1 |
| 39 | + |
| 40 | + for (let i = 0; i < lines.length; i++) { |
| 41 | + if (lines[i].trim().startsWith('```')) { |
| 42 | + code_block_start_index = i |
| 43 | + for (let j = i + 1; j < lines.length; j++) { |
| 44 | + if (lines[j].trim().startsWith('```')) { |
| 45 | + code_block_end_index = j |
| 46 | + break |
60 | 47 | } |
61 | | - in_code_block = false |
62 | | - first_line_of_block = null |
63 | | - content_lines = [] |
64 | | - } else { |
65 | | - in_code_block = true |
66 | 48 | } |
67 | | - continue |
| 49 | + break |
68 | 50 | } |
| 51 | + } |
69 | 52 |
|
70 | | - if (in_code_block) { |
71 | | - if (first_line_of_block === null) { |
72 | | - first_line_of_block = line |
73 | | - } else { |
74 | | - content_lines.push(line) |
75 | | - } |
76 | | - } |
| 53 | + if (code_block_start_index === -1) { |
| 54 | + return null |
77 | 55 | } |
78 | 56 |
|
79 | | - // Handle unclosed block at end of file |
80 | | - if (in_code_block && first_line_of_block) { |
81 | | - const completion_info = extract_path_and_position(first_line_of_block) |
82 | | - if (completion_info) { |
83 | | - // This case is not handled and will result in returning null. |
84 | | - } |
| 57 | + if (code_block_end_index === -1) { |
| 58 | + code_block_end_index = lines.length - 1 |
85 | 59 | } |
86 | 60 |
|
87 | | - return null |
| 61 | + const first_line_of_block = lines[code_block_start_index + 1] |
| 62 | + const completion_info = extract_path_and_position(first_line_of_block) |
| 63 | + |
| 64 | + if (!completion_info) { |
| 65 | + return null |
| 66 | + } |
| 67 | + |
| 68 | + const results: (CompletionItem | TextItem)[] = [] |
| 69 | + const text_before = lines.slice(0, code_block_start_index).join('\n').trim() |
| 70 | + if (text_before) { |
| 71 | + results.push({ type: 'text', content: text_before }) |
| 72 | + } |
| 73 | + |
| 74 | + const { workspace_name, relative_path } = extract_workspace_and_path({ |
| 75 | + raw_file_path: completion_info.path, |
| 76 | + is_single_root_folder_workspace: params.is_single_root_folder_workspace |
| 77 | + }) |
| 78 | + |
| 79 | + const content_lines = lines.slice( |
| 80 | + code_block_start_index + 2, |
| 81 | + code_block_end_index |
| 82 | + ) |
| 83 | + |
| 84 | + results.push({ |
| 85 | + type: 'completion', |
| 86 | + file_path: relative_path, |
| 87 | + content: cleanup_api_response({ content: content_lines.join('\n') }), |
| 88 | + line: completion_info.line, |
| 89 | + character: completion_info.character, |
| 90 | + workspace_name |
| 91 | + }) |
| 92 | + |
| 93 | + const text_after = lines |
| 94 | + .slice(code_block_end_index + 1) |
| 95 | + .join('\n') |
| 96 | + .trim() |
| 97 | + if (text_after) { |
| 98 | + results.push({ type: 'text', content: text_after }) |
| 99 | + } |
| 100 | + |
| 101 | + return results |
88 | 102 | } |
0 commit comments