@@ -12,10 +12,12 @@ use crate::model::FileNode;
12
12
13
13
/// Build a file tree using session data from core traversal
14
14
pub fn build_file_tree_from_session ( session : & mut Code2PromptSession ) -> Result < Vec < FileNode > > {
15
- // Load codebase data using the session
16
- session
17
- . load_codebase ( )
18
- . context ( "Failed to load codebase from session" ) ?;
15
+ // Only load codebase if not already loaded (performance optimization)
16
+ if session. data . files . is_none ( ) {
17
+ session
18
+ . load_codebase ( )
19
+ . context ( "Failed to load codebase from session" ) ?;
20
+ }
19
21
20
22
// Get the files data from session
21
23
let files_data = session
@@ -42,12 +44,9 @@ pub fn build_file_tree_from_session(session: &mut Code2PromptSession) -> Result<
42
44
Ok ( root_nodes)
43
45
}
44
46
45
- /// Build directory hierarchy from file paths - simplified approach
46
- fn build_directory_hierarchy (
47
- root : & std:: path:: Path ,
48
- file_paths : & [ String ] ,
49
- ) -> Result < Vec < FileNode > > {
50
- let entries = fs:: read_dir ( root) . context ( "Failed to read root directory" ) ?;
47
+ /// Build a lightweight file tree for navigation only (no file content loading)
48
+ pub fn build_lightweight_file_tree ( root_path : & std:: path:: Path ) -> Result < Vec < FileNode > > {
49
+ let entries = fs:: read_dir ( root_path) . context ( "Failed to read root directory" ) ?;
51
50
let mut root_children = Vec :: new ( ) ;
52
51
53
52
for entry in entries {
@@ -56,81 +55,59 @@ fn build_directory_hierarchy(
56
55
57
56
let mut node = FileNode :: new ( path, 0 ) ;
58
57
59
- // Check if this file/directory should be selected based on session data
60
- let relative_path = node. path . strip_prefix ( root) . unwrap_or ( & node. path ) ;
61
- let relative_str = relative_path. to_string_lossy ( ) ;
62
-
63
- node. is_selected = file_paths. iter ( ) . any ( |file_path| {
64
- file_path == & relative_str || file_path. starts_with ( & format ! ( "{}/" , relative_str) )
65
- } ) ;
66
-
67
- // For directories, recursively load if they contain session files
58
+ // For directories, mark as not loaded for lazy loading
68
59
if node. is_directory {
69
- let has_session_files = file_paths
70
- . iter ( )
71
- . any ( |file_path| file_path. starts_with ( & format ! ( "{}/" , relative_str) ) ) ;
72
-
73
- if has_session_files {
74
- // Load children for this directory since it contains files from session
75
- if let Ok ( children) = build_directory_children ( root, & node. path , file_paths, 1 ) {
76
- node. children = children;
77
- node. is_expanded = true ;
78
- }
79
- }
60
+ node. children_loaded = false ;
80
61
}
81
62
63
+ // Don't pre-select any files in lightweight mode
64
+ node. is_selected = false ;
65
+
82
66
root_children. push ( node) ;
83
67
}
84
68
69
+ // Sort nodes
70
+ root_children. sort_by ( |a, b| match ( a. is_directory , b. is_directory ) {
71
+ ( true , false ) => std:: cmp:: Ordering :: Less ,
72
+ ( false , true ) => std:: cmp:: Ordering :: Greater ,
73
+ _ => a. name . cmp ( & b. name ) ,
74
+ } ) ;
75
+
85
76
Ok ( root_children)
86
77
}
87
78
88
- /// Recursively build children for a directory
89
- fn build_directory_children (
79
+ /// Build directory hierarchy from file paths - simplified approach
80
+ fn build_directory_hierarchy (
90
81
root : & std:: path:: Path ,
91
- dir_path : & std:: path:: Path ,
92
82
file_paths : & [ String ] ,
93
- level : usize ,
94
83
) -> Result < Vec < FileNode > > {
95
- if level > 3 {
96
- return Ok ( Vec :: new ( ) ) ; // Prevent too deep recursion
97
- }
98
-
99
- let entries = fs:: read_dir ( dir_path) . context ( "Failed to read directory" ) ?;
100
- let mut children = Vec :: new ( ) ;
84
+ let entries = fs:: read_dir ( root) . context ( "Failed to read root directory" ) ?;
85
+ let mut root_children = Vec :: new ( ) ;
101
86
102
87
for entry in entries {
103
88
let entry = entry. context ( "Failed to read directory entry" ) ?;
104
89
let path = entry. path ( ) ;
105
90
106
- let mut node = FileNode :: new ( path, level ) ;
91
+ let mut node = FileNode :: new ( path, 0 ) ;
107
92
108
- // Check selection against session data
93
+ // Check if this file/directory should be selected based on session data
109
94
let relative_path = node. path . strip_prefix ( root) . unwrap_or ( & node. path ) ;
110
95
let relative_str = relative_path. to_string_lossy ( ) ;
111
96
112
- node. is_selected = file_paths. contains ( & relative_str. to_string ( ) ) ;
97
+ node. is_selected = file_paths. iter ( ) . any ( |file_path| {
98
+ file_path == & relative_str || file_path. starts_with ( & format ! ( "{}/" , relative_str) )
99
+ } ) ;
113
100
114
- // Recursively load subdirectories that contain session files
101
+ // For directories, mark as not loaded for lazy loading
102
+ // This prevents initial recursive loading and improves performance
115
103
if node. is_directory {
116
- let has_session_files = file_paths
117
- . iter ( )
118
- . any ( |file_path| file_path. starts_with ( & format ! ( "{}/" , relative_str) ) ) ;
119
-
120
- if has_session_files {
121
- if let Ok ( grandchildren) =
122
- build_directory_children ( root, & node. path , file_paths, level + 1 )
123
- {
124
- node. children = grandchildren;
125
- node. is_expanded = true ;
126
- }
127
- }
104
+ node. children_loaded = false ;
128
105
}
129
106
130
- children . push ( node) ;
107
+ root_children . push ( node) ;
131
108
}
132
109
133
- Ok ( children )
110
+ Ok ( root_children )
134
111
}
135
112
136
113
/// Sort file nodes (directories first, then alphabetically)
0 commit comments