@@ -4,81 +4,85 @@ import * as vscode from 'vscode'
44import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
55
66export const replace_fragment_symbol = async ( params : {
7- instruction : string
7+ instructions : string [ ]
88 workspace_provider : WorkspaceProvider
9- } ) : Promise < string > => {
10- const regex = / # F r a g m e n t \( ( .+ ?) : ( \d + ) : ( \d + ) - ( \d + ) : ( \d + ) \) / g
9+ } ) : Promise < string [ ] > => {
10+ return Promise . all (
11+ params . instructions . map ( async ( instruction ) => {
12+ const regex = / # F r a g m e n t \( ( .+ ?) : ( \d + ) : ( \d + ) - ( \d + ) : ( \d + ) \) / g
1113
12- const matches = [ ... params . instruction . matchAll ( regex ) ]
13- if ( matches . length === 0 ) return params . instruction
14+ const matches = [ ...instruction . matchAll ( regex ) ]
15+ if ( matches . length === 0 ) return instruction
1416
15- let result = params . instruction
17+ let result = instruction
1618
17- for ( const match of matches ) {
18- const full_match = match [ 0 ]
19- const file_path = match [ 1 ]
20- const start_line = parseInt ( match [ 2 ] , 10 )
21- const end_line = parseInt ( match [ 4 ] , 10 )
19+ for ( const match of matches ) {
20+ const full_match = match [ 0 ]
21+ const file_path = match [ 1 ]
22+ const start_line = parseInt ( match [ 2 ] , 10 )
23+ const end_line = parseInt ( match [ 4 ] , 10 )
2224
23- let content = ''
24- try {
25- let absolute_path = file_path
26- if ( ! path . isAbsolute ( file_path ) ) {
27- const roots = params . workspace_provider . get_workspace_roots ( )
28- for ( const root of roots ) {
29- const test_path = path . join ( root , file_path )
30- if ( fs . existsSync ( test_path ) ) {
31- absolute_path = test_path
32- break
33- }
25+ let content = ''
26+ try {
27+ let absolute_path = file_path
28+ if ( ! path . isAbsolute ( file_path ) ) {
29+ const roots = params . workspace_provider . get_workspace_roots ( )
30+ for ( const root of roots ) {
31+ const test_path = path . join ( root , file_path )
32+ if ( fs . existsSync ( test_path ) ) {
33+ absolute_path = test_path
34+ break
35+ }
3436
35- if ( roots . length > 1 ) {
36- const workspace_name =
37- params . workspace_provider . get_workspace_name ( root )
38- const prefix = `${ workspace_name } /`
39- const prefix_win = `${ workspace_name } \\`
37+ if ( roots . length > 1 ) {
38+ const workspace_name =
39+ params . workspace_provider . get_workspace_name ( root )
40+ const prefix = `${ workspace_name } /`
41+ const prefix_win = `${ workspace_name } \\`
4042
41- if (
42- file_path . startsWith ( prefix ) ||
43- file_path . startsWith ( prefix_win )
44- ) {
45- const stripped_test_path = path . join (
46- root ,
47- file_path . substring ( prefix . length )
48- )
49- if ( fs . existsSync ( stripped_test_path ) ) {
50- absolute_path = stripped_test_path
51- break
43+ if (
44+ file_path . startsWith ( prefix ) ||
45+ file_path . startsWith ( prefix_win )
46+ ) {
47+ const stripped_test_path = path . join (
48+ root ,
49+ file_path . substring ( prefix . length )
50+ )
51+ if ( fs . existsSync ( stripped_test_path ) ) {
52+ absolute_path = stripped_test_path
53+ break
54+ }
55+ }
5256 }
5357 }
5458 }
55- }
56- }
5759
58- const file_uri = vscode . Uri . file ( absolute_path )
59- const document = await vscode . workspace . openTextDocument ( file_uri )
60+ const file_uri = vscode . Uri . file ( absolute_path )
61+ const document = await vscode . workspace . openTextDocument ( file_uri )
6062
61- const start_pos = new vscode . Position (
62- start_line - 1 ,
63- parseInt ( match [ 3 ] , 10 ) - 1
64- )
65- const end_pos = new vscode . Position (
66- end_line - 1 ,
67- parseInt ( match [ 5 ] , 10 ) - 1
68- )
69- const valid_range = document . validateRange (
70- new vscode . Range ( start_pos , end_pos )
71- )
72- content = document . getText ( valid_range )
73- } catch ( e ) {
74- content = `// Error reading file ${ file_path } `
75- }
63+ const start_pos = new vscode . Position (
64+ start_line - 1 ,
65+ parseInt ( match [ 3 ] , 10 ) - 1
66+ )
67+ const end_pos = new vscode . Position (
68+ end_line - 1 ,
69+ parseInt ( match [ 5 ] , 10 ) - 1
70+ )
71+ const valid_range = document . validateRange (
72+ new vscode . Range ( start_pos , end_pos )
73+ )
74+ content = document . getText ( valid_range )
75+ } catch ( e ) {
76+ content = `// Error reading file ${ file_path } `
77+ }
7678
77- const range_str = ` (${ start_line } -${ end_line } )`
78- const replacement = `\n\n\`${ file_path } \`${ range_str } \n\n\`\`\`\n${ content } \n\`\`\`\n\n`
79+ const range_str = ` (${ start_line } -${ end_line } )`
80+ const replacement = `\n\n\`${ file_path } \`${ range_str } \n\n\`\`\`\n${ content } \n\`\`\`\n\n`
7981
80- result = result . replace ( full_match , replacement )
81- }
82+ result = result . replace ( full_match , replacement )
83+ }
8284
83- return result
85+ return result
86+ } )
87+ )
8488}
0 commit comments