Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Nov 27, 2024
1 parent 1f3e9a6 commit a372e56
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ local function accept(ctx, item, callback)
text_edits_lib.apply(all_text_edits)

-- Expand the snippet
require('blink.cmp.config').accept.expand_snippet(item.textEdit.newText)
require('blink.cmp.config').snippets.expand(item.textEdit.newText)

-- OR Normal: Apply the text edit and move the cursor
else
Expand Down
18 changes: 7 additions & 11 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,24 @@ local list = {
---------- State ----------

function list.show(context, items)
-- reset state for new context
local is_new_context = not list.context or list.context.id ~= context.id
if is_new_context then list.preview_undo_text_edit = nil end

list.context = context
list.items = list.fuzzy(context, items or list.items)
vim.print(#list.items)

if #list.items == 0 then
list.hide_emitter:emit({ context = context })
else
vim.print('emitting')
list.show_emitter:emit({ items = list.items, context = context })
end

-- reset state for new context
if not is_new_context then return end
-- todo: some logic to maintain the selection if the user moved the cursor?
list.select(list.config.selection == 'preselect' and 1 or nil)
list.preview_undo_text_edit = nil
end

function list.fuzzy(context, items)
vim.print(#items)
local fuzzy = require('blink.cmp.fuzzy')
local sources = require('blink.cmp.sources.lib')

Expand All @@ -91,15 +89,13 @@ function list.hide() list.hide_emitter:emit({ context = list.context }) end
function list.get_selected_item() return list.items[list.selected_item_idx] end

function list.select(idx)
if idx ~= nil and not list.items[idx] then
error('Invalid index while selecting from the completion list. ' .. idx)
end
local item = list.items[idx]

list.undo_preview()
if list.config.selection == 'auto_insert' then list.apply_preview(list.items[idx]) end
if list.config.selection == 'auto_insert' and item then list.apply_preview(item) end

list.selected_item_idx = idx
list.select_emitter:emit({ idx = idx, item = list.items[idx], items = list.items, context = list.context })
list.select_emitter:emit({ idx = idx, item = item, items = list.items, context = list.context })
end

function list.select_next()
Expand Down
9 changes: 2 additions & 7 deletions lua/blink/cmp/completion/windows/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ function menu.open_with_items(context, items)
-- it's possible for the window to close after updating the position
-- if there was nowhere to place the window
if not menu.win:is_open() then return end

-- todo: some logic to maintain the selection if the user moved the cursor?
vim.api.nvim_win_set_cursor(menu.win:get_win(), { 1, 0 })
end

function menu.open()
Expand All @@ -82,11 +79,9 @@ function menu.close()
end

function menu.set_selected_item_idx(idx)
if idx == nil then menu.win:set_option_value('cursorline', false) end
menu.win:set_option_value('cursorline', true)

menu.win:set_option_value('cursorline', idx ~= nil)
menu.selected_item_idx = idx
if menu.win:is_open() then vim.api.nvim_win_set_cursor(menu.win:get_win(), { idx, 0 }) end
if menu.win:is_open() then vim.api.nvim_win_set_cursor(menu.win:get_win(), { idx or 1, 0 }) end
end

--- TODO: Don't switch directions if the context is the same
Expand Down
7 changes: 5 additions & 2 deletions lua/blink/cmp/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ function highlights.setup()
vim.api.nvim_set_hl(0, hl_group, opts)
end

set_hl('BlinkCmpLabel', { link = use_nvim_cmp and 'CmpItemAbbr' or 'Pmenu' })
if use_nvim_cmp then
set_hl('BlinkCmpLabel', { link = 'CmpItemAbbr' })
set_hl('BlinkCmpLabelMatch', { link = 'CmpItemAbbrMatch' })
end

set_hl('BlinkCmpLabelDeprecated', { link = use_nvim_cmp and 'CmpItemAbbrDeprecated' or 'NonText' })
set_hl('BlinkCmpLabelMatch', { link = use_nvim_cmp and 'CmpItemAbbrMatch' or 'Pmenu' })
set_hl('BlinkCmpLabelDetail', { link = use_nvim_cmp and 'CmpItemMenu' or 'NonText' })
set_hl('BlinkCmpLabelDescription', { link = use_nvim_cmp and 'CmpItemMenu' or 'NonText' })
set_hl('BlinkCmpKind', { link = use_nvim_cmp and 'CmpItemKind' or 'Special' })
Expand Down
8 changes: 4 additions & 4 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ end

function cmp.show_documentation()
local menu = require('blink.cmp.completion.windows.menu')
local documentation = require('blink.cmp.windows.documentation')
local documentation = require('blink.cmp.completion.windows.documentation')
if documentation.win:is_open() or not menu.win:is_open() then return end

local item = require('blink.cmp.completion.list').get_selected_item()
Expand All @@ -92,7 +92,7 @@ function cmp.show_documentation()
end

function cmp.hide_documentation()
local documentation = require('blink.cmp.windows.documentation')
local documentation = require('blink.cmp.completion.windows.documentation')
if not documentation.win:is_open() then return end

vim.schedule(function() documentation.win:close() end)
Expand All @@ -101,7 +101,7 @@ end

--- @param count? number
function cmp.scroll_documentation_up(count)
local documentation = require('blink.cmp.windows.documentation')
local documentation = require('blink.cmp.completion.windows.documentation')
if not documentation.win:is_open() then return end

vim.schedule(function() documentation.scroll_up(count or 4) end)
Expand All @@ -110,7 +110,7 @@ end

--- @param count? number
function cmp.scroll_documentation_down(count)
local documentation = require('blink.cmp.windows.documentation')
local documentation = require('blink.cmp.completion.windows.documentation')
if not documentation.win:is_open() then return end

vim.schedule(function() documentation.scroll_down(count or 4) end)
Expand Down

0 comments on commit a372e56

Please sign in to comment.