@@ -149,6 +149,50 @@ def _read_file_content(file_path: str) -> str:
149149 return f"Error reading file: { str (e )} "
150150
151151
152+ def _sort_children (children : list [dict [str , Any ]]) -> list [dict [str , Any ]]:
153+ """
154+ Sort children nodes with:
155+ 1. README.md first
156+ 2. Regular files (not starting with dot)
157+ 3. Hidden files (starting with dot)
158+ 4. Regular directories (not starting with dot)
159+ 5. Hidden directories (starting with dot)
160+ All groups are sorted alphanumerically within themselves.
161+
162+ Parameters
163+ ----------
164+ children : list[dict[str, Any]]
165+ List of file and directory nodes to sort.
166+
167+ Returns
168+ -------
169+ list[dict[str, Any]]
170+ Sorted list according to the specified order.
171+ """
172+ # Separate files and directories
173+ files = [child for child in children if child ["type" ] == "file" ]
174+ directories = [child for child in children if child ["type" ] == "directory" ]
175+
176+ # Find README.md
177+ readme_files = [f for f in files if f ["name" ].lower () == "readme.md" ]
178+ other_files = [f for f in files if f ["name" ].lower () != "readme.md" ]
179+
180+ # Separate hidden and regular files/directories
181+ regular_files = [f for f in other_files if not f ["name" ].startswith ("." )]
182+ hidden_files = [f for f in other_files if f ["name" ].startswith ("." )]
183+ regular_dirs = [d for d in directories if not d ["name" ].startswith ("." )]
184+ hidden_dirs = [d for d in directories if d ["name" ].startswith ("." )]
185+
186+ # Sort each group alphanumerically
187+ regular_files .sort (key = lambda x : x ["name" ])
188+ hidden_files .sort (key = lambda x : x ["name" ])
189+ regular_dirs .sort (key = lambda x : x ["name" ])
190+ hidden_dirs .sort (key = lambda x : x ["name" ])
191+
192+ # Combine all groups in the desired order
193+ return readme_files + regular_files + hidden_files + regular_dirs + hidden_dirs
194+
195+
152196def _scan_directory (
153197 path : str ,
154198 query : dict [str , Any ],
@@ -329,6 +373,9 @@ def _scan_directory(
329373 result ["file_count" ] += subdir ["file_count" ]
330374 result ["dir_count" ] += 1 + subdir ["dir_count" ]
331375
376+ result ["children" ] = _sort_children (result ["children" ])
377+ return result
378+
332379 except PermissionError :
333380 print (f"Permission denied: { path } " )
334381
@@ -390,7 +437,7 @@ def _create_file_content_string(files: list[dict[str, Any]]) -> str:
390437 Create a formatted string of file contents with separators.
391438
392439 This function takes a list of files and generates a formatted string where each file’s
393- content is separated by a divider. If a README.md file is found, it is placed at the top.
440+ content is separated by a divider.
394441
395442 Parameters
396443 ----------
@@ -405,21 +452,9 @@ def _create_file_content_string(files: list[dict[str, Any]]) -> str:
405452 output = ""
406453 separator = "=" * 48 + "\n "
407454
408- # First add README.md if it exists
409- for file in files :
410- if not file ["content" ]:
411- continue
412-
413- if file ["path" ].lower () == "/readme.md" :
414- output += separator
415- output += f"File: { file ['path' ]} \n "
416- output += separator
417- output += f"{ file ['content' ]} \n \n "
418- break
419-
420455 # Then add all other files in their original order
421456 for file in files :
422- if not file ["content" ] or file [ "path" ]. lower () == "/readme.md" :
457+ if not file ["content" ]:
423458 continue
424459
425460 output += separator
0 commit comments