forked from dmtrKovalenko/fff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.lua
More file actions
943 lines (781 loc) · 32.7 KB
/
preview.lua
File metadata and controls
943 lines (781 loc) · 32.7 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
local utils = require('fff.utils')
local file_picker = require('fff.file_picker')
local image = require('fff.file_picker.image')
local location_utils = require('fff.location_utils')
local rust = require('fff.rust')
local M = {}
-- Preview buffers are scratch buffers. Detect the file's language and attach
-- highlighting directly, but keep buffer filetype empty to avoid ftplugin and
-- LSP side effects that are meant for real editing buffers.
local function attach_preview_highlighter(bufnr, filetype)
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then return end
pcall(vim.treesitter.stop, bufnr)
vim.api.nvim_set_option_value('filetype', '', { buf = bufnr })
vim.api.nvim_set_option_value('syntax', '', { buf = bufnr })
if not filetype or filetype == '' then return end
local lang_ok, lang = pcall(vim.treesitter.language.get_lang, filetype)
if not lang_ok or not lang then lang = filetype end
if pcall(vim.treesitter.language.add, lang) then
pcall(vim.treesitter.start, bufnr, lang)
else
vim.api.nvim_set_option_value('syntax', filetype, { buf = bufnr })
end
end
local function set_buffer_lines(bufnr, lines)
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then return end
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
end
local function append_buffer_lines(bufnr, lines)
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then return end
if not lines or #lines == 0 then return end
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
local current_lines = vim.api.nvim_buf_line_count(bufnr)
vim.api.nvim_buf_set_lines(bufnr, current_lines, current_lines, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
end
local function find_existing_buffer(file_path)
local abs_path = vim.fn.resolve(vim.fn.fnamemodify(file_path, ':p'))
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(bufnr) then
local buf_name = vim.api.nvim_buf_get_name(bufnr)
if buf_name ~= '' then
local buf_path = vim.fn.resolve(vim.fn.fnamemodify(buf_name, ':p'))
if buf_path == abs_path then return bufnr end
end
end
end
return nil
end
local function cleanup_file_operation()
if M.state.file_operation then
if M.state.file_operation.fd then pcall(vim.uv.fs_close, M.state.file_operation.fd) end
M.state.file_operation = nil
end
end
--- Process raw chunk data into complete lines, joining any leftover bytes
--- from the previous chunk and storing any trailing partial line for the next.
--- @param data string Raw chunk data
--- @return string[] Complete lines (may be empty if the entire chunk is a partial line)
local function split_chunk_with_remainder(data)
if not data or data == '' then return {} end
local fo = M.state.file_operation
local prefix = fo and fo.remainder or ''
local combined = prefix .. data
local lines = vim.split(combined, '\n', { plain = true })
if combined:sub(-1) ~= '\n' then
-- Data doesn't end on a line boundary: last element is a partial line
local partial = table.remove(lines) or ''
if fo then fo.remainder = partial end
else
-- Data ends on a line boundary: remove the trailing empty element
if #lines > 0 and lines[#lines] == '' then table.remove(lines) end
if fo then fo.remainder = '' end
end
return lines
end
local function init_dynamic_loading_async(file_path, callback)
cleanup_file_operation()
M.state.loaded_lines = 0
M.state.total_file_lines = nil
M.state.has_more_content = true
M.state.is_loading = false
local generation = M.state.preview_generation
vim.uv.fs_open(file_path, 'r', 438, function(err, fd)
-- Stale callback: preview moved on to a different file
if M.state.preview_generation ~= generation then
if fd then pcall(vim.uv.fs_close, fd) end
return
end
if err or not fd then
callback(false, 'Failed to open file: ' .. (err or 'unknown error'))
return
end
M.state.file_operation = {
fd = fd,
file_path = file_path,
position = 0,
remainder = '',
}
callback(true)
end)
end
local function load_forward_chunk_async(target_size, callback)
if not M.state.file_operation or not M.state.file_operation.fd then
callback('', 'No file handle available')
return
end
M.state.is_loading = true
local chunk_size = target_size or (M.config.chunk_size or 16384)
local generation = M.state.preview_generation
vim.uv.fs_read(M.state.file_operation.fd, chunk_size, M.state.file_operation.position, function(err, data)
vim.schedule(function()
-- Stale callback: a newer preview has started, discard this result
if M.state.preview_generation ~= generation then return end
M.state.is_loading = false
if err then
callback('', 'Read error: ' .. err)
return
end
if not data or #data == 0 then
M.state.has_more_content = false
-- Flush any remaining partial line as the final piece of data
local final_remainder = M.state.file_operation and M.state.file_operation.remainder or ''
cleanup_file_operation()
if final_remainder ~= '' then
callback(final_remainder .. '\n', nil)
else
callback('', nil)
end
return
end
if M.state.file_operation then M.state.file_operation.position = M.state.file_operation.position + #data end
callback(data, nil)
end)
end)
end
local function load_next_chunk_async(chunk_size, callback)
if not M.state.file_operation or not M.state.has_more_content or M.state.is_loading then
callback('', nil)
return
end
load_forward_chunk_async(chunk_size, callback)
end
-- Forward declaration for ensure_content_loaded_async (used in read_file_streaming_async callback)
local ensure_content_loaded_async
local function read_file_streaming_async(file_path, callback)
local generation = M.state.preview_generation
init_dynamic_loading_async(file_path, function(success, error_msg)
if M.state.preview_generation ~= generation then return end
if not success then
callback(nil, error_msg)
return
end
-- Calculate initial chunk size based on location information
local initial_chunk_size = M.config.chunk_size
if M.state.location then
local target_line = location_utils.get_target_line(M.state.location)
if target_line then
-- Estimate bytes needed: assume ~100 bytes per line average
-- Add some buffer (50%) to account for variation in line lengths
local estimated_bytes = target_line * 100 * 1.5
-- Cap at reasonable maximum to avoid memory issues
local max_initial_chunk = M.config.max_size or (10 * 1024 * 1024) -- 10MB default
initial_chunk_size = math.min(estimated_bytes, max_initial_chunk)
-- Ensure we don't go below the standard chunk size
initial_chunk_size = math.max(initial_chunk_size, M.config.chunk_size)
end
end
load_next_chunk_async(initial_chunk_size, function(data, err)
if M.state.preview_generation ~= generation then return end
if data and data ~= '' then
local lines = split_chunk_with_remainder(data)
M.state.loaded_lines = #lines
M.state.content_height = #lines
-- If we have a location and didn't load enough lines, try to load more
local loading_more = false
if M.state.location then
local target_line = location_utils.get_target_line(M.state.location)
if target_line and #lines < target_line and M.state.has_more_content then
loading_more = true
vim.schedule(function()
if M.state.preview_generation == generation then ensure_content_loaded_async(target_line) end
end)
end
end
callback(lines, err, loading_more)
else
callback(nil, err)
end
end)
end)
end
ensure_content_loaded_async = function(target_line)
if not M.state.bufnr or not vim.api.nvim_buf_is_valid(M.state.bufnr) then return end
if not M.state.has_more_content or M.state.is_loading then return end
-- Guard against missing file handle: without it load_next_chunk_async returns
-- synchronously with empty data, which triggers apply_location_highlighting
-- -> ensure_content_loaded_async again, causing infinite recursion (stack overflow).
if not M.state.file_operation then
M.state.has_more_content = false
return
end
local current_buffer_lines = vim.api.nvim_buf_line_count(M.state.bufnr)
local buffer_needed = target_line + 50
if current_buffer_lines >= buffer_needed then return end
local generation = M.state.preview_generation
-- Use a larger chunk to reach the target faster instead of many small 8KB reads
local lines_needed = buffer_needed - current_buffer_lines
local estimated_bytes = math.max(M.config.chunk_size, lines_needed * 120)
load_next_chunk_async(estimated_bytes, function(data, err)
-- Stale callback: preview moved on to a different file
if M.state.preview_generation ~= generation then return end
if not M.state.bufnr or not vim.api.nvim_buf_is_valid(M.state.bufnr) then return end
if err then return end
if data and data ~= '' then
local chunk_lines = split_chunk_with_remainder(data)
if #chunk_lines > 0 then append_buffer_lines(M.state.bufnr, chunk_lines) end
M.state.content_height = vim.api.nvim_buf_line_count(M.state.bufnr)
M.state.loaded_lines = M.state.content_height
-- If we still haven't loaded enough, schedule another chunk
if M.state.loaded_lines < buffer_needed and M.state.has_more_content then
vim.schedule(function()
if M.state.preview_generation == generation then ensure_content_loaded_async(target_line) end
end)
else
-- Enough content loaded — re-apply location highlighting so the
-- preview scrolls to the correct line now that it exists in the buffer
M.apply_location_highlighting(M.state.bufnr)
end
else
-- EOF with no additional data — mark loading as finished to prevent
-- apply_location_highlighting -> ensure_content_loaded_async recursion,
-- then apply highlighting with whatever content we have.
M.state.has_more_content = false
M.apply_location_highlighting(M.state.bufnr)
end
end)
end
local function link_buffer_content(source_bufnr, target_bufnr)
local lines = vim.api.nvim_buf_get_lines(source_bufnr, 0, -1, false)
set_buffer_lines(target_bufnr, lines)
local source_ft = vim.api.nvim_get_option_value('filetype', { buf = source_bufnr })
if source_ft ~= '' then attach_preview_highlighter(target_bufnr, source_ft) end
M.state.has_more_content = false
M.state.total_file_lines = #lines
M.state.loaded_lines = #lines
M.state.content_height = #lines
return true
end
M.config = nil
M.state = {
bufnr = nil,
winid = nil,
current_file = nil,
scroll_offset = 0,
content_height = 0,
loaded_lines = 0,
total_file_lines = nil,
loading_chunk_size = 1000,
is_loading = false,
has_more_content = true,
file_handle = nil, ---@type uv.uv_fs_t|nil
file_operation = nil, -- Ongoing file operation: {fd?: any, file_path?: string, position?: number}
location = nil, -- Current location data for highlighting
location_namespace = nil, -- Namespace for location highlighting
preview_generation = 0, -- Monotonically increasing token to detect stale async callbacks
is_binary_preview = false, -- Whether the current preview is a hex dump
hex_byte_offset = 0, -- Next byte offset for hex dump paging
}
--- Setup preview configuration
--- @param config table Configuration options
function M.setup(config)
M.config = config or {}
-- Create namespace for location highlighting
if not M.state.location_namespace then
M.state.location_namespace = vim.api.nvim_create_namespace('fff_preview_location')
end
end
--- Check if file is too big for initial preview (inspired by snacks.nvim)
--- @param file_path string Path to the file
--- @return boolean True if file is too big for initial preview
function M.is_big_file(file_path)
-- Only check file size for early detection - no line limits with dynamic loading
local stat = vim.uv.fs_stat(file_path)
if stat and stat.size > M.config.max_size then return true end
return false
end
--- Get file information
--- @param file_path string Path to the file
--- @return table | nil File information
function M.get_file_info(file_path)
local stat = vim.uv.fs_stat(file_path)
if not stat then return nil end
local info = {
name = vim.fn.fnamemodify(file_path, ':t'),
path = file_path,
size = stat.size,
modified = stat.mtime.sec,
accessed = stat.atime.sec,
type = stat.type,
}
info.extension = vim.fn.fnamemodify(file_path, ':e'):lower()
info.filetype = utils.detect_filetype(file_path) or 'text'
info.size_formatted = utils.format_file_size(info.size)
info.modified_formatted = os.date('%Y-%m-%d %H:%M:%S', info.modified)
info.accessed_formatted = os.date('%Y-%m-%d %H:%M:%S', info.accessed)
return info
end
--- Create file info content without custom borders
--- @param file table File information from search results
--- @param info table File system information
--- @param file_index number|nil Index of the file in search results (for score lookup)
--- @return table Lines for the file info content
function M.create_file_info_content(file, info, file_index)
local lines = {}
local score = file_index and file_picker.get_file_score(file_index) or nil
table.insert(
lines,
string.format('Size: %-8s │ Total Score: %d', info.size_formatted or 'N/A', score and score.total or 0)
)
table.insert(
lines,
string.format('Type: %-8s │ Match Type: %s', info.filetype or 'text', score and score.match_type or 'unknown')
)
table.insert(
lines,
string.format(
'Git: %-8s │ Frecency Mod: %d, Acc: %d',
file.git_status or 'clear',
file.modification_frecency_score or 0,
file.access_frecency_score or 0
)
)
if score then
table.insert(
lines,
string.format(
'Score Breakdown: base=%d, name_bonus=%d, special_bonus=%d',
score.base_score,
score.filename_bonus,
score.special_filename_bonus
)
)
table.insert(
lines,
string.format(
'Score Modifiers: frec_boost=%d, dist_penalty=%d, current_penalty=%d',
score.frecency_boost,
score.distance_penalty,
score.current_file_penalty or 0
)
)
else
table.insert(lines, 'Score Breakdown: N/A (no score data available)')
end
table.insert(lines, '')
-- Time information section
table.insert(lines, 'TIMINGS')
table.insert(lines, string.rep('─', 50))
table.insert(lines, string.format('Modified: %s', info.modified_formatted or 'N/A'))
table.insert(lines, string.format('Last Access: %s', info.accessed_formatted or 'N/A'))
return lines
end
--- Create file info content for grep mode items.
--- Shows grep-specific metadata: match location, frecency, file info.
---@param item table Grep match item with file + match metadata
---@param info table File system information from get_file_info
---@return table Lines for the file info content
function M.create_grep_file_info_content(item, info)
local lines = {}
-- Match location info
local match_count = item.match_ranges and #item.match_ranges or 0
table.insert(
lines,
string.format('Match: line %d, col %d │ Ranges: %d', item.line_number or 0, (item.col or 0) + 1, match_count)
)
table.insert(
lines,
string.format('Byte Offset: %-12d │ Size: %s', item.byte_offset or 0, info.size_formatted or 'N/A')
)
table.insert(lines, string.format('Type: %-8s │ Git: %s', info.filetype or 'text', item.git_status or 'clean'))
-- Fuzzy match score (only available in fuzzy grep mode)
if item.fuzzy_score then table.insert(lines, string.format('Fuzzy Score: %d', item.fuzzy_score)) end
-- Frecency info
local total = item.total_frecency_score or 0
local acc = item.access_frecency_score or 0
local mod = item.modification_frecency_score or 0
table.insert(lines, string.format('Frecency: total=%d, access=%d, modification=%d', total, acc, mod))
-- Ordering explanation
table.insert(lines, 'Order: files sorted by frecency desc, matches by line asc')
table.insert(lines, '')
-- Time information section
table.insert(lines, 'TIMINGS')
table.insert(lines, string.rep('─', 50))
table.insert(lines, string.format('Modified: %s', info.modified_formatted or 'N/A'))
table.insert(lines, string.format('Last Access: %s', info.accessed_formatted or 'N/A'))
return lines
end
--- Preview a regular file
--- @param file_path string Path to the file
--- @param bufnr number Buffer number for preview
--- @return boolean Success status
function M.preview_file(file_path, bufnr)
-- Early size detection to prevent memory issues
if M.is_big_file(file_path) then
local info = M.get_file_info(file_path)
local lines = {
'File too large for preview',
string.format(
'Size: %s (max: %s)',
info and info.size_formatted or 'Unknown',
string.format('%.1fMB', M.config.max_size / 1024 / 1024)
),
'',
'Use a text editor to view this file.',
}
set_buffer_lines(bufnr, lines)
return true
end
local info = M.get_file_info(file_path)
if not info then return false end
-- if the buffer is already opened for this file we reuse the buffer directly
local existing_bufnr = find_existing_buffer(file_path)
if existing_bufnr then
local success = link_buffer_content(existing_bufnr, bufnr)
if success then
local file_config = M.get_file_config(file_path)
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
vim.api.nvim_set_option_value('readonly', true, { buf = bufnr })
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = bufnr })
if M.state.winid and vim.api.nvim_win_is_valid(M.state.winid) then
vim.api.nvim_set_option_value('wrap', file_config.wrap_lines or M.config.wrap_lines, { win = M.state.winid })
end
M.state.scroll_offset = 0
-- Apply location highlighting if available (delayed to ensure buffer is ready)
local gen = M.state.preview_generation
vim.schedule(function()
if M.state.preview_generation == gen then M.apply_location_highlighting(bufnr) end
end)
return true
end
end
M.state.current_file = file_path
M.state.bufnr = bufnr
local generation = M.state.preview_generation
read_file_streaming_async(file_path, function(content, err, loading_more)
if M.state.preview_generation ~= generation then
-- Preview moved on to a different file, discard
cleanup_file_operation()
return
end
if err or not content then
if M.state.current_file == file_path then
set_buffer_lines(bufnr, { 'Failed to load file: ' .. (err or 'unknown error') })
end
return
end
if M.state.current_file == file_path then
-- Guard against buffer being destroyed while async read was in-flight
if not vim.api.nvim_buf_is_valid(bufnr) then return end
M.clear_preview_visual_state(bufnr)
set_buffer_lines(bufnr, content)
local file_config = M.get_file_config(file_path)
attach_preview_highlighter(bufnr, info.filetype)
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
vim.api.nvim_set_option_value('readonly', true, { buf = bufnr })
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = bufnr })
if M.state.winid and vim.api.nvim_win_is_valid(M.state.winid) then
vim.api.nvim_set_option_value('wrap', file_config.wrap_lines or M.config.wrap_lines, { win = M.state.winid })
end
M.state.content_height = #content
M.state.scroll_offset = 0
-- Apply location highlighting if available (delayed to ensure buffer is ready).
-- Skip when more content is being loaded asynchronously to reach the target line —
-- ensure_content_loaded_async will re-apply highlighting once the target is in the buffer.
if not loading_more then
vim.schedule(function()
if M.state.preview_generation == generation then M.apply_location_highlighting(bufnr) end
end)
end
end
end)
return true
end
-- Hex preview highlight support: dynamically create hl groups from "#rrggbb"
local hex_ns = nil
local hex_hl_cache = {}
local function ensure_hex_ns()
if not hex_ns then hex_ns = vim.api.nvim_create_namespace('fff_hex_preview') end
return hex_ns
end
local function get_hex_hl_group(hex_color)
local cached = hex_hl_cache[hex_color]
if cached then return cached end
local group = 'FffHex_' .. hex_color:sub(2)
vim.api.nvim_set_hl(0, group, { fg = hex_color })
hex_hl_cache[hex_color] = group
return group
end
--- Apply hex highlight spans to a buffer
--- @param bufnr number Buffer number
--- @param highlights table Array of {line_0idx, col_start, col_end, "#rrggbb"}
--- @param line_offset number Lines to add to each highlight line index (for header)
local function apply_hex_highlights(bufnr, highlights, line_offset)
if not highlights or not vim.api.nvim_buf_is_valid(bufnr) then return end
local ns = ensure_hex_ns()
for _, hl in ipairs(highlights) do
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, hl[1] + line_offset, hl[2], {
end_col = hl[3],
hl_group = get_hex_hl_group(hl[4]),
})
end
end
--- Load a page of hex dump content from the Rust backend
--- @param file_path string Path to the binary file
--- @param byte_offset number Byte offset to start reading from
--- @return table|nil Result with lines, highlights, has_more, next_offset
local function load_hex_page(file_path, byte_offset)
local ok, result = pcall(rust.hex_dump, file_path, byte_offset, 4096)
if ok and result then return result end
return nil
end
--- Load more hex content when scrolling near the end of the buffer
local function load_more_hex_content()
if not M.state.bufnr or not vim.api.nvim_buf_is_valid(M.state.bufnr) then return end
if not M.state.has_more_content or not M.state.current_file then return end
local current_lines = vim.api.nvim_buf_line_count(M.state.bufnr)
local result = load_hex_page(M.state.current_file, M.state.hex_byte_offset)
if result and result.lines and #result.lines > 0 then
append_buffer_lines(M.state.bufnr, result.lines)
M.state.hex_byte_offset = result.next_offset
M.state.has_more_content = result.has_more
M.state.content_height = vim.api.nvim_buf_line_count(M.state.bufnr)
M.state.loaded_lines = M.state.content_height
apply_hex_highlights(M.state.bufnr, result.highlights, current_lines)
else
M.state.has_more_content = false
end
end
--- Preview a binary file using hexyl-powered hex dump with paging
--- @param file_path string Path to the file
--- @param bufnr number Buffer number for preview
--- @return boolean Success status
function M.preview_binary_file(file_path, bufnr)
local info = M.get_file_info(file_path)
local lines = {}
M.state.is_binary_preview = true
M.state.hex_byte_offset = 0
-- Build header synchronously (file -b is fast, typically <10ms)
if vim.fn.executable('file') == 1 then
local output = vim.fn.system({ 'file', '-b', file_path })
if vim.v.shell_error == 0 and output then
local file_type = output:gsub('\n', '')
table.insert(lines, 'Binary file: ' .. file_type)
if info and info.size_formatted then table.insert(lines, 'Size: ' .. info.size_formatted) end
table.insert(lines, '')
end
end
local hex_result = load_hex_page(file_path, 0)
if hex_result and hex_result.lines then
for _, hex_line in ipairs(hex_result.lines) do
table.insert(lines, hex_line)
end
M.state.hex_byte_offset = hex_result.next_offset
M.state.has_more_content = hex_result.has_more
end
set_buffer_lines(bufnr, lines)
attach_preview_highlighter(bufnr, 'text')
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
vim.api.nvim_set_option_value('readonly', true, { buf = bufnr })
M.state.content_height = #lines
M.state.loaded_lines = #lines
if hex_result and hex_result.highlights then
local header_lines = #lines - (hex_result.lines and #hex_result.lines or 0)
apply_hex_highlights(bufnr, hex_result.highlights, header_lines)
end
return true
end
--- Get file-specific configuration
--- @param file_path string Path to the file
--- @return table Configuration for the file
function M.get_file_config(file_path)
if not M.config or not M.config.filetypes then return {} end
local filetype = utils.detect_filetype(file_path) or 'text'
return M.config.filetypes[filetype] or {}
end
--- @param file_path string Path to the file or directory
--- @param bufnr number Buffer number for preview
--- @param location table|nil Optional location data for highlighting
--- @param is_binary boolean|nil Whether the file is binary (from Rust indexer)
--- @return boolean if the preview was successful
function M.preview(file_path, bufnr, location, is_binary)
if not file_path or file_path == '' then return false end
-- Bump generation to invalidate any in-flight async callbacks from previous previews
M.state.preview_generation = M.state.preview_generation + 1
if M.state.file_handle then
---@diagnostic disable-next-line: undefined-field
M.state.file_handle:close()
M.state.file_handle = nil
end
M.state.loaded_lines = 0
M.state.total_file_lines = nil
M.state.has_more_content = true
M.state.is_loading = false
M.state.hex_byte_offset = 0
M.state.is_binary_preview = false
M.state.current_file = file_path
M.state.bufnr = bufnr
M.state.location = location
if image.is_image(file_path) then
M.clear_buffer(bufnr)
if not M.state.winid or not vim.api.nvim_win_is_valid(M.state.winid) then return false end
return image.display_image(file_path, bufnr)
elseif is_binary then
return M.preview_binary_file(file_path, bufnr)
else
return M.preview_file(file_path, bufnr)
end
end
function M.scroll(lines)
if not M.state.bufnr or not vim.api.nvim_buf_is_valid(M.state.bufnr) then return end
if not M.state.winid or not vim.api.nvim_win_is_valid(M.state.winid) then return end
local win_height = vim.api.nvim_win_get_height(M.state.winid)
local current_buffer_lines = vim.api.nvim_buf_line_count(M.state.bufnr)
local current_offset = M.state.scroll_offset or 0
local new_offset = current_offset + lines
-- If scrolling down and approaching end of loaded content, try to load more
if lines > 0 and not M.state.is_loading then
local target_line = new_offset + win_height
local buffer_needed = target_line + 20
if current_buffer_lines < buffer_needed and M.state.has_more_content then
if M.state.is_binary_preview then
load_more_hex_content()
-- Re-read line count after loading more
current_buffer_lines = vim.api.nvim_buf_line_count(M.state.bufnr)
else
ensure_content_loaded_async(target_line)
end
end
end
local content_height = current_buffer_lines
local half_screen = math.floor(win_height / 2)
local max_scroll = math.max(0, content_height + half_screen - win_height)
new_offset = math.max(0, math.min(max_scroll, new_offset))
if new_offset ~= current_offset then
M.state.scroll_offset = new_offset
M.state.content_height = content_height
local target_line = math.min(content_height, math.max(1, new_offset + 1))
vim.api.nvim_win_call(M.state.winid, function()
vim.api.nvim_win_set_cursor(M.state.winid, { target_line, 0 })
vim.cmd('normal! zt')
end)
end
end
--- Set preview window
--- @param winid number Window ID for the preview
function M.set_preview_window(winid) M.state.winid = winid end
--- Update file info buffer
--- @param file table File information from search results (or grep match item)
--- @param bufnr number Buffer number for file info
--- @param file_index number|nil Index of the file in search results (for score lookup, file mode only)
--- @return boolean Success status
function M.update_file_info_buffer(file, bufnr, file_index)
if not file then
set_buffer_lines(bufnr, { 'No file selected' })
return false
end
local info = M.get_file_info(file.path)
if not info then
set_buffer_lines(bufnr, { 'File info unavailable' })
return false
end
-- Detect grep mode items by the presence of line_number (grep-specific field)
local file_info_lines
if file.line_number ~= nil then
file_info_lines = M.create_grep_file_info_content(file, info)
else
file_info_lines = M.create_file_info_content(file, info, file_index)
end
set_buffer_lines(bufnr, file_info_lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
vim.api.nvim_set_option_value('readonly', true, { buf = bufnr })
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = bufnr })
-- Set wrap on the window (wrap is window-local, not buffer-local)
local wins = vim.fn.win_findbuf(bufnr)
for _, win in ipairs(wins) do
if vim.api.nvim_win_is_valid(win) then vim.api.nvim_set_option_value('wrap', false, { win = win }) end
end
return true
end
function M.clear_preview_visual_state(bufnr)
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then return end
-- Only clear visual state, don't affect buffer functionality
-- Clear namespaces and extmarks for this buffer only
vim.api.nvim_buf_clear_namespace(bufnr, -1, 0, -1)
-- Clear location highlights
if M.state.location_namespace then location_utils.clear_location_highlights(bufnr, M.state.location_namespace) end
local wins = vim.fn.win_findbuf(bufnr)
for _, win in ipairs(wins) do
if vim.api.nvim_win_is_valid(win) then
-- Reset folds
pcall(vim.api.nvim_win_call, win, function()
if vim.fn.has('folding') == 1 then
vim.cmd('normal! zE') -- eliminate all folds
vim.opt_local.foldenable = false -- disable folding
end
end)
end
end
image.clear_buffer_images(bufnr)
end
function M.clear_buffer(bufnr)
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then return end
cleanup_file_operation()
M.clear_preview_visual_state(bufnr)
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
attach_preview_highlighter(bufnr, '')
vim.api.nvim_set_option_value('buftype', 'nofile', { buf = bufnr })
set_buffer_lines(bufnr, {})
end
function M.clear()
-- Bump generation to invalidate any in-flight async callbacks
M.state.preview_generation = M.state.preview_generation + 1
cleanup_file_operation()
M.state.loaded_lines = 0
M.state.total_file_lines = nil
M.state.has_more_content = true
M.state.is_loading = false
if M.state.bufnr and vim.api.nvim_buf_is_valid(M.state.bufnr) then M.clear_buffer(M.state.bufnr) end
M.state.current_file = nil
M.state.scroll_offset = 0
M.state.content_height = 0
M.state.location = nil
M.state.is_binary_preview = false
M.state.hex_byte_offset = 0
end
--- Apply location highlighting to the preview buffer
--- @param bufnr number Buffer number
function M.apply_location_highlighting(bufnr)
-- Ensure namespace is created
if not M.state.location_namespace then
M.state.location_namespace = vim.api.nvim_create_namespace('fff_preview_location')
end
-- Always clear previous location highlights first
if vim.api.nvim_buf_is_valid(bufnr) then
location_utils.clear_location_highlights(bufnr, M.state.location_namespace)
end
if not M.state.location then return end
location_utils.highlight_location(bufnr, M.state.location, M.state.location_namespace)
if M.state.winid and vim.api.nvim_win_is_valid(M.state.winid) then
local target_line = location_utils.get_target_line(M.state.location)
if target_line then
local buffer_lines = vim.api.nvim_buf_line_count(bufnr)
if target_line > buffer_lines and M.state.has_more_content then
-- Target line is beyond loaded content — load more first.
-- ensure_content_loaded_async will re-apply highlighting when done.
ensure_content_loaded_async(target_line)
return
end
M.scroll_to_line(target_line)
end
end
end
--- Scroll preview to a specific line
--- @param line number Target line number (1-indexed)
function M.scroll_to_line(line)
if not M.state.winid or not vim.api.nvim_win_is_valid(M.state.winid) then return end
if not M.state.bufnr or not vim.api.nvim_buf_is_valid(M.state.bufnr) then return end
local win_height = vim.api.nvim_win_get_height(M.state.winid)
local buffer_lines = vim.api.nvim_buf_line_count(M.state.bufnr)
local target_line = math.max(1, math.min(line, buffer_lines))
local half_screen = math.floor(win_height / 2)
local new_offset = math.max(0, target_line - half_screen)
M.state.scroll_offset = new_offset
pcall(vim.api.nvim_win_call, M.state.winid, function()
vim.api.nvim_win_set_cursor(M.state.winid, { target_line, 0 })
vim.cmd('normal! zt')
end)
end
return M