Improve patching implementation

This commit is contained in:
hrsh7th
2021-08-06 18:14:20 +09:00
parent 399cee4e24
commit f8caf5647d
3 changed files with 11 additions and 38 deletions

View File

@@ -8,7 +8,6 @@ local menu = require('cmp.menu')
local misc = require('cmp.utils.misc')
local config = require('cmp.config')
local types = require('cmp.types')
local patch = require('cmp.utils.patch')
local core = {}
@@ -237,10 +236,16 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
end
-- First, emulates vim's `<C-y>` behavior and then confirms LSP functionalities.
patch.apply(
pre,
completion_item.textEdit.range,
e:get_word(),
local range = types.lsp.Range.to_vim(pre.bufnr, e:get_insert_range())
local before_text = string.sub(pre.cursor_line, range.start.col, pre.cursor.col - 1)
local after_text = string.sub(pre.cursor_line, pre.cursor.col, pre.cursor.col + (range['end'].col - e.context.cursor.col) - 1)
local before_len = vim.fn.strchars(before_text)
local after_len = vim.fn.strchars(after_text)
local keys = string.rep('<C-g>U<Right>', after_len) .. string.rep('<BS>', before_len + after_len) .. e:get_word()
keymap.feedkeys(
keys,
'n',
vim.schedule_wrap(function()
vim.fn['cmp#confirm']({
request_offset = e.context.cursor.col,

View File

@@ -192,7 +192,7 @@ entry.get_vim_item = function(self, suggeset_offset)
return self.cache:ensure({ 'get_vim_item', suggeset_offset }, function()
local diff = vim.str_byteindex(self.context.cursor_line, self:get_replace_range()['end'].character)
local item = config.get().formatting.format(self, suggeset_offset)
item.word = str.remove_suffix(item.word, string.sub(self.context.cursor_line, self.context.cursor.col, diff + 1))
item.word = str.remove_suffix(item.word, string.sub(self.context.cursor_line, self.context.cursor.col, diff))
item.equal = 1
item.empty = 1
item.dup = self.completion_item.dup or 1

View File

@@ -1,32 +0,0 @@
local keymap = require('cmp.utils.keymap')
local types = require('cmp.types')
local patch = {}
---@type table<number, function>
patch.callbacks = {}
---Apply oneline textEdit
---@param ctx cmp.Context
---@param range lsp.Range
---@param word string
---@param callback function
patch.apply = function(ctx, range, word, callback)
local ok = true
ok = ok and range.start.line == ctx.cursor.row - 1
ok = ok and range.start.line == range['end'].line
if not ok then
error("text_edit's range must be current one line.")
end
range = types.lsp.Range.to_vim(ctx.bufnr, range)
local before = string.sub(ctx.cursor_before_line, range.start.col)
local after = string.sub(ctx.cursor_after_line, ctx.cursor.col, range['end'].col)
local before_len = vim.fn.strchars(before)
local after_len = vim.fn.strchars(after)
local keys = string.rep('<Left>', after_len) .. string.rep('<BS>', after_len + before_len) .. word
keymap.feedkeys(keys, 'n', callback)
end
return patch