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

@@ -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