@@ -7,41 +7,30 @@ import { shrink_file } from '../context/utils/shrink-file/shrink-file'
77import { is_binary_file } from './is-binary'
88import { normalize_path } from './normalize-path'
99
10- export class FilesCollector {
11- private workspace_provider : WorkspaceProvider
12- private open_editors_provider ?: OpenEditorsProvider
13- private workspace_roots : string [ ] = [ ]
14-
15- constructor ( params : {
10+ export namespace FilesCollector {
11+ export const collect_files = async ( params : {
1612 workspace_provider : WorkspaceProvider
1713 open_editors_provider ?: OpenEditorsProvider
18- } ) {
19- this . workspace_provider = params . workspace_provider
20- this . open_editors_provider = params . open_editors_provider
21-
22- this . workspace_roots = this . workspace_provider . get_workspace_roots ( )
23- }
24-
25- async collect_files ( params ?: {
2614 additional_paths ?: string [ ]
2715 no_context ?: boolean
2816 shrink ?: boolean
29- } ) : Promise < { other_files : string ; recent_files : string } > {
30- const additional_paths = ( params ?. additional_paths ?? [ ] ) . map ( ( p ) => {
31- if ( this . workspace_roots . length > 0 ) {
32- return path . join ( this . workspace_roots [ 0 ] , p )
17+ } ) : Promise < { other_files : string ; recent_files : string } > => {
18+ const workspace_roots = params . workspace_provider . get_workspace_roots ( )
19+ const additional_paths = ( params . additional_paths ?? [ ] ) . map ( ( p ) => {
20+ if ( workspace_roots . length > 0 ) {
21+ return path . join ( workspace_roots [ 0 ] , p )
3322 }
3423 return p
3524 } )
3625
3726 const context_files_list : string [ ] = [ ]
3827
39- if ( params ? .no_context ) {
28+ if ( params . no_context ) {
4029 context_files_list . push ( ...additional_paths )
4130 } else {
42- const workspace_files = this . workspace_provider . get_checked_files ( )
31+ const workspace_files = params . workspace_provider . get_checked_files ( )
4332 const open_editor_files =
44- this . open_editors_provider ?. get_checked_files ( ) || [ ]
33+ params . open_editors_provider ?. get_checked_files ( ) || [ ]
4534 context_files_list . push (
4635 ...workspace_files ,
4736 ...open_editor_files ,
@@ -53,7 +42,8 @@ export class FilesCollector {
5342
5443 // Sort context files based on modification time and selection timestamp
5544 const { other_files : other_paths , recent_files : recent_paths } =
56- this . sort_context_files ( {
45+ sort_context_files ( {
46+ workspace_provider : params . workspace_provider ,
5747 files : context_files
5848 } )
5949
@@ -72,20 +62,21 @@ export class FilesCollector {
7262 let content = is_binary ? 'Binary file' : buffer . toString ( 'utf8' )
7363
7464 if ( ! is_binary ) {
75- const range = this . workspace_provider . get_range ( file_path )
65+ const range = params . workspace_provider . get_range ( file_path )
7666 if ( range ) {
77- content = this . workspace_provider . apply_range_to_content (
67+ content = params . workspace_provider . apply_range_to_content (
7868 content ,
7969 range
8070 )
8171 }
8272
83- if ( params ? .shrink ) {
73+ if ( params . shrink ) {
8474 content = shrink_file ( content , path . extname ( file_path ) )
8575 }
8676 }
8777
88- const workspace_root = this . _get_workspace_root_for_file ( file_path )
78+ const workspace_root =
79+ params . workspace_provider . get_workspace_root_for_file ( file_path )
8980
9081 let display_path = normalize_path ( file_path )
9182 if ( workspace_root ) {
@@ -94,9 +85,9 @@ export class FilesCollector {
9485 )
9586
9687 display_path = relative_path
97- if ( this . workspace_roots . length > 1 ) {
88+ if ( workspace_roots . length > 1 ) {
9889 const workspace_name =
99- this . workspace_provider . get_workspace_name ( workspace_root )
90+ params . workspace_provider . get_workspace_name ( workspace_root )
10091 display_path = `${ workspace_name } /${ relative_path } `
10192 }
10293 }
@@ -120,14 +111,13 @@ export class FilesCollector {
120111 }
121112 }
122113
123- private _get_workspace_root_for_file ( file_path : string ) : string | undefined {
124- return this . workspace_provider . get_workspace_root_for_file ( file_path )
125- }
126-
127- public sort_context_files ( params : { files : string [ ] } ) : {
114+ export const sort_context_files = ( params : {
115+ workspace_provider : WorkspaceProvider
116+ files : string [ ]
117+ } ) : {
128118 other_files : string [ ]
129119 recent_files : string [ ]
130- } {
120+ } => {
131121 const recently_modified : Array < { path : string ; mtime : number } > = [ ]
132122 const older_files : Array < { path : string ; timestamp : number } > = [ ]
133123 const now = Date . now ( )
@@ -141,7 +131,7 @@ export class FilesCollector {
141131
142132 const mtime = stats . mtimeMs
143133 const selection_timestamp =
144- this . workspace_provider . get_selection_timestamp ( file_path ) ?? now
134+ params . workspace_provider . get_selection_timestamp ( file_path ) ?? now
145135
146136 if ( now - mtime < ONE_AND_HALF_HOURS ) {
147137 recently_modified . push ( { path : file_path , mtime } )
0 commit comments