@@ -342,31 +342,49 @@ utils.confirm_keys = function (prompt, content)
342342 --- | fE
343343end
344344
345+ --- @type integer , integer Buffer & window used for checking wrapped line height.
346+ utils .__wrapped_buf , utils .__wrapped_win = nil , nil ;
347+
345348--- Gets line number for wrapped text.
346349--- @param lines string[]
347350--- @param width ? integer
348351--- @return integer
349- utils .wrapped_height = function (lines , width )
352+ utils .wrapped_height = function (lines , width )
350353 --- | fS
351354
352355 width = width or vim .o .columns ;
353- local height = 0 ;
354356
355- for _ , line in ipairs (lines ) do
356- local len = vim .fn .strdisplaywidth (line );
357+ if type (utils .__wrapped_buf ) ~= " number" or vim .api .nvim_buf_is_valid (utils .__wrapped_buf ) then
358+ utils .__wrapped_buf = vim .api .nvim_create_buf (false , true );
359+ end
357360
358- if len <= width then
359- height = height + 1 ;
360- else
361- height = height + math.floor (vim .fn .strchars (line ) / width );
361+ local win_config = {
362+ hide = true ,
363+ relative = " editor" ,
362364
363- if vim .fn .strchars (line ) % width ~= 0 then
364- height = height + 1 ;
365- end
366- end
365+ row = 5 ,
366+ col = 5 ,
367+
368+ width = width ,
369+ height = 1 ,
370+
371+ style = " minimal" ,
372+ };
373+
374+ if type (utils .__wrapped_win ) ~= " number" or vim .api .nvim_win_is_valid (utils .__wrapped_win ) then
375+ utils .__wrapped_win = vim .api .nvim_open_win (utils .__wrapped_buf , false , win_config );
376+ else
377+ vim .api .nvim_win_set_config (utils .__wrapped_win , win_config );
367378 end
368379
369- return height ;
380+ vim .wo [utils .__wrapped_win ].wrap = true ;
381+ vim .wo [utils .__wrapped_win ].linebreak = true ;
382+ vim .wo [utils .__wrapped_win ].breakindent = true ;
383+
384+ vim .api .nvim_buf_set_lines (utils .__wrapped_buf , 0 , - 1 , false , lines );
385+
386+ local text_height = vim .api .nvim_win_text_height (utils .__wrapped_win , { start_row = 0 , end_row = - 1 });
387+ return text_height .all ;
370388
371389 --- | fE
372390end
0 commit comments