@@ -6,6 +6,28 @@ local rust = require('fff.rust')
66
77local M = {}
88
9+ -- Preview buffers are scratch buffers. Detect the file's language and attach
10+ -- highlighting directly, but keep buffer filetype empty to avoid ftplugin and
11+ -- LSP side effects that are meant for real editing buffers.
12+ local function attach_preview_highlighter (bufnr , filetype )
13+ if not bufnr or not vim .api .nvim_buf_is_valid (bufnr ) then return end
14+
15+ pcall (vim .treesitter .stop , bufnr )
16+ vim .api .nvim_set_option_value (' filetype' , ' ' , { buf = bufnr })
17+ vim .api .nvim_set_option_value (' syntax' , ' ' , { buf = bufnr })
18+
19+ if not filetype or filetype == ' ' then return end
20+
21+ local lang_ok , lang = pcall (vim .treesitter .language .get_lang , filetype )
22+ if not lang_ok or not lang then lang = filetype end
23+
24+ if pcall (vim .treesitter .language .add , lang ) then
25+ pcall (vim .treesitter .start , bufnr , lang )
26+ else
27+ vim .api .nvim_set_option_value (' syntax' , filetype , { buf = bufnr })
28+ end
29+ end
30+
931local function set_buffer_lines (bufnr , lines )
1032 if not bufnr or not vim .api .nvim_buf_is_valid (bufnr ) then return end
1133
@@ -274,7 +296,7 @@ local function link_buffer_content(source_bufnr, target_bufnr)
274296 set_buffer_lines (target_bufnr , lines )
275297
276298 local source_ft = vim .api .nvim_get_option_value (' filetype' , { buf = source_bufnr })
277- if source_ft ~= ' ' then vim . api . nvim_set_option_value ( ' filetype ' , source_ft , { buf = target_bufnr } ) end
299+ if source_ft ~= ' ' then attach_preview_highlighter ( target_bufnr , source_ft ) end
278300
279301 M .state .has_more_content = false
280302 M .state .total_file_lines = # lines
@@ -532,7 +554,7 @@ function M.preview_file(file_path, bufnr)
532554 set_buffer_lines (bufnr , content )
533555
534556 local file_config = M .get_file_config (file_path )
535- vim . api . nvim_set_option_value ( ' filetype ' , info .filetype , { buf = bufnr } )
557+ attach_preview_highlighter ( bufnr , info .filetype )
536558 vim .api .nvim_set_option_value (' modifiable' , false , { buf = bufnr })
537559 vim .api .nvim_set_option_value (' readonly' , true , { buf = bufnr })
538560 vim .api .nvim_set_option_value (' buftype' , ' nofile' , { buf = bufnr })
@@ -651,7 +673,7 @@ function M.preview_binary_file(file_path, bufnr)
651673 end
652674
653675 set_buffer_lines (bufnr , lines )
654- vim . api . nvim_set_option_value ( ' filetype ' , ' text' , { buf = bufnr } )
676+ attach_preview_highlighter ( bufnr , ' text' )
655677 vim .api .nvim_set_option_value (' modifiable' , false , { buf = bufnr })
656678 vim .api .nvim_set_option_value (' readonly' , true , { buf = bufnr })
657679
@@ -838,11 +860,8 @@ function M.clear_buffer(bufnr)
838860 cleanup_file_operation ()
839861 M .clear_preview_visual_state (bufnr )
840862
841- pcall (vim .treesitter .stop , bufnr )
842-
843863 vim .api .nvim_set_option_value (' modifiable' , true , { buf = bufnr })
844- vim .api .nvim_set_option_value (' filetype' , ' ' , { buf = bufnr })
845- vim .api .nvim_set_option_value (' syntax' , ' ' , { buf = bufnr })
864+ attach_preview_highlighter (bufnr , ' ' )
846865 vim .api .nvim_set_option_value (' buftype' , ' nofile' , { buf = bufnr })
847866
848867 set_buffer_lines (bufnr , {})
0 commit comments