Skip to content

Commit

Permalink
feat: command line completions
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 11, 2024
1 parent 605cb67 commit b20184f
Show file tree
Hide file tree
Showing 25 changed files with 916 additions and 380 deletions.
12 changes: 6 additions & 6 deletions lua/blink/cmp/completion/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ local function accept(ctx, item, callback)

-- Create an undo point, if it's not a snippet, since the snippet engine should handle undo
if
item.insertTextFormat ~= vim.lsp.protocol.InsertTextFormat.Snippet
ctx.mode == 'default'
and item.insertTextFormat ~= vim.lsp.protocol.InsertTextFormat.Snippet
and require('blink.cmp.config').completion.accept.create_undo_point
then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-g>u', true, true, true), 'n', true)
end

-- Add brackets to the text edit if needed
local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item)
local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(ctx, vim.bo.filetype, item)
item.textEdit = text_edit_with_brackets

-- Snippet
if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
assert(ctx.mode == 'default', 'Snippets are only supported in default mode')

-- We want to handle offset_encoding and the text edit api can do this for us
-- so we empty the newText and apply
local temp_text_edit = vim.deepcopy(item.textEdit)
Expand All @@ -56,10 +59,7 @@ local function accept(ctx, item, callback)
table.insert(all_text_edits, item.textEdit)
text_edits_lib.apply(all_text_edits)
-- TODO: should move the cursor only by the offset since text edit handles everything else?
vim.api.nvim_win_set_cursor(0, {
vim.api.nvim_win_get_cursor(0)[1],
item.textEdit.range.start.character + #item.textEdit.newText + offset,
})
ctx.set_cursor({ ctx.get_cursor()[1], item.textEdit.range.start.character + #item.textEdit.newText + offset })
end

-- Let the source execute the item itself
Expand Down
6 changes: 5 additions & 1 deletion lua/blink/cmp/completion/brackets/kind.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
local utils = require('blink.cmp.completion.brackets.utils')

--- @param ctx blink.cmp.Context
--- @param filetype string
--- @param item blink.cmp.CompletionItem
--- @return 'added' | 'check_semantic_token' | 'skipped', lsp.TextEdit | lsp.InsertReplaceEdit, number
local function add_brackets(filetype, item)
local function add_brackets(ctx, filetype, item)
local text_edit = item.textEdit
assert(text_edit ~= nil, 'Got nil text edit while adding brackets via kind')
local brackets_for_filetype = utils.get_for_filetype(filetype, item)

-- skip if we're not in default mode
if ctx.mode ~= 'default' then return 'skipped', text_edit, 0 end

-- if there's already the correct brackets in front, skip but indicate the cursor should move in front of the bracket
-- TODO: what if the brackets_for_filetype[1] == '' or ' ' (haskell/ocaml)?
if utils.has_brackets_in_front(text_edit, brackets_for_filetype[1]) then
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ end

function list.fuzzy(context, items_by_source)
local fuzzy = require('blink.cmp.fuzzy')
local filtered_items = fuzzy.fuzzy(fuzzy.get_query(), items_by_source)
local filtered_items = fuzzy.fuzzy(context:get_keyword(), items_by_source)

-- apply the per source max_items
filtered_items = require('blink.cmp.sources.lib').apply_max_items_for_completions(context, filtered_items)
Expand Down
255 changes: 0 additions & 255 deletions lua/blink/cmp/completion/trigger.lua

This file was deleted.

Loading

0 comments on commit b20184f

Please sign in to comment.