@@ -126,7 +126,7 @@ function M.setup_global_file_tracking()
126126 if file_path and file_path ~= ' ' and not vim .startswith (file_path , ' term://' ) then
127127 -- never block the UI
128128 vim .schedule (function ()
129- local stat = vim .loop .fs_stat (file_path )
129+ local stat = vim .uv .fs_stat (file_path )
130130 if stat and stat .type == ' file' then
131131 local relative_path = vim .fn .fnamemodify (file_path , ' :.' )
132132 pcall (fuzzy .access_file , relative_path )
@@ -136,6 +136,26 @@ function M.setup_global_file_tracking()
136136 end ,
137137 desc = ' Track file access for FFF frecency' ,
138138 })
139+
140+ -- make sure that this won't work correctly if autochdir plugins are enabled
141+ -- using a pure :cd command but will work using lua api or :e command
142+ vim .api .nvim_create_autocmd (' DirChanged' , {
143+ group = group ,
144+ callback = function ()
145+ local new_cwd = vim .v .event .cwd
146+ if M .is_initialized () and new_cwd and new_cwd ~= M .config .base_path then
147+ vim .schedule (function ()
148+ local ok , err = pcall (M .change_indexing_directory , new_cwd )
149+ if not ok then
150+ vim .notify (' FFF: Failed to change indexing directory: ' .. tostring (err ), vim .log .levels .ERROR )
151+ else
152+ M .config .base_path = new_cwd
153+ end
154+ end )
155+ end
156+ end ,
157+ desc = ' Automatically sync FFF directory changes' ,
158+ })
139159end
140160
141161function M .setup_commands ()
@@ -234,12 +254,7 @@ function M.find_in_git_root()
234254 return
235255 end
236256
237- local picker_ok , picker_ui = pcall (require , ' fff.picker_ui' )
238- if picker_ok then
239- picker_ui .open ({ title = ' Git Files' , cwd = git_root })
240- else
241- vim .notify (' Failed to load picker UI' , vim .log .levels .ERROR )
242- end
257+ M .find_files_in_dir (git_root )
243258end
244259
245260--- Trigger rescan of files in the current directory
@@ -331,76 +346,6 @@ function M.get_preview(file_path)
331346 return table.concat (lines , ' \n ' )
332347end
333348
334- function M .debug_file_ordering ()
335- print (' FFF Debug File Ordering' )
336- print (' =======================' )
337-
338- if not M .is_initialized () then
339- print (' File picker not initialized. Run :FFFScan first.' )
340- return
341- end
342-
343- local picker = M .picker or M .core
344- print (' Getting top 10 files with debug info...' )
345-
346- -- Enable debug mode temporarily
347- local old_debug = M .config .debug .show_scores
348- M .config .debug .show_scores = true
349-
350- -- Search with empty query to get default ordering
351- local files = picker .search_files (' ' , 10 )
352-
353- print (' 🏆 TOP FILES (in order they appear):' )
354- print (' =' .. string.rep (' =' , 70 ))
355-
356- for i , file in ipairs (files ) do
357- local frecency_stars = ' '
358- if file .frecency_score > 0 then frecency_stars = ' ⭐' .. file .frecency_score end
359-
360- -- Extract directory information
361- local dir = vim .fn .fnamemodify (file .relative_path , ' :h' )
362- local filename = vim .fn .fnamemodify (file .relative_path , ' :t' )
363- local dir_display = (dir == ' .' or dir == ' ' ) and ' root' or dir
364-
365- -- Get score information for this file
366- local score = picker .get_file_score (i )
367-
368- print (string.format (' %2d. %s%s' , i , filename , frecency_stars ))
369- print (string.format (' Path: %s/' , dir_display ))
370- print (string.format (' Debug: %s' , score and score .match_type or ' no debug info' ))
371- if score then
372- print (
373- string.format (
374- ' Total Score: %d (base=%d, name_bonus=%d, special_bonus=%d, frec=%d, dist=%d)' ,
375- score .total ,
376- score .base_score ,
377- score .filename_bonus ,
378- score .special_filename_bonus ,
379- score .frecency_boost ,
380- score .distance_penalty
381- )
382- )
383- else
384- print (' Total Score: N/A (no score data)' )
385- end
386-
387- local now = os.time ()
388- local age_hours = math.floor ((now - file .modified ) / 3600 )
389- local age_days = math.floor (age_hours / 24 )
390- print (string.format (' Age: %d hours (%d days) since last modified' , age_hours , age_days ))
391- print (' ' )
392- end
393-
394- print (' 💡 EXPLANATION:' )
395- print (' • Files are sorted by FRECENCY first (⭐ score), then by modification time' )
396- print (' • Frecency combines how often AND how recently you accessed files' )
397- print (' • The file at #1 has either:' )
398- print (' - Highest frecency score, OR' )
399- print (' - Same frecency as others but most recent modification' )
400-
401- M .config .debug .show_scores = old_debug
402- end
403-
404349function M .health_check ()
405350 local health = {
406351 ok = true ,
460405
461406function M .is_initialized () return M .state and M .state .initialized or false end
462407
408+ --- Find files in a specific directory
409+ --- @param directory string Directory path to search in
410+ function M .find_files_in_dir (directory )
411+ if not directory then
412+ vim .notify (' Directory path required for find_files_in_dir' , vim .log .levels .ERROR )
413+ return
414+ end
415+
416+ M .change_indexing_directory (directory )
417+
418+ local picker_ok , picker_ui = pcall (require , ' fff.picker_ui' )
419+ if picker_ok then
420+ picker_ui .open ({ title = ' Files in ' .. vim .fn .fnamemodify (directory , ' :t' ) })
421+ else
422+ vim .notify (' Failed to load picker UI' , vim .log .levels .ERROR )
423+ end
424+ end
425+
426+ --- Change the base directory for the file picker
427+ --- @param new_path string New directory path to use as base
428+ --- @return boolean ` true` if successful , ` false ` otherwise
429+ function M .change_indexing_directory (new_path )
430+ if not new_path or new_path == ' ' then
431+ vim .notify (' Directory path is required' , vim .log .levels .ERROR )
432+ return false
433+ end
434+
435+ local expanded_path = vim .fn .expand (new_path )
436+
437+ if vim .fn .isdirectory (expanded_path ) ~= 1 then
438+ vim .notify (' Directory does not exist: ' .. expanded_path , vim .log .levels .ERROR )
439+ return false
440+ end
441+
442+ local ok , result = pcall (fuzzy .restart_index_in_path , expanded_path )
443+ if not ok then
444+ vim .notify (' Failed to change directory: ' .. result , vim .log .levels .ERROR )
445+ return false
446+ end
447+
448+ M .config .base_path = expanded_path
449+ return true
450+ end
451+
463452return M
0 commit comments