Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-line virtual text preview #102

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ require('fittencode').setup({
require('cmp').setup({
sources = { name = 'fittencode', group_index = 1 },
mapping = {
['<cr>'] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = false }),
-- Accept multi-line completion
['<c-y>'] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = false }),
}
})
```
Expand Down
30 changes: 11 additions & 19 deletions lua/fittencode/sources/cmp/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,23 @@ end

-- Use `get_word` so that the word is the same as in `core.confirm`
-- https://github.com/hrsh7th/nvim-cmp/pull/1860
---@param suggestions string
-- https://github.com/hrsh7th/nvim-cmp/pull/2002
---@param suggestions string[]
---@return lsp.CompletionResponse?
local function convert_to_lsp_completion_response(line, character, cursor_before_line, suggestions)
cursor_before_line = cursor_before_line or ''
local LABEL_LIMIT = 80
local label = cursor_before_line .. suggestions
if #label > LABEL_LIMIT then
label = string.sub(label, 1, LABEL_LIMIT)
end
label = label:gsub('\n', '<\\n>')
local text = cursor_before_line .. table.concat(suggestions, '\n')
local first = suggestions[1]
local label = (#first > LABEL_LIMIT or #suggestions > 1) and string.sub(first, 1, LABEL_LIMIT - 3) .. '...' or first
local items = {}
table.insert(items, {
label = label,
word = label,
textEdit = {
range = {
start = { line = line, character = character },
['end'] = { line = line, character = character },
},
newText = suggestions,
},
label = cursor_before_line .. label,
insertText = text,
documentation = {
kind = 'markdown',
value = '```' .. vim.bo.ft .. '\n' .. cursor_before_line .. suggestions .. '\n```',
value = '```' .. vim.bo.ft .. '\n' .. text .. '\n```',
},
insertTextMode = 1,
cmp = {
kind_hl_group = 'CmpItemKindFittenCode',
kind_text = 'FittenCode',
Expand All @@ -105,11 +96,12 @@ function source:complete(request, callback)
return
end
Engine.generate_one_stage(row, col, true, 0, function(suggestions)
if not suggestions then
-- We skip the suggestion where the first element is empty, as nvim-cmp trim the completion item
-- Ref: `lazy/nvim-cmp/lua/cmp/entry.lua`
if not suggestions or #suggestions == 0 or suggestions[1] == '' then
callback()
return
end
suggestions = table.concat(suggestions, '\n')
local cursor_before_line = request.context.cursor_before_line:sub(request.offset)
local line = request.context.cursor.line
local character = request.context.cursor.character
Expand Down