@@ -24,7 +24,7 @@ export class FilesCollector {
2424 additional_paths ?: string [ ]
2525 no_context ?: boolean
2626 compact ?: boolean
27- } ) : Promise < string > {
27+ } ) : Promise < { other_files : string ; recent_files : string } > {
2828 const additional_paths = ( params ?. additional_paths ?? [ ] ) . map ( ( p ) => {
2929 if ( this . workspace_roots . length > 0 ) {
3030 return path . join ( this . workspace_roots [ 0 ] , p )
@@ -50,69 +50,78 @@ export class FilesCollector {
5050 const context_files = [ ...new Set ( context_files_list ) ]
5151
5252 // Sort context files based on modification time and selection timestamp
53- const sorted_context_files = this . _sort_context_files ( {
54- files : context_files
55- } )
56-
57- let collected_text = ''
58-
59- for ( const file_path of sorted_context_files ) {
60- try {
61- if ( ! fs . existsSync ( file_path ) ) continue
62- const stats = fs . statSync ( file_path )
63-
64- if ( stats . isDirectory ( ) ) continue
65-
66- let content = fs . readFileSync ( file_path , 'utf8' )
67-
68- const range = this . workspace_provider . get_range ( file_path )
69- if ( range ) {
70- content = this . workspace_provider . apply_range_to_content (
71- content ,
72- range
73- )
74- }
75-
76- if ( params ?. compact ) {
77- content = compact_file ( content , path . extname ( file_path ) )
78- }
79-
80- const workspace_root = this . _get_workspace_root_for_file ( file_path )
81-
82- if ( ! workspace_root ) {
83- collected_text += `<file path="${ file_path . replace (
84- / \\ / g,
85- '/'
86- ) } ">\n<![CDATA[\n${ content } \n]]>\n</file>\n`
87- continue
53+ const { other_files : other_paths , recent_files : recent_paths } =
54+ this . _sort_context_files ( {
55+ files : context_files
56+ } )
57+
58+ const process_paths = ( paths : string [ ] ) => {
59+ let collected_text = ''
60+ for ( const file_path of paths ) {
61+ try {
62+ if ( ! fs . existsSync ( file_path ) ) continue
63+ const stats = fs . statSync ( file_path )
64+
65+ if ( stats . isDirectory ( ) ) continue
66+
67+ let content = fs . readFileSync ( file_path , 'utf8' )
68+
69+ const range = this . workspace_provider . get_range ( file_path )
70+ if ( range ) {
71+ content = this . workspace_provider . apply_range_to_content (
72+ content ,
73+ range
74+ )
75+ }
76+
77+ if ( params ?. compact ) {
78+ content = compact_file ( content , path . extname ( file_path ) )
79+ }
80+
81+ const workspace_root = this . _get_workspace_root_for_file ( file_path )
82+
83+ if ( ! workspace_root ) {
84+ collected_text += `<file path="${ file_path . replace (
85+ / \\ / g,
86+ '/'
87+ ) } ">\n<![CDATA[\n${ content } \n]]>\n</file>\n`
88+ continue
89+ }
90+
91+ const relative_path = path
92+ . relative ( workspace_root , file_path )
93+ . replace ( / \\ / g, '/' )
94+
95+ // Get the workspace name to prefix the path if there are multiple workspaces
96+ let display_path = relative_path
97+ if ( this . workspace_roots . length > 1 ) {
98+ const workspace_name =
99+ this . workspace_provider . get_workspace_name ( workspace_root )
100+ display_path = `${ workspace_name } /${ relative_path } `
101+ }
102+
103+ collected_text += `<file path="${ display_path } ">\n<![CDATA[\n${ content } \n]]>\n</file>\n`
104+ } catch ( error ) {
105+ console . error ( `Error reading file ${ file_path } :` , error )
88106 }
89-
90- const relative_path = path
91- . relative ( workspace_root , file_path )
92- . replace ( / \\ / g, '/' )
93-
94- // Get the workspace name to prefix the path if there are multiple workspaces
95- let display_path = relative_path
96- if ( this . workspace_roots . length > 1 ) {
97- const workspace_name =
98- this . workspace_provider . get_workspace_name ( workspace_root )
99- display_path = `${ workspace_name } /${ relative_path } `
100- }
101-
102- collected_text += `<file path="${ display_path } ">\n<![CDATA[\n${ content } \n]]>\n</file>\n`
103- } catch ( error ) {
104- console . error ( `Error reading file ${ file_path } :` , error )
105107 }
108+ return collected_text
106109 }
107110
108- return collected_text
111+ return {
112+ other_files : process_paths ( other_paths ) ,
113+ recent_files : process_paths ( recent_paths )
114+ }
109115 }
110116
111117 private _get_workspace_root_for_file ( file_path : string ) : string | undefined {
112118 return this . workspace_provider . get_workspace_root_for_file ( file_path )
113119 }
114120
115- private _sort_context_files ( params : { files : string [ ] } ) : string [ ] {
121+ private _sort_context_files ( params : { files : string [ ] } ) : {
122+ other_files : string [ ]
123+ recent_files : string [ ]
124+ } {
116125 const recently_modified : Array < { path : string ; mtime : number } > = [ ]
117126 const older_files : Array < { path : string ; timestamp : number } > = [ ]
118127 const now = Date . now ( )
@@ -153,9 +162,9 @@ export class FilesCollector {
153162
154163 recently_modified . sort ( ( a , b ) => a . mtime - b . mtime )
155164
156- return [
157- ... older_files . map ( ( f ) => f . path ) ,
158- ... recently_modified . map ( ( f ) => f . path )
159- ]
165+ return {
166+ other_files : older_files . map ( ( f ) => f . path ) ,
167+ recent_files : recently_modified . map ( ( f ) => f . path )
168+ }
160169 }
161170}
0 commit comments