-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathpicker_ui.lua
More file actions
2681 lines (2265 loc) · 92.9 KB
/
picker_ui.lua
File metadata and controls
2681 lines (2265 loc) · 92.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local M = {}
local conf = require('fff.conf')
local file_picker = require('fff.file_picker')
local preview = require('fff.file_picker.preview')
local utils = require('fff.utils')
local location_utils = require('fff.location_utils')
local combo_renderer = require('fff.combo_renderer')
local list_renderer = require('fff.list_renderer')
local scrollbar = require('fff.scrollbar')
local rust = require('fff.rust')
local BORDER_PRESETS = {
single = { '┌', '─', '┐', '│', '┘', '─', '└', '│' },
double = { '╔', '═', '╗', '║', '╝', '═', '╚', '║' },
rounded = { '╭', '─', '╮', '│', '╯', '─', '╰', '│' },
solid = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' },
shadow = { '', '', ' ', ' ', ' ', ' ', ' ', '' },
none = { '', '', '', '', '', '', '', '' },
}
local T_JUNCTION_PRESETS = {
single = { '├', '┤' },
double = { '╠', '╣' },
rounded = { '├', '┤' }, -- Rounded only affects corners
solid = { '▌', '▐' },
shadow = { '', '' },
none = { '', '' },
}
--- Get border characters from vim.o.winborder for custom connected borders
--- @return table Array of 8 border characters
--- @return table Array of 2 T-junction characters (left, right)
local function get_border_chars()
local winborder = vim.o.winborder or 'single'
if BORDER_PRESETS[winborder] then return BORDER_PRESETS[winborder], T_JUNCTION_PRESETS[winborder] end
-- Fallback to single for unknown border styles
return BORDER_PRESETS.single, T_JUNCTION_PRESETS.single
end
local function get_prompt_position()
local config = M.state.config
if config and config.layout and config.layout.prompt_position then
local terminal_width = vim.o.columns
local terminal_height = vim.o.lines
return utils.resolve_config_value(
config.layout.prompt_position,
terminal_width,
terminal_height,
function(value) return utils.is_one_of(value, { 'top', 'bottom' }) end,
'bottom',
'layout.prompt_position'
)
end
return 'bottom'
end
local function get_preview_position()
local config = M.state.config
if config and config.layout and config.layout.preview_position then
local terminal_width = vim.o.columns
local terminal_height = vim.o.lines
local position = utils.resolve_config_value(
config.layout.preview_position,
terminal_width,
terminal_height,
function(value) return utils.is_one_of(value, { 'left', 'right', 'top', 'bottom' }) end,
'right',
'layout.preview_position'
)
local flex = config.layout.flex
if flex then
local size = flex.size or 130
local wrap = flex.wrap or 'top'
if terminal_width < size then return wrap end
end
return position
end
return 'right'
end
local function compute_layout(config)
local debug_enabled_in_preview = M.enabled_preview() and config.debug and config.debug.enabled or false
local terminal_width = vim.o.columns
local terminal_height = vim.o.lines
local width_ratio = utils.resolve_config_value(
config.layout.width,
terminal_width,
terminal_height,
utils.is_valid_ratio,
0.8,
'layout.width'
)
local height_ratio = utils.resolve_config_value(
config.layout.height,
terminal_width,
terminal_height,
utils.is_valid_ratio,
0.8,
'layout.height'
)
local width = math.floor(terminal_width * width_ratio)
local height = math.floor(terminal_height * height_ratio)
local col_ratio_default = 0.5 - (width_ratio / 2)
local col_ratio = col_ratio_default
if config.layout.col ~= nil then
col_ratio = utils.resolve_config_value(
config.layout.col,
terminal_width,
terminal_height,
utils.is_valid_ratio,
col_ratio_default,
'layout.col'
)
end
local row_ratio_default = 0.5 - (height_ratio / 2)
local row_ratio = row_ratio_default
if config.layout.row ~= nil then
row_ratio = utils.resolve_config_value(
config.layout.row,
terminal_width,
terminal_height,
utils.is_valid_ratio,
row_ratio_default,
'layout.row'
)
end
local col = math.floor(terminal_width * col_ratio)
local row = math.floor(terminal_height * row_ratio)
local prompt_position = get_prompt_position()
local preview_position = get_preview_position()
local preview_size_ratio = utils.resolve_config_value(
config.layout.preview_size,
terminal_width,
terminal_height,
utils.is_valid_ratio,
0.4,
'layout.preview_size'
)
local layout_config = {
total_width = width,
total_height = height,
start_col = col,
start_row = row,
preview_position = preview_position,
prompt_position = prompt_position,
debug_enabled = debug_enabled_in_preview,
preview_width = M.enabled_preview() and math.floor(width * preview_size_ratio) or 0,
preview_height = M.enabled_preview() and math.floor(height * preview_size_ratio) or 0,
separator_width = 3,
file_info_height = debug_enabled_in_preview and 10 or 0,
}
local layout = M.calculate_layout_dimensions(layout_config)
return layout, debug_enabled_in_preview
end
--- Build window config tables for list, input, preview, and file_info windows.
--- @param layout table The computed layout from calculate_layout_dimensions
--- @param config table The picker config
--- @return table window_configs Table with list, input, preview, file_info keys
local function build_window_configs(layout, config)
local border_chars, t_junctions = get_border_chars()
local prompt_position = get_prompt_position()
local title = ' ' .. (config.title or 'FFFiles') .. ' '
-- List border: when prompt at bottom, list has top+sides (no bottom); when top, T-junctions at top
local list_border = prompt_position == 'bottom'
and { border_chars[1], border_chars[2], border_chars[3], border_chars[4], '', '', '', border_chars[8] }
or {
t_junctions[1],
border_chars[2],
t_junctions[2],
border_chars[4],
border_chars[5],
border_chars[6],
border_chars[7],
border_chars[8],
}
local list_cfg = {
relative = 'editor',
width = layout.list_width,
height = layout.list_height,
col = layout.list_col,
row = layout.list_row,
border = list_border,
style = 'minimal',
}
if prompt_position == 'bottom' then
list_cfg.title = title
list_cfg.title_pos = 'left'
end
-- Input border: inverse of list border
local input_border = prompt_position == 'bottom'
and {
t_junctions[1],
border_chars[2],
t_junctions[2],
border_chars[4],
border_chars[5],
border_chars[6],
border_chars[7],
border_chars[8],
}
or { border_chars[1], border_chars[2], border_chars[3], border_chars[4], '', '', '', border_chars[8] }
local input_cfg = {
relative = 'editor',
width = layout.input_width,
height = 1,
col = layout.input_col,
row = layout.input_row,
border = input_border,
style = 'minimal',
}
if prompt_position == 'top' then
input_cfg.title = title
input_cfg.title_pos = 'left'
end
local preview_cfg = nil
if layout.preview then
preview_cfg = {
relative = 'editor',
width = layout.preview.width,
height = layout.preview.height,
col = layout.preview.col,
row = layout.preview.row,
style = 'minimal',
border = border_chars,
title = ' Preview ',
title_pos = 'left',
}
end
local file_info_cfg = nil
if layout.file_info then
file_info_cfg = {
relative = 'editor',
width = layout.file_info.width,
height = layout.file_info.height,
col = layout.file_info.col,
row = layout.file_info.row,
style = 'minimal',
border = border_chars,
title = ' File Info ',
title_pos = 'left',
}
end
return {
list = list_cfg,
input = input_cfg,
preview = preview_cfg,
file_info = file_info_cfg,
}
end
--- Calculate layout dimensions and positions for all windows
--- @param cfg table
--- @return table Layout configuration
function M.calculate_layout_dimensions(cfg)
local BORDER_SIZE = 2
local PROMPT_HEIGHT = 2
local SEPARATOR_WIDTH = 1
local SEPARATOR_HEIGHT = 1
if not utils.is_one_of(cfg.preview_position, { 'left', 'right', 'top', 'bottom' }) then
error('Invalid preview position: ' .. tostring(cfg.preview_position))
end
local layout = {}
local preview_enabled = M.enabled_preview()
-- Section 1: Base dimensions and bounds checking
local total_width = math.max(0, cfg.total_width - BORDER_SIZE)
local total_height = math.max(0, cfg.total_height - BORDER_SIZE - PROMPT_HEIGHT)
-- Section 2: Calculate dimensions based on preview position
if cfg.preview_position == 'left' then
local separator_width = preview_enabled and SEPARATOR_WIDTH or 0
local list_width = math.max(0, total_width - cfg.preview_width - separator_width)
local list_height = total_height
layout.list_col = cfg.start_col + cfg.preview_width + 3 -- +3 for borders and separator
layout.list_width = list_width
layout.list_height = list_height
layout.input_col = layout.list_col
layout.input_width = list_width
if preview_enabled then
layout.preview = {
col = cfg.start_col + 1,
row = cfg.start_row + 1,
width = cfg.preview_width,
height = list_height,
}
end
elseif cfg.preview_position == 'right' then
local separator_width = preview_enabled and SEPARATOR_WIDTH or 0
local list_width = math.max(0, total_width - cfg.preview_width - separator_width)
local list_height = total_height
layout.list_col = cfg.start_col + 1
layout.list_width = list_width
layout.list_height = list_height
layout.input_col = layout.list_col
layout.input_width = list_width
if preview_enabled then
layout.preview = {
col = cfg.start_col + list_width + 3, -- +3 for borders and separator (matches original)
row = cfg.start_row + 1,
width = cfg.preview_width,
height = list_height,
}
end
elseif cfg.preview_position == 'top' then
local separator_height = preview_enabled and SEPARATOR_HEIGHT or 0
local list_height = math.max(0, total_height - cfg.preview_height - separator_height)
layout.list_col = cfg.start_col + 1
layout.list_width = total_width
layout.list_height = list_height
layout.input_col = layout.list_col
layout.input_width = total_width
layout.list_start_row = cfg.start_row + (preview_enabled and (cfg.preview_height + separator_height) or 0) + 1
if preview_enabled then
layout.preview = {
col = cfg.start_col + 1,
row = cfg.start_row + 1,
width = total_width,
height = cfg.preview_height,
}
end
else
local separator_height = preview_enabled and SEPARATOR_HEIGHT or 0
local list_height = math.max(0, total_height - cfg.preview_height - separator_height)
layout.list_col = cfg.start_col + 1
layout.list_width = total_width
layout.list_height = list_height
layout.input_col = layout.list_col
layout.input_width = total_width
layout.list_start_row = cfg.start_row + 1
if preview_enabled then
layout.preview = {
col = cfg.start_col + 1,
width = total_width,
height = cfg.preview_height,
}
end
end
-- Section 3: Position prompt and adjust row positions
if cfg.preview_position == 'left' or cfg.preview_position == 'right' then
if cfg.prompt_position == 'top' then
layout.input_row = cfg.start_row + 1
layout.list_row = cfg.start_row + PROMPT_HEIGHT + 1
else
layout.list_row = cfg.start_row + 1
layout.input_row = cfg.start_row + cfg.total_height - BORDER_SIZE
end
if layout.preview then
if cfg.prompt_position == 'top' then
layout.preview.row = cfg.start_row + 1
layout.preview.height = cfg.total_height - BORDER_SIZE
else
layout.preview.row = cfg.start_row + 1
layout.preview.height = cfg.total_height - BORDER_SIZE
end
end
else
local list_start_row = layout.list_start_row
if cfg.prompt_position == 'top' then
layout.input_row = list_start_row
layout.list_row = list_start_row + BORDER_SIZE
layout.list_height = math.max(0, layout.list_height - BORDER_SIZE)
else
layout.list_row = list_start_row
layout.input_row = list_start_row + layout.list_height + 1
end
if cfg.preview_position == 'bottom' and layout.preview then
if cfg.prompt_position == 'top' then
layout.preview.row = layout.list_row + layout.list_height + 1
else
layout.preview.row = layout.input_row + PROMPT_HEIGHT
end
end
end
-- Section 4: Position debug panel (if enabled)
if cfg.debug_enabled and preview_enabled and layout.preview then
if cfg.preview_position == 'left' or cfg.preview_position == 'right' then
layout.file_info = {
width = layout.preview.width,
height = cfg.file_info_height,
col = layout.preview.col,
row = layout.preview.row,
}
layout.preview.row = layout.preview.row + cfg.file_info_height + SEPARATOR_HEIGHT + 1
layout.preview.height = math.max(3, layout.preview.height - cfg.file_info_height - SEPARATOR_HEIGHT - 1)
else
layout.file_info = {
width = layout.preview.width,
height = cfg.file_info_height,
col = layout.preview.col,
row = layout.preview.row,
}
layout.preview.row = layout.preview.row + cfg.file_info_height + SEPARATOR_HEIGHT + 1
layout.preview.height = math.max(3, layout.preview.height - cfg.file_info_height - SEPARATOR_HEIGHT - 1)
end
end
return layout
end
local preview_config = conf.get().preview
if preview_config then preview.setup(preview_config) end
M.state = {
active = false,
layout = nil,
input_win = nil,
input_buf = nil,
list_win = nil,
list_buf = nil,
file_info_win = nil,
file_info_buf = nil,
preview_win = nil,
preview_buf = nil,
items = {},
filtered_items = {},
cursor = 1,
top = 1,
query = '',
item_line_map = {},
location = nil, -- Current location from search results
-- History cycling state
history_offset = nil, -- Current offset in history (nil = not cycling, 0 = first query)
next_search_force_combo_boost = false, -- Force combo boost on next search (for history recall)
-- Combo state
combo_visible = true, -- Whether to show combo indicator (hidden after significant navigation)
combo_initial_cursor = nil, -- Initial cursor position when combo was shown
-- Pagination state
pagination = {
page_index = 0, -- Current page index (0-based)
page_size = 20, -- Items per page (updated dynamically)
total_matched = 0, -- Total results from last search
prefetch_margin = 5, -- Trigger refetch when within N items of edge
-- Grep file-based pagination: stores the file_offset for each visited page
-- so we can go backwards. grep_file_offsets[1] = 0 (page 0 starts at file 0),
-- grep_file_offsets[2] = next_file_offset from page 0, etc.
grep_file_offsets = {},
grep_next_file_offset = 0,
},
config = nil, -- @type FFFConfig|nil
-- Custom renderer (optional, defaults to file_renderer if not provided)
renderer = nil,
ns_id = nil,
last_status_info = nil,
last_preview_file = nil,
last_preview_location = nil, -- Track last preview location to detect changes
preview_timer = nil, ---@type uv.uv_timer_t|nil -- Separate timer for preview updates
preview_debounce_ms = 100, -- Preview is more expensive, debounce more
-- Set of selected file paths: { [filepath] = true }
-- Uses Set pattern: selected items exist as keys with value true, deselected items are removed (nil)
-- This allows O(1) lookup and automatic deduplication without needing to filter false values
selected_files = {},
-- Grep mode: per-occurrence selection keyed by "path:line:col"
-- Values are the full item tables so quickfix can be built from selections alone (survives page changes)
selected_items = {},
-- Mode: nil or 'grep' — controls search/render/select behaviour
mode = nil,
-- Grep-specific config overrides (max_file_size, smart_case, etc.)
grep_config = nil,
-- Grep search mode: 'plain', 'regex', or 'fuzzy'
grep_mode = 'plain',
-- Regex fallback error: set when regex compilation fails and search fell back to literal
grep_regex_fallback_error = nil,
-- Cross-mode suggestion state: when primary search yields 0 results,
-- we query the opposite mode and show those as suggestions.
-- suggestion_items: array of items from the opposite search
-- suggestion_source: 'grep' (suggestions from grep) or 'files' (suggestions from file search)
suggestion_items = nil,
suggestion_source = nil,
}
function M.create_ui()
local config = M.state.config
if not config then return false end
if not M.state.ns_id then
M.state.ns_id = vim.api.nvim_create_namespace('fff_picker_status')
combo_renderer.init(M.state.ns_id)
end
local layout, debug_enabled_in_preview = compute_layout(config)
M.state.layout = layout
M.state.input_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = M.state.input_buf })
M.state.list_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = M.state.list_buf })
if M.enabled_preview() then
M.state.preview_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = M.state.preview_buf })
end
if debug_enabled_in_preview then
M.state.file_info_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = M.state.file_info_buf })
else
M.state.file_info_buf = nil
end
local win_configs = build_window_configs(layout, config)
M.state.list_win = vim.api.nvim_open_win(M.state.list_buf, false, win_configs.list)
if debug_enabled_in_preview and win_configs.file_info then
M.state.file_info_win = vim.api.nvim_open_win(M.state.file_info_buf, false, win_configs.file_info)
else
M.state.file_info_win = nil
end
if M.enabled_preview() and win_configs.preview then
M.state.preview_win = vim.api.nvim_open_win(M.state.preview_buf, false, win_configs.preview)
end
M.state.input_win = vim.api.nvim_open_win(M.state.input_buf, false, win_configs.input)
M.setup_buffers()
M.setup_windows()
M.setup_keymaps()
vim.api.nvim_set_current_win(M.state.input_win)
preview.set_preview_window(M.state.preview_win)
M.update_results_sync()
M.clear_preview()
M.update_status()
return true
end
function M.setup_buffers()
vim.api.nvim_buf_set_name(M.state.input_buf, 'fffile search')
vim.api.nvim_buf_set_name(M.state.list_buf, 'fffiles list')
if M.enabled_preview() then vim.api.nvim_buf_set_name(M.state.preview_buf, 'fffile preview') end
vim.api.nvim_set_option_value('buftype', 'prompt', { buf = M.state.input_buf })
vim.api.nvim_set_option_value('filetype', 'fff_input', { buf = M.state.input_buf })
vim.fn.prompt_setprompt(M.state.input_buf, M.state.config.prompt)
-- Changing the contents of the input buffer will trigger Neovim to guess the language in order to provide
-- syntax highlighting. This makes sure that it's always off.
vim.api.nvim_create_autocmd('Syntax', {
buffer = M.state.input_buf,
callback = function() vim.api.nvim_set_option_value('syntax', '', { buf = M.state.input_buf }) end,
})
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = M.state.list_buf })
vim.api.nvim_set_option_value('filetype', 'fff_list', { buf = M.state.list_buf })
vim.api.nvim_set_option_value('modifiable', false, { buf = M.state.list_buf })
if M.state.file_info_buf then
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = M.state.file_info_buf })
vim.api.nvim_set_option_value('filetype', 'fff_file_info', { buf = M.state.file_info_buf })
vim.api.nvim_set_option_value('modifiable', false, { buf = M.state.file_info_buf })
end
if M.enabled_preview() then
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = M.state.preview_buf })
vim.api.nvim_set_option_value('filetype', 'fff_preview', { buf = M.state.preview_buf })
vim.api.nvim_set_option_value('modifiable', false, { buf = M.state.preview_buf })
end
end
function M.setup_windows()
local hl = M.state.config.hl
local win_hl = string.format('Normal:%s,FloatBorder:%s,FloatTitle:%s', hl.normal, hl.border, hl.title)
vim.api.nvim_set_option_value('wrap', false, { win = M.state.input_win })
vim.api.nvim_set_option_value('cursorline', false, { win = M.state.input_win })
vim.api.nvim_set_option_value('number', false, { win = M.state.input_win })
vim.api.nvim_set_option_value('relativenumber', false, { win = M.state.input_win })
vim.api.nvim_set_option_value('signcolumn', 'no', { win = M.state.input_win })
vim.api.nvim_set_option_value('foldcolumn', '0', { win = M.state.input_win })
vim.api.nvim_set_option_value('winhighlight', win_hl, { win = M.state.input_win })
vim.api.nvim_set_option_value('wrap', false, { win = M.state.list_win })
vim.api.nvim_set_option_value('cursorline', false, { win = M.state.list_win })
vim.api.nvim_set_option_value('number', false, { win = M.state.list_win })
vim.api.nvim_set_option_value('relativenumber', false, { win = M.state.list_win })
vim.api.nvim_set_option_value('signcolumn', 'yes:1', { win = M.state.list_win }) -- Enable signcolumn for git status borders
vim.api.nvim_set_option_value('foldcolumn', '0', { win = M.state.list_win })
vim.api.nvim_set_option_value('winhighlight', win_hl, { win = M.state.list_win })
if M.state.file_info_win and vim.api.nvim_win_is_valid(M.state.file_info_win) then
vim.api.nvim_set_option_value('wrap', false, { win = M.state.file_info_win })
vim.api.nvim_set_option_value('cursorline', false, { win = M.state.file_info_win })
vim.api.nvim_set_option_value('number', false, { win = M.state.file_info_win })
vim.api.nvim_set_option_value('relativenumber', false, { win = M.state.file_info_win })
vim.api.nvim_set_option_value('signcolumn', 'no', { win = M.state.file_info_win })
vim.api.nvim_set_option_value('foldcolumn', '0', { win = M.state.file_info_win })
vim.api.nvim_set_option_value('winhighlight', win_hl, { win = M.state.file_info_win })
end
if M.enabled_preview() then
vim.api.nvim_set_option_value('wrap', false, { win = M.state.preview_win })
vim.api.nvim_set_option_value('cursorline', M.state.mode == 'grep', { win = M.state.preview_win })
local cursorlineopt = utils.resolve_config_value(
preview_config.cursorlineopt,
vim.o.columns,
vim.o.lines,
function(value)
if type(value) ~= 'string' or #value == 0 then return false end
local has_line = false
local has_screenline = false
for opt in value:gmatch('[^,]+') do
if not utils.is_one_of(opt:gsub('%s+', ''), { 'line', 'screenline', 'number', 'both' }) then return false end
if opt == 'line' or opt == 'both' then has_line = true end
if opt == 'screenline' then has_screenline = true end
end
if has_line and has_screenline then return false end
return true
end,
'both',
'preview.cursorlineopt'
)
vim.api.nvim_set_option_value(
'cursorlineopt',
M.state.mode == 'grep' and cursorlineopt or vim.o.cursorlineopt,
{ win = M.state.preview_win }
)
vim.api.nvim_set_option_value(
'number',
M.state.mode == 'grep' or (preview_config and preview_config.line_numbers or false),
{ win = M.state.preview_win }
)
vim.api.nvim_set_option_value('relativenumber', false, { win = M.state.preview_win })
vim.api.nvim_set_option_value('signcolumn', 'no', { win = M.state.preview_win })
vim.api.nvim_set_option_value('foldcolumn', '0', { win = M.state.preview_win })
vim.api.nvim_set_option_value('winhighlight', win_hl, { win = M.state.preview_win })
end
local picker_group = vim.api.nvim_create_augroup('fff_picker_focus', { clear = true })
--- Check if a window is one of the picker windows
--- @param win number Window handle to check
--- @return boolean
local function is_picker_window(win)
if not win or not vim.api.nvim_win_is_valid(win) then return false end
local picker_windows = { M.state.input_win, M.state.list_win }
if M.state.preview_win then table.insert(picker_windows, M.state.preview_win) end
if M.state.file_info_win then table.insert(picker_windows, M.state.file_info_win) end
for _, picker_win in ipairs(picker_windows) do
if picker_win and vim.api.nvim_win_is_valid(picker_win) and win == picker_win then return true end
end
return false
end
vim.api.nvim_create_autocmd('WinLeave', {
group = picker_group,
callback = function()
if not M.state.active then return end
local leaving_win = vim.api.nvim_get_current_win()
-- Only care if we're leaving a picker window
if not is_picker_window(leaving_win) then return end
-- Schedule check to allow the window switch to complete
vim.schedule(function()
if not M.state.active then return end
local new_win = vim.api.nvim_get_current_win()
-- Close picker only if we moved to a non-picker window
if not is_picker_window(new_win) then M.close() end
end)
end,
desc = 'Close picker when focus leaves picker windows',
})
vim.api.nvim_create_autocmd('VimResized', {
group = picker_group,
callback = function()
if not M.state.active then return end
vim.schedule(function()
if not M.state.active then return end
M.relayout()
end)
end,
desc = 'Re-layout picker on terminal resize',
})
end
local function set_keymap(mode, keys, handler, opts)
local normalized_keys
if type(keys) == 'string' then
normalized_keys = { keys }
elseif type(keys) == 'table' then
normalized_keys = keys
else
normalized_keys = {}
end
for _, key in ipairs(normalized_keys) do
vim.keymap.set(mode, key, handler, opts)
end
end
function M.focus_list_win()
if not M.state.active then return end
if not M.state.list_win or not vim.api.nvim_win_is_valid(M.state.list_win) then return end
vim.cmd('stopinsert')
vim.api.nvim_set_current_win(M.state.list_win)
end
function M.focus_preview_win()
if not M.state.active then return end
if not M.state.preview_win or not vim.api.nvim_win_is_valid(M.state.preview_win) then return end
vim.cmd('stopinsert')
vim.api.nvim_set_current_win(M.state.preview_win)
end
local function move_list_cursor(direction)
if not M.state.active then return end
local items = M.state.filtered_items
if #items == 0 then return end
local new_cursor = M.state.cursor + direction
new_cursor = math.max(1, math.min(new_cursor, #items))
if new_cursor ~= M.state.cursor then
M.state.cursor = new_cursor
M.render_list()
if M.state.mode == 'grep' or M.state.suggestion_source == 'grep' then
M.update_preview_smart()
else
M.update_preview()
end
M.update_status()
end
end
function M.setup_keymaps()
local keymaps = M.state.config.keymaps
local input_opts = { buffer = M.state.input_buf, noremap = true, silent = true }
local list_opts = { buffer = M.state.list_buf, noremap = true, silent = true }
vim.keymap.set('i', '<C-w>', function()
local col = vim.fn.col('.') - 1
local line = vim.fn.getline('.')
local prompt_len = #M.state.config.prompt
if col <= prompt_len then return '' end
local text_part = line:sub(prompt_len + 1, col)
local after_cursor = line:sub(col + 1)
local new_text = text_part:gsub('%S*%s*$', '')
local new_line = M.state.config.prompt .. new_text .. after_cursor
local new_col = prompt_len + #new_text
vim.fn.setline('.', new_line)
vim.fn.cursor(vim.fn.line('.'), new_col + 1)
return ''
end, input_opts)
set_keymap('i', keymaps.move_up, M.move_up, input_opts)
set_keymap('i', keymaps.move_down, M.move_down, input_opts)
set_keymap('i', keymaps.cycle_previous_query, M.recall_query_from_history, input_opts)
set_keymap('n', 'j', M.move_down, input_opts)
set_keymap('n', 'k', M.move_up, input_opts)
set_keymap('n', keymaps.focus_list, M.focus_list_win, input_opts)
set_keymap('n', keymaps.focus_preview, M.focus_preview_win, input_opts)
-- Input buffer: both modes
set_keymap({ 'i', 'n' }, keymaps.close, M.close, input_opts)
set_keymap({ 'i', 'n' }, keymaps.select, M.select, input_opts)
set_keymap({ 'i', 'n' }, keymaps.select_split, function() M.select('split') end, input_opts)
set_keymap({ 'i', 'n' }, keymaps.select_vsplit, function() M.select('vsplit') end, input_opts)
set_keymap({ 'i', 'n' }, keymaps.select_tab, function() M.select('tab') end, input_opts)
set_keymap({ 'i', 'n' }, keymaps.preview_scroll_up, M.scroll_preview_up, input_opts)
set_keymap({ 'i', 'n' }, keymaps.preview_scroll_down, M.scroll_preview_down, input_opts)
set_keymap({ 'i', 'n' }, keymaps.toggle_debug, M.toggle_debug, input_opts)
set_keymap({ 'i', 'n' }, keymaps.toggle_select, M.toggle_select, input_opts)
set_keymap({ 'i', 'n' }, keymaps.send_to_quickfix, M.send_to_quickfix, input_opts)
set_keymap({ 'i', 'n' }, keymaps.cycle_grep_modes, M.cycle_grep_modes, input_opts)
-- List buffer
set_keymap('n', keymaps.close, M.close, list_opts)
set_keymap('n', 'q', M.close, list_opts)
set_keymap('n', 'j', function() move_list_cursor(1) end, list_opts)
set_keymap('n', 'k', function() move_list_cursor(-1) end, list_opts)
set_keymap('n', 'i', M.focus_input_win, list_opts)
set_keymap('n', keymaps.focus_preview, M.focus_preview_win, list_opts)
set_keymap('n', keymaps.select, M.select, list_opts)
set_keymap('n', keymaps.select_split, function() M.select('split') end, list_opts)
set_keymap('n', keymaps.select_vsplit, function() M.select('vsplit') end, list_opts)
set_keymap('n', keymaps.select_tab, function() M.select('tab') end, list_opts)
set_keymap('n', keymaps.preview_scroll_up, M.scroll_preview_up, list_opts)
set_keymap('n', keymaps.preview_scroll_down, M.scroll_preview_down, list_opts)
set_keymap('n', keymaps.toggle_debug, M.toggle_debug, list_opts)
set_keymap('n', keymaps.toggle_select, M.toggle_select, list_opts)
set_keymap('n', keymaps.send_to_quickfix, M.send_to_quickfix, list_opts)
-- Preview buffer
if M.state.preview_buf then
local preview_opts = { buffer = M.state.preview_buf, noremap = true, silent = true }
set_keymap('n', keymaps.close, M.close, preview_opts)
set_keymap('n', 'q', M.close, preview_opts)
set_keymap('n', 'i', M.focus_input_win, preview_opts)
set_keymap('n', keymaps.focus_list, M.focus_list_win, preview_opts)
set_keymap('n', keymaps.select, M.select, preview_opts)
set_keymap('n', keymaps.select_split, function() M.select('split') end, preview_opts)
set_keymap('n', keymaps.select_vsplit, function() M.select('vsplit') end, preview_opts)
set_keymap('n', keymaps.select_tab, function() M.select('tab') end, preview_opts)
set_keymap('n', keymaps.toggle_debug, M.toggle_debug, preview_opts)
set_keymap('n', keymaps.toggle_select, M.toggle_select, preview_opts)
set_keymap('n', keymaps.send_to_quickfix, M.send_to_quickfix, preview_opts)
end
vim.api.nvim_buf_attach(M.state.input_buf, false, {
on_lines = function()
vim.schedule(function() M.on_input_change() end)
end,
})
end
function M.focus_input_win()
if not M.state.active then return end
if not M.state.input_win or not vim.api.nvim_win_is_valid(M.state.input_win) then return end
vim.api.nvim_set_current_win(M.state.input_win)
vim.api.nvim_win_call(M.state.input_win, function() vim.cmd('startinsert!') end)
end
function M.toggle_debug()
local config_changed = conf.toggle_debug()
if config_changed then
local current_query = M.state.query
local current_items = M.state.items
local current_cursor = M.state.cursor
-- Preserve mode-specific state across close/open cycle
local current_mode = M.state.mode
local current_renderer = M.state.renderer
local current_grep_mode = M.state.grep_mode
local current_grep_config = M.state.grep_config
local current_filtered_items = M.state.filtered_items
local current_selected_files = M.state.selected_files
local current_selected_items = M.state.selected_items
M.close()
M.open({
mode = current_mode,
renderer = current_renderer,
grep_config = current_grep_config,
})
M.state.query = current_query
M.state.items = current_items
M.state.cursor = current_cursor
M.state.grep_mode = current_grep_mode
M.state.filtered_items = current_filtered_items
M.state.selected_files = current_selected_files
M.state.selected_items = current_selected_items
M.render_list()
M.update_preview()
M.update_status()
vim.schedule(function()
if M.state.active and M.state.input_win then
vim.api.nvim_set_current_win(M.state.input_win)
vim.cmd('startinsert!')
end
end)
else
M.update_results()
end
end
--- Cycle through grep search modes based on configured modes list.
--- Only works when the picker is in grep mode. Triggers a re-search
--- with the current query using the new mode.
function M.cycle_grep_modes()
if not M.state.active or M.state.mode ~= 'grep' then return end
local config = conf.get()
-- Use grep_config.modes if provided, otherwise fall back to global config
---@diagnostic disable-next-line: undefined-field
local modes = (M.state.grep_config and M.state.grep_config.modes)
or config.grep.modes
or { 'plain', 'regex', 'fuzzy' }
-- If only one mode configured, no cycling needed
if #modes <= 1 then return end
local current_idx = 1
for i, m in ipairs(modes) do
if m == M.state.grep_mode then
current_idx = i
break
end
end
M.state.grep_mode = modes[(current_idx % #modes) + 1]
-- Clear fallback error when switching away from regex
if M.state.grep_mode ~= 'regex' then M.state.grep_regex_fallback_error = nil end
-- Force status refresh by clearing the cached value
M.state.last_status_info = nil
M.update_status()
-- Re-run the search with the current query in the new mode
if M.state.query ~= '' then M.update_results_sync() end
end
function M.on_input_change()
if not M.state.active then return end
local lines = vim.api.nvim_buf_get_lines(M.state.input_buf, 0, -1, false)
local prompt_len = #M.state.config.prompt
local query = ''
if #lines > 1 then
-- join without any separator because it is a use case for a path copy from the terminal buffer
local all_text = table.concat(lines, '')
if all_text:sub(1, prompt_len) == M.state.config.prompt then
query = all_text:sub(prompt_len + 1)
else
query = all_text
end
query = query:gsub('\r', ''):match('^%s*(.-)%s*$') or ''
vim.api.nvim_set_option_value('modifiable', true, { buf = M.state.input_buf })
vim.api.nvim_buf_set_lines(M.state.input_buf, 0, -1, false, { M.state.config.prompt .. query })
-- Move cursor to end
vim.schedule(function()
if M.state.active and M.state.input_win and vim.api.nvim_win_is_valid(M.state.input_win) then
vim.api.nvim_win_set_cursor(M.state.input_win, { 1, prompt_len + #query })
end
end)