@@ -90,6 +90,7 @@ local preview_buf, preview_winid
9090--- create a preview window according given window
9191--- default is under the given window
9292local function create_preview_win (content , main_winid )
93+ -- Get the configuration of the main window
9394 local win_conf = api .nvim_win_get_config (main_winid )
9495 local opt = {
9596 relative = win_conf .relative ,
@@ -98,45 +99,75 @@ local function create_preview_win(content, main_winid)
9899 anchor = win_conf .anchor ,
99100 focusable = false ,
100101 }
102+
103+ -- Determine content width and apply constraints
101104 local content_width = util .get_max_content_length (content )
102105 local max_win_width = api .nvim_win_get_width (win_conf .win )
103- if content_width < win_conf .width then
104- opt .width = win_conf .width
105- else
106- opt .width = math.min (max_win_width , content_width )
107- end
106+ opt .width = math.min (max_win_width , math.max (content_width , win_conf .width ))
107+
108+ -- Get dimensions of the main window
108109 local winheight = api .nvim_win_get_height (win_conf .win )
109110 local margin = config .ui .border == ' none' and 0 or 2
110111 local north = win_conf .anchor :sub (1 , 1 ) == ' N'
111112 local row = util .is_ten and win_conf .row or win_conf .row [false ]
112- local valid_top_height = north and row - 1 or row - win_conf .height - margin - 1
113- local valid_bot_height = north and winheight - row - win_conf .height - margin
114- or winheight - row - margin
113+
114+ -- Calculate available space above (valid_top_height) and below (valid_bot_height)
115+ local valid_top_height = math.max (0 , row - margin )
116+ local valid_bot_height = north and (winheight - row - win_conf .height - margin )
117+ or (winheight - row - margin )
118+
119+ -- Determine the preview window height
115120 local new_win_height = # content + margin
116- -- action is NW under cursor and top is enough to show preview
117- local east_or_west = win_conf .anchor :sub (2 , 2 )
118121 new_win_height = math.min (new_win_height , math.max (valid_bot_height , valid_top_height ))
122+
123+ -- Adjust anchor and position based on available space
124+ local east_or_west = win_conf .anchor :sub (2 , 2 )
125+
119126 if north then
120127 if valid_top_height >= new_win_height then
121128 opt .anchor = ' S' .. east_or_west
122129 opt .row = row
123- opt .height = math.min ( valid_top_height , # content )
130+ opt .height = new_win_height
124131 elseif valid_bot_height >= new_win_height then
125132 opt .anchor = ' N' .. east_or_west
126133 opt .row = row + win_conf .height + margin
127- opt .height = math.min (valid_bot_height , # content ) - 2
134+ opt .height = new_win_height
135+ else
136+ -- Fallback: Fit within whichever space is larger
137+ if valid_top_height > valid_bot_height then
138+ opt .anchor = ' S' .. east_or_west
139+ opt .row = row
140+ opt .height = valid_top_height
141+ else
142+ opt .anchor = ' N' .. east_or_west
143+ opt .row = row + win_conf .height + margin
144+ opt .height = valid_bot_height
145+ end
128146 end
129147 else
130148 if valid_bot_height >= new_win_height then
131149 opt .anchor = ' N' .. east_or_west
132150 opt .row = row
133- opt .height = math.min ( valid_bot_height , # content )
134- else
151+ opt .height = new_win_height
152+ elseif valid_top_height >= new_win_height then
135153 opt .anchor = ' S' .. east_or_west
136154 opt .row = row - win_conf .height - margin
137- opt .height = math.min (valid_top_height , # content )
155+ opt .height = new_win_height
156+ else
157+ -- Fallback: Fit within whichever space is larger
158+ if valid_bot_height > valid_top_height then
159+ opt .anchor = ' N' .. east_or_west
160+ opt .row = row
161+ opt .height = valid_bot_height
162+ else
163+ opt .anchor = ' S' .. east_or_west
164+ opt .row = row - win_conf .height - margin
165+ opt .height = valid_top_height
166+ end
138167 end
139168 end
169+
170+ -- Create the preview window with calculated options and set content/buffer options.
140171 preview_buf , preview_winid = win
141172 :new_float (opt , false , true )
142173 :setlines (content )
0 commit comments