Skip to content

Commit

Permalink
feat(menu.draw): columns can be function
Browse files Browse the repository at this point in the history
may be fix #820
  • Loading branch information
Parsifa1 committed Jan 21, 2025
1 parent 5b83998 commit a24a76a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lua/blink/cmp/completion/windows/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function menu.open_with_items(context, items)
menu.items = items
menu.selected_item_idx = menu.selected_item_idx ~= nil and math.min(menu.selected_item_idx, #items) or nil

if not menu.renderer then menu.renderer = require('blink.cmp.completion.windows.render').new(config.draw) end
if not menu.renderer then menu.renderer = require('blink.cmp.completion.windows.render').new(context, config.draw) end
menu.renderer:draw(context, menu.win:get_buf(), items)

local auto_show = menu.auto_show
Expand Down Expand Up @@ -106,7 +106,7 @@ function menu.update_position()
return
end

local alignment_start_col = menu.renderer:get_alignment_start_col()
local alignment_start_col = menu.renderer:get_alignment_start_col(context)

-- place the window at the start col of the current text we're fuzzy matching against
-- so the window doesnt move around as we type
Expand Down
14 changes: 11 additions & 3 deletions lua/blink/cmp/completion/windows/render/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
--- @field gap number
--- @field columns blink.cmp.DrawColumn[]
---
--- @field new fun(draw: blink.cmp.Draw): blink.cmp.Renderer
--- @field new fun(context: blink.cmp.Context, draw: blink.cmp.Draw): blink.cmp.Renderer
--- @field draw fun(self: blink.cmp.Renderer, context: blink.cmp.Context, bufnr: number, items: blink.cmp.CompletionItem[])
--- @field get_component_column_location fun(self: blink.cmp.Renderer, component_name: string): { column_idx: number, component_idx: number }
--- @field get_component_start_col fun(self: blink.cmp.Renderer, component_name: string): number
Expand All @@ -16,8 +16,16 @@ local ns = vim.api.nvim_create_namespace('blink_cmp_renderer')
--- @diagnostic disable-next-line: missing-fields
local renderer = {}

function renderer.new(draw)
function renderer.new(context, draw)
--- Convert the component names in the columns to the component definitions
--- @type blink.cmp.DrawColumn[]
local columns = draw.columns
if type(columns) == "function" then
columns = columns(context)
draw.columns = columns
end
--- @cast columns blink.cmp.DrawColumn[]

--- @type blink.cmp.DrawComponent[][]
local columns_definitions = vim.tbl_map(function(column)
local components = {}
Expand All @@ -31,7 +39,7 @@ function renderer.new(draw)
components = components,
gap = column.gap or 0,
}
end, draw.columns)
end, columns)

local padding = type(draw.padding) == 'number' and { draw.padding, draw.padding } or draw.padding
--- @cast padding number[]
Expand Down
5 changes: 5 additions & 0 deletions lua/blink/cmp/completion/windows/render/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ function text_lib.apply_component_width(text, component)
local width = component.width or {}
if width.fixed ~= nil then return text_lib.set_width(text, width.fixed, component) end
if width.min ~= nil then text = text_lib.pad(text, width.min) end
if type(width.max) == 'function' then
local max_width = width.max()
if max_width ~= nil then text = text_lib.truncate(text, max_width, component.ellipsis) end
else
if width.max ~= nil then text = text_lib.truncate(text, width.max, component.ellipsis) end
end
return text
end

Expand Down
3 changes: 2 additions & 1 deletion lua/blink/cmp/config/completion/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function window.validate(config)
align_to = {
config.draw.align_to,
function(align)
if type(config.draw.columns) == 'function' then return true end
if align == 'none' or align == 'cursor' then return true end
for _, column in ipairs(config.draw.columns) do
for _, component in ipairs(column) do
Expand Down Expand Up @@ -193,7 +194,7 @@ function window.validate(config)
config.draw.columns,
function(columns)
local available_components = vim.tbl_keys(config.draw.components)

if type(columns) == 'function' then return true end
if type(columns) ~= 'table' or #columns == 0 then return false end
for _, column in ipairs(columns) do
if #column == 0 then return false end
Expand Down

0 comments on commit a24a76a

Please sign in to comment.