Skip to content

Commit

Permalink
feat: allow multiline ghost text
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Oct 20, 2024
1 parent 0bea5e4 commit ecb1b51
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions lua/care/ghost_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,37 @@ function Ghost_text.new()
if (not self.config.enabled) or not self.entry then
return
end
local word = self.entry:get_insert_word()
local completion_item = self.entry.completion_item
local text
if completion_item.textEdit and completion_item.textEdit.newText then
text = vim.trim(completion_item.textEdit.newText)
else
text = vim.trim(completion_item.insertText or completion_item.label)
end

if self.entry.completion_item.insertTextFormat == 2 then
-- TODO: replace with stable api once available
text = tostring(vim.lsp._snippet_grammar.parse(text))
end

local offset = self.entry:get_offset()
local cursor = vim.api.nvim_win_get_cursor(self.win)
local text_after_filter = word:sub(cursor[2] - offset + 1)
if self.config.position == "inline" then
self.extmark_id = vim.api.nvim_buf_set_extmark(
vim.api.nvim_win_get_buf(self.win),
self.ns,
cursor[1] - 1,
cursor[2],
{
virt_text = { { text_after_filter, "@care.ghost_text" } },
virt_text_pos = "inline",
}
)
elseif self.config.position == "overlay" then
self.extmark_id = vim.api.nvim_buf_set_extmark(
vim.api.nvim_win_get_buf(self.win),
self.ns,
cursor[1] - 1,
cursor[2],
{
virt_text = { { text_after_filter, "@care.ghost_text" } },
virt_text_pos = "overlay",
ephemeral = true,
}
)
end
local text_after_filter = text:sub(cursor[2] - offset + 1)
local lines = vim.split(text_after_filter, "\n")
local virt_text = { { lines[1], "@care.ghost_text" } }
local virt_lines = vim.iter(lines)
:skip(1)
:map(function(line)
return { { line, "@care.ghost_text" } }
end)
:totable()
self.extmark_id =
vim.api.nvim_buf_set_extmark(vim.api.nvim_win_get_buf(self.win), self.ns, cursor[1] - 1, cursor[2], {
virt_text = virt_text,
virt_text_pos = self.config.position,
virt_lines = virt_lines,
ephemeral = false,
})
end,
})
return self
Expand Down

0 comments on commit ecb1b51

Please sign in to comment.