Skip to content

Commit 973f06a

Browse files
committed
feat: position documentation based on desired size, not max size
1 parent cd3aa32 commit 973f06a

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

lua/blink/cmp/config.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@
134134
--- @alias blink.cmp.WindowBorder 'single' | 'double' | 'rounded' | 'solid' | 'shadow' | 'padded' | 'none' | blink.cmp.WindowBorderChar[]
135135
---
136136
--- @class blink.cmp.DocumentationConfig
137-
--- @field min_width? number
138137
--- @field max_width? number
139138
--- @field max_height? number
139+
--- @field desired_min_width? number
140+
--- @field desired_min_height? number
140141
--- @field border? blink.cmp.WindowBorder
141142
--- @field direction_priority? blink.cmp.DocumentationDirectionPriorityConfig
142143
--- @field auto_show? boolean
@@ -397,9 +398,10 @@ local config = {
397398
},
398399
},
399400
documentation = {
400-
min_width = 10,
401401
max_width = 80,
402402
max_height = 20,
403+
desired_min_width = 40,
404+
desired_min_height = 10,
403405
border = 'padded',
404406
winblend = 0,
405407
winhighlight = 'Normal:BlinkCmpDoc,FloatBorder:BlinkCmpDocBorder,CursorLine:BlinkCmpDocCursorLine,Search:None',

lua/blink/cmp/windows/documentation.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ function docs.update_position()
121121
end
122122

123123
-- decide direction, width and height of window
124-
local pos = docs.win:get_direction_with_window_constraints(autocomplete.win, direction_priority)
124+
local width = docs.win:get_width()
125+
local height = docs.win:get_height()
126+
local pos = docs.win:get_direction_with_window_constraints(
127+
autocomplete.win,
128+
direction_priority,
129+
{ width = math.min(width, config.desired_min_width), height = math.min(height, config.desired_min_height) }
130+
)
125131

126132
-- couldn't find anywhere to place the window
127133
if not pos then

lua/blink/cmp/windows/lib/init.lua

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
--- @field get_width fun(self: blink.cmp.Window): number
3030
--- @field get_cursor_screen_position fun(): { distance_from_top: number, distance_from_bottom: number }
3131
--- @field get_vertical_direction_and_height fun(self: blink.cmp.Window, direction_priority: ("n" | "s")[]): { height: number, direction: 'n' | 's' }?
32-
--- @field get_direction_with_window_constraints fun(self: blink.cmp.Window, anchor_win: blink.cmp.Window, direction_priority: ("n" | "s" | "e" | "w")[]): { width: number, height: number, direction: 'n' | 's' | 'e' | 'w' }?
32+
--- @field get_direction_with_window_constraints fun(self: blink.cmp.Window, anchor_win: blink.cmp.Window, direction_priority: ("n" | "s" | "e" | "w")[], desired_min_size?: { width: number, height: number }): { width: number, height: number, direction: 'n' | 's' | 'e' | 'w' }?
3333

3434
--- @type blink.cmp.Window
3535
--- @diagnostic disable-next-line: missing-fields
@@ -236,7 +236,7 @@ function win:get_vertical_direction_and_height(direction_priority)
236236
return { height = height - border_size.vertical, direction = direction }
237237
end
238238

239-
function win:get_direction_with_window_constraints(anchor_win, direction_priority)
239+
function win:get_direction_with_window_constraints(anchor_win, direction_priority, desired_min_size)
240240
local cursor_constraints = self.get_cursor_screen_position()
241241

242242
local anchor_config = vim.fn.screenpos(anchor_win:get_win(), 1, 1)
@@ -289,6 +289,19 @@ function win:get_direction_with_window_constraints(anchor_win, direction_priorit
289289
local direction_priority_by_space = vim.fn.sort(vim.deepcopy(direction_priority), function(a, b)
290290
local constraints_a = direction_constraints[a]
291291
local constraints_b = direction_constraints[b]
292+
293+
local is_desired_a = desired_min_size.height <= constraints_a.vertical
294+
and desired_min_size.width <= constraints_a.horizontal
295+
local is_desired_b = desired_min_size.height <= constraints_b.vertical
296+
and desired_min_size.width <= constraints_b.horizontal
297+
298+
-- prioritize "a" if it has the desired size
299+
if is_desired_a then return -1 end
300+
301+
-- prioritize "b" if it has the desired size and "a" doesn't
302+
if not is_desired_a and is_desired_b then return 1 end
303+
304+
-- neither have the desired size, so pick based on which has the most space
292305
local distance_a = math.min(max_height, constraints_a.vertical, constraints_a.horizontal)
293306
local distance_b = math.min(max_height, constraints_b.vertical, constraints_b.horizontal)
294307
return distance_a < distance_b and 1 or distance_a > distance_b and -1 or 0

0 commit comments

Comments
 (0)