Skip to content

Commit

Permalink
fix(blink): use plain insertion instead of snippet.
Browse files Browse the repository at this point in the history
Replace snippet-based insertion with plain text insertion for completion
items generated by LLMs. Since LLM responses already contain appropriate
indentation, using VSCode's snippet-style insertion mechanism would
cause unwanted double-indentation, as the editor attempts to apply
indentation rules on top of the pre-formatted text.

Note: Currently, plain text insertion has a known upstream issue where
the cursor position is incorrectly set to the end of the first suggested
line instead of the last line.

See Saghen/blink.cmp#1123 for progress.
  • Loading branch information
milanglacier committed Jan 31, 2025
1 parent 880262e commit cd668aa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
38 changes: 17 additions & 21 deletions lua/minuet/blink.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
local M = {}
local config = require('minuet').config
local utils = require 'minuet.utils'
local max_label_width = nil

local has_blink = pcall(require, 'blink.cmp.config')

if has_blink then
max_label_width = require('blink.cmp.config').completion.menu.draw.components.label.width.max
else
vim.notify('Please install blink.cmp!', vim.log.levels.ERROR)
end

if vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = 'BlinkCmpItemKindMinuet' })) then
vim.api.nvim_set_hl(0, 'BlinkCmpItemKindMinuet', { link = 'BlinkCmpItemKind' })
Expand Down Expand Up @@ -92,22 +83,27 @@ function M:get_completions(ctx, callback)
end
end

local success, max_label_width = pcall(function()
return require('blink.cmp.config').completion.menu.draw.components.label.width.max
end)
if not success then
max_label_width = 60
end

local multi_lines_indicators = ' [...]'

local items = {}
for _, result in ipairs(new_data) do
local line_entry = vim.split(result, '\n')
local item_label = nil
if #line_entry == 1 then
local item_lines = vim.split(result, '\n')
local item_label

if #item_lines == 1 then
item_label = result
else
for _, line in ipairs(line_entry) do
line = utils.remove_spaces({ line })[1]
if line and line ~= '' then
line = utils.truncate_text(line, max_label_width - 3)
item_label = line
break
end
end
item_label = vim.fn.strcharpart(item_lines[1], 0, max_label_width - #multi_lines_indicators)
.. multi_lines_indicators
end

table.insert(items, {
label = item_label,
filterText = result,
Expand All @@ -119,7 +115,7 @@ function M:get_completions(ctx, callback)
},
-- TODO: use the provider name as kind name like nvim-cmp
-- when blink supports non-lsp kind name.
kind = vim.lsp.protocol.CompletionItemKind.Text,
kind = vim.lsp.protocol.CompletionItemKind.Snippet,
})
end
callback {
Expand Down
20 changes: 0 additions & 20 deletions lua/minuet/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,24 +441,4 @@ M.list_dedup = function(list)
return items_cleaned
end

-- Refer: /~https://github.com/MunifTanjim/nui.nvim
function M.truncate_text(text, max_length)
if vim.fn.strdisplaywidth(text) <= max_length then
return text .. ' […]'
end

local low, high = 0, vim.fn.strchars(text)
local mid

while low < high do
mid = math.floor((low + high + 1) / 2)
if vim.fn.strdisplaywidth(vim.fn.strcharpart(text, 0, mid)) < max_length then
low = mid
else
high = mid - 1
end
end

return vim.fn.strcharpart(text, 0, low) .. ' […]'
end
return M

0 comments on commit cd668aa

Please sign in to comment.