Skip to content

Commit

Permalink
Merge pull request #102 from luozhiya/multi-line
Browse files Browse the repository at this point in the history
Support multi-line virtual text preview
  • Loading branch information
luozhiya authored Jul 28, 2024
2 parents 7c16b77 + 448ff1e commit cdd9995
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
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

0 comments on commit cdd9995

Please sign in to comment.