@@ -13,10 +13,6 @@ M._prev_line_count_by_win = {}
1313local _update_depth = 0
1414local _update_buf = nil
1515
16- local function is_win_showing_buf (win , buf )
17- return win and vim .api .nvim_win_is_valid (win ) and vim .api .nvim_win_get_buf (win ) == buf
18- end
19-
2016--- Begin a batch of buffer writes — toggle modifiable once for the whole batch.
2117--- Returns true if the batch was opened (buffer is valid). Must be paired with end_update().
2218--- @return boolean
@@ -324,60 +320,29 @@ end
324320_G .opencode_fold_expr = M .fold_expr
325321_G .opencode_fold_text = M .fold_text
326322
327- function M .snapshot_open_folds (win , prev_folds )
328- local ok , result = pcall (vim .api .nvim_win_call , win , function ()
329- local open = {}
330- for _ , range in ipairs (prev_folds ) do
331- if vim .fn .foldclosed (range .from ) == - 1 then
332- open [# open + 1 ] = range .from
333- end
334- end
335- return open
336- end )
337- return ok and result or {}
338- end
339-
340- function M .restore_open_folds (win , open_fold_starts )
341- pcall (vim .api .nvim_win_call , win , function ()
342- local view = vim .fn .winsaveview ()
343- vim .cmd (' silent! normal! zX' )
344- for _ , lnum in ipairs (open_fold_starts ) do
345- if vim .fn .foldclosed (lnum ) ~= - 1 then
346- vim .fn .cursor (lnum , 1 )
347- vim .cmd (' silent! normal! zo' )
348- end
349- end
350- vim .fn .winrestview (view )
351- end )
352- end
353-
354323--- Set the folds for the output buffer
355324--- @param fold_ranges table< {from : number , to : number } >
356325function M .set_folds (fold_ranges )
357326 local windows = state .windows
358- local buf = windows .output_buf
359- if not M .mounted () then
327+ if not windows or not windows .output_buf or not vim .api .nvim_buf_is_valid (windows .output_buf ) then
360328 return
361329 end
362330
363331 local folds = fold_ranges or {}
364- local ok , prev_folds = pcall (vim .api .nvim_buf_get_var , buf , ' opencode_folds' )
365- if ok and # folds == # prev_folds and vim .deep_equal (prev_folds , folds ) then
366- return
367- end
368-
332+ local buf = windows .output_buf
369333 local win = windows .output_win
370- local win_owns_buf = is_win_showing_buf (win , buf )
371334
372- local open_fold_starts = {}
373- if ok and win_owns_buf then
374- open_fold_starts = M . snapshot_open_folds ( win , prev_folds )
335+ local ok , prev_folds = pcall ( vim . api . nvim_buf_get_var , buf , ' opencode_folds ' )
336+ if ok and vim . deep_equal ( prev_folds , folds ) then
337+ return
375338 end
376339
377340 vim .api .nvim_buf_set_var (buf , ' opencode_folds' , folds )
378341
379- if win_owns_buf then
380- M .restore_open_folds (win , open_fold_starts )
342+ if win and vim .api .nvim_win_is_valid (win ) and vim .api .nvim_win_get_buf (win ) == buf then
343+ pcall (vim .api .nvim_win_call , win , function ()
344+ vim .cmd (' silent! normal! zX' )
345+ end )
381346 end
382347end
383348
0 commit comments