Don't use nvim_set_decoration_provider for now

This commit is contained in:
hrsh7th
2021-09-03 21:29:13 +09:00
parent 157390d2c1
commit 62a6a6c063

View File

@@ -21,26 +21,20 @@ core.suspending = false
core.GHOST_TEXT_NS = vim.api.nvim_create_namespace('cmp:GHOST_TEXT') core.GHOST_TEXT_NS = vim.api.nvim_create_namespace('cmp:GHOST_TEXT')
vim.api.nvim_set_decoration_provider(core.GHOST_TEXT_NS, {
on_win = function()
if config.get().experimental.ghost_text then
core.ghost_text(core.menu:get_first_entry())
end
end,
})
---@type cmp.Menu ---@type cmp.Menu
core.menu = menu.new({ core.menu = menu.new({
on_select = function(e) on_select = function(e)
for _, c in ipairs(e:get_commit_characters()) do for _, c in ipairs(e:get_commit_characters()) do
keymap.listen('i', c, core.on_keymap) keymap.listen('i', c, core.on_keymap)
end end
core.ghost_text(e)
end, end,
}) })
---Show ghost text if possible ---Show ghost text if possible
---@param e cmp.Entry ---@param e cmp.Entry
core.ghost_text = function(e) core.ghost_text = function(e)
vim.api.nvim_buf_clear_namespace(0, core.GHOST_TEXT_NS, 0, -1)
if not e then if not e then
return return
end end
@@ -58,12 +52,11 @@ core.ghost_text = function(e)
text = string.sub(str.oneline(text), diff + 1) text = string.sub(str.oneline(text), diff + 1)
if #text > 0 then if #text > 0 then
vim.api.nvim_buf_set_extmark(ctx.bufnr, core.GHOST_TEXT_NS, ctx.cursor.row - 1, ctx.cursor.col - 1, { vim.api.nvim_buf_set_extmark(ctx.bufnr, core.GHOST_TEXT_NS, ctx.cursor.row - 1, ctx.cursor.col - 1, {
right_gravity = false,
virt_text = { { text, 'Comment' } }, virt_text = { { text, 'Comment' } },
virt_text_pos = 'overlay', virt_text_pos = 'overlay',
virt_text_win_col = ctx.virtcol - 1, virt_text_win_col = ctx.virtcol - 1,
hl_mode = 'combine', priority = 1,
priority = 0,
ephemeral = true,
}) })
end end
end end
@@ -198,6 +191,7 @@ core.on_change = function(event)
if ctx:changed(ctx.prev_context) then if ctx:changed(ctx.prev_context) then
debug.log('changed') debug.log('changed')
core.menu:restore(ctx) core.menu:restore(ctx)
core.ghost_text(core.menu:get_first_entry())
if vim.tbl_contains(config.get().completion.autocomplete or {}, event) then if vim.tbl_contains(config.get().completion.autocomplete or {}, event) then
core.complete(ctx) core.complete(ctx)
@@ -266,15 +260,8 @@ core.filter = async.throttle(function()
end end
end end
local prev = core.menu:get_first_entry()
core.menu:update(ctx, core.get_sources()) core.menu:update(ctx, core.get_sources())
local next = core.menu:get_first_entry() core.ghost_text(core.menu:get_first_entry())
local prev_word = prev and prev:get_word()
local next_word = next and next:get_word()
if prev_word ~= next_word then
vim.cmd [[redraw!]]
end
end, 50) end, 50)
---Confirm completion. ---Confirm completion.
@@ -390,6 +377,7 @@ core.reset = function()
core.menu:reset() core.menu:reset()
core.get_context() -- To prevent new event core.get_context() -- To prevent new event
core.ghost_text(nil)
end end
return core return core