Skip to content

Commit

Permalink
feat!: rename align_to_component to align_to, add cursor option
Browse files Browse the repository at this point in the history
Closes #344
  • Loading branch information
Saghen committed Dec 24, 2024
1 parent e670720 commit 9387c75
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ completion.menu = {
-- Controls how the completion items are rendered on the popup window
completion.menu.draw = {
-- Aligns the keyword you've typed to a component in the menu
align_to_component = 'label', -- or 'none' to disable
align_to = 'label', -- or 'none' to disable, or 'cursor' to align to the cursor
-- Left and right padding, optionally { left, right } for different padding on each side
padding = 1,
-- Gap between columns
Expand Down
15 changes: 11 additions & 4 deletions lua/blink/cmp/completion/windows/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function menu.update_position()
return
end

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

-- 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 All @@ -117,12 +117,19 @@ function menu.update_position()
win:set_win_config({
relative = 'editor',
row = cmdline_position[1] + row,
col = math.max(cmdline_position[2] + context.bounds.start_col - start_col, 0),
col = math.max(cmdline_position[2] + context.bounds.start_col - alignment_start_col, 0),
})
else
local cursor_col = context.get_cursor()[2]
local col = context.bounds.start_col - cursor_col - (context.bounds.length == 0 and 0 or 1) - border_size.left
win:set_win_config({ relative = 'cursor', row = row, col = col - start_col })

local col = context.bounds.start_col
- alignment_start_col
- cursor_col
- (context.bounds.length == 0 and 0 or 1)
- border_size.left
if config.draw.align_to == 'cursor' then col = 0 end

win:set_win_config({ relative = 'cursor', row = row, col = col - alignment_start_col })
end

win:set_height(pos.height)
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/completion/windows/render/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function renderer:get_component_start_col(component_name)
end

function renderer:get_alignment_start_col()
local component_name = self.def.align_to_component
if component_name == nil or component_name == 'none' then return 0 end
local component_name = self.def.align_to
if component_name == nil or component_name == 'none' or component_name == 'cursor' then return 0 end
return self:get_component_start_col(component_name)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/windows/render/types.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- @class blink.cmp.Draw
--- @field align_to_component? string | 'none' Align the window to the component with the given name
--- @field align_to? string | 'none' | 'cursor' Align the window to the component with the given name, or to the cursor
--- @field padding? number | number[] Padding on the left and right of the grid
--- @field gap? number Gap between columns
--- @field columns? { [number]: string, gap?: number }[] Components to render, grouped by column
Expand Down
8 changes: 4 additions & 4 deletions lua/blink/cmp/config/completion/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ local window = {
-- Controls how the completion items are rendered on the popup window
draw = {
-- Aligns the keyword you've typed to a component in the menu
align_to_component = 'label', -- or 'none' to disable
align_to = 'cursor', -- or 'none' to disable
-- Left and right padding, optionally { left, right } for different padding on each side
padding = 1,
-- Gap between columns
Expand Down Expand Up @@ -161,10 +161,10 @@ function window.validate(config)
}, config.order)

validate('completion.menu.draw', {
align_to_component = {
config.draw.align_to_component,
align_to = {
config.draw.align_to,
function(align)
if align == 'none' 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
if component == align then return true end
Expand Down

0 comments on commit 9387c75

Please sign in to comment.